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

Bump deps and crate version #23

Merged
merged 2 commits into from
Jan 11, 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
218 changes: 115 additions & 103 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
[package]
name = "cw-asset"
description = "Helper library for interacting with Cosmos assets (native coins and CW20 tokens)"
version = "3.0.0"
version = "3.1.0"
authors = ["Larry Engineer <larry@delphidigital.io>"]
edition = "2021"
license = "GPL-3.0-or-later"
repository = "https://github.com/mars-protocol/cw-asset"

[dependencies]
cosmwasm-schema = "1.2"
cosmwasm-std = "1.2"
cw20 = "1.0"
cw-address-like = "1"
cw-storage-plus = "1.0"
thiserror = "1"
cosmwasm-schema = "1.5.0"
cosmwasm-std = "1.5.0"
cw20 = "1.1.2"
cw-address-like = "1.0.4"
cw-storage-plus = "1.2.0"
thiserror = "1.0.56"

[dev-dependencies]
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.195", default-features = false, features = ["derive"] }
34 changes: 14 additions & 20 deletions src/asset.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{convert::TryFrom, fmt, str::FromStr};

use cosmwasm_schema::cw_serde;
use cosmwasm_std::{to_binary, Addr, Api, BankMsg, Binary, Coin, CosmosMsg, Uint128, WasmMsg};
use cosmwasm_std::{to_json_binary, Addr, Api, BankMsg, Binary, Coin, CosmosMsg, Uint128, WasmMsg};
use cw20::Cw20ExecuteMsg;
use cw_address_like::AddressLike;

