#asset #cosmos #assets #cw #cw20 #coins

cw-asset

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

15 releases (9 stable)

Uses new Rust 2021

2.4.0 Nov 8, 2022
2.2.0 Jul 10, 2022
1.0.3 Mar 24, 2022
0.3.5 Feb 7, 2022
0.3.1 Dec 31, 2021

#201 in Rust patterns

Download history 350/week @ 2022-08-14 470/week @ 2022-08-21 365/week @ 2022-08-28 405/week @ 2022-09-04 685/week @ 2022-09-11 329/week @ 2022-09-18 194/week @ 2022-09-25 259/week @ 2022-10-02 217/week @ 2022-10-09 134/week @ 2022-10-16 161/week @ 2022-10-23 281/week @ 2022-10-30 630/week @ 2022-11-06 585/week @ 2022-11-13 368/week @ 2022-11-20 183/week @ 2022-11-27

1,774 downloads per month
Used in 11 crates (8 directly)

GPL-3.0-or-later

75KB
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 GNU Public License v3.


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, StdResult};
use cw_asset::Asset;

fn transfer_two_assets(api: &dyn Api) -> StdResult<Response> {
    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, StdResult};
use cw_asset::{Asset, AssetList};

fn transfer_multiple_assets(api: &dyn Api) -> StdResult<Response> {
    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, AssetUnchecked};

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

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

Dependencies

~2.4–4MB
~82K SLoC