pub type Asset = AssetBase<Addr>;
Aliased Type§
struct Asset {
pub info: AssetInfoBase<Addr>,
pub amount: Uint128,
}
Fields§
§info: AssetInfoBase<Addr>
Specifies the asset’s type (CW20 or native)
amount: Uint128
Specifies the asset’s amount
Implementations§
source§impl Asset
impl Asset
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()))
}