From a8c5aeeb3e3be2de1dab22fd973a0c9c1e12dd1e Mon Sep 17 00:00:00 2001 From: Markus Waas Date: Fri, 28 Jun 2024 12:26:30 +0200 Subject: [PATCH] feat: add more stargate examples --- contracts/atomic-order-example/src/tests.rs | 1 + .../injective-cosmwasm-mock/src/contract.rs | 2 +- .../injective-cosmwasm-mock/src/handle.rs | 1 + .../src/testing/test_exchange.rs | 2 + .../src/testing/test_exchange_derivative.rs | 2 + .../injective-cosmwasm-mock/src/utils.rs | 2 + .../src/contract.rs | 14 +++-- .../src/handle.rs | 25 +++++++++ .../src/lib.rs | 1 + .../src/msg.rs | 10 +++- .../src/query.rs | 15 +++++- .../src/reply.rs | 6 +-- .../src/spot_market_order_msg.rs | 54 +++++++++++++++++++ .../src/testing/test_auction.rs | 6 +-- .../src/testing/test_auth.rs | 4 +- .../src/testing/test_bank.rs | 27 +++++++--- .../src/testing/test_exchange.rs | 34 ++++++++++-- .../src/testing/test_exchange_derivative.rs | 16 +++--- .../src/testing/test_oracle.rs | 8 ++- .../src/utils.rs | 4 +- 20 files changed, 195 insertions(+), 39 deletions(-) create mode 100644 contracts/injective-cosmwasm-stargate-example/src/spot_market_order_msg.rs diff --git a/contracts/atomic-order-example/src/tests.rs b/contracts/atomic-order-example/src/tests.rs index 7dc618c7..1f39b916 100644 --- a/contracts/atomic-order-example/src/tests.rs +++ b/contracts/atomic-order-example/src/tests.rs @@ -136,6 +136,7 @@ fn create_spot_market_handler() -> impl HandlesMarketIdQuery { status: MarketStatus::Active, min_price_tick_size: FPDecimal::from_str("0.000000000000001").unwrap(), min_quantity_tick_size: FPDecimal::from_str("1000000000000000").unwrap(), + min_notional: FPDecimal::ONE, }), }; SystemResult::Ok(ContractResult::from(to_json_binary(&response))) diff --git a/contracts/injective-cosmwasm-mock/src/contract.rs b/contracts/injective-cosmwasm-mock/src/contract.rs index 22e36d97..9ca4263f 100644 --- a/contracts/injective-cosmwasm-mock/src/contract.rs +++ b/contracts/injective-cosmwasm-mock/src/contract.rs @@ -1,6 +1,6 @@ use crate::{ error::ContractError, - handle::{handle_test_transient_derivative_order, handle_test_transient_spot_order}, + handle::{handle_test_market_spot_order, handle_test_transient_derivative_order, handle_test_transient_spot_order}, msg::{ExecuteMsg, InstantiateMsg, QueryMsg}, query::{ handle_aggregate_account_volume_query, handle_aggregate_market_volume_query, handle_contract_registration_info_query, diff --git a/contracts/injective-cosmwasm-mock/src/handle.rs b/contracts/injective-cosmwasm-mock/src/handle.rs index 39f6e40d..82caa25d 100644 --- a/contracts/injective-cosmwasm-mock/src/handle.rs +++ b/contracts/injective-cosmwasm-mock/src/handle.rs @@ -2,6 +2,7 @@ use crate::{ contract::{CREATE_DERIVATIVE_ORDER_REPLY_ID, CREATE_SPOT_ORDER_REPLY_ID, MSG_EXEC}, msg::{MSG_CREATE_DERIVATIVE_LIMIT_ORDER_ENDPOINT, MSG_CREATE_SPOT_LIMIT_ORDER_ENDPOINT}, order_management::{create_derivative_limit_order, create_spot_limit_order, create_stargate_msg, encode_bytes_message}, + spot_market_order_msg::create_spot_market_order_message, state::{CacheOrderInfo, ORDER_CALL_CACHE}, ContractError, }; diff --git a/contracts/injective-cosmwasm-mock/src/testing/test_exchange.rs b/contracts/injective-cosmwasm-mock/src/testing/test_exchange.rs index 9db7b659..506296c7 100644 --- a/contracts/injective-cosmwasm-mock/src/testing/test_exchange.rs +++ b/contracts/injective-cosmwasm-mock/src/testing/test_exchange.rs @@ -148,6 +148,7 @@ fn test_query_spot_market() { let ticker = "INJ/USDT".to_string(); let min_price_tick_size = FPDecimal::must_from_str("0.000000000000001"); let min_quantity_tick_size = FPDecimal::must_from_str("1000000000000000"); + let min_notional = FPDecimal::must_from_str("1"); exchange .instant_spot_market_launch( @@ -158,6 +159,7 @@ fn test_query_spot_market() { quote_denom: QUOTE_DENOM.to_string(), min_price_tick_size: dec_to_proto(min_price_tick_size), min_quantity_tick_size: dec_to_proto(min_quantity_tick_size), + min_notional: dec_to_proto(min_notional), }, &env.signer, ) diff --git a/contracts/injective-cosmwasm-mock/src/testing/test_exchange_derivative.rs b/contracts/injective-cosmwasm-mock/src/testing/test_exchange_derivative.rs index f5f42516..a9480c2d 100644 --- a/contracts/injective-cosmwasm-mock/src/testing/test_exchange_derivative.rs +++ b/contracts/injective-cosmwasm-mock/src/testing/test_exchange_derivative.rs @@ -49,6 +49,7 @@ fn test_query_derivative_market() { let maintenance_margin_ratio = FPDecimal::must_from_str("0.05"); let min_price_tick_size = FPDecimal::must_from_str("1000.0"); let min_quantity_tick_size = FPDecimal::must_from_str("1000000000000000"); + let min_notional = FPDecimal::must_from_str("1"); let quote_denom = QUOTE_DENOM.to_string(); let maker_fee_rate = FPDecimal::ZERO; let taker_fee_rate = FPDecimal::ZERO; @@ -69,6 +70,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: dec_to_proto(min_notional), }, &env.signer, ) diff --git a/contracts/injective-cosmwasm-mock/src/utils.rs b/contracts/injective-cosmwasm-mock/src/utils.rs index a3ce3efb..53cc8649 100644 --- a/contracts/injective-cosmwasm-mock/src/utils.rs +++ b/contracts/injective-cosmwasm-mock/src/utils.rs @@ -326,6 +326,7 @@ pub fn launch_spot_market(exchange: &Exchange, 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: dec_to_proto(FPDecimal::must_from_str("1")), }, signer, ) @@ -364,6 +365,7 @@ pub fn launch_perp_market(exchange: &Exchange, signer: &Signin maintenance_margin_ratio: "50000000000000000".to_owned(), min_price_tick_size: "1000000000000000000000".to_owned(), min_quantity_tick_size: "1000000000000000".to_owned(), + min_notional: dec_to_proto(FPDecimal::must_from_str("1")), }, signer, ) diff --git a/contracts/injective-cosmwasm-stargate-example/src/contract.rs b/contracts/injective-cosmwasm-stargate-example/src/contract.rs index aec4b87a..3e23266a 100644 --- a/contracts/injective-cosmwasm-stargate-example/src/contract.rs +++ b/contracts/injective-cosmwasm-stargate-example/src/contract.rs @@ -1,8 +1,8 @@ use crate::{ error::ContractError, - handle::{handle_test_transient_derivative_order, handle_test_transient_spot_order}, + handle::{handle_test_market_spot_order, handle_test_transient_derivative_order, handle_test_transient_spot_order}, msg::{ExecuteMsg, InstantiateMsg, QueryMsg}, - query::handle_query_stargate, + query::{handle_query_bank_params, handle_query_spot_market, handle_query_stargate_raw}, reply::{handle_create_derivative_order_reply_stargate, handle_create_order_reply_stargate}, }; use cosmwasm_std::{entry_point, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdResult}; @@ -34,6 +34,12 @@ pub fn execute( price, quantity, } => handle_test_transient_spot_order(deps, env, &info, market_id, subaccount_id, price, quantity), + ExecuteMsg::TestMarketOrderStargate { + market_id, + subaccount_id, + price, + quantity, + } => handle_test_market_spot_order(deps, env.contract.address.as_str(), market_id, subaccount_id, price, quantity), ExecuteMsg::TestTraderTransientDerivativeOrders { market_id, subaccount_id, @@ -47,7 +53,9 @@ pub fn execute( #[cfg_attr(not(feature = "library"), entry_point)] pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { match msg { - QueryMsg::QueryStargate { path, query_request } => handle_query_stargate(&deps.querier, path, query_request), + QueryMsg::QueryStargateRaw { path, query_request } => handle_query_stargate_raw(&deps.querier, path, query_request), + QueryMsg::QueryBankParams {} => handle_query_bank_params(deps), + QueryMsg::QuerySpotMarket { market_id } => handle_query_spot_market(deps, &market_id), } } diff --git a/contracts/injective-cosmwasm-stargate-example/src/handle.rs b/contracts/injective-cosmwasm-stargate-example/src/handle.rs index db93e335..8c6bdc0d 100644 --- a/contracts/injective-cosmwasm-stargate-example/src/handle.rs +++ b/contracts/injective-cosmwasm-stargate-example/src/handle.rs @@ -2,6 +2,7 @@ use crate::{ contract::{CREATE_DERIVATIVE_ORDER_REPLY_ID, CREATE_SPOT_ORDER_REPLY_ID}, msg::{MSG_CREATE_DERIVATIVE_LIMIT_ORDER_ENDPOINT, MSG_CREATE_SPOT_LIMIT_ORDER_ENDPOINT}, order_management::{create_derivative_limit_order, create_spot_limit_order, create_stargate_msg, encode_bytes_message}, + spot_market_order_msg::create_spot_market_order_message, state::{CacheOrderInfo, ORDER_CALL_CACHE}, ContractError, }; @@ -12,6 +13,30 @@ use injective_math::{scale::Scaled, FPDecimal}; pub const MSG_EXEC: &str = "/cosmos.authz.v1beta1.MsgExec"; +pub fn handle_test_market_spot_order( + deps: DepsMut, + sender: &str, + market_id: MarketId, + subaccount_id: SubaccountId, + price: String, + quantity: String, +) -> Result, ContractError> { + let querier = InjectiveQuerier::new(&deps.querier); + let spot_market = querier.query_spot_market(&market_id).unwrap().market.unwrap(); + + let order_msg = create_spot_market_order_message( + FPDecimal::must_from_str(price.as_str()), + FPDecimal::must_from_str(quantity.as_str()), + OrderType::Sell, + sender, + subaccount_id.as_str(), + "", + &spot_market, + )?; + + Ok(Response::new().add_message(order_msg)) +} + pub fn handle_test_transient_spot_order( deps: DepsMut, env: Env, diff --git a/contracts/injective-cosmwasm-stargate-example/src/lib.rs b/contracts/injective-cosmwasm-stargate-example/src/lib.rs index 44ab7abb..39968d45 100644 --- a/contracts/injective-cosmwasm-stargate-example/src/lib.rs +++ b/contracts/injective-cosmwasm-stargate-example/src/lib.rs @@ -6,6 +6,7 @@ pub mod msg; mod order_management; mod query; mod reply; +mod spot_market_order_msg; mod state; #[cfg(test)] mod testing; diff --git a/contracts/injective-cosmwasm-stargate-example/src/msg.rs b/contracts/injective-cosmwasm-stargate-example/src/msg.rs index 4efaae39..b3f913ee 100644 --- a/contracts/injective-cosmwasm-stargate-example/src/msg.rs +++ b/contracts/injective-cosmwasm-stargate-example/src/msg.rs @@ -25,12 +25,20 @@ pub enum ExecuteMsg { quantity: String, margin: String, }, + TestMarketOrderStargate { + market_id: MarketId, + subaccount_id: SubaccountId, + price: String, + quantity: String, + }, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum QueryMsg { - QueryStargate { path: String, query_request: String }, + QueryStargateRaw { path: String, query_request: String }, + QueryBankParams {}, + QuerySpotMarket { market_id: String }, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] diff --git a/contracts/injective-cosmwasm-stargate-example/src/query.rs b/contracts/injective-cosmwasm-stargate-example/src/query.rs index 807d27c3..5ac86cd9 100644 --- a/contracts/injective-cosmwasm-stargate-example/src/query.rs +++ b/contracts/injective-cosmwasm-stargate-example/src/query.rs @@ -1,10 +1,11 @@ use crate::msg::QueryStargateResponse; use base64::engine::general_purpose::STANDARD as BASE64_STANDARD; use base64::Engine as _; -use cosmwasm_std::{to_json_binary, to_json_vec, Binary, ContractResult, QuerierWrapper, QueryRequest, StdError, StdResult, SystemResult}; +use cosmwasm_std::{to_json_binary, to_json_vec, Binary, ContractResult, Deps, QuerierWrapper, QueryRequest, StdError, StdResult, SystemResult}; use injective_cosmwasm::InjectiveQueryWrapper; +use injective_std::types::{cosmos::bank::v1beta1::BankQuerier, injective::exchange::v1beta1::ExchangeQuerier}; -pub fn handle_query_stargate(querier: &QuerierWrapper, path: String, query_request: String) -> StdResult { +pub fn handle_query_stargate_raw(querier: &QuerierWrapper, path: String, query_request: String) -> StdResult { let data = Binary::from_base64(&query_request)?; let request = &QueryRequest::::Stargate { path, data }; let raw = to_json_vec(request).map_err(|serialize_err| StdError::generic_err(format!("Serializing QueryRequest: {}", serialize_err)))?; @@ -23,3 +24,13 @@ pub fn handle_query_stargate(querier: &QuerierWrapper, pa value: String::from_utf8(decoded_value)?, }) } + +pub fn handle_query_spot_market(deps: Deps, market_id: &str) -> StdResult { + let querier = ExchangeQuerier::new(&deps.querier); + to_json_binary(&querier.spot_market(market_id.to_string())?) +} + +pub fn handle_query_bank_params(deps: Deps) -> StdResult { + let querier = BankQuerier::new(&deps.querier); + to_json_binary(&querier.params()?) +} diff --git a/contracts/injective-cosmwasm-stargate-example/src/reply.rs b/contracts/injective-cosmwasm-stargate-example/src/reply.rs index 02455269..17f076ad 100644 --- a/contracts/injective-cosmwasm-stargate-example/src/reply.rs +++ b/contracts/injective-cosmwasm-stargate-example/src/reply.rs @@ -1,4 +1,4 @@ -use crate::{encode_helper::encode_proto_message, query::handle_query_stargate, state::ORDER_CALL_CACHE, ContractError}; +use crate::{encode_helper::encode_proto_message, query::handle_query_stargate_raw, state::ORDER_CALL_CACHE, ContractError}; use cosmwasm_std::{DepsMut, Event, Reply, Response}; use injective_cosmwasm::InjectiveQueryWrapper; @@ -37,7 +37,7 @@ pub fn handle_create_order_reply_stargate(deps: DepsMut, market_id: order_info.market_id.clone().into(), subaccount_id: order_info.subaccount.clone().into(), }); - let stargate_response = handle_query_stargate( + let stargate_response = handle_query_stargate_raw( &deps.querier, "/injective.exchange.v1beta1.Query/TraderSpotTransientOrders".to_string(), encode_query_message, @@ -64,7 +64,7 @@ pub fn handle_create_derivative_order_reply_stargate(deps: DepsMut StdResult> { + let msg = create_spot_market_order(price, quantity, order_type, sender, subaccount_id, fee_recipient, market); + + let mut order_bytes = vec![]; + Exchange::MsgCreateSpotMarketOrder::encode(&msg, &mut order_bytes).unwrap(); + + Ok(CosmosMsg::Stargate { + type_url: Exchange::MsgCreateSpotMarketOrder::TYPE_URL.to_string(), + value: order_bytes.into(), + }) +} + +fn create_spot_market_order( + price: FPDecimal, + quantity: FPDecimal, + order_type: OrderType, + sender: &str, + subaccount_id: &str, + fee_recipient: &str, + market: &SpotMarket, +) -> Exchange::MsgCreateSpotMarketOrder { + let rounded_quantity = round_to_min_tick(quantity, market.min_quantity_tick_size); + let rounded_price = round_to_nearest_tick(price, market.min_price_tick_size); + + Exchange::MsgCreateSpotMarketOrder { + sender: sender.to_string(), + order: Some(Exchange::SpotOrder { + market_id: market.market_id.as_str().into(), + order_info: Some(Exchange::OrderInfo { + subaccount_id: subaccount_id.to_string(), + fee_recipient: fee_recipient.to_string(), + price: rounded_price.to_proto_string(), + quantity: rounded_quantity.to_proto_string(), + cid: "".to_string(), + }), + order_type: order_type as i32, + trigger_price: "".to_string(), + }), + } +} diff --git a/contracts/injective-cosmwasm-stargate-example/src/testing/test_auction.rs b/contracts/injective-cosmwasm-stargate-example/src/testing/test_auction.rs index fbd7dc38..544b8c03 100644 --- a/contracts/injective-cosmwasm-stargate-example/src/testing/test_auction.rs +++ b/contracts/injective-cosmwasm-stargate-example/src/testing/test_auction.rs @@ -48,7 +48,7 @@ pub struct LastAuctionResult { fn test_current_auction_basket() { let env = Setup::new(ExchangeType::None); let wasm = Wasm::new(&env.app); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.auction.v1beta1.Query/CurrentAuctionBasket".to_string(), query_request: "".to_string(), }; @@ -69,7 +69,7 @@ fn test_current_auction_basket() { fn test_auction_params() { let env = Setup::new(ExchangeType::None); let wasm = Wasm::new(&env.app); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.auction.v1beta1.Query/AuctionParams".to_string(), query_request: "".to_string(), }; @@ -87,7 +87,7 @@ fn test_auction_params() { fn test_last_auction_result() { let env = Setup::new(ExchangeType::None); let wasm = Wasm::new(&env.app); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.auction.v1beta1.Query/LastAuctionResult".to_string(), query_request: "".to_string(), }; diff --git a/contracts/injective-cosmwasm-stargate-example/src/testing/test_auth.rs b/contracts/injective-cosmwasm-stargate-example/src/testing/test_auth.rs index dc5bf2e0..6bf84b1a 100644 --- a/contracts/injective-cosmwasm-stargate-example/src/testing/test_auth.rs +++ b/contracts/injective-cosmwasm-stargate-example/src/testing/test_auth.rs @@ -12,7 +12,7 @@ use injective_test_tube::{Account, Module, Wasm}; fn test_query_auth_params() { let env = Setup::new(ExchangeType::None); let wasm = Wasm::new(&env.app); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/cosmos.auth.v1beta1.Query/Params".to_string(), query_request: "".to_string(), }; @@ -30,7 +30,7 @@ fn test_query_auth_account() { let wasm = Wasm::new(&env.app); let user_address = env.users[0].account.address().to_string(); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/cosmos.auth.v1beta1.Query/Account".to_string(), query_request: encode_proto_message(QueryAccountRequest { address: user_address.to_owned(), diff --git a/contracts/injective-cosmwasm-stargate-example/src/testing/test_bank.rs b/contracts/injective-cosmwasm-stargate-example/src/testing/test_bank.rs index 3e4280fe..bd6f4c71 100644 --- a/contracts/injective-cosmwasm-stargate-example/src/testing/test_bank.rs +++ b/contracts/injective-cosmwasm-stargate-example/src/testing/test_bank.rs @@ -1,20 +1,31 @@ use crate::{ encode_helper::encode_proto_message, msg::{QueryMsg, QueryStargateResponse}, - testing::type_helpers::{BankParams, ParamResponse, QueryBalanceResponse, QuerySupplyOffResponse, QueryDenomMetadataResponse}, + testing::type_helpers::{BankParams, ParamResponse, QueryBalanceResponse, QueryDenomMetadataResponse, QuerySupplyOffResponse}, utils::{ExchangeType, Setup}, }; use cosmos_sdk_proto::cosmos::bank::v1beta1::{QueryBalanceRequest, QueryDenomMetadataRequest, QuerySupplyOfRequest}; use cosmwasm_std::{Coin, Uint128}; -use injective_test_tube::{Account, Module, TokenFactory, Wasm}; use injective_std::types::injective::tokenfactory::v1beta1::MsgCreateDenom; +use injective_test_tube::{Account, Module, TokenFactory, Wasm}; #[test] #[cfg_attr(not(feature = "integration"), ignore)] fn test_query_bank_params() { let env = Setup::new(ExchangeType::None); let wasm = Wasm::new(&env.app); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryBankParams {}; + + let contract_response: ParamResponse = wasm.query(&env.contract_address, &query_msg).unwrap(); + assert!(contract_response.params.default_send_enabled); +} + +#[test] +#[cfg_attr(not(feature = "integration"), ignore)] +fn test_query_bank_params_raw() { + let env = Setup::new(ExchangeType::None); + let wasm = Wasm::new(&env.app); + let query_msg = QueryMsg::QueryStargateRaw { path: "/cosmos.bank.v1beta1.Query/Params".to_string(), query_request: "".to_string(), }; @@ -22,7 +33,7 @@ fn test_query_bank_params() { let contract_response: QueryStargateResponse = wasm.query(&env.contract_address, &query_msg).unwrap(); let contract_response = contract_response.value; let response: ParamResponse = serde_json::from_str(&contract_response).unwrap(); - assert_eq!(response.params.default_send_enabled, true); + assert!(response.params.default_send_enabled); } #[test] @@ -35,11 +46,13 @@ fn test_query_denom_metadata() { let create_denom_msg = MsgCreateDenom { sender: env.users[0].account.address().to_string(), subdenom: "cw".to_string(), + name: "CosmWasm".to_string(), + symbol: "CW".to_string(), }; let denom = token_factory.create_denom(create_denom_msg, &env.users[0].account).unwrap(); let denom_name = denom.data.new_token_denom; - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/cosmos.bank.v1beta1.Query/DenomMetadata".to_string(), query_request: encode_proto_message(QueryDenomMetadataRequest { denom: denom_name.to_owned(), @@ -58,7 +71,7 @@ fn test_query_bank_balance() { let env = Setup::new(ExchangeType::None); let wasm = Wasm::new(&env.app); let user_address = env.users[0].account.address().to_string(); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/cosmos.bank.v1beta1.Query/Balance".to_string(), query_request: encode_proto_message(QueryBalanceRequest { address: user_address.to_owned(), @@ -83,7 +96,7 @@ fn test_query_bank_balance() { fn test_query_supply_of() { let env = Setup::new(ExchangeType::None); let wasm = Wasm::new(&env.app); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/cosmos.bank.v1beta1.Query/SupplyOf".to_string(), query_request: encode_proto_message(QuerySupplyOfRequest { denom: "inj".to_string() }), }; diff --git a/contracts/injective-cosmwasm-stargate-example/src/testing/test_exchange.rs b/contracts/injective-cosmwasm-stargate-example/src/testing/test_exchange.rs index d002291b..72087e59 100644 --- a/contracts/injective-cosmwasm-stargate-example/src/testing/test_exchange.rs +++ b/contracts/injective-cosmwasm-stargate-example/src/testing/test_exchange.rs @@ -7,7 +7,7 @@ use crate::{ ExchangeType, Setup, BASE_DECIMALS, BASE_DENOM, QUOTE_DECIMALS, }, }; -use cosmwasm_std::{from_json, Addr}; +use cosmwasm_std::{from_json, Addr, Coin}; use injective_cosmwasm::{checked_address_to_subaccount_id, MarketId, SubaccountDepositResponse}; use injective_std::types::injective::exchange::v1beta1::{Deposit, MsgDeposit, QuerySubaccountDepositRequest, QuerySubaccountDepositsRequest}; use injective_test_tube::{Account, Exchange, Module, Wasm}; @@ -18,7 +18,7 @@ fn test_exchange_param() { let env = Setup::new(ExchangeType::None); let wasm = Wasm::new(&env.app); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.exchange.v1beta1.Query/QueryExchangeParams".to_string(), query_request: "".to_string(), }; @@ -82,7 +82,7 @@ fn test_query_subaccount_deposit() { } ); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.exchange.v1beta1.Query/SubaccountDeposit".to_string(), query_request: encode_proto_message(QuerySubaccountDepositRequest { subaccount_id: subaccount_id.to_string(), @@ -134,3 +134,31 @@ fn test_query_trader_transient_spot_orders() { let expected_order_info = "{\"value\":\"{\\\"orders\\\":[{\\\"price\\\":\\\"0.000000000009800000\\\",\\\"quantity\\\":\\\"1000000000000000000.000000000000000000\\\",\\\"fillable\\\":\\\"1000000000000000000.000000000000000000\\\",\\\"isBuy\\\":false,"; assert!(transient_query.unwrap().value.contains(expected_order_info)); } + +#[test] +#[cfg_attr(not(feature = "integration"), ignore)] +fn test_query_trader_spot_market_order() { + let env = Setup::new(ExchangeType::Spot); + let wasm = Wasm::new(&env.app); + let market_id = env.market_id.unwrap(); + + let subaccount_id = checked_address_to_subaccount_id(&Addr::unchecked(env.contract_address.to_owned()), 0u32); + + execute_all_authorizations(&env.app, &env.users[0].account, env.contract_address.clone()); + 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); + + wasm.execute( + &env.contract_address, + &ExecuteMsg::TestMarketOrderStargate { + market_id: MarketId::new(market_id).unwrap(), + subaccount_id: subaccount_id.clone(), + price: scale_price.to_string(), + quantity: scale_quantity.to_string(), + }, + &[Coin::new(1000000000000000000000, "inj")], + &env.users[0].account, + ) + .unwrap(); +} diff --git a/contracts/injective-cosmwasm-stargate-example/src/testing/test_exchange_derivative.rs b/contracts/injective-cosmwasm-stargate-example/src/testing/test_exchange_derivative.rs index 2db473a4..26f46fb9 100644 --- a/contracts/injective-cosmwasm-stargate-example/src/testing/test_exchange_derivative.rs +++ b/contracts/injective-cosmwasm-stargate-example/src/testing/test_exchange_derivative.rs @@ -32,7 +32,7 @@ fn test_query_perpetual_market_info() { let derivative_market_id = get_perpetual_market_id(&exchange, ticker.to_owned()); let market_id = MarketId::new(derivative_market_id.clone()).unwrap(); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.exchange.v1beta1.Query/PerpetualMarketInfo".to_string(), query_request: encode_proto_message(QueryPerpetualMarketInfoRequest { market_id: market_id.to_owned().into(), @@ -86,7 +86,7 @@ fn test_query_derivative_market() { .unwrap(); let derivative_market_id = get_perpetual_market_id(&exchange, ticker.to_owned()); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.exchange.v1beta1.Query/DerivativeMarket".to_string(), query_request: encode_proto_message(QueryDerivativeMarketRequest { market_id: derivative_market_id.to_owned(), @@ -130,7 +130,7 @@ fn test_query_effective_subaccount_position() { margin, ); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.exchange.v1beta1.Query/SubaccountEffectivePositionInMarket".to_string(), query_request: encode_proto_message(QuerySubaccountEffectivePositionInMarketRequest { market_id: market_id.to_owned(), @@ -167,7 +167,7 @@ fn test_query_vanilla_subaccount_position() { margin, ); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.exchange.v1beta1.Query/SubaccountPositionInMarket".to_string(), query_request: encode_proto_message(QuerySubaccountPositionInMarketRequest { subaccount_id: subaccount_id.to_string(), @@ -214,7 +214,7 @@ fn test_query_trader_derivative_orders() { margin, ); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.exchange.v1beta1.Query/TraderDerivativeOrders".to_string(), query_request: encode_proto_message(QueryTraderDerivativeOrdersRequest { subaccount_id: subaccount_id.to_string(), @@ -250,7 +250,7 @@ fn test_query_perpetual_market_funding() { let exchange = Exchange::new(&env.app); let ticker = "INJ/USDT".to_string(); let derivative_market_id = get_perpetual_market_id(&exchange, ticker.to_owned()); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.exchange.v1beta1.Query/PerpetualMarketFunding".to_string(), query_request: encode_proto_message(QueryPerpetualMarketFundingRequest { market_id: derivative_market_id.to_owned(), @@ -271,7 +271,7 @@ fn test_query_derivative_market_mid_price_and_tob() { let market_id = env.market_id.unwrap(); add_perp_initial_liquidity(&env.app, market_id.to_owned()); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.exchange.v1beta1.Query/DerivativeMidPriceAndTOB".to_string(), query_request: encode_proto_message(QueryDerivativeMidPriceAndTobRequest { market_id: market_id.to_owned(), @@ -293,7 +293,7 @@ fn test_query_derivative_market_orderbook() { let liquidity_orders = get_initial_perp_liquidity_orders_vector(); add_derivative_orders(&env.app, market_id.clone(), liquidity_orders.to_owned(), None); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.exchange.v1beta1.Query/DerivativeOrderbook".to_string(), query_request: encode_proto_message(QueryDerivativeOrderbookRequest { market_id: market_id.to_owned(), diff --git a/contracts/injective-cosmwasm-stargate-example/src/testing/test_oracle.rs b/contracts/injective-cosmwasm-stargate-example/src/testing/test_oracle.rs index c6ebf71c..2efe2651 100644 --- a/contracts/injective-cosmwasm-stargate-example/src/testing/test_oracle.rs +++ b/contracts/injective-cosmwasm-stargate-example/src/testing/test_oracle.rs @@ -21,7 +21,7 @@ fn test_query_oracle_price() { let wasm = Wasm::new(&env.app); let oracle = Oracle::new(&env.app); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.oracle.v1beta1.Query/OraclePrice".to_string(), query_request: encode_proto_message(QueryOraclePriceRequest { oracle_type: OracleType::PriceFeed as i32, @@ -59,13 +59,11 @@ fn test_query_oracle_volatility() { let base_info = Some(OracleInfo { symbol: env.denoms["base"].to_owned(), oracle_type: OracleType::PriceFeed as i32, - scale_factor: 6u32, }); let quote_info = Some(OracleInfo { symbol: env.denoms["quote"].to_owned(), oracle_type: OracleType::PriceFeed as i32, - scale_factor: 6u32, }); let oracle_history_options = Some(OracleHistoryOptions { @@ -74,7 +72,7 @@ fn test_query_oracle_volatility() { include_metadata: true, }); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.oracle.v1beta1.Query/OracleVolatility".to_string(), query_request: encode_proto_message(QueryOracleVolatilityRequest { base_info, @@ -108,7 +106,7 @@ fn test_query_pyth_oracle_price() { .unwrap(); let price_pyth_oracle_response = FPDecimal::must_from_str(price_pyth_oracle_response.price_state.unwrap().ema_price.as_str()); - let query_msg = QueryMsg::QueryStargate { + let query_msg = QueryMsg::QueryStargateRaw { path: "/injective.oracle.v1beta1.Query/PythPrice".to_string(), query_request: encode_proto_message(QueryPythPriceRequest { price_id: INJ_PYTH_PRICE_ID.to_string(), diff --git a/contracts/injective-cosmwasm-stargate-example/src/utils.rs b/contracts/injective-cosmwasm-stargate-example/src/utils.rs index 2d65e8ae..ce9a3a22 100644 --- a/contracts/injective-cosmwasm-stargate-example/src/utils.rs +++ b/contracts/injective-cosmwasm-stargate-example/src/utils.rs @@ -416,6 +416,7 @@ pub fn add_spot_order_as(app: &InjectiveTestApp, market_id: String, trader: &Use fee_recipient: trader.account.address(), price, quantity, + cid: "".to_string(), }), order_type: order_type.into(), trigger_price: "".to_string(), @@ -541,6 +542,7 @@ pub fn add_derivative_order_as( fee_recipient: trader.address(), price, quantity, + cid: "".to_string(), }), margin, order_type: order_type.into(), @@ -838,7 +840,7 @@ pub fn create_some_usdt_price_attestation(human_price: &str, decimal_precision: pub fn get_stargate_query_result(contract_response: RunnerResult) -> serde_json::Result { let contract_response = contract_response.unwrap().value; serde_json::from_str::(&contract_response).map_err(|error| { - println!("{} \n {}", error.to_string(), contract_response); + println!("{} \n {}", error, contract_response); error }) }