Skip to content

Commit

Permalink
subaccount deposit query test
Browse files Browse the repository at this point in the history
  • Loading branch information
jbernal87 committed Feb 5, 2024
1 parent ac155f5 commit ab36941
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 52 deletions.
16 changes: 10 additions & 6 deletions contracts/injective-cosmwasm-mock/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use crate::{
error::ContractError,
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
};
use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
use cw2::set_contract_version;
use injective_cosmwasm::{create_deposit_msg, InjectiveMsgWrapper, InjectiveQuerier, InjectiveQueryWrapper};
use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};

#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;

const CONTRACT_NAME: &str = "crates.io:injective:dummy";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -31,10 +34,11 @@ pub fn execute(

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps<InjectiveQueryWrapper>, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
let querier:InjectiveQuerier = InjectiveQuerier::new(&deps.querier);
let querier: InjectiveQuerier = InjectiveQuerier::new(&deps.querier);

match msg {
QueryMsg::TestSpotMarketQuery { market_id } => to_json_binary(&querier.query_spot_market(&market_id)?),
QueryMsg::TestExchangeParamsQuery {} => to_json_binary(&querier.query_exchange_params()?),
QueryMsg::TestSubAccountDepositQuery { subaccount_id, denom } => to_json_binary(&querier.query_subaccount_deposit(&subaccount_id, &denom)?), }
QueryMsg::TestSubAccountDepositQuery { subaccount_id, denom } => to_json_binary(&querier.query_subaccount_deposit(&subaccount_id, &denom)?),
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::utils::{human_to_proto, str_coin, Setup, BASE_DECIMALS, BASE_DENOM, QUOTE_DECIMALS, QUOTE_DENOM};
use crate::{
msg::{ExecuteMsg, QueryMsg},
utils::test_setup,
utils::{human_to_dec, human_to_proto, str_coin, Setup, BASE_DECIMALS, BASE_DENOM, QUOTE_DECIMALS, QUOTE_DENOM},
};
use cosmwasm_std::{Addr, Coin};
use injective_cosmwasm::{checked_address_to_subaccount_id, ExchangeParamsResponse, MarketId};

use cosmwasm_std::Coin;
use injective_cosmwasm::{ExchangeParamsResponse, MarketId, SubaccountDepositResponse};
use injective_math::{scale::Scaled, FPDecimal};
use injective_std::types::injective::exchange::v1beta1::{
Deposit, MsgDeposit, MsgInstantSpotMarketLaunch, QuerySpotMarketsRequest, QuerySubaccountDepositsRequest,
Expand All @@ -18,21 +18,20 @@ pub fn dec_to_proto(val: FPDecimal) -> String {
#[test]
#[cfg_attr(not(feature = "integration"), ignore)]
fn test_msg_deposit() {
let (app, accs, contract_address) = test_setup();
let env = Setup::new();

let wasm = Wasm::new(&app);
let buyer = &accs[1];
let wasm = Wasm::new(&env.app);
let user = &env.users[0];

// Execute contract
let buyer_subaccount_id = checked_address_to_subaccount_id(&Addr::unchecked(buyer.address()), 1u32);
let res = wasm.execute(
&contract_address,
&env.contract_address,
&ExecuteMsg::TestDepositMsg {
subaccount_id: buyer_subaccount_id,
subaccount_id: user.subaccount_id.clone(),
amount: Coin::new(100, "usdt"),
},
&[Coin::new(100, "usdt")],
buyer,
&user.account,
);
assert!(res.is_ok(), "Execution failed with error: {:?}", res.unwrap_err());
}
Expand Down Expand Up @@ -161,7 +160,7 @@ fn test_query_subaccount_deposit() {

let response = exchange
.query_subaccount_deposits(&QuerySubaccountDepositsRequest {
subaccount_id: env.users[0].subaccount_id.to_string(),
subaccount_id: env.users[0].subaccount_id.clone().to_string(),
subaccount: None,
})
.unwrap();
Expand All @@ -180,4 +179,18 @@ fn test_query_subaccount_deposit() {
total_balance: human_to_proto("100.0", QUOTE_DECIMALS),
}
);

let query_msg = QueryMsg::TestSubAccountDepositQuery {
subaccount_id: env.users[0].subaccount_id.clone(),
denom: BASE_DENOM.to_string(),
};
let contract_response: SubaccountDepositResponse = wasm.query(&env.contract_address, &query_msg).unwrap();
assert_eq!(contract_response.deposits.total_balance, human_to_dec("10.0", BASE_DECIMALS));

let query_msg = QueryMsg::TestSubAccountDepositQuery {
subaccount_id: env.users[0].subaccount_id.clone(),
denom: QUOTE_DENOM.to_string(),
};
let contract_response: SubaccountDepositResponse = wasm.query(&env.contract_address, &query_msg).unwrap();
assert_eq!(contract_response.deposits.available_balance, human_to_dec("100.0", QUOTE_DECIMALS));
}
45 changes: 11 additions & 34 deletions contracts/injective-cosmwasm-mock/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::msg::InstantiateMsg;
use cosmwasm_std::{coin, Addr, Coin};
use injective_cosmwasm::{checked_address_to_subaccount_id, SubaccountId};
use injective_math::{scale::Scaled, FPDecimal};
use injective_test_tube::injective_cosmwasm::{checked_address_to_subaccount_id, SubaccountId};
use injective_test_tube::{Account, InjectiveTestApp, Module, SigningAccount, Wasm};
use std::collections::HashMap;

Expand Down Expand Up @@ -58,11 +58,11 @@ impl Setup {
])
.unwrap();

let trader_subaccount_id = checked_address_to_subaccount_id(&Addr::unchecked(user.address()), 1u32);
let user_subaccount_id = checked_address_to_subaccount_id(&Addr::unchecked(user.address()), 1u32);

users.push(UserInfo {
account: user,
subaccount_id: trader_subaccount_id,
subaccount_id: user_subaccount_id,
});
}

