From b274e05b5c566efa6006cb31a0a36afd650b8f37 Mon Sep 17 00:00:00 2001 From: jbernal87 Date: Mon, 4 Mar 2024 15:40:55 +0100 Subject: [PATCH] more check orders --- Cargo.lock | 83 ++++++++++++++++--- contracts/injective-cosmwasm-mock/Cargo.toml | 2 + .../injective-cosmwasm-mock/src/contract.rs | 14 ++-- contracts/injective-cosmwasm-mock/src/msg.rs | 2 +- .../src/order_management.rs | 9 +- .../src/testing/query_exchange_test.rs | 27 +++--- .../injective-cosmwasm-mock/src/utils.rs | 29 ++++++- 7 files changed, 133 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bea96f65..f603c0c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -385,8 +385,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73c9d2043a9e617b0d602fbc0a0ecd621568edbf3a9774890a6d562389bd8e1c" dependencies = [ "prost 0.11.9", - "prost-types", - "tendermint-proto", + "prost-types 0.11.9", + "tendermint-proto 0.32.2", +] + +[[package]] +name = "cosmos-sdk-proto" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32560304ab4c365791fd307282f76637213d8083c1a98490c35159cd67852237" +dependencies = [ + "prost 0.12.3", + "prost-types 0.12.3", + "tendermint-proto 0.34.1", ] [[package]] @@ -396,7 +407,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af13955d6f356272e6def9ff5e2450a7650df536d8934f47052a20c76513d2f6" dependencies = [ "bip32", - "cosmos-sdk-proto", + "cosmos-sdk-proto 0.19.0", "ecdsa 0.16.9", "eyre", "getrandom", @@ -1557,6 +1568,7 @@ dependencies = [ name = "injective-cosmwasm-mock" version = "1.0.0" dependencies = [ + "cosmos-sdk-proto 0.20.0", "cosmwasm-std", "cw-storage-plus 1.2.0", "cw2 0.16.0", @@ -1564,6 +1576,7 @@ dependencies = [ "injective-math 0.2.4", "injective-std 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "injective-test-tube", + "log", "prost 0.11.9", "protobuf 3.3.0", "schemars", @@ -1621,7 +1634,7 @@ dependencies = [ "cosmwasm-std", "osmosis-std-derive", "prost 0.11.9", - "prost-types", + "prost-types 0.11.9", "schemars", "serde 1.0.196", "serde-cw-value", @@ -1637,7 +1650,7 @@ dependencies = [ "cosmwasm-std", "osmosis-std-derive", "prost 0.11.9", - "prost-types", + "prost-types 0.11.9", "schemars", "serde 1.0.196", "serde-cw-value", @@ -2171,6 +2184,16 @@ dependencies = [ "prost-derive 0.11.9", ] +[[package]] +name = "prost" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "146c289cda302b98a28d40c8b3b90498d6e526dd24ac2ecea73e4e491685b94a" +dependencies = [ + "bytes", + "prost-derive 0.12.3", +] + [[package]] name = "prost-derive" version = "0.9.0" @@ -2197,6 +2220,19 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "prost-derive" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efb6c9a1dd1def8e2124d17e83a20af56f1570d6c2d2bd9e266ccb768df3840e" +dependencies = [ + "anyhow", + "itertools", + "proc-macro2", + "quote", + "syn 2.0.48", +] + [[package]] name = "prost-types" version = "0.11.9" @@ -2206,6 +2242,15 @@ dependencies = [ "prost 0.11.9", ] +[[package]] +name = "prost-types" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "193898f59edcf43c26227dcd4c8427f00d99d61e95dcde58dabd49fa291d470e" +dependencies = [ + "prost 0.12.3", +] + [[package]] name = "protobuf" version = "2.28.0" @@ -2914,7 +2959,7 @@ dependencies = [ "num-traits", "once_cell", "prost 0.11.9", - "prost-types", + "prost-types 0.11.9", "ripemd", "serde 1.0.196", "serde_bytes", @@ -2924,7 +2969,7 @@ dependencies = [ "signature 2.2.0", "subtle", "subtle-encoding", - "tendermint-proto", + "tendermint-proto 0.32.2", "time", "zeroize", ] @@ -2954,7 +2999,25 @@ dependencies = [ "num-derive", "num-traits", "prost 0.11.9", - "prost-types", + "prost-types 0.11.9", + "serde 1.0.196", + "serde_bytes", + "subtle-encoding", + "time", +] + +[[package]] +name = "tendermint-proto" +version = "0.34.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b797dd3d2beaaee91d2f065e7bdf239dc8d80bba4a183a288bc1279dd5a69a1e" +dependencies = [ + "bytes", + "flex-error", + "num-derive", + "num-traits", + "prost 0.12.3", + "prost-types 0.12.3", "serde 1.0.196", "serde_bytes", "subtle-encoding", @@ -2986,7 +3049,7 @@ dependencies = [ "subtle-encoding", "tendermint", "tendermint-config", - "tendermint-proto", + "tendermint-proto 0.32.2", "thiserror", "time", "tokio", @@ -3017,7 +3080,7 @@ dependencies = [ "prost 0.11.9", "serde 1.0.196", "serde_json 1.0.111", - "tendermint-proto", + "tendermint-proto 0.32.2", "thiserror", ] diff --git a/contracts/injective-cosmwasm-mock/Cargo.toml b/contracts/injective-cosmwasm-mock/Cargo.toml index 4752b9fb..b0fcb9a2 100644 --- a/contracts/injective-cosmwasm-mock/Cargo.toml +++ b/contracts/injective-cosmwasm-mock/Cargo.toml @@ -41,6 +41,8 @@ thiserror = { version = "1.0.56" } protobuf = "3.3.0" prost = "0.11.9" injective-std = { version = "0.1.5" } +cosmos-sdk-proto = { version = "0.20.0", default-features = false } +log = "0.4.20" [dev-dependencies] injective-test-tube = "1.1.7" diff --git a/contracts/injective-cosmwasm-mock/src/contract.rs b/contracts/injective-cosmwasm-mock/src/contract.rs index 207ea3f9..7ef1e541 100644 --- a/contracts/injective-cosmwasm-mock/src/contract.rs +++ b/contracts/injective-cosmwasm-mock/src/contract.rs @@ -1,9 +1,8 @@ use crate::msg::MSG_CREATE_SPOT_MARKET_ORDER_ENDPOINT; -use crate::order_management::{create_spot_market_order, create_stargate_msg}; +use crate::order_management::{create_spot_market_order, create_stargate_msg, encode_bytes_message}; use crate::{ error::ContractError, msg::{ExecuteMsg, InstantiateMsg, QueryMsg}, - types, }; use cosmos_sdk_proto::{cosmos::authz::v1beta1::MsgExec, traits::Message, Any}; @@ -11,7 +10,6 @@ use cosmwasm_std::{entry_point, to_json_binary, Binary, Deps, DepsMut, Env, Mess use cw2::set_contract_version; use injective_cosmwasm::{create_deposit_msg, InjectiveMsgWrapper, InjectiveQuerier, InjectiveQueryWrapper, OrderType}; use injective_math::FPDecimal; -use prost::Message; const CONTRACT_NAME: &str = "crates.io:injective:dummy"; const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -37,22 +35,22 @@ pub fn execute( ExecuteMsg::TestDepositMsg { subaccount_id, amount } => { Ok(Response::new().add_message(create_deposit_msg(env.contract.address, subaccount_id, amount))) } - ExecuteMsg::TestTraderTransientSpotOrders { market_id, subaccount_id } => { + ExecuteMsg::TestTraderTransientSpotOrders { market_id, subaccount_id, price, quantity } => { let querier: InjectiveQuerier = InjectiveQuerier::new(&deps.querier); let spot_market = querier.query_spot_market(&market_id).unwrap().market.unwrap(); + deps.api.debug(&info.sender.as_str()); let order_msg = create_spot_market_order( - FPDecimal::must_from_str("1"), - FPDecimal::must_from_str("1"), + FPDecimal::must_from_str(price.as_str()), + FPDecimal::must_from_str(quantity.as_str()), OrderType::Buy, &info.sender.as_str(), subaccount_id.as_str(), &spot_market, ); - let mut order_bytes = vec![]; - types::MsgCreateSpotMarketOrder::encode(&order_msg, &mut order_bytes).unwrap(); + let order_bytes = encode_bytes_message(&order_msg); let msg_exec = MsgExec { grantee: env.contract.address.to_string(), diff --git a/contracts/injective-cosmwasm-mock/src/msg.rs b/contracts/injective-cosmwasm-mock/src/msg.rs index d02db8a3..561be59c 100644 --- a/contracts/injective-cosmwasm-mock/src/msg.rs +++ b/contracts/injective-cosmwasm-mock/src/msg.rs @@ -14,7 +14,7 @@ pub struct InstantiateMsg {} #[serde(rename_all = "snake_case")] pub enum ExecuteMsg { TestDepositMsg { subaccount_id: SubaccountId, amount: Coin }, - TestTraderTransientSpotOrders { market_id: MarketId, subaccount_id: SubaccountId }, + TestTraderTransientSpotOrders { market_id: MarketId, subaccount_id: SubaccountId, price:String, quantity: String }, TestTraderTransientDerivativeOrders { market_id: MarketId, subaccount_id: SubaccountId }, } diff --git a/contracts/injective-cosmwasm-mock/src/order_management.rs b/contracts/injective-cosmwasm-mock/src/order_management.rs index d991a3cc..9414cac6 100644 --- a/contracts/injective-cosmwasm-mock/src/order_management.rs +++ b/contracts/injective-cosmwasm-mock/src/order_management.rs @@ -2,7 +2,8 @@ use crate::types; use cosmwasm_std::{to_json_binary, Coin, CosmosMsg, StdResult, SubMsg, Uint128, WasmMsg}; use injective_cosmwasm::{InjectiveMsgWrapper, OrderType, SpotMarket}; use injective_math::FPDecimal; - +use crate::types::MsgCreateSpotMarketOrder; +use prost::Message; pub fn create_stargate_msg(type_url: &str, value: Vec) -> StdResult> { Ok(CosmosMsg::Stargate { @@ -34,3 +35,9 @@ pub fn create_spot_market_order( }), } } + +pub(crate) fn encode_bytes_message(order_msg: &MsgCreateSpotMarketOrder) -> Vec { + let mut order_bytes = vec![]; + types::MsgCreateSpotMarketOrder::encode(&order_msg, &mut order_bytes).unwrap(); + order_bytes +} \ No newline at end of file diff --git a/contracts/injective-cosmwasm-mock/src/testing/query_exchange_test.rs b/contracts/injective-cosmwasm-mock/src/testing/query_exchange_test.rs index 00d4e925..418be8c0 100644 --- a/contracts/injective-cosmwasm-mock/src/testing/query_exchange_test.rs +++ b/contracts/injective-cosmwasm-mock/src/testing/query_exchange_test.rs @@ -8,7 +8,7 @@ use crate::{ }, }; -use crate::utils::execute_all_authorizations; +use crate::utils::{execute_all_authorizations, scale_price_quantity_for_spot_market_dec}; use cosmwasm_std::{Addr, Coin}; use injective_cosmwasm::exchange::response::QueryOrderbookResponse; use injective_cosmwasm::exchange::types::VolumeByType; @@ -473,35 +473,35 @@ fn test_query_spot_market_orderbook() { let res: QueryOrderbookResponse = wasm.query(&env.contract_address, &query_msg).unwrap(); let buys_price_level = res.buys_price_level; let sells_price_level = res.sells_price_level; - assert_eq!(buys_price_level.len(), 2); - assert_eq!(sells_price_level.len(), 2); + assert_eq!(buys_price_level.len(), 4); + assert_eq!(sells_price_level.len(), 4); assert_eq!( buys_price_level[0], PriceLevel { - p: human_to_dec(liquidity_orders[2].price.as_str(), QUOTE_DECIMALS - BASE_DECIMALS), - q: human_to_dec(liquidity_orders[2].quantity.as_str(), BASE_DECIMALS), + p: human_to_dec(liquidity_orders[sells_price_level.len()].price.as_str(), QUOTE_DECIMALS - BASE_DECIMALS), + q: human_to_dec(liquidity_orders[sells_price_level.len()].quantity.as_str(), BASE_DECIMALS), } ); assert_eq!( buys_price_level[1], PriceLevel { - p: human_to_dec(liquidity_orders[3].price.as_str(), QUOTE_DECIMALS - BASE_DECIMALS), - q: human_to_dec(liquidity_orders[3].quantity.as_str(), BASE_DECIMALS), + p: human_to_dec(liquidity_orders[sells_price_level.len()+1].price.as_str(), QUOTE_DECIMALS - BASE_DECIMALS), + q: human_to_dec(liquidity_orders[sells_price_level.len()+1].quantity.as_str(), BASE_DECIMALS), } ); assert_eq!( sells_price_level[0], PriceLevel { - p: human_to_dec(liquidity_orders[1].price.as_str(), QUOTE_DECIMALS - BASE_DECIMALS), - q: human_to_dec(liquidity_orders[1].quantity.as_str(), BASE_DECIMALS), + p: human_to_dec(liquidity_orders[sells_price_level.len()-1].price.as_str(), QUOTE_DECIMALS - BASE_DECIMALS), + q: human_to_dec(liquidity_orders[sells_price_level.len()-1].quantity.as_str(), BASE_DECIMALS), } ); assert_eq!( sells_price_level[1], PriceLevel { - p: human_to_dec(liquidity_orders[0].price.as_str(), QUOTE_DECIMALS - BASE_DECIMALS), - q: human_to_dec(liquidity_orders[0].quantity.as_str(), BASE_DECIMALS), + p: human_to_dec(liquidity_orders[sells_price_level.len()-2].price.as_str(), QUOTE_DECIMALS - BASE_DECIMALS), + q: human_to_dec(liquidity_orders[sells_price_level.len()-2].quantity.as_str(), BASE_DECIMALS), } ); } @@ -749,12 +749,17 @@ fn test_query_trader_transient_spot_orders() { execute_all_authorizations(&env.app, &env.users[0].account, env.contract_address.clone()); println!("{}", &env.users[0].account.address()); + add_spot_initial_liquidity(&env.app, market_id.clone()); + + let (scale_price, scale_quantity) = scale_price_quantity_for_spot_market_dec("9.8", "1", &BASE_DECIMALS, "E_DECIMALS); let res = wasm.execute( &env.contract_address, &ExecuteMsg::TestTraderTransientSpotOrders { market_id: MarketId::new(market_id).unwrap(), subaccount_id: subaccount_id.clone(), + price:dec_to_proto(scale_price), + quantity: dec_to_proto(scale_quantity), }, &[], &env.users[0].account, diff --git a/contracts/injective-cosmwasm-mock/src/utils.rs b/contracts/injective-cosmwasm-mock/src/utils.rs index d044d400..e3118bf1 100644 --- a/contracts/injective-cosmwasm-mock/src/utils.rs +++ b/contracts/injective-cosmwasm-mock/src/utils.rs @@ -319,7 +319,7 @@ pub fn launch_spot_market(exchange: &Exchange, signer: &Signin base_denom: BASE_DENOM.to_string(), 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("1000000000000000")), + min_quantity_tick_size: dec_to_proto(FPDecimal::must_from_str("1")), }, signer, ) @@ -437,6 +437,16 @@ pub fn add_spot_orders(app: &InjectiveTestApp, market_id: String, orders: Vec Vec { vec![ + HumanOrder { + price: "15".to_string(), + quantity: "10".to_string(), + order_type: OrderType::Sell, + }, + HumanOrder { + price: "12".to_string(), + quantity: "5".to_string(), + order_type: OrderType::Sell, + }, HumanOrder { price: "10.2".to_string(), quantity: "5".to_string(), @@ -457,6 +467,16 @@ pub fn get_initial_liquidity_orders_vector() -> Vec { quantity: "5".to_string(), order_type: OrderType::Buy, }, + HumanOrder { + price: "8".to_string(), + quantity: "5".to_string(), + order_type: OrderType::Buy, + }, + HumanOrder { + price: "5".to_string(), + quantity: "10".to_string(), + order_type: OrderType::Buy, + }, ] } @@ -664,13 +684,18 @@ pub fn dec_to_proto(val: FPDecimal) -> String { } pub fn scale_price_quantity_for_spot_market(price: &str, quantity: &str, base_decimals: &i32, quote_decimals: &i32) -> (String, String) { + let (scaled_price, scaled_quantity) = scale_price_quantity_for_spot_market_dec(price, quantity, base_decimals, quote_decimals); + (dec_to_proto(scaled_price), dec_to_proto(scaled_quantity)) +} + +pub fn scale_price_quantity_for_spot_market_dec(price: &str, quantity: &str, base_decimals: &i32, quote_decimals: &i32) -> (FPDecimal, FPDecimal) { let price_dec = FPDecimal::must_from_str(price.replace('_', "").as_str()); let quantity_dec = FPDecimal::must_from_str(quantity.replace('_', "").as_str()); let scaled_price = price_dec.scaled(quote_decimals - base_decimals); let scaled_quantity = quantity_dec.scaled(*base_decimals); - (dec_to_proto(scaled_price), dec_to_proto(scaled_quantity)) + (scaled_price, scaled_quantity) } pub fn scale_price_quantity_perp_market(price: &str, quantity: &str, margin_ratio: &str, quote_decimals: &i32) -> (String, String, String) {