Skip to content

Commit

Permalink
Move cw20/native constructors to the base types
Browse files Browse the repository at this point in the history
  • Loading branch information
larry0x committed Dec 31, 2021
1 parent 528050f commit 7fa2a9c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "cw-asset"
description = "Helper library for interacting with Cosmos assets (SDK coins and CW20 tokens)"
version = "0.3.0"
version = "0.3.1"
authors = ["larry <larry@delphidigital.io>"]
edition = "2018"
license = "GPL-3.0-or-later"
Expand Down
50 changes: 26 additions & 24 deletions src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ pub struct AssetBase<T> {
pub amount: Uint128,
}

impl<T> AssetBase<T> {
/// Create a new `AssetBase` instance based on given asset info and amount
pub fn new<B: Into<Uint128>>(info: AssetInfoBase<T>, amount: B) -> Self {
Self {
info,
amount: amount.into(),
}
}

/// Create a new `AssetBase` instance representing a CW20 token of given contract address and amount
pub fn cw20<A: Into<T>, B: Into<Uint128>>(contract_addr: A, amount: B) -> Self {
Self {
info: AssetInfoBase::cw20(contract_addr),
amount: amount.into(),
}
}

/// Create a new `AssetBase` instance representing a native coin of given denom
pub fn native<A: Into<String>, B: Into<Uint128>>(denom: A, amount: B) -> Self {
Self {
info: AssetInfoBase::native(denom),
amount: amount.into(),
}
}
}

pub type AssetUnchecked = AssetBase<String>;
pub type Asset = AssetBase<Addr>;

Expand Down Expand Up @@ -66,30 +92,6 @@ impl From<&Coin> for Asset {
}

impl Asset {
/// Create a new `AssetBase` instance based on given asset info and amount
pub fn new<B: Into<Uint128>>(info: AssetInfo, amount: B) -> Self {
Self {
info,
amount: amount.into(),
}
}

/// Create a new `AssetBase` instance representing a CW20 token of given contract address and amount
pub fn cw20<B: Into<Uint128>>(contract_addr: Addr, amount: B) -> Self {
Self {
info: AssetInfoBase::cw20(contract_addr),
amount: amount.into(),
}
}

/// Create a new `AssetBase` instance representing a native coin of given denom
pub fn native<A: Into<String>, B: Into<Uint128>>(denom: A, amount: B) -> Self {
Self {
info: AssetInfoBase::native(denom),
amount: amount.into(),
}
}

/// Generate a message that sends a CW20 token to the specified recipient with a binary payload
///
/// NOTE: Only works for CW20 tokens
Expand Down
22 changes: 12 additions & 10 deletions src/asset_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ pub enum AssetInfoBase<T> {
Native(String), // the native token's denom
}

impl<T> AssetInfoBase<T> {
/// Create a new `AssetInfoBase` instance representing a CW20 token of given contract address
pub fn cw20<A: Into<T>>(contract_addr: A) -> Self {
AssetInfoBase::Cw20(contract_addr.into())
}

/// Create a new `AssetInfoBase` instance representing a native token of given denom
pub fn native<A: Into<String>>(denom: A) -> Self {
AssetInfoBase::Native(denom.into())
}
}

pub type AssetInfoUnchecked = AssetInfoBase<String>;
pub type AssetInfo = AssetInfoBase<Addr>;

Expand Down Expand Up @@ -50,16 +62,6 @@ impl fmt::Display for AssetInfo {
}

impl AssetInfo {
/// Create a new `AssetInfoBase` instance representing a CW20 token of given contract address
pub fn cw20<A: Into<Addr>>(contract_addr: A) -> Self {
AssetInfo::Cw20(contract_addr.into())
}

/// Create a new `AssetInfoBase` instance representing a native token of given denom
pub fn native<A: Into<String>>(denom: A) -> Self {
AssetInfo::Native(denom.into())
}

/// Query an address' balance of the asset
pub fn query_balance<T: Into<String>>(
&self,
Expand Down

0 comments on commit 7fa2a9c

Please sign in to comment.