Expand Down Expand Up @@ -259,26 +259,24 @@ impl Asset {
/// MockCommand {},
/// }
///
/// use cosmwasm_std::{to_binary, Addr, Response};
/// use cosmwasm_std::{to_json_binary, Addr, Response};
/// use cw_asset::{Asset, AssetError};
///
/// fn send_asset(
/// asset: &Asset,
/// contract_addr: &Addr,
/// msg: &MockReceiveMsg,
/// ) -> Result<Response, AssetError> {
/// let msg = asset.send_msg(contract_addr, to_binary(msg)?)?;
/// let msg = asset.send_msg(contract_addr, to_json_binary(msg)?)?;
///
/// Ok(Response::new()
/// .add_message(msg)
/// .add_attribute("asset_sent", asset.to_string()))
/// Ok(Response::new().add_message(msg).add_attribute("asset_sent", asset.to_string()))
/// }
/// ```
pub fn send_msg<A: Into<String>>(&self, to: A, msg: Binary) -> Result<CosmosMsg, AssetError> {
match &self.info {
AssetInfo::Cw20(contract_addr) => Ok(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: contract_addr.into(),
msg: to_binary(&Cw20ExecuteMsg::Send {
msg: to_json_binary(&Cw20ExecuteMsg::Send {
contract: to.into(),
amount: self.amount,
msg,
Expand All @@ -301,9 +299,7 @@ impl Asset {
/// fn transfer_asset(asset: &Asset, recipient_addr: &Addr) -> Result<Response, AssetError> {
/// let msg = asset.transfer_msg(recipient_addr)?;
///
/// Ok(Response::new()
/// .add_message(msg)
/// .add_attribute("asset_sent", asset.to_string()))
/// Ok(Response::new().add_message(msg).add_attribute("asset_sent", asset.to_string()))
/// }
/// ```
pub fn transfer_msg<A: Into<String>>(&self, to: A) -> Result<CosmosMsg, AssetError> {
Expand All @@ -317,7 +313,7 @@ impl Asset {
})),
AssetInfo::Cw20(contract_addr) => Ok(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: contract_addr.into(),
msg: to_binary(&Cw20ExecuteMsg::Transfer {
msg: to_json_binary(&Cw20ExecuteMsg::Transfer {
recipient: to.into(),
amount: self.amount,
})?,
Expand All @@ -344,9 +340,7 @@ impl Asset {
/// ) -> Result<Response, AssetError> {
/// let msg = asset.transfer_from_msg(user_addr, contract_addr)?;
///
/// Ok(Response::new()
/// .add_message(msg)
/// .add_attribute("asset_drawn", asset.to_string()))
/// Ok(Response::new().add_message(msg).add_attribute("asset_drawn", asset.to_string()))
/// }
/// ```
pub fn transfer_from_msg<A: Into<String>, B: Into<String>>(
Expand All @@ -357,7 +351,7 @@ impl Asset {
match &self.info {
AssetInfo::Cw20(contract_addr) => Ok(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: contract_addr.into(),
msg: to_binary(&Cw20ExecuteMsg::TransferFrom {
msg: to_json_binary(&Cw20ExecuteMsg::TransferFrom {
owner: from.into(),
recipient: to.into(),
amount: self.amount,
Expand Down Expand Up @@ -601,16 +595,16 @@ mod tests {
let token = Asset::cw20(Addr::unchecked("mock_token"), 123456u128);
let coin = Asset::native("uusd", 123456u128);

let bin_msg = to_binary(&MockExecuteMsg::MockCommand {}).unwrap();
let bin_msg = to_json_binary(&MockExecuteMsg::MockCommand {}).unwrap();
let msg = token.send_msg("mock_contract", bin_msg.clone()).unwrap();
assert_eq!(
msg,
CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: String::from("mock_token"),
msg: to_binary(&Cw20ExecuteMsg::Send {
msg: to_json_binary(&Cw20ExecuteMsg::Send {
contract: String::from("mock_contract"),
amount: Uint128::new(123456),
msg: to_binary(&MockExecuteMsg::MockCommand {}).unwrap()
msg: to_json_binary(&MockExecuteMsg::MockCommand {}).unwrap()
})
.unwrap(),
funds: vec![]
Expand All @@ -630,7 +624,7 @@ mod tests {
msg,
CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: String::from("mock_token"),
msg: to_binary(&Cw20ExecuteMsg::Transfer {
msg: to_json_binary(&Cw20ExecuteMsg::Transfer {
recipient: String::from("alice"),
amount: Uint128::new(123456)
})
Expand All @@ -653,7 +647,7 @@ mod tests {
msg,
CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: String::from("mock_token"),
msg: to_binary(&Cw20ExecuteMsg::TransferFrom {
msg: to_json_binary(&Cw20ExecuteMsg::TransferFrom {
owner: String::from("bob"),
recipient: String::from("charlie"),
amount: Uint128::new(123456)
Expand Down
4 changes: 2 additions & 2 deletions src/asset_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{any::type_name, fmt, str::FromStr};

use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
to_binary, Addr, Api, BalanceResponse, BankQuery, QuerierWrapper, QueryRequest, StdError,
to_json_binary, Addr, Api, BalanceResponse, BankQuery, QuerierWrapper, QueryRequest, StdError,
StdResult, Uint128, WasmQuery,
};
use cw20::{BalanceResponse as Cw20BalanceResponse, Cw20QueryMsg};
Expand Down Expand Up @@ -204,7 +204,7 @@ impl AssetInfo {
let response: Cw20BalanceResponse =
querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: contract_addr.into(),
msg: to_binary(&Cw20QueryMsg::Balance {
msg: to_json_binary(&Cw20QueryMsg::Balance {
address: address.into(),
})?,
}))?;
Expand Down
60 changes: 16 additions & 44 deletions src/asset_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ impl FromStr for AssetListUnchecked {
return Ok(Self(vec![]));
}

s
.split(',')
.map(AssetUnchecked::from_str)
.collect::<Result<_, _>>()
.map(Self)
s.split(',').map(AssetUnchecked::from_str).collect::<Result<_, _>>().map(Self)
}
}

Expand Down Expand Up @@ -84,11 +80,7 @@ impl fmt::Display for AssetList {
let s = if self.is_empty() {
"[]".to_string()
} else {
self.0
.iter()
.map(|asset| asset.to_string())
.collect::<Vec<_>>()
.join(",")
self.0.iter().map(|asset| asset.to_string()).collect::<Vec<_>>().join(",")
};

write!(f, "{s}")
Expand Down Expand Up @@ -174,10 +166,8 @@ impl AssetList {
/// ```rust
/// use cw_asset::{Asset, AssetList};
///
/// let list = AssetList::from(vec![
/// Asset::native("uluna", 12345u128),
/// Asset::native("uusd", 67890u128),
/// ]);
/// let list =
/// AssetList::from(vec![Asset::native("uluna", 12345u128), Asset::native("uusd", 67890u128)]);
///
/// let vec: Vec<Asset> = list.to_vec();
/// ```
Expand All @@ -190,14 +180,11 @@ impl AssetList {
/// ```rust
/// use cw_asset::{Asset, AssetList};
///
/// let list = AssetList::from(vec![
/// Asset::native("uluna", 12345u128),
/// Asset::native("uusd", 67890u128),
/// ]);
/// let list =
/// AssetList::from(vec![Asset::native("uluna", 12345u128), Asset::native("uusd", 67890u128)]);
///
/// let len = list.len(); // should be two
/// ```
///
// NOTE: I do have `is_empty` implemented, but clippy still throws a warnin
// saying I don't have it. Must be a clippy bug...
#[allow(clippy::len_without_is_empty)]
Expand Down Expand Up @@ -252,10 +239,8 @@ impl AssetList {
/// ```rust
/// use cw_asset::{Asset, AssetInfo, AssetList};
///
/// let mut list = AssetList::from(vec![
/// Asset::native("uluna", 12345u128),
/// Asset::native("uusd", 67890u128),
/// ]);
/// let mut list =
/// AssetList::from(vec![Asset::native("uluna", 12345u128), Asset::native("uusd", 67890u128)]);
///
/// let list_halved = list.apply(|a| a.amount = a.amount.multiply_ratio(1u128, 2u128));
/// ```
Expand All @@ -269,10 +254,8 @@ impl AssetList {
/// ```rust
/// use cw_asset::{Asset, AssetList};
///
/// let mut list = AssetList::from(vec![
/// Asset::native("uluna", 12345u128),
/// Asset::native("uusd", 0u128),
/// ]);
/// let mut list =
/// AssetList::from(vec![Asset::native("uluna", 12345u128), Asset::native("uusd", 0u128)]);
/// let mut len = list.len(); // should be two
///
/// list.purge();
Expand Down Expand Up @@ -429,25 +412,17 @@ impl AssetList {
/// use cosmwasm_std::{Addr, Response};
/// use cw_asset::{AssetError, AssetList};
///
/// fn transfer_assets(
/// list: &AssetList,
/// recipient_addr: &Addr,
/// ) -> Result<Response, AssetError> {
/// fn transfer_assets(list: &AssetList, recipient_addr: &Addr) -> Result<Response, AssetError> {
/// let msgs = list.transfer_msgs(recipient_addr)?;
///
/// Ok(Response::new()
/// .add_messages(msgs)
/// .add_attribute("assets_sent", list.to_string()))
/// Ok(Response::new().add_messages(msgs).add_attribute("assets_sent", list.to_string()))
/// }
/// ```
pub fn transfer_msgs<A: Into<String> + Clone>(
&self,
to: A,
) -> Result<Vec<CosmosMsg>, AssetError> {
self.0
.iter()
.map(|asset| asset.transfer_msg(to.clone()))
.collect()
self.0.iter().map(|asset| asset.transfer_msg(to.clone())).collect()
}
}

Expand All @@ -473,17 +448,14 @@ mod test_helpers {
}

pub fn mock_list() -> AssetList {
AssetList::from(vec![
Asset::native("uusd", 69420u128),
Asset::new(mock_token(), 88888u128),
])
AssetList::from(vec![Asset::native("uusd", 69420u128), Asset::new(mock_token(), 88888u128)])
}
}

#[cfg(test)]
mod tests {
use cosmwasm_std::{
testing::MockApi, to_binary, BankMsg, Coin, CosmosMsg, Decimal, OverflowError,
testing::MockApi, to_json_binary, BankMsg, Coin, CosmosMsg, Decimal, OverflowError,
OverflowOperation, StdError, Uint128, WasmMsg,
};
use cw20::Cw20ExecuteMsg;
Expand Down Expand Up @@ -691,7 +663,7 @@ mod tests {
}),
CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: String::from("mock_token"),
msg: to_binary(&Cw20ExecuteMsg::Transfer {
msg: to_json_binary(&Cw20ExecuteMsg::Transfer {
recipient: String::from("alice"),
amount: Uint128::new(88888)
})
Expand Down
8 changes: 4 additions & 4 deletions src/testing/custom_mock_querier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{
from_binary, from_slice, testing::MockQuerier, Addr, Coin, Empty, Querier, QuerierResult,
QueryRequest, StdResult, SystemError, WasmQuery,
from_json, testing::MockQuerier, Addr, Coin, Empty, Querier, QuerierResult, QueryRequest,
StdResult, SystemError, WasmQuery,
};
use cw20::Cw20QueryMsg;

Expand All @@ -22,7 +22,7 @@ impl Default for CustomMockQuerier {

impl Querier for CustomMockQuerier {
fn raw_query(&self, bin_request: &[u8]) -> QuerierResult {
let request: QueryRequest<Empty> = match from_slice(bin_request) {
let request: QueryRequest<Empty> = match from_json(bin_request) {
Ok(v) => v,
Err(e) => {
return Err(SystemError::InvalidRequest {
Expand All @@ -45,7 +45,7 @@ impl CustomMockQuerier {
}) => {
let contract_addr = Addr::unchecked(contract_addr);

let parse_cw20_query: StdResult<Cw20QueryMsg> = from_binary(msg);
let parse_cw20_query: StdResult<Cw20QueryMsg> = from_json(msg);
if let Ok(cw20_query) = parse_cw20_query {
return self.cw20_querier.handle_query(&contract_addr, cw20_query);
}
Expand Down
6 changes: 3 additions & 3 deletions src/testing/cw20_querier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use cosmwasm_std::{to_binary, Addr, QuerierResult, SystemError, Uint128};
use cosmwasm_std::{to_json_binary, Addr, QuerierResult, SystemError, Uint128};
use cw20::{BalanceResponse, Cw20QueryMsg};

#[derive(Default)]
Expand Down Expand Up @@ -38,7 +38,7 @@ impl Cw20Querier {
},
};

Ok(to_binary(&BalanceResponse {
Ok(to_json_binary(&BalanceResponse {
balance: *balance,
})
.into())
Expand All @@ -57,7 +57,7 @@ impl Cw20Querier {
let contract_addr = Addr::unchecked(contract);
let user_addr = Addr::unchecked(user);

let contract_balances = self.balances.entry(contract_addr).or_insert_with(HashMap::new);
let contract_balances = self.balances.entry(contract_addr).or_default();
contract_balances.insert(user_addr, Uint128::new(balance));
}
}
1 change: 1 addition & 0 deletions src/testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ mod custom_mock_querier;
mod cw20_querier;
mod helpers;

#[allow(unused_imports)]
pub use custom_mock_querier::CustomMockQuerier;
pub use helpers::mock_dependencies;
Loading