Skip to content

Commit 22d8dfa

Browse files
authored
Merge pull request #257 from InjectiveLabs/fix/injective_testing
fix: launch_spot_market_atom
2 parents ad2b508 + 76b3e20 commit 22d8dfa

7 files changed

Lines changed: 166 additions & 113 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ injective-cosmwasm = { version = "0.3.3", path = "./packages/injective-cosmwasm
2121
injective-math = { version = "0.3.3", path = "./packages/injective-math" }
2222
injective-std = { version = "1.16.0-beta.2" }
2323
injective-test-tube = { version = "1.16.0-beta.2.1" }
24-
injective-testing = { version = "1.1.7", path = "./packages/injective-testing" }
24+
injective-testing = { version = "1.1.10", path = "./packages/injective-testing" }
2525
primitive-types = { version = "0.12.2", default-features = false }
2626
prost = { version = "0.13.4", features = [ "prost-derive" ] }
2727
rand = { version = "0.4.6" }

build.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
# Usage: ./build.sh [contract-name]
3+
# Purpose: Builds a CosmWasm smart contract using the cosmwasm/optimizer Docker image.
4+
# - Defaults to building the 'injective-cosmwasm-mock' contract if no argument is provided.
5+
# - Specify a contract name (e.g., 'atomic-order-example') to build a different contract.
6+
# - The contract must be a directory in 'contracts/' (e.g., 'contracts/[contract-name]').
7+
# - Run from the workspace root (e.g., cw-injective) to ensure access to the workspace Cargo.toml.
8+
# Examples:
9+
# ./build.sh # Builds injective-cosmwasm-mock
10+
# ./build.sh atomic-order-example # Builds atomic-order-example
11+
# ./build.sh dummy # Builds dummy
12+
# ./build.sh injective-cosmwasm-stargate-example # Builds injective-cosmwasm-stargate-example
13+
# Output: Produces an optimized .wasm file in target/wasm32-unknown-unknown/release/
14+
15+
ARCH=""
16+
17+
# Set architecture suffix for arm64
18+
if [[ $(arch) = "arm64" ]]; then
19+
ARCH=-arm64
20+
fi
21+
22+
# Default contract name
23+
DEFAULT_CONTRACT="injective-cosmwasm-mock"
24+
CONTRACT=${1:-$DEFAULT_CONTRACT}
25+
26+
# Validate that the contract directory exists
27+
if [[ ! -d "contracts/$CONTRACT" ]]; then
28+
echo "Error: Contract directory 'contracts/$CONTRACT' does not exist."
29+
echo "Available contracts:"
30+
ls -1 contracts/
31+
exit 1
32+
fi
33+
34+
# Run the optimizer with the specified or default contract
35+
docker run --rm -v "$(pwd)":/code \
36+
-v "$HOME/.cargo/git":/usr/local/cargo/git \
37+
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
38+
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
39+
cosmwasm/optimizer${ARCH}:0.16.1 /code/contracts/$CONTRACT
40+
41+
42+
43+
44+
45+

contracts/injective-cosmwasm-mock/src/testing/test_exchange.rs

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use crate::{
22
msg::{ExecuteMsg, QueryMsg},
33
utils::{
44
add_spot_initial_liquidity, add_spot_order_as, add_spot_orders, dec_to_proto, execute_all_authorizations,
5-
get_initial_liquidity_orders_vector, get_spot_market_id, human_to_dec, human_to_proto, scale_price_quantity_for_spot_market,
6-
scale_price_quantity_for_spot_market_dec, str_coin, ExchangeType, HumanOrder, Setup, BASE_DECIMALS, BASE_DENOM, QUOTE_DECIMALS, QUOTE_DENOM,
5+
get_initial_liquidity_orders_vector, human_to_dec, human_to_proto, scale_price_quantity_for_spot_market,
6+
scale_price_quantity_for_spot_market_dec, str_coin, ExchangeType, HumanOrder, Setup,
77
},
88
};
99
use cosmwasm_std::{Addr, Coin};
@@ -24,6 +24,10 @@ use injective_test_tube::{
2424
},
2525
Account, Exchange, Module, RunnerResult, Wasm,
2626
};
27+
use injective_testing::{
28+
mocks::{MOCK_BASE_DECIMALS, MOCK_BASE_DENOM, MOCK_QUOTE_DECIMALS, MOCK_QUOTE_DENOM},
29+
test_tube::exchange::get_spot_market_id,
30+
};
2731

