Type Alias cw_asset::Asset

source ·
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

source

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()))
}
source

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()))
}
source

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 Display for Asset

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<&Coin> for Asset

source§

fn from(coin: &Coin) -> Self

Converts to this type from the input type.
source§

impl From<Coin> for Asset

source§

fn from(coin: Coin) -> Self

Converts to this type from the input type.
source§

impl PartialEq<Coin> for Asset

source§

fn eq(&self, other: &Coin) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.