Skip to content

Commit

Permalink
more check orders
Browse files Browse the repository at this point in the history
  • Loading branch information
jbernal87 committed Mar 4, 2024
1 parent cdcc995 commit b274e05
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 33 deletions.
83 changes: 73 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions contracts/injective-cosmwasm-mock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 6 additions & 8 deletions contracts/injective-cosmwasm-mock/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
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};
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 prost::Message;

const CONTRACT_NAME: &str = "crates.io:injective:dummy";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion contracts/injective-cosmwasm-mock/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}

Expand Down
9 changes: 8 additions & 1 deletion contracts/injective-cosmwasm-mock/src/order_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::types;
use cosmwasm_std::{to_json_binary, Coin, CosmosMsg, StdResult, SubMsg, Uint128, WasmMsg};

Check warning on line 2 in contracts/injective-cosmwasm-mock/src/order_management.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused imports: `Coin`, `SubMsg`, `Uint128`, `WasmMsg`, `to_json_binary`

Check warning on line 2 in contracts/injective-cosmwasm-mock/src/order_management.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused imports: `Coin`, `SubMsg`, `Uint128`, `WasmMsg`, `to_json_binary`
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<u8>) -> StdResult<CosmosMsg<InjectiveMsgWrapper>> {
Ok(CosmosMsg::Stargate {
Expand Down Expand Up @@ -34,3 +35,9 @@ pub fn create_spot_market_order(
}),
}
}

pub(crate) fn encode_bytes_message(order_msg: &MsgCreateSpotMarketOrder) -> Vec<u8> {
let mut order_bytes = vec![];
types::MsgCreateSpotMarketOrder::encode(&order_msg, &mut order_bytes).unwrap();
order_bytes
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
}
);
}
Expand Down Expand Up @@ -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, &QUOTE_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,
Expand Down
Loading

0 comments on commit b274e05

Please sign in to comment.