Skip to content

Commit

Permalink
derivative transient test
Browse files Browse the repository at this point in the history
  • Loading branch information
jbernal87 committed May 2, 2024
1 parent 232e4b5 commit 5a3b27c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
27 changes: 24 additions & 3 deletions contracts/injective-cosmwasm-stargate-example/src/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ pub struct QueryTraderSpotOrdersRequest {
pub subaccount_id: ::prost::alloc::string::String,
}

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, Eq, ::prost::Message, ::serde::Serialize, ::serde::Deserialize, ::schemars::JsonSchema)]
pub struct QueryTraderDerivativeOrdersRequest {
/// Market ID for the market
#[prost(string, tag = "1")]
#[serde(alias = "marketID")]
pub market_id: ::prost::alloc::string::String,
/// SubaccountID of the trader
#[prost(string, tag = "2")]
#[serde(alias = "subaccountID")]
pub subaccount_id: ::prost::alloc::string::String,
}

pub fn handle_create_order_reply_stargate(deps: DepsMut<InjectiveQueryWrapper>, _msg: &Reply) -> Result<Response, ContractError> {
let mut response_str = "Something went wrong".to_string();
if let Some(mut cache) = ORDER_CALL_CACHE.may_load(deps.storage)? {
Expand All @@ -44,13 +57,21 @@ pub fn handle_create_order_reply_stargate(deps: DepsMut<InjectiveQueryWrapper>,

pub fn handle_create_derivative_order_reply_stargate(deps: DepsMut<InjectiveQueryWrapper>, _msg: &Reply) -> Result<Response, ContractError> {
let mut response_str = "Something went wrong".to_string();
let querier: InjectiveQuerier = InjectiveQuerier::new(&deps.querier);

if let Some(mut cache) = ORDER_CALL_CACHE.may_load(deps.storage)? {
if !cache.is_empty() {
let order_info = &cache[0];
let response = querier.query_trader_transient_derivative_orders(&order_info.market_id, &order_info.subaccount);
response_str = format!("{:?}", &response);
let encode_query_message = encode_proto_message(QueryTraderDerivativeOrdersRequest {
market_id: order_info.market_id.clone().into(),
subaccount_id: order_info.subaccount.clone().into(),
});
let stargate_response = handle_query_stargate(&deps.querier, "/injective.exchange.v1beta1.Query/TraderDerivativeTransientOrders".to_string(), encode_query_message);
response_str = match stargate_response {
Ok(binary) => {
String::from_utf8(binary.0).unwrap_or_else(|e| format!("Failed to decode binary to string: {:?}", e))
},
Err(e) => format!("Error: {:?}", e),
};
cache.clear();
ORDER_CALL_CACHE.save(deps.storage, &cache)?;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ fn test_query_trader_transient_derivative_orders() {
.iter()
.find(|e| e.ty == "wasm-transient_derivative_order")
.and_then(|event| event.attributes.iter().find(|a| a.key == "query_str"));

println!("{:?}", transient_query);
assert!(transient_query.is_some());
let expected_order_info = "Ok(TraderDerivativeOrdersResponse { orders: Some([TrimmedDerivativeLimitOrder { price: FPDecimal { num: 9700000000000000000000000, sign: 1 }, quantity: FPDecimal { num: 100000000000000000, sign: 1 }, margin: FPDecimal { num: 1940000000000000000000000, sign: 1 }, fillable: FPDecimal { num: 100000000000000000, sign: 1 }, isBuy: true";
let expected_order_info = "{\"value\":\"{\\\"orders\\\":[{\\\"price\\\":\\\"9700000.000000000000000000\\\",\\\"quantity\\\":\\\"0.100000000000000000\\\",\\\"margin\\\":\\\"1940000.000000000000000000\\\",\\\"fillable\\\":\\\"0.100000000000000000\\\",\\\"isBuy\\\":true,";
assert!(transient_query.unwrap().value.contains(expected_order_info));
}

0 comments on commit 5a3b27c

Please sign in to comment.