Skip to content

Commit

Permalink
feat: impl astroport asset support (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
pacmanifold authored Jun 27, 2023
1 parent c79caf5 commit 9ac15cf
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 7 deletions.
95 changes: 89 additions & 6 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "apollo-cw-asset"
description = "Helper library for interacting with Cosmos assets (SDK coins and CW20 tokens)"
version = "0.1.0"
version = "0.1.1"
authors = ["larry <[email protected]>", "Apollo DAO Contributors <[email protected]>"]
edition = "2021"
license = "MIT"
Expand All @@ -12,13 +12,15 @@ doctest = false # do not run doc tests

[features]
default = []
astroport = ["dep:astroport"]

[dependencies]
cosmwasm-std = "1"
cw-storage-plus = "1.0.1"
cw20 = "1.0.1"
schemars = "0.8.11"
serde = { version = "1.0.152", default-features = false, features = ["derive"] }
astroport = { version = "2.8.0", optional = true }

[dev-dependencies]
test-case = "2.2.2"
20 changes: 20 additions & 0 deletions src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,26 @@ impl TryFrom<AssetUnchecked> for Cw20Coin {
}
}

#[cfg(feature = "astroport")]
impl From<astroport::asset::Asset> for Asset {
fn from(asset: astroport::asset::Asset) -> Self {
Self {
info: asset.info.into(),
amount: asset.amount,
}
}
}

#[cfg(feature = "astroport")]
impl From<Asset> for astroport::asset::Asset {
fn from(asset: Asset) -> Self {
Self {
info: asset.info.into(),
amount: asset.amount,
}
}
}

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 {
Expand Down
60 changes: 60 additions & 0 deletions src/asset_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ impl From<AssetInfo> for Denom {
}
}

#[cfg(feature = "astroport")]
impl From<astroport::asset::AssetInfo> for AssetInfo {
fn from(value: astroport::asset::AssetInfo) -> Self {
match value {
astroport::asset::AssetInfo::Token { contract_addr } => AssetInfo::Cw20(contract_addr),
astroport::asset::AssetInfo::NativeToken { denom } => AssetInfo::Native(denom),
}
}
}

#[cfg(feature = "astroport")]
impl From<AssetInfo> for astroport::asset::AssetInfo {
fn from(value: AssetInfo) -> Self {
match value {
AssetInfoBase::Cw20(addr) => astroport::asset::AssetInfo::Token {
contract_addr: addr,
},
AssetInfoBase::Native(denom) => astroport::asset::AssetInfo::NativeToken { denom },
}
}
}

impl AssetInfoUnchecked {
/// Validate contract address (if any) and returns a new `AssetInfo`
/// instance
Expand Down Expand Up @@ -329,4 +351,42 @@ mod test {
let info = AssetInfoUnchecked::Cw20("mock_token".to_string());
assert_eq!(AssetInfoUnchecked::Cw20("mock_token".to_string()), info);
}

#[test]
#[cfg(feature = "astroport")]
fn from_astro_asset_info() {
let info = astroport::asset::AssetInfo::Token {
contract_addr: Addr::unchecked("mock_token"),
};
let info2: AssetInfo = info.into();
assert_eq!(info2, AssetInfo::Cw20(Addr::unchecked("mock_token")));

let info = astroport::asset::AssetInfo::NativeToken {
denom: "uusd".to_string(),
};
let info2: AssetInfo = info.into();
assert_eq!(info2, AssetInfo::Native("uusd".to_string()));
}

#[test]
#[cfg(feature = "astroport")]
fn into_astro_asset_info() {
let info = AssetInfo::Cw20(Addr::unchecked("mock_token"));
let info2: astroport::asset::AssetInfo = info.into();
assert_eq!(
info2,
astroport::asset::AssetInfo::Token {
contract_addr: Addr::unchecked("mock_token")
}
);

let info = AssetInfo::Native("uusd".to_string());
let info2: astroport::asset::AssetInfo = info.into();
assert_eq!(
info2,
astroport::asset::AssetInfo::NativeToken {
denom: "uusd".to_string()
}
);
}
}
43 changes: 43 additions & 0 deletions src/asset_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ impl<T> Default for AssetListBase<T> {
pub type AssetListUnchecked = AssetListBase<String>;
pub type AssetList = AssetListBase<Addr>;

#[cfg(feature = "astroport")]
impl From<AssetList> for Vec<astroport::asset::Asset> {
fn from(value: AssetList) -> Self {
value
.0
.into_iter()
.map(|asset| asset.into())
.collect::<Vec<astroport::asset::Asset>>()
}
}

impl From<Vec<AssetUnchecked>> for AssetListUnchecked {
fn from(assets: Vec<AssetUnchecked>) -> Self {
Self(assets)
Expand Down Expand Up @@ -305,6 +316,26 @@ mod test_helpers {
Asset::new(mock_token(), 88888u128),
])
}

#[cfg(feature = "astroport")]
pub fn mock_astro_list() -> Vec<astroport::asset::Asset> {
use cosmwasm_std::Uint128;

vec![
astroport::asset::Asset {
info: astroport::asset::AssetInfo::NativeToken {
denom: "uusd".to_string(),
},
amount: Uint128::from(69420u128),
},
astroport::asset::Asset {
info: astroport::asset::AssetInfo::Token {
contract_addr: Addr::unchecked("mock_token"),
},
amount: Uint128::from(88888u128),
},
]
}
}

#[cfg(test)]
Expand Down Expand Up @@ -573,4 +604,16 @@ mod tests {
]
);
}

#[test]
#[cfg(feature = "astroport")]
fn from_assetlist_for_vec_astro_asset_info() {
use crate::asset_list::test_helpers::mock_astro_list;

let list = mock_list();

let vec_asset_info = Vec::<astroport::asset::Asset>::from(list);

assert_eq!(vec_asset_info, mock_astro_list());
}
}

0 comments on commit 9ac15cf

Please sign in to comment.