Skip to content

Commit

Permalink
refactor 1
Browse files Browse the repository at this point in the history
  • Loading branch information
jbernal87 committed Mar 9, 2024
1 parent 50f00df commit 7b08898
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 27 deletions.
12 changes: 12 additions & 0 deletions contracts/injective-cosmwasm-mock/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ pub fn query(deps: Deps<InjectiveQueryWrapper>, _env: Env, msg: QueryMsg) -> Std
QueryMsg::TestMarketAtomicExecutionFeeMultiplier { market_id } => {
to_json_binary(&querier.query_market_atomic_execution_fee_multiplier(&market_id)?)
}
QueryMsg::TestQueryGrants {
grantee,
granter,
msg_type_url,
pagination,
} => {
deps.api.debug("Before query in contract");
let res = &querier.query_grants(grantee.as_str(), granter.as_str(), msg_type_url.as_str(), &pagination)?;
deps.api.debug(format!("{:?}", res).as_str());
to_json_binary(&querier.query_grants(grantee.as_str(), granter.as_str(), msg_type_url.as_str(), &pagination)?)
}
QueryMsg::TestGranteeGrants { grantee, pagination } => to_json_binary(&querier.query_grantee_grants(grantee.as_str(), &pagination)?),
}
}

Expand Down
10 changes: 10 additions & 0 deletions contracts/injective-cosmwasm-mock/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,14 @@ pub enum QueryMsg {
TestMarketAtomicExecutionFeeMultiplier {
market_id: MarketId,
},
TestQueryGrants {
grantee: String,
granter: String,
msg_type_url: String,
pagination: Option<u32>,
},
TestGranteeGrants {
grantee: String,
pagination: Option<u32>,
},
}
48 changes: 48 additions & 0 deletions contracts/injective-cosmwasm-mock/src/testing/authz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::{
msg::QueryMsg,
utils::{ExchangeType, Setup},
};

use crate::utils::create_generic_authorization;
use injective_cosmwasm::authz::response::GrantsResponse;
use injective_test_tube::injective_cosmwasm::authz::response::GranteeGrantsResponse;
use injective_test_tube::{Account, Module, RunnerResult, Wasm};

#[test]
#[cfg_attr(not(feature = "integration"), ignore)]
fn test_query_grants() {
let env = Setup::new(ExchangeType::None);
let wasm = Wasm::new(&env.app);

let msg_type_url = "/injective.exchange.v1beta1.MsgCreateDerivativeMarketOrder".to_string();
let granter = &env.users[0].account;
let grantee = env.contract_address.clone();

create_generic_authorization(&env.app, granter, grantee.to_owned(), msg_type_url.to_owned(), None);

let query_msg = QueryMsg::TestQueryGrants {
grantee,
granter: granter.address(),
msg_type_url,
pagination: None,
};
let _res: RunnerResult<GrantsResponse> = wasm.query(&env.contract_address, &query_msg);
//todo fix query
}

