Skip to content

Commit

Permalink
added orderbook print
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrobot committed Mar 4, 2024
1 parent b274e05 commit 74ce1c4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
14 changes: 9 additions & 5 deletions contracts/injective-cosmwasm-mock/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cosmos_sdk_proto::{cosmos::authz::v1beta1::MsgExec, traits::Message, Any};
use cosmwasm_std::{entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, ReplyOn, Response, StdResult, SubMsg};

Check warning on line 9 in contracts/injective-cosmwasm-mock/src/contract.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `ReplyOn`

Check warning on line 9 in contracts/injective-cosmwasm-mock/src/contract.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `ReplyOn`
use cw2::set_contract_version;
use injective_cosmwasm::{create_deposit_msg, InjectiveMsgWrapper, InjectiveQuerier, InjectiveQueryWrapper, OrderType};
use injective_math::FPDecimal;
use injective_math::{scale::Scaled, FPDecimal};

const CONTRACT_NAME: &str = "crates.io:injective:dummy";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -35,15 +35,19 @@ 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, price, quantity } => {
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(price.as_str()),
FPDecimal::must_from_str(quantity.as_str()),
FPDecimal::must_from_str(price.as_str()).scaled(18),
FPDecimal::must_from_str(quantity.as_str()).scaled(18),
OrderType::Buy,
&info.sender.as_str(),
subaccount_id.as_str(),
Expand Down
20 changes: 15 additions & 5 deletions contracts/injective-cosmwasm-mock/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@ use injective_math::FPDecimal;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

pub const MSG_CREATE_SPOT_MARKET_ORDER_ENDPOINT: &str =
"/injective.exchange.v1beta1.MsgCreateSpotMarketOrder";
pub const MSG_CREATE_SPOT_MARKET_ORDER_ENDPOINT: &str = "/injective.exchange.v1beta1.MsgCreateSpotMarketOrder";

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct InstantiateMsg {}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
TestDepositMsg { subaccount_id: SubaccountId, amount: Coin },
TestTraderTransientSpotOrders { market_id: MarketId, subaccount_id: SubaccountId, price:String, quantity: String },
TestTraderTransientDerivativeOrders { market_id: MarketId, subaccount_id: SubaccountId },
TestDepositMsg {
subaccount_id: SubaccountId,
amount: Coin,
},
TestTraderTransientSpotOrders {
market_id: MarketId,
subaccount_id: SubaccountId,
price: String,
quantity: String,
},
TestTraderTransientDerivativeOrders {
market_id: MarketId,
subaccount_id: SubaccountId,
},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use injective_cosmwasm::{
use injective_math::FPDecimal;
use injective_std::types::injective::exchange::v1beta1::{
Deposit, MsgDeposit, MsgInstantPerpetualMarketLaunch, MsgInstantSpotMarketLaunch, OrderType, QueryAggregateMarketVolumeResponse,
QuerySubaccountDepositsRequest,
QuerySpotOrderbookRequest, QuerySubaccountDepositsRequest,
};
use injective_test_tube::injective_cosmwasm::get_default_subaccount_id_for_checked_address;
use injective_test_tube::{Account, Exchange, Module, RunnerResult, Wasm};
Expand Down Expand Up @@ -485,23 +485,32 @@ fn test_query_spot_market_orderbook() {
assert_eq!(
buys_price_level[1],
PriceLevel {
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),
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[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),
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[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),
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),
}
);
}
Expand Down Expand Up @@ -742,6 +751,7 @@ fn test_query_derivative_orders_to_cancel_up_to_amount() {}
#[cfg_attr(not(feature = "integration"), ignore)]
fn test_query_trader_transient_spot_orders() {
let env = Setup::new(ExchangeType::Spot);
let exchange = Exchange::new(&env.app);
let wasm = Wasm::new(&env.app);
let market_id = env.market_id.unwrap();

Expand All @@ -753,13 +763,26 @@ fn test_query_trader_transient_spot_orders() {

let (scale_price, scale_quantity) = scale_price_quantity_for_spot_market_dec("9.8", "1", &BASE_DECIMALS, &QUOTE_DECIMALS);

println!("{:?}", scale_price);
println!("{:?}", scale_quantity);

let res = exchange.query_spot_market_orderbook(&QuerySpotOrderbookRequest {
market_id: market_id.clone(),
order_side: 0i32,
limit_cumulative_quantity: "".to_string(),
limit_cumulative_notional: "".to_string(),
limit: 0u64,
});

println!("{:#?}", res);

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),
price: scale_price.to_string(),
quantity: scale_quantity.to_string(),
},
&[],
&env.users[0].account,
Expand Down

0 comments on commit 74ce1c4

Please sign in to comment.