Skip to content

Commit

Permalink
add subaccount deposit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jbernal87 committed Apr 25, 2024
1 parent f82826b commit 7295a31
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 18 deletions.
6 changes: 3 additions & 3 deletions contracts/injective-cosmwasm-stargate-example/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::{
};
use cosmwasm_std::{entry_point, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
use cw2::set_contract_version;
use injective_cosmwasm::{create_deposit_msg, InjectiveMsgWrapper, InjectiveQuerier, InjectiveQueryWrapper};
use injective_cosmwasm::{create_deposit_msg, InjectiveMsgWrapper, InjectiveQueryWrapper};

const CONTRACT_NAME: &str = "crates.io:injective:dummy";
const CONTRACT_NAME: &str = "crates.io:injective:dummy-stargate-contract";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const CREATE_SPOT_ORDER_REPLY_ID: u64 = 0u64;
pub const CREATE_DERIVATIVE_ORDER_REPLY_ID: u64 = 1u64;
Expand All @@ -23,7 +23,7 @@ pub fn instantiate(deps: DepsMut, _env: Env, _info: MessageInfo, _msg: Instantia

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut<InjectiveQueryWrapper>,
_deps: DepsMut<InjectiveQueryWrapper>,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
Expand Down
2 changes: 1 addition & 1 deletion contracts/injective-cosmwasm-stargate-example/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use cosmwasm_std::Coin;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use injective_cosmwasm::{CancellationStrategy, MarketId, OracleInfo, OracleType, OrderSide, SubaccountId};
use injective_cosmwasm::{ SubaccountId};

pub const MSG_CREATE_SPOT_LIMIT_ORDER_ENDPOINT: &str = "/injective.exchange.v1beta1.MsgCreateSpotLimitOrder";
pub const MSG_CREATE_DERIVATIVE_LIMIT_ORDER_ENDPOINT: &str = "/injective.exchange.v1beta1.MsgCreateDerivativeLimitOrder";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::utils::{BASE_DECIMALS, BASE_DENOM, str_coin};

#[test]
#[cfg_attr(not(feature = "integration"), ignore)]
fn test_msg_deposit() {
fn test_query_exchange_params() {
let env = Setup::new(ExchangeType::None);

let wasm = Wasm::new(&env.app);
Expand All @@ -35,11 +35,7 @@ fn test_msg_deposit() {
let contract_response = contract_response.value;
let response: ParamResponse<AuthParams> = serde_json::from_str(&contract_response).unwrap();
assert_eq!(response.params.max_memo_characters, "256");
// let response: Value = serde_json::from_str(&contract_response).unwrap();
// let response = response.as_object().unwrap();
// println!("{:?}", response);
// assert_eq!(response.get("params").unwrap().as_object().unwrap().get("max_memo_characters").unwrap().as_str().unwrap(), "256");
}
}



Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
use cosmwasm_std::{Addr, from_json};
use injective_test_tube::{Account, Module, Wasm};
use injective_cosmwasm::checked_address_to_subaccount_id;
use crate::msg::{QueryMsg, QueryStargateResponse};
use crate::testing::type_helpers::ParamResponse;
use crate::testing::type_helpers::ExchangeParams;
use crate::utils::{BASE_DECIMALS, BASE_DENOM, ExchangeType, Setup, str_coin};
use base64::{encode, Engine};
use base64::prelude::BASE64_STANDARD;
use crate::{
msg::{QueryStargateResponse, QueryMsg},
utils::{
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,
},
testing::type_helpers::{ParamResponse, ExchangeParams},
};

use injective_std::types::injective::exchange::v1beta1::{Deposit, MsgDeposit, MsgInstantSpotMarketLaunch, OrderType, QueryAggregateMarketVolumeResponse, QuerySubaccountDepositRequest, QuerySubaccountDepositResponse, QuerySubaccountDepositsRequest};

use cosmwasm_std::{Addr, from_json, to_json_string};
use injective_test_tube::{injective_cosmwasm::get_default_subaccount_id_for_checked_address, Account, Exchange, Module, RunnerResult, Wasm};
use injective_cosmwasm::{checked_address_to_subaccount_id, SubaccountDepositResponse, SubaccountId};
use prost::Message;
use serde_json::to_vec;
use injective_std::types::injective::oracle::v1beta1::GrantPriceFeederPrivilegeProposal;
use crate::order_management::encode_bytes_message;
use crate::testing::type_helpers::AuthParams;
use crate::utils::encode_proto_message;

#[test]
#[cfg_attr(not(feature = "integration"), ignore)]
Expand All @@ -29,4 +45,71 @@ fn test_exchange_param() {
println!("{:?}", response);
let listing_fee_coin = str_coin("1000", BASE_DENOM, BASE_DECIMALS);
assert_eq!(response.params.spot_market_instant_listing_fee, listing_fee_coin);
}
}

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

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: amount.to_string(),
denom: env.denoms[denom_key].clone(),
}),
},
&env.users[0].account,
)
.unwrap();
};

make_deposit("10000000000000000000", "base");
make_deposit("100000000", "quote");

let response = exchange
.query_subaccount_deposits(&QuerySubaccountDepositsRequest {
subaccount_id: subaccount_id.to_string(),
subaccount: None,
})
.unwrap();

assert_eq!(
response.deposits[&env.denoms["base"].clone()],
Deposit {
available_balance: human_to_proto("10.0", BASE_DECIMALS),
total_balance: human_to_proto("10.0", BASE_DECIMALS),
}
);
assert_eq!(
response.deposits[&env.denoms["quote"].clone()],
Deposit {
available_balance: human_to_proto("100.0", QUOTE_DECIMALS),
total_balance: human_to_proto("100.0", QUOTE_DECIMALS),
}
);


let query_msg = QueryMsg::QueryStargate {
path: "/injective.exchange.v1beta1.Query/SubaccountDeposit".to_string(),
query_request: encode_proto_message(QuerySubaccountDepositRequest {
subaccount_id: subaccount_id.to_string(),
denom: env.denoms["base"].clone(),
}),
};
let contract_response: QueryStargateResponse = wasm.query(&env.contract_address, &query_msg).unwrap();
let contract_response = contract_response.value;
let contract_response: SubaccountDepositResponse = serde_json::from_str(&contract_response).unwrap();
println!("{:?}", contract_response);
let deposit = contract_response.deposits;
assert_eq!(deposit.total_balance, human_to_dec("10.0", BASE_DECIMALS));
}

9 changes: 9 additions & 0 deletions contracts/injective-cosmwasm-stargate-example/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ use injective_test_tube::{
use injective_testing::human_to_i64;
use prost::Message;
use std::{collections::HashMap, ops::Neg, str::FromStr};
use base64::Engine;
use base64::prelude::BASE64_STANDARD;

pub const EXCHANGE_DECIMALS: i32 = 18i32;
pub const BASE_DECIMALS: i32 = 18i32;
Expand Down Expand Up @@ -831,3 +833,10 @@ pub fn create_some_usdt_price_attestation(human_price: &str, decimal_precision:
publish_time,
}
}


pub fn encode_proto_message<T: Message>(msg: T) -> String {
let mut buf = vec![];
T::encode(&msg, &mut buf).unwrap();
BASE64_STANDARD.encode(&buf)
}

0 comments on commit 7295a31

Please sign in to comment.