#[test]
#[cfg_attr(not(feature = "integration"), ignore)]
fn test_query_grantee_grants() {
let env = Setup::new(ExchangeType::None);
let wasm = Wasm::new(&env.app);

let msg_type_url = "/injective.exchange.v1beta1.MsgCreateDerivativeMarketOrder".to_string();
let granter = &env.users[0].account;
let grantee = env.contract_address.clone();

create_generic_authorization(&env.app, granter, grantee.to_owned(), msg_type_url.to_owned(), None);

let query_msg = QueryMsg::TestGranteeGrants { grantee, pagination: None };
let _res: RunnerResult<GranteeGrantsResponse> = wasm.query(&env.contract_address, &query_msg);
//todo fix query
}
1 change: 1 addition & 0 deletions contracts/injective-cosmwasm-mock/src/testing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod authz;
mod query_exchange_derivative_tests;
mod query_exchange_tests;
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ fn test_query_trader_transient_derivative_orders() {
.and_then(|event| event.attributes.iter().find(|a| a.key == "query_str"));

assert!(transient_query.is_some());
println!("{:?}", transient_query);
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";
assert!(transient_query.unwrap().value.contains(expected_order_info));
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::{
msg::{ExecuteMsg, QueryMsg},
utils::{
add_spot_initial_liquidity, add_spot_order_as, add_spot_orders, dec_to_proto, get_initial_liquidity_orders_vector, get_spot_market_id,
human_to_dec, human_to_proto, scale_price_quantity_for_spot_market, str_coin, ExchangeType, HumanOrder, Setup, BASE_DECIMALS, BASE_DENOM,
QUOTE_DECIMALS, QUOTE_DENOM,
add_spot_initial_liquidity, add_spot_order_as, add_spot_orders, dec_to_proto, execute_all_authorizations,
get_initial_liquidity_orders_vector, get_spot_market_id, human_to_dec, human_to_proto, scale_price_quantity_for_spot_market,
scale_price_quantity_for_spot_market_dec, str_coin, ExchangeType, HumanOrder, Setup, BASE_DECIMALS, BASE_DENOM, QUOTE_DECIMALS, QUOTE_DENOM,
},
};

use crate::utils::{execute_all_authorizations, scale_price_quantity_for_spot_market_dec};
use cosmwasm_std::{Addr, Coin};
use injective_cosmwasm::{
checked_address_to_subaccount_id,
Expand All @@ -20,8 +19,8 @@ use injective_math::FPDecimal;
use injective_std::types::injective::exchange::v1beta1::{
Deposit, MsgDeposit, MsgInstantSpotMarketLaunch, OrderType, QueryAggregateMarketVolumeResponse, QuerySubaccountDepositsRequest,
};
use injective_test_tube::injective_cosmwasm::get_default_subaccount_id_for_checked_address;
use injective_test_tube::{Account, Exchange, Module, RunnerResult, Wasm};

use injective_test_tube::{injective_cosmwasm::get_default_subaccount_id_for_checked_address, Account, Exchange, Module, RunnerResult, Wasm};

#[test]
#[cfg_attr(not(feature = "integration"), ignore)]
Expand Down Expand Up @@ -71,37 +70,24 @@ fn test_query_subaccount_deposit() {

let subaccount_id = checked_address_to_subaccount_id(&Addr::unchecked(env.users[0].account.address()), 1u32);

{
let make_deposit = |amount: &str, denom_key: &str| {
exchange
.deposit(
MsgDeposit {
sender: env.users[0].account.address(),
subaccount_id: subaccount_id.to_string(),
amount: Some(injective_std::types::cosmos::base::v1beta1::Coin {
amount: "10000000000000000000".to_string(),
denom: env.denoms["base"].clone(),
amount: amount.to_string(),
denom: env.denoms[denom_key].clone(),
}),
},
&env.users[0].account,
)
.unwrap();
}
};

{
exchange
.deposit(
MsgDeposit {
sender: env.users[0].account.address(),
subaccount_id: subaccount_id.to_string(),
amount: Some(injective_std::types::cosmos::base::v1beta1::Coin {
amount: "100000000".to_string(),
denom: env.denoms["quote"].clone(),
}),
},
&env.users[0].account,
)
.unwrap();
}
make_deposit("10000000000000000000", "base");
make_deposit("100000000", "quote");

let response = exchange
.query_subaccount_deposits(&QuerySubaccountDepositsRequest {
Expand Down Expand Up @@ -218,7 +204,7 @@ fn test_query_trader_spot_orders() {
let res: TraderSpotOrdersResponse = wasm.query(&env.contract_address, &query_msg).unwrap();
let orders = res.orders.clone().unwrap();

assert_eq!(orders.len(), 1);
assert_eq!(orders.len(), 1, "Expected exactly one order in the response");
let expected_orders = TrimmedSpotLimitOrder {
price: human_to_dec("10.01", QUOTE_DECIMALS - BASE_DECIMALS),
quantity: human_to_dec("5.1", BASE_DECIMALS),
Expand Down
1 change: 1 addition & 0 deletions contracts/injective-cosmwasm-mock/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ pub fn create_send_authorization(app: &InjectiveTestApp, granter: &SigningAccoun

pub fn execute_all_authorizations(app: &InjectiveTestApp, granter: &SigningAccount, grantee: String) {
create_generic_authorization(app, granter, grantee.clone(), MSG_CREATE_SPOT_LIMIT_ORDER_ENDPOINT.to_string(), None);

create_generic_authorization(
app,
granter,
Expand Down

0 comments on commit 7b08898

Please sign in to comment.