2832
#[test]
2933
#[cfg_attr(not(feature = "integration"), ignore)]
@@ -57,7 +61,7 @@ fn test_exchange_params() {
5761
assert!(res.params.is_some());
5862
let params = res.params.unwrap();
5963

60-
let listing_fee_coin = str_coin("20", BASE_DENOM, BASE_DECIMALS);
64+
let listing_fee_coin = str_coin("20", MOCK_BASE_DENOM, MOCK_BASE_DECIMALS);
6165
assert_eq!(params.spot_market_instant_listing_fee, listing_fee_coin);
6266
assert_eq!(params.derivative_market_instant_listing_fee, listing_fee_coin);
6367
assert_eq!(params.trading_rewards_vesting_duration, 604800);
@@ -102,31 +106,31 @@ fn test_query_subaccount_deposit() {
102106
assert_eq!(
103107
response.deposits[&env.denoms["base"].clone()],
104108
Deposit {
105-
available_balance: human_to_proto("10.0", BASE_DECIMALS),
106-
total_balance: human_to_proto("10.0", BASE_DECIMALS),
109+
available_balance: human_to_proto("10.0", MOCK_BASE_DECIMALS),
110+
total_balance: human_to_proto("10.0", MOCK_BASE_DECIMALS),
107111
}
108112
);
109113
assert_eq!(
110114
response.deposits[&env.denoms["quote"].clone()],
111115
Deposit {
112-
available_balance: human_to_proto("100.0", QUOTE_DECIMALS),
113-
total_balance: human_to_proto("100.0", QUOTE_DECIMALS),
116+
available_balance: human_to_proto("100.0", MOCK_QUOTE_DECIMALS),
117+
total_balance: human_to_proto("100.0", MOCK_QUOTE_DECIMALS),
114118
}
115119
);
116120

117121
let query_msg = QueryMsg::TestSubAccountDepositQuery {
118122
subaccount_id: subaccount_id.clone(),
119-
denom: BASE_DENOM.to_string(),
123+
denom: MOCK_BASE_DENOM.to_string(),
120124
};
121125
let contract_response: SubaccountDepositResponse = wasm.query(&env.contract_address, &query_msg).unwrap();
122-
assert_eq!(contract_response.deposits.total_balance, human_to_dec("10.0", BASE_DECIMALS));
126+
assert_eq!(contract_response.deposits.total_balance, human_to_dec("10.0", MOCK_BASE_DECIMALS));
123127

124128
let query_msg = QueryMsg::TestSubAccountDepositQuery {
125129
subaccount_id: subaccount_id.clone(),
126-
denom: QUOTE_DENOM.to_string(),
130+
denom: MOCK_QUOTE_DENOM.to_string(),
127131
};
128132
let contract_response: SubaccountDepositResponse = wasm.query(&env.contract_address, &query_msg).unwrap();
129-
assert_eq!(contract_response.deposits.available_balance, human_to_dec("100.0", QUOTE_DECIMALS));
133+
assert_eq!(contract_response.deposits.available_balance, human_to_dec("100.0", MOCK_QUOTE_DECIMALS));
130134
}
131135

132136
#[test]
@@ -160,13 +164,13 @@ fn test_query_spot_market() {
160164
MsgInstantSpotMarketLaunch {
161165
sender: env.signer.address(),
162166
ticker: ticker.clone(),
163-
base_denom: BASE_DENOM.to_string(),
164-
quote_denom: QUOTE_DENOM.to_string(),
167+
base_denom: MOCK_BASE_DENOM.to_string(),
168+
quote_denom: MOCK_QUOTE_DENOM.to_string(),
165169
min_price_tick_size: dec_to_proto(min_price_tick_size),
166170
min_quantity_tick_size: dec_to_proto(min_quantity_tick_size),
167171
min_notional: dec_to_proto(min_notional),
168-
base_decimals: BASE_DECIMALS as u32,
169-
quote_decimals: QUOTE_DECIMALS as u32,
172+
base_decimals: MOCK_BASE_DECIMALS as u32,
173+
quote_decimals: MOCK_QUOTE_DECIMALS as u32,
170174
},
171175
&env.signer,
172176
)
@@ -182,8 +186,8 @@ fn test_query_spot_market() {
182186
let response_market = res.market.unwrap();
183187
assert_eq!(response_market.market_id.as_str(), spot_market_id);
184188
assert_eq!(response_market.ticker.as_str(), ticker);
185-
assert_eq!(response_market.base_denom.as_str(), BASE_DENOM);
186-
assert_eq!(response_market.quote_denom.as_str(), QUOTE_DENOM);
189+
assert_eq!(response_market.base_denom.as_str(), MOCK_BASE_DENOM);
190+
assert_eq!(response_market.quote_denom.as_str(), MOCK_QUOTE_DENOM);
187191
assert_eq!(response_market.min_price_tick_size.clone().to_string(), min_price_tick_size.to_string());
188192
assert_eq!(response_market.min_quantity_tick_size.to_string(), min_quantity_tick_size.to_string());
189193
}
@@ -205,17 +209,17 @@ fn test_query_trader_spot_orders() {
205209
};
206210

207211
{
208-
let (price, quantity) = scale_price_quantity_for_spot_market("10.01", "5.1", &BASE_DECIMALS, &QUOTE_DECIMALS);
212+
let (price, quantity) = scale_price_quantity_for_spot_market("10.01", "5.1", &MOCK_BASE_DECIMALS, &MOCK_QUOTE_DECIMALS);
209213
add_spot_order_as(&env.app, market_id.to_owned(), &env.users[0], price, quantity, OrderType::Sell);
210214

211215
let res: TraderSpotOrdersResponse = wasm.query(&env.contract_address, &query_msg).unwrap();
212216
let orders = res.orders.clone().unwrap();
213217

214218
assert_eq!(orders.len(), 1, "Expected exactly one order in the response");
215219
let expected_orders = TrimmedSpotLimitOrder {
216-
price: human_to_dec("10.01", QUOTE_DECIMALS - BASE_DECIMALS),
217-
quantity: human_to_dec("5.1", BASE_DECIMALS),
218-
fillable: human_to_dec("5.1", BASE_DECIMALS),
220+
price: human_to_dec("10.01", MOCK_QUOTE_DECIMALS - MOCK_BASE_DECIMALS),
221+
quantity: human_to_dec("5.1", MOCK_BASE_DECIMALS),
222+
fillable: human_to_dec("5.1", MOCK_BASE_DECIMALS),
219223
isBuy: false,
220224
order_hash: "".to_string(),
221225
};
@@ -226,17 +230,17 @@ fn test_query_trader_spot_orders() {
226230
}
227231

228232
{
229-
let (price, quantity) = scale_price_quantity_for_spot_market("9.90", "0.5", &BASE_DECIMALS, &QUOTE_DECIMALS);
233+
let (price, quantity) = scale_price_quantity_for_spot_market("9.90", "0.5", &MOCK_BASE_DECIMALS, &MOCK_QUOTE_DECIMALS);
230234
add_spot_order_as(&env.app, market_id.to_owned(), &env.users[0], price, quantity, OrderType::Buy);
231235

232236
let res: TraderSpotOrdersResponse = wasm.query(&env.contract_address, &query_msg).unwrap();
233237
let orders = res.orders.clone().unwrap();
234238

235239
assert_eq!(orders.len(), 2);
236240
let expected_order = TrimmedSpotLimitOrder {
237-
price: human_to_dec("9.90", QUOTE_DECIMALS - BASE_DECIMALS),
238-
quantity: human_to_dec("0.5", BASE_DECIMALS),
239-
fillable: human_to_dec("0.5", BASE_DECIMALS),
241+
price: human_to_dec("9.90", MOCK_QUOTE_DECIMALS - MOCK_BASE_DECIMALS),
242+
quantity: human_to_dec("0.5", MOCK_BASE_DECIMALS),
243+
fillable: human_to_dec("0.5", MOCK_BASE_DECIMALS),
240244
isBuy: true,
241245
order_hash: "".to_string(),
242246
};
@@ -261,9 +265,9 @@ fn test_query_spot_market_mid_price_and_tob() {
261265
};
262266

263267
let res: MarketMidPriceAndTOBResponse = wasm.query(&env.contract_address, &query_msg).unwrap();
264-
assert_eq!(res.mid_price, Some(human_to_dec("10", QUOTE_DECIMALS - BASE_DECIMALS)));
265-
assert_eq!(res.best_buy_price, Some(human_to_dec("9.9", QUOTE_DECIMALS - BASE_DECIMALS)));
266-
assert_eq!(res.best_sell_price, Some(human_to_dec("10.1", QUOTE_DECIMALS - BASE_DECIMALS)));
268+
assert_eq!(res.mid_price, Some(human_to_dec("10", MOCK_QUOTE_DECIMALS - MOCK_BASE_DECIMALS)));
269+
assert_eq!(res.best_buy_price, Some(human_to_dec("9.9", MOCK_QUOTE_DECIMALS - MOCK_BASE_DECIMALS)));
270+
assert_eq!(res.best_sell_price, Some(human_to_dec("10.1", MOCK_QUOTE_DECIMALS - MOCK_BASE_DECIMALS)));
267271
}
268272

269273
#[test]
@@ -291,18 +295,21 @@ fn test_query_spot_market_orderbook() {
291295
assert_eq!(
292296
buys_price_level[0],
293297
PriceLevel {
294-
p: human_to_dec(liquidity_orders[sells_price_level.len()].price.as_str(), QUOTE_DECIMALS - BASE_DECIMALS),
295-
q: human_to_dec(liquidity_orders[sells_price_level.len()].quantity.as_str(), BASE_DECIMALS),
298+
p: human_to_dec(
299+
liquidity_orders[sells_price_level.len()].price.as_str(),
300+
MOCK_QUOTE_DECIMALS - MOCK_BASE_DECIMALS
301+
),
302+
q: human_to_dec(liquidity_orders[sells_price_level.len()].quantity.as_str(), MOCK_BASE_DECIMALS),
296303
}
297304
);
298305
assert_eq!(
299306
buys_price_level[1],
300307
PriceLevel {
301308
p: human_to_dec(
302309
liquidity_orders[sells_price_level.len() + 1].price.as_str(),
303-
QUOTE_DECIMALS - BASE_DECIMALS
310+
MOCK_QUOTE_DECIMALS - MOCK_BASE_DECIMALS
304311
),
305-
q: human_to_dec(liquidity_orders[sells_price_level.len() + 1].quantity.as_str(), BASE_DECIMALS),
312+
q: human_to_dec(liquidity_orders[sells_price_level.len() + 1].quantity.as_str(), MOCK_BASE_DECIMALS),
306313
}
307314
);
308315

@@ -311,19 +318,19 @@ fn test_query_spot_market_orderbook() {
311318
PriceLevel {
312319
p: human_to_dec(
313320
liquidity_orders[sells_price_level.len() - 1].price.as_str(),
314-
QUOTE_DECIMALS - BASE_DECIMALS
321+
MOCK_QUOTE_DECIMALS - MOCK_BASE_DECIMALS
315322
),
316-
q: human_to_dec(liquidity_orders[sells_price_level.len() - 1].quantity.as_str(), BASE_DECIMALS),
323+
q: human_to_dec(liquidity_orders[sells_price_level.len() - 1].quantity.as_str(), MOCK_BASE_DECIMALS),
317324
}
318325
);
319326
assert_eq!(
320327
sells_price_level[1],
321328
PriceLevel {
322329
p: human_to_dec(
323330
liquidity_orders[sells_price_level.len() - 2].price.as_str(),
324-
QUOTE_DECIMALS - BASE_DECIMALS
331+
MOCK_QUOTE_DECIMALS - MOCK_BASE_DECIMALS
325332
),
326-
q: human_to_dec(liquidity_orders[sells_price_level.len() - 2].quantity.as_str(), BASE_DECIMALS),
333+
q: human_to_dec(liquidity_orders[sells_price_level.len() - 2].quantity.as_str(), MOCK_BASE_DECIMALS),
327334
}
328335
);
329336
}
@@ -419,13 +426,13 @@ fn test_query_aggregate_account_volume() {
419426
let res: QueryAggregateVolumeResponse = wasm.query(&env.contract_address, &query_msg).unwrap();
420427
assert!(res.aggregate_volumes.is_none());
421428

422-
let (price, quantity) = scale_price_quantity_for_spot_market("9.9", "1", &BASE_DECIMALS, &QUOTE_DECIMALS);
429+
let (price, quantity) = scale_price_quantity_for_spot_market("9.9", "1", &MOCK_BASE_DECIMALS, &MOCK_QUOTE_DECIMALS);
423430
add_spot_order_as(&env.app, market_id.clone(), &env.users[1], price, quantity, OrderType::Sell);
424431

425432
let res: QueryAggregateVolumeResponse = wasm.query(&env.contract_address, &query_msg).unwrap();
426433
let volume: &VolumeByType = &res.aggregate_volumes.unwrap()[0].volume;
427434
assert_eq!(volume.maker_volume, FPDecimal::ZERO);
428-
assert_eq!(volume.taker_volume, human_to_dec("9.9", QUOTE_DECIMALS));
435+
assert_eq!(volume.taker_volume, human_to_dec("9.9", MOCK_QUOTE_DECIMALS));
429436
}
430437

431438
#[test]
@@ -438,7 +445,7 @@ fn test_query_market_atomic_execution_fee_multiplier() {
438445
market_id: MarketId::new(market_id.clone()).unwrap(),
439446
};
440447
let res: QueryMarketAtomicExecutionFeeMultiplierResponse = wasm.query(&env.contract_address, &query_msg).unwrap();
441-
assert_eq!(res.multiplier, human_to_dec("0.0000025", QUOTE_DECIMALS));
448+
assert_eq!(res.multiplier, human_to_dec("0.0000025", MOCK_QUOTE_DECIMALS));
442449
}
443450

444451
#[test]
@@ -458,16 +465,16 @@ fn test_query_spot_orders_to_cancel_up_to_amount() {
458465
};
459466

460467
{
461-
let (price, quantity) = scale_price_quantity_for_spot_market("9.90", "1", &BASE_DECIMALS, &QUOTE_DECIMALS);
468+
let (price, quantity) = scale_price_quantity_for_spot_market("9.90", "1", &MOCK_BASE_DECIMALS, &MOCK_QUOTE_DECIMALS);
462469
add_spot_order_as(&env.app, market_id.to_owned(), &env.users[0], price, quantity, OrderType::Buy);
463470

464471
let res: TraderSpotOrdersResponse = wasm.query(&env.contract_address, &query_spot_msg).unwrap();
465472
let orders = res.orders.clone().unwrap();
466473

467474
let expected_order = TrimmedSpotLimitOrder {
468-
price: human_to_dec("9.90", QUOTE_DECIMALS - BASE_DECIMALS),
469-
quantity: human_to_dec("1", BASE_DECIMALS),
470-
fillable: human_to_dec("1", BASE_DECIMALS),
475+
price: human_to_dec("9.90", MOCK_QUOTE_DECIMALS - MOCK_BASE_DECIMALS),
476+
quantity: human_to_dec("1", MOCK_BASE_DECIMALS),
477+
fillable: human_to_dec("1", MOCK_BASE_DECIMALS),
471478
isBuy: true,
472479
order_hash: "".to_string(),
473480
};
@@ -481,8 +488,8 @@ fn test_query_spot_orders_to_cancel_up_to_amount() {
481488
let query_spot_cancel_msg = QueryMsg::TestSpotOrdersToCancelUpToAmount {
482489
market_id: MarketId::new(market_id.clone()).unwrap(),
483490
subaccount_id: SubaccountId::new(subaccount_id).unwrap(),
484-
base_amount: human_to_dec("0", BASE_DECIMALS),
485-
quote_amount: human_to_dec("0.2", QUOTE_DECIMALS),
491+
base_amount: human_to_dec("0", MOCK_BASE_DECIMALS),
492+
quote_amount: human_to_dec("0.2", MOCK_QUOTE_DECIMALS),
486493
strategy: CancellationStrategy::UnspecifiedOrder,
487494
reference_price: None,
488495
};
@@ -504,7 +511,7 @@ fn test_query_trader_transient_spot_orders() {
504511
execute_all_authorizations(&env.app, &env.users[0].account, env.contract_address.clone());
505512
add_spot_initial_liquidity(&env.app, market_id.clone());
506513

507-
let (scale_price, scale_quantity) = scale_price_quantity_for_spot_market_dec("9.8", "1", &BASE_DECIMALS, &QUOTE_DECIMALS);
514+
let (scale_price, scale_quantity) = scale_price_quantity_for_spot_market_dec("9.8", "1", &MOCK_BASE_DECIMALS, &MOCK_QUOTE_DECIMALS);
508515

509516
let res = wasm
510517
.execute(

0 commit comments

Comments
 (0)