pub struct AssetBase<T: AddressLike> {
pub info: AssetInfoBase<T>,
pub amount: Uint128,
}
Expand description
Represents a fungible asset with a known amount
Each asset instance contains two values: info
, which specifies the asset’s
type (CW20 or native), and its amount
, which specifies the asset’s amount.
Fields§
§info: AssetInfoBase<T>
Specifies the asset’s type (CW20 or native)
amount: Uint128
Specifies the asset’s amount
Implementations§
source§impl<T: AddressLike> AssetBase<T>
impl<T: AddressLike> AssetBase<T>
sourcepub fn new<A: Into<AssetInfoBase<T>>, B: Into<Uint128>>(
info: A,
amount: B,
) -> Self
pub fn new<A: Into<AssetInfoBase<T>>, B: Into<Uint128>>( info: A, amount: B, ) -> Self
Create a new asset instance based on given asset info and amount
To create an unchecked instance, the info
parameter may be either
checked or unchecked; to create a checked instance, the info
paramter
must also be checked.
use cosmwasm_std::Addr;
use cw_asset::{Asset, AssetInfo};
let info1 = AssetInfo::cw20(Addr::unchecked("token_addr"));
let asset1 = Asset::new(info1, 12345u128);
let info2 = AssetInfo::native("uusd");
let asset2 = Asset::new(info2, 67890u128);
source§impl AssetBase<String>
impl AssetBase<String>
sourcepub fn from_sdk_string(s: &str) -> Result<Self, AssetError>
pub fn from_sdk_string(s: &str) -> Result<Self, AssetError>
Parse a string of the format {amount}{denom}
into an AssetUnchecked
object. This is the format that Cosmos SDK uses to stringify native
coins. For example:
12345uatom
69420ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2
88888factory/osmo1z926ax906k0ycsuckele6x5hh66e2m4m6ry7dn
Since native coin denoms can only start with a non-numerial character, while its amount can only contain numerical characters, we simply consider the first non-numerical character and all that comes after as the denom, while all that comes before it as the amount. This is the approach used in the Steak Hub contract.
sourcepub fn check(
&self,
api: &dyn Api,
optional_whitelist: Option<&[&str]>,
) -> Result<Asset, AssetError>
pub fn check( &self, api: &dyn Api, optional_whitelist: Option<&[&str]>, ) -> Result<Asset, AssetError>
Validate data contained in an unchecked asset instnace, return a new checked asset instance:
- For CW20 tokens, assert the contract address is valid;
- For SDK coins, assert that the denom is included in a given whitelist; skip if the whitelist is not provided.
use cosmwasm_std::{Addr, Api};
use cw_asset::{Asset, AssetUnchecked};
fn validate_asset(api: &dyn Api, asset_unchecked: &AssetUnchecked) {
match asset_unchecked.check(api, Some(&["uatom", "uluna"])) {
Ok(asset) => println!("asset is valid: {}", asset.to_string()),
Err(err) => println!("asset is invalid! reason: {}", err),
}
}
source§impl AssetBase<Addr>
impl AssetBase<Addr>
sourcepub fn send_msg<A: Into<String>>(
&self,
to: A,
msg: Binary,
) -> Result<CosmosMsg, AssetError>
pub fn send_msg<A: Into<String>>( &self, to: A, msg: Binary, ) -> Result<CosmosMsg, AssetError>
Generate a message that sends a CW20 token to the specified recipient with a binary payload.
NOTE: Only works for CW20 tokens. Returns error if invoked on an Asset
instance representing a native coin, as native coins do not have an
equivalent method mplemented.
use serde::Serialize;
#[derive(Serialize)]
enum MockReceiveMsg {
MockCommand {},
}
use cosmwasm_std::{to_json_binary, Addr, Response};
use cw_asset::{Asset, AssetError};
fn send_asset(
asset: &Asset,
contract_addr: &Addr,
msg: &MockReceiveMsg,
) -> Result<Response, AssetError> {
let msg = asset.send_msg(contract_addr, to_json_binary(msg)?)?;
Ok(Response::new().add_message(msg).add_attribute("asset_sent", asset.to_string()))
}
sourcepub fn transfer_msg<A: Into<String>>(
&self,
to: A,
) -> Result<CosmosMsg, AssetError>
pub fn transfer_msg<A: Into<String>>( &self, to: A, ) -> Result<CosmosMsg, AssetError>
Generate a message that transfers the asset from the sender to to a specified account.
use cosmwasm_std::{Addr, Response};
use cw_asset::{Asset, AssetError};
fn transfer_asset(asset: &Asset, recipient_addr: &Addr) -> Result<Response, AssetError> {
let msg = asset.transfer_msg(recipient_addr)?;
Ok(Response::new().add_message(msg).add_attribute("asset_sent", asset.to_string()))
}
sourcepub fn transfer_from_msg<A: Into<String>, B: Into<String>>(
&self,
from: A,
to: B,
) -> Result<CosmosMsg, AssetError>
pub fn transfer_from_msg<A: Into<String>, B: Into<String>>( &self, from: A, to: B, ) -> Result<CosmosMsg, AssetError>
Generate a message that draws the asset from the account specified by
from
to the one specified by to
.
NOTE: Only works for CW20 tokens. Returns error if invoked on an Asset
instance representing a native coin, as native coins do not have an
equivalent method implemented.
use cosmwasm_std::{Addr, Response};
use cw_asset::{Asset, AssetError};
fn draw_asset(
asset: &Asset,
user_addr: &Addr,
contract_addr: &Addr,
) -> Result<Response, AssetError> {
let msg = asset.transfer_from_msg(user_addr, contract_addr)?;
Ok(Response::new().add_message(msg).add_attribute("asset_drawn", asset.to_string()))
}
Trait Implementations§
source§impl<'de, T> Deserialize<'de> for AssetBase<T>where
T: Deserialize<'de> + AddressLike,
impl<'de, T> Deserialize<'de> for AssetBase<T>where
T: Deserialize<'de> + AddressLike,
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl<T: AddressLike + JsonSchema> JsonSchema for AssetBase<T>
impl<T: AddressLike + JsonSchema> JsonSchema for AssetBase<T>
source§fn schema_name() -> String
fn schema_name() -> String
source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref
keyword. Read moreimpl<T: AddressLike> StructuralPartialEq for AssetBase<T>
Auto Trait Implementations§
impl<T> Freeze for AssetBase<T>where
T: Freeze,
impl<T> RefUnwindSafe for AssetBase<T>where
T: RefUnwindSafe,
impl<T> Send for AssetBase<T>where
T: Send,
impl<T> Sync for AssetBase<T>where
T: Sync,
impl<T> Unpin for AssetBase<T>where
T: Unpin,
impl<T> UnwindSafe for AssetBase<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more