Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl From Asset/AssetInfo for cw-asset::Asset/AssetInfo #398

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions packages/astroport/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cosmwasm-schema = "1.1"
astroport-circular-buffer = { version = "0.1", path = "../circular_buffer" }
cw-utils = "1.0"
cw3 = "1.0"
cw-asset = "3.0.0"

# optional
injective-math = { version = "0.1", optional = true }
Expand Down
104 changes: 104 additions & 0 deletions packages/astroport/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cosmwasm_std::{
QuerierWrapper, ReplyOn, StdError, StdResult, SubMsg, Uint128, Uint256, WasmMsg,
};
use cw20::{Cw20Coin, Cw20CoinVerified, Cw20ExecuteMsg, Cw20QueryMsg, Denom, MinterResponse};
use cw_asset::{Asset as CwAsset, AssetInfo as CwAssetInfo};
use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey};
use cw_utils::must_pay;
use itertools::Itertools;
Expand Down Expand Up @@ -129,6 +130,23 @@ impl TryFrom<Asset> for Cw20Coin {
}
}

impl From<Asset> for CwAsset {
fn from(asset: Asset) -> CwAsset {
Self::new(Into::<CwAssetInfo>::into(asset.info), asset.amount)
}
}

impl TryFrom<CwAsset> for Asset {
type Error = StdError;

fn try_from(cw_asset: CwAsset) -> StdResult<Self> {
cw_asset
.info
.try_into()
.map(|cw_asset_info| Self::new(cw_asset_info, cw_asset.amount))
}
}

impl Asset {
/// Constructs a new [`Asset`] object.
pub fn new<A: Into<Uint128>>(info: AssetInfo, amount: A) -> Self {
Expand Down Expand Up @@ -422,6 +440,27 @@ impl From<Addr> for AssetInfo {
}
}

impl From<AssetInfo> for CwAssetInfo {
fn from(asset_info: AssetInfo) -> Self {
match asset_info {
AssetInfo::Token { contract_addr } => Self::Cw20(contract_addr),
AssetInfo::NativeToken { denom } => Self::Native(denom),
}
}
}

impl TryFrom<CwAssetInfo> for AssetInfo {
type Error = StdError;

fn try_from(cw_asset_info: CwAssetInfo) -> StdResult<Self> {
match cw_asset_info {
CwAssetInfo::Native(denom) => Ok(Self::native(denom)),
CwAssetInfo::Cw20(contract_addr) => Ok(Self::cw20(contract_addr)),
_ => Err(StdError::generic_err("CwAssetInfo variant unknown")),
}
}
}

impl AssetInfo {
/// Returns an [`AssetInfo`] object representing the denomination for native asset.
pub fn native<A: Into<String>>(denom: A) -> Self {
Expand Down Expand Up @@ -1144,4 +1183,69 @@ mod tests {
let denom2: Denom = info.try_into().unwrap();
assert_eq!(denom, denom2);
}

#[test]
fn test_from_asset_info_for_cw_asset_info() {
let asset_info_native = AssetInfo::native("denom");
let asset_info_cw20 = AssetInfo::cw20(Addr::unchecked("cw20"));
assert_eq!(CwAssetInfo::native("denom"), asset_info_native.into());
assert_eq!(
CwAssetInfo::cw20(Addr::unchecked("cw20")),
asset_info_cw20.into()
)
}

#[test]
fn test_try_from_from_cw_asset_info_for_asset_info() {
let cw_asset_info_native = CwAssetInfo::native("denom");
let cw_asset_info_cw20 = CwAssetInfo::cw20(Addr::unchecked("cw20"));
assert_eq!(
AssetInfo::native("denom"),
cw_asset_info_native.try_into().unwrap()
);
assert_eq!(
AssetInfo::cw20(Addr::unchecked("cw20")),
cw_asset_info_cw20.try_into().unwrap()
)
}

#[test]
fn test_from_asset_for_cw_asset() {
let asset_native = Asset::new(AssetInfo::native("denom"), Uint128::one());
let asset_cw20 = Asset::new(
AssetInfo::cw20(Addr::unchecked("cw20")),
Uint128::from(2_u128),
);
assert_eq!(
CwAsset::new(CwAssetInfo::native("denom"), Uint128::one()),
Into::<CwAsset>::into(asset_native)
);
assert_eq!(
CwAsset::new(
CwAssetInfo::cw20(Addr::unchecked("cw20")),
Uint128::from(2_u128)
),
Into::<CwAsset>::into(asset_cw20)
)
}

#[test]
fn test_try_from_cw_asset_for_asset() {
let asset_native = CwAsset::new(CwAssetInfo::native("denom"), Uint128::one());
let asset_cw20 = CwAsset::new(
CwAssetInfo::cw20(Addr::unchecked("cw20")),
Uint128::from(2_u128),
);
assert_eq!(
Asset::new(AssetInfo::native("denom"), Uint128::one()),
asset_native.try_into().unwrap()
);
assert_eq!(
Asset::new(
AssetInfo::cw20(Addr::unchecked("cw20")),
Uint128::from(2_u128)
),
asset_cw20.try_into().unwrap()
)
}
}
Loading