#assets #tokens #cosmos #helper #coin #native #representation

cw-asset

Helper library for interacting with Cosmos assets (native coins and CW20 tokens)

19 releases (stable)

4.0.0 Aug 23, 2024
3.1.1 Feb 13, 2024
3.1.0 Jan 11, 2024
3.0.0 Feb 3, 2023
0.3.1 Dec 31, 2021

#5 in #coin

Download history 1701/week @ 2024-08-26 1709/week @ 2024-09-02 1021/week @ 2024-09-09 1177/week @ 2024-09-16 1238/week @ 2024-09-23 1302/week @ 2024-09-30 1234/week @ 2024-10-07 2039/week @ 2024-10-14 1735/week @ 2024-10-21 1563/week @ 2024-10-28 1478/week @ 2024-11-04 1729/week @ 2024-11-11 1964/week @ 2024-11-18 1518/week @ 2024-11-25 2833/week @ 2024-12-02 2650/week @ 2024-12-09

9,032 downloads per month
Used in 104 crates (47 directly)

Apache-2.0

76KB
1.5K SLoC

cw-asset

A unified representation of various types of Cosmos fungible assets, and helper functions for interacting with them

License

Contents of this repository are open source under Apache 2.0.


lib.rs:

A unified representation of various types of Cosmos fungible assets, and helper functions for interacting with them

Basic usage

The following code generates messages the sends some SDK coins and CW20 tokens to a recipient:

use cosmwasm_std::{Api, Response};
use cw_asset::{Asset, AssetError};

fn transfer_two_assets(api: &dyn Api) -> Result<Response, AssetError> {
    let asset1 = Asset::native("uusd", 12345u128);
    let msg1 = asset1.transfer_msg("recipient_addr")?;

    let asset2 = Asset::cw20(api.addr_validate("token_addr")?, 67890u128);
    let msg2 = asset1.transfer_msg("recipient_addr")?;

    Ok(Response::new()
        .add_message(msg1)
        .add_message(msg2)
        .add_attribute("asset_sent", asset1.to_string())
        .add_attribute("asset_sent", asset2.to_string()))
}

Asset list

An AssetList struct is also provided for dealing with multiple assets at the same time:

use cosmwasm_std::{Api, Response};
use cw_asset::{Asset, AssetError, AssetList};

fn transfer_multiple_assets(api: &dyn Api) -> Result<Response, AssetError> {
    let assets = AssetList::from(vec![
        Asset::native("uusd", 12345u128),
        Asset::cw20(api.addr_validate("token_addr")?, 67890u128),
    ]);

    let msgs = assets.transfer_msgs(api.addr_validate("recipient_addr")?)?;

    Ok(Response::new().add_messages(msgs).add_attribute("assets_sent", assets.to_string()))
}

Use in messages

Asset and AssetList each comes with an unchecked counterpart which contains unverified addresses and/or denoms, and implements traits that allow them to be serialized into JSON, so that they can be directly used in Cosmos messages:

use cosmwasm_schema::cw_serde;
use cw_asset::AssetUnchecked;

#[cw_serde]
pub enum ExecuteMsg {
    Deposit {
        asset: AssetUnchecked,
    },
}

Although Asset and AssetList also implement the related traits, hence can also be used in messages, it is not recommended to do so; it is a good security practice to never trust addresses passed in by messages to be valid. Instead, also validate them yourselves:

use cosmwasm_std::{Api, StdResult};
use cw_asset::{Asset, AssetError, AssetUnchecked};

const ACCEPTED_DENOMS: &[&str] = &["uatom", "uosmo", "uluna"];

fn validate_deposit(api: &dyn Api, asset_unchecked: AssetUnchecked) -> Result<(), AssetError> {
    let asset: Asset = asset_unchecked.check(api, Some(ACCEPTED_DENOMS))?;
    Ok(())
}

Dependencies

~4.5–8MB
~159K SLoC