Skip to content

Commit

Permalink
feat: update cosmwasm-std (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
gorgos committed Jul 30, 2024
1 parent 1919a2c commit bcb2a2c
Show file tree
Hide file tree
Showing 22 changed files with 727 additions and 323 deletions.
814 changes: 607 additions & 207 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions contracts/atomic-order-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ exclude = [
crate-type = [ "cdylib", "rlib" ]

[features]
# for more explicit tests, cargo test --features=backtraces
backtraces = [ "cosmwasm-std/backtraces" ]
# use library feature to disable all instantiate/execute/query exports
library = [ ]

Expand All @@ -29,11 +27,11 @@ optimize = """docker run --rm -v "$(pwd)":/code \
"""

[dependencies]
cosmwasm-std = { version = "1.5.0", features = [ "abort", "cosmwasm_1_2", "cosmwasm_1_3", "cosmwasm_1_4", "iterator", "stargate" ] }
cosmwasm-std = { version = "2.1.0", features = [ "abort", "cosmwasm_1_2", "cosmwasm_1_3", "cosmwasm_1_4", "cosmwasm_2_0", "iterator", "stargate" ] }
cosmwasm-storage = { version = "1.5.0", features = [ "iterator" ] }
cw-storage-plus = { version = "1.2.0" }
cw-utils = { version = "0.16.0" }
cw2 = { version = "0.16.0" }
cw-storage-plus = { version = "2.0.0" }
cw-utils = { version = "2.0.0" }
cw2 = { version = "2.0.0" }
injective-cosmwasm = { version = "0.2.23-rc1", path = "../../packages/injective-cosmwasm" }
injective-math = { version = "0.2.4", path = "../../packages/injective-math" }
injective-protobuf = { version = "0.2.2", path = "../../packages/injective-protobuf" }
Expand All @@ -43,5 +41,5 @@ serde = { version = "1.0.137", default-features = false, features =
thiserror = { version = "1.0.31" }

[dev-dependencies]
cosmwasm-schema = { version = "1.5.0" }
cw-multi-test = { version = "0.16.2" }
cosmwasm-schema = { version = "2.1.0" }
cw-multi-test = { version = "2.1.0" }
6 changes: 4 additions & 2 deletions contracts/atomic-order-example/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub fn reply(
) -> Result<Response<InjectiveMsgWrapper>, ContractError> {
match msg.id {
ATOMIC_ORDER_REPLY_ID => handle_atomic_order_reply(deps, env, msg),
_ => Err(ContractError::UnrecognisedReply(msg.id)),
_ => Err(ContractError::UnrecognizedReply(msg.id)),
}
}

Expand All @@ -153,11 +153,13 @@ fn handle_atomic_order_reply(
msg.result
.into_result()
.map_err(ContractError::SubMsgFailure)?
.data
.msg_responses
.first()
.ok_or_else(|| ContractError::ReplyParseFailure {
id,
err: "Missing reply data".to_owned(),
})?
.value
.as_slice(),
)
.map_err(|err| ContractError::ReplyParseFailure {
Expand Down
2 changes: 1 addition & 1 deletion contracts/atomic-order-example/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum ContractError {
SubMsgFailure(String),

#[error("Unrecognised reply id: {0}")]
UnrecognisedReply(u64),
UnrecognizedReply(u64),

#[error("Invalid reply from sub-message {id}, {err}")]
ReplyParseFailure { id: u64, err: String },
Expand Down
27 changes: 16 additions & 11 deletions contracts/atomic-order-example/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::str::FromStr;

use cosmwasm_std::testing::{mock_info, MockApi, MockStorage};
use cosmwasm_std::testing::{message_info, MockApi, MockStorage};
use cosmwasm_std::{
coins, to_json_binary, BankMsg, Binary, ContractResult, CosmosMsg, OwnedDeps, QuerierResult,
Reply, SubMsgResponse, SubMsgResult, SystemResult, Uint128,
coins, to_json_binary, Addr, BankMsg, Binary, ContractResult, CosmosMsg, OwnedDeps,
QuerierResult, Reply, SubMsgResponse, SubMsgResult, SystemResult, Uint128,
};

use injective_cosmwasm::InjectiveMsg::CreateSpotMarketOrder;
Expand All @@ -26,14 +26,14 @@ fn test_deps() -> OwnedDeps<MockStorage, MockApi, WasmMockQuerier, InjectiveQuer

#[test]
fn proper_initialization() {
let sender_addr = "inj1x2ck0ql2ngyxqtw8jteyc0tchwnwxv7npaungt";
let sender_addr = Addr::unchecked("inj1x2ck0ql2ngyxqtw8jteyc0tchwnwxv7npaungt");
let msg = InstantiateMsg {
market_id: MarketId::new(
"0x78c2d3af98c517b164070a739681d4bd4d293101e7ffc3a30968945329b47ec6".to_string(),
)
.expect("failed to create market_id"),
};
let info = mock_info(sender_addr, &coins(1000, "earth"));
let info = message_info(&sender_addr, &coins(1000, "earth"));

// we can just call .unwrap() to assert this was a success
let res = instantiate(test_deps().as_mut_deps(), inj_mock_env(), info, msg).unwrap();
Expand All @@ -42,8 +42,8 @@ fn proper_initialization() {

#[test]
fn test_swap() {
let contract_addr = "inj14hj2tavq8fpesdwxxcu44rty3hh90vhujaxlnz";
let sender_addr = "inj1x2ck0ql2ngyxqtw8jteyc0tchwnwxv7npaungt";
let contract_addr = Addr::unchecked("inj14hj2tavq8fpesdwxxcu44rty3hh90vhujaxlnz");
let sender_addr = Addr::unchecked("inj1x2ck0ql2ngyxqtw8jteyc0tchwnwxv7npaungt");
let market_id = MarketId::new(
"0x78c2d3af98c517b164070a739681d4bd4d293101e7ffc3a30968945329b47ec6".to_string(),
)
Expand All @@ -52,12 +52,12 @@ fn test_swap() {
let msg = InstantiateMsg {
market_id: market_id.clone(),
};
let info = mock_info(contract_addr, &coins(1000, "earth"));
let info = message_info(&contract_addr, &coins(1000, "earth"));
let mut deps = test_deps();
let env = inj_mock_env();
let _ = instantiate(deps.as_mut_deps(), env.clone(), info, msg);

let info = mock_info(sender_addr, &coins(9000, "usdt"));
let info = message_info(&sender_addr, &coins(9000, "usdt"));
let msg = ExecuteMsg::SwapSpot {
quantity: i32_to_dec(8),
price: i32_to_dec(1000),
Expand Down Expand Up @@ -96,20 +96,25 @@ fn test_swap() {
);

let binary_response = Binary::from_base64("CkIweGRkNzI5MmY2ODcwMzIwOTc2YTUxYTUwODBiMGQ2NDU5M2NhZjE3OWViM2YxOTNjZWVlZGFiNGVhNWUxNDljZWISQwoTODAwMDAwMDAwMDAwMDAwMDAwMBIWMTAwMDAwMDAwMDAwMDAwMDAwMDAwMBoUMzYwMDAwMDAwMDAwMDAwMDAwMDA=").unwrap();

#[allow(deprecated)]
let reply_msg = Reply {
id: ATOMIC_ORDER_REPLY_ID,
result: SubMsgResult::Ok(SubMsgResponse {
events: vec![],
data: Some(binary_response),
msg_responses: vec![],
data: None,
}),
payload: binary_response,
gas_used: 1,
};

let transfers_response = reply(deps.as_mut_deps(), inj_mock_env(), reply_msg);
let messages = transfers_response.unwrap().messages;
assert_eq!(messages.len(), 1);

if let CosmosMsg::Bank(BankMsg::Send { to_address, amount }) = &messages[0].msg {
assert_eq!(to_address, sender_addr);
assert_eq!(*to_address, sender_addr.to_string());
assert_eq!(2, amount.len());
assert_eq!(amount[0].denom, "INJ");
assert_eq!(amount[0].amount, Uint128::from(8u128));
Expand Down
8 changes: 3 additions & 5 deletions contracts/dummy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ exclude = [
crate-type = [ "cdylib", "rlib" ]

[features]
# for more explicit tests, cargo test --features=backtraces
backtraces = [ "cosmwasm-std/backtraces" ]
# use library feature to disable all instantiate/execute/query exports
library = [ ]

Expand All @@ -29,9 +27,9 @@ optimize = """docker run --rm -v "$(pwd)":/code \
"""

[dependencies]
cosmwasm-std = { version = "1.5.0", features = [ "abort", "cosmwasm_1_2", "cosmwasm_1_3", "cosmwasm_1_4", "iterator", "stargate" ] }
cw-storage-plus = { version = "1.2.0" }
cw2 = { version = "0.16.0" }
cosmwasm-std = { version = "2.1.0", features = [ "abort", "cosmwasm_1_2", "cosmwasm_1_3", "cosmwasm_1_4", "cosmwasm_2_0", "iterator", "stargate" ] }
cw-storage-plus = { version = "2.0.0" }
cw2 = { version = "2.0.0" }
injective-cosmwasm = { version = "0.2.23-rc1", path = "../../packages/injective-cosmwasm" }
schemars = "0.8.8"
serde = { version = "1.0.136", default-features = false, features = [ "derive" ] }
Expand Down
10 changes: 6 additions & 4 deletions contracts/dummy/src/mock_pyth_attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ pub fn execute_trigger_pyth_update(
mod tests {
use std::marker::PhantomData;

use cosmwasm_std::testing::{mock_env, mock_info, MockApi, MockStorage};
use cosmwasm_std::{Api, CustomQuery, DepsMut, OwnedDeps, Querier, QuerierWrapper, Storage};
use cosmwasm_std::testing::{message_info, mock_env, MockApi, MockStorage};
use cosmwasm_std::{
Addr, Api, CustomQuery, DepsMut, OwnedDeps, Querier, QuerierWrapper, Storage,
};

use injective_cosmwasm::{InjectiveQueryWrapper, WasmMockQuerier};

Expand All @@ -55,10 +57,10 @@ mod tests {

#[test]
pub fn test_send_pyth() {
let sender_addr = "inj1x2ck0ql2ngyxqtw8jteyc0tchwnwxv7npaungt";
let sender_addr = Addr::unchecked("inj1x2ck0ql2ngyxqtw8jteyc0tchwnwxv7npaungt");

let msg = ExecuteMsg::TriggerPythUpdate { price: 10000 };
let info = mock_info(sender_addr, &[]);
let info = message_info(&sender_addr, &[]);
let env = mock_env();
let res = execute(inj_mock_deps().as_mut_deps(), env, info, msg);
assert!(res.is_ok())
Expand Down
15 changes: 6 additions & 9 deletions contracts/injective-cosmwasm-mock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ exclude = [
crate-type = [ "cdylib", "rlib" ]

[features]
# for more explicit tests, cargo test --features=backtraces
backtraces = [ "cosmwasm-std/backtraces" ]
# use library feature to disable all instantiate/execute/query exports
integration = [ ]
library = [ ]
Expand All @@ -31,21 +29,20 @@ optimize = """docker run --rm -v "$(pwd)":/code \

[dependencies]
cosmos-sdk-proto = { version = "0.20.0", default-features = false }
cosmwasm-schema = { version = "1.5.0" }
cosmwasm-std = { version = "1.5.0", features = [ "abort", "cosmwasm_1_2", "cosmwasm_1_3", "cosmwasm_1_4", "iterator", "stargate" ] }
cw-storage-plus = { version = "1.2.0" }
cw2 = { version = "0.16.0" }
cosmwasm-schema = { version = "2.1.0" }
cosmwasm-std = { version = "2.1.0", features = [ "abort", "cosmwasm_1_2", "cosmwasm_1_3", "cosmwasm_1_4", "cosmwasm_2_0", "iterator", "stargate" ] }
cw-storage-plus = { version = "2.0.0" }
cw2 = { version = "2.0.0" }
injective-cosmwasm = { path = "../../packages/injective-cosmwasm" }
injective-math = { path = "../../packages/injective-math" }
injective-protobuf = { path = "../../packages/injective-protobuf" }
injective-std = { version = "0.1.5" }
injective-std = { version = "1.13.2-testnet", path = "../../packages/injective-std" }
prost = { version = "0.12.4" }
protobuf = { version = "3.3.0" }
schemars = { version = "0.8.16" }
serde = { version = "1.0.196", default-features = false, features = [ "derive" ] }
thiserror = { version = "1.0.56" }

[dev-dependencies]
injective-std = { version = "0.1.5" }
injective-test-tube = { version = "1.1.7" }
injective-test-tube = { version = "1.13.2-a90d21322682bbfdfc72445dc0a9b45ee49f1084", path = "../../../test-tube/packages/injective-test-tube" }
injective-testing = { version = "0.1.6" }
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ fn test_query_derivative_market() {
maintenance_margin_ratio: dec_to_proto(maintenance_margin_ratio),
min_price_tick_size: dec_to_proto(min_price_tick_size),
min_quantity_tick_size: dec_to_proto(min_quantity_tick_size),
min_notional: "1".to_string(),
},
&env.signer,
)
Expand Down
3 changes: 3 additions & 0 deletions contracts/injective-cosmwasm-mock/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ pub fn launch_spot_market(exchange: &Exchange<InjectiveTestApp>, signer: &Signin
quote_denom: QUOTE_DENOM.to_string(),
min_price_tick_size: dec_to_proto(FPDecimal::must_from_str("0.000000000000001")),
min_quantity_tick_size: dec_to_proto(FPDecimal::must_from_str("1")),
min_notional: "1".to_string(),
},
signer,
)
Expand Down Expand Up @@ -364,6 +365,7 @@ pub fn launch_perp_market(exchange: &Exchange<InjectiveTestApp>, signer: &Signin
maintenance_margin_ratio: "50000000000000000".to_owned(),
min_price_tick_size: "1000000000000000000000".to_owned(),
min_quantity_tick_size: "1000000000000000".to_owned(),
min_notional: "1".to_string(),
},
signer,
)
Expand Down Expand Up @@ -755,6 +757,7 @@ pub fn set_address_of_pyth_contract(app: &InjectiveTestApp, validator: &SigningA
metadata: "".to_string(),
title: "Set Pyth contract address".to_string(),
summary: "Set Pyth contract address".to_string(),
expedited: false,
},
validator,
)
Expand Down
4 changes: 2 additions & 2 deletions packages/injective-cosmwasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ version = "0.2.23-rc1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cosmwasm-std = { version = "1.5.0", features = [ "abort", "cosmwasm_1_2", "cosmwasm_1_3", "cosmwasm_1_4", "iterator", "stargate" ] }
cw-storage-plus = { version = "1.2.0" }
cosmwasm-std = { version = "2.1.0", features = [ "abort", "cosmwasm_1_2", "cosmwasm_1_3", "cosmwasm_1_4", "cosmwasm_2_0", "iterator", "stargate" ] }
cw-storage-plus = { version = "2.0.0" }
ethereum-types = { version = "0.5.2" }
hex = { version = "0.4.3", features = [ "serde" ] }
injective-math = { version = "0.2.4", path = "../injective-math" }
Expand Down
2 changes: 1 addition & 1 deletion packages/injective-cosmwasm/src/exchange/subaccount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ mod tests {
&mock_dependencies().as_ref(),
);

assert_eq!(address.unwrap(), "inj1khsfhyavadcvzug67pufytaz2cq36ljkrsr0nv");
assert_eq!(address.unwrap().to_string(), "inj1khsfhyavadcvzug67pufytaz2cq36ljkrsr0nv".to_string());
}
}
10 changes: 9 additions & 1 deletion packages/injective-cosmwasm/src/exchange/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,16 @@ impl Into<String> for SubaccountId {
impl KeyDeserialize for SubaccountId {
type Output = SubaccountId;

const KEY_ELEMS: u16 = 42;

#[inline(always)]
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
fn from_vec(value: Vec<u8>) -> std::result::Result<SubaccountId, StdError> {
Ok(SubaccountId::unchecked(String::from_vec(value)?))
}

fn from_slice(value: &[u8]) -> StdResult<Self::Output> {
Self::from_vec(value.to_vec())
}
}

impl<'a> PrimaryKey<'a> for SubaccountId {
Expand All @@ -391,6 +397,8 @@ impl<'a> Prefixer<'a> for SubaccountId {
impl KeyDeserialize for &SubaccountId {
type Output = SubaccountId;

const KEY_ELEMS: u16 = 42;

#[inline(always)]
fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
Self::Output::from_vec(value)
Expand Down
Loading

0 comments on commit bcb2a2c

Please sign in to comment.