Expand Down Expand Up @@ -90,37 +90,10 @@ impl Setup {
}
}

pub fn test_setup() -> (InjectiveTestApp, Vec<SigningAccount>, String) {
let app = InjectiveTestApp::new();
let mut accs = app
.init_accounts(&[Coin::new(1_000_000_000_000, "usdt"), Coin::new(1_000_000_000_000, "inj")], 2)
.unwrap();
accs.push(app.init_account(&[Coin::new(1_000_000_000_000_000_000_000_000_000, "inj")]).unwrap());

let seller = &accs[0];
let buyer = &accs[1];

// `Wasm` is the module we use to interact with cosmwasm releated logic on the appchain
// it implements `Module` trait which you will see more later.
let wasm = Wasm::new(&app);

// Load compiled wasm bytecode
let wasm_byte_code = std::fs::read("../../artifacts/injective_cosmwasm_mock-x86_64.wasm").unwrap();
let code_id = wasm.store_code(&wasm_byte_code, None, buyer).unwrap().data.code_id;

// Instantiate contract
let contract_address: String = wasm
.instantiate(code_id, &InstantiateMsg {}, Some(&seller.address()), Some("mock-contract"), &[], seller)
.unwrap()
.data
.address;
assert!(!contract_address.is_empty(), "Contract address is empty");

(app, accs, contract_address)
}

pub fn human_to_dec(raw_number: &str, decimals: i32) -> FPDecimal {
FPDecimal::must_from_str(&raw_number.replace('_', "")).scaled(decimals)
impl Default for Setup {
fn default() -> Self {
Self::new()
}
}

pub fn wasm_file(contract_name: String) -> String {
Expand Down Expand Up @@ -150,3 +123,7 @@ pub fn str_coin(human_amount: &str, denom: &str, decimals: i32) -> Coin {
pub fn human_to_proto(raw_number: &str, decimals: i32) -> String {
FPDecimal::must_from_str(&raw_number.replace('_', "")).scaled(18 + decimals).to_string()
}

pub fn human_to_dec(raw_number: &str, decimals: i32) -> FPDecimal {
FPDecimal::must_from_str(&raw_number.replace('_', "")).scaled(decimals)
}

0 comments on commit ab36941

Please sign in to comment.