-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add reply for transient test esqueleton
- Loading branch information
Showing
11 changed files
with
304 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
contracts/injective-cosmwasm-stargate-example/src/handle.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,114 @@ | ||
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}, | ||
state::{CacheOrderInfo, ORDER_CALL_CACHE}, | ||
ContractError, | ||
}; | ||
use cosmos_sdk_proto::{cosmos::authz::v1beta1::MsgExec, Any}; | ||
use cosmwasm_std::{DepsMut, Env, MessageInfo, Response, SubMsg}; | ||
use injective_cosmwasm::{InjectiveMsgWrapper, InjectiveQuerier, InjectiveQueryWrapper, MarketId, OrderType, SubaccountId}; | ||
use injective_math::{scale::Scaled, FPDecimal}; | ||
|
||
pub const MSG_EXEC: &str = "/cosmos.authz.v1beta1.MsgExec"; | ||
|
||
pub fn handle_test_transient_spot_order( | ||
deps: DepsMut<InjectiveQueryWrapper>, | ||
env: Env, | ||
info: &MessageInfo, | ||
market_id: MarketId, | ||
subaccount_id: SubaccountId, | ||
price: String, | ||
quantity: String, | ||
) -> Result<Response<InjectiveMsgWrapper>, ContractError> { | ||
let querier = InjectiveQuerier::new(&deps.querier); | ||
let spot_market = querier.query_spot_market(&market_id).unwrap().market.unwrap(); | ||
|
||
let order_msg = create_spot_limit_order( | ||
FPDecimal::must_from_str(price.as_str()).scaled(18i32), | ||
FPDecimal::must_from_str(quantity.as_str()).scaled(18i32), | ||
OrderType::Sell, | ||
info.sender.as_str(), | ||
subaccount_id.as_str(), | ||
&spot_market, | ||
); | ||
|
||
let order_bytes = encode_bytes_message(&order_msg).unwrap(); | ||
|
||
let msg_exec = MsgExec { | ||
grantee: env.contract.address.to_string(), | ||
msgs: vec![Any { | ||
type_url: MSG_CREATE_SPOT_LIMIT_ORDER_ENDPOINT.to_string(), | ||
value: order_bytes, | ||
}], | ||
}; | ||
|
||
let order_submessage = SubMsg::reply_on_success( | ||
create_stargate_msg(MSG_EXEC, msg_exec.encode_to_vec()).unwrap(), | ||
CREATE_SPOT_ORDER_REPLY_ID, | ||
); | ||
|
||
save_cache_info(deps, market_id, subaccount_id)?; | ||
|
||
Ok(Response::new().add_submessage(order_submessage)) | ||
} | ||
|
||
pub fn handle_test_transient_derivative_order( | ||
deps: DepsMut<InjectiveQueryWrapper>, | ||
env: Env, | ||
info: &MessageInfo, | ||
market_id: MarketId, | ||
subaccount_id: SubaccountId, | ||
price: String, | ||
quantity: String, | ||
margin: String, | ||
) -> Result<Response<InjectiveMsgWrapper>, ContractError> { | ||
let querier: InjectiveQuerier = InjectiveQuerier::new(&deps.querier); | ||
let market = querier.query_derivative_market(&market_id).unwrap().market.unwrap(); | ||
|
||
let order_msg = create_derivative_limit_order( | ||
FPDecimal::must_from_str(price.as_str()).scaled(18i32), | ||
FPDecimal::must_from_str(quantity.as_str()).scaled(18i32), | ||
FPDecimal::must_from_str(margin.as_str()).scaled(18i32), | ||
OrderType::Buy, | ||
info.sender.as_str(), | ||
subaccount_id.as_str(), | ||
&market, | ||
); | ||
|
||
let order_bytes = encode_bytes_message(&order_msg).unwrap(); | ||
|
||
let msg_exec = MsgExec { | ||
grantee: env.contract.address.to_string(), | ||
msgs: vec![Any { | ||
type_url: MSG_CREATE_DERIVATIVE_LIMIT_ORDER_ENDPOINT.to_string(), | ||
value: order_bytes, | ||
}], | ||
}; | ||
|
||
let order_submessage = SubMsg::reply_on_success( | ||
create_stargate_msg(MSG_EXEC, msg_exec.encode_to_vec()).unwrap(), | ||
CREATE_DERIVATIVE_ORDER_REPLY_ID, | ||
); | ||
|
||
save_cache_info(deps, market_id, subaccount_id)?; | ||
|
||
Ok(Response::new().add_submessage(order_submessage)) | ||
} | ||
|
||
fn save_cache_info(deps: DepsMut<InjectiveQueryWrapper>, market_id: MarketId, subaccount_id: SubaccountId) -> Result<(), ContractError> { | ||
let cache_order_info = CacheOrderInfo { | ||
subaccount: subaccount_id, | ||
market_id, | ||
}; | ||
|
||
let mut order_cache = match ORDER_CALL_CACHE.may_load(deps.storage)? { | ||
Some(order_cache) => order_cache, | ||
None => vec![], | ||
}; | ||
|
||
order_cache.push(cache_order_info); | ||
|
||
ORDER_CALL_CACHE.save(deps.storage, &order_cache)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
contracts/injective-cosmwasm-stargate-example/src/reply.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::state::ORDER_CALL_CACHE; | ||
use crate::ContractError; | ||
use cosmwasm_std::{DepsMut, Event, Reply, Response}; | ||
use injective_cosmwasm::{InjectiveQuerier, InjectiveQueryWrapper}; | ||
|
||
pub fn handle_create_order_reply(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_spot_orders(&order_info.market_id, &order_info.subaccount); | ||
response_str = format!("{:?}", &response); | ||
cache.clear(); | ||
ORDER_CALL_CACHE.save(deps.storage, &cache)?; | ||
} | ||
}; | ||
|
||
Ok(Response::new().add_event(Event::new("transient_order").add_attributes([("query_str", response_str)]))) | ||
} | ||
|
||
pub fn handle_create_derivative_order_reply(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); | ||
cache.clear(); | ||
ORDER_CALL_CACHE.save(deps.storage, &cache)?; | ||
} | ||
}; | ||
|
||
Ok(Response::new().add_event(Event::new("transient_derivative_order").add_attributes([("query_str", response_str)]))) | ||
} |
11 changes: 11 additions & 0 deletions
11
contracts/injective-cosmwasm-stargate-example/src/state.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use cosmwasm_schema::cw_serde; | ||
use cw_storage_plus::Item; | ||
use injective_cosmwasm::{MarketId, SubaccountId}; | ||
|
||
pub const ORDER_CALL_CACHE: Item<Vec<CacheOrderInfo>> = Item::new("order_call_cache"); | ||
|
||
#[cw_serde] | ||
pub struct CacheOrderInfo { | ||
pub subaccount: SubaccountId, | ||
pub market_id: MarketId, | ||
} |
2 changes: 2 additions & 0 deletions
2
contracts/injective-cosmwasm-stargate-example/src/testing/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
mod test_auction; | ||
mod test_bank; | ||
mod test_exchange; | ||
mod test_exchange_derivative; | ||
mod test_oracle; | ||
mod type_helpers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.