Skip to content

Commit

Permalink
test: add tests for contract migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
gorgos committed Feb 24, 2024
1 parent 61a2c07 commit a6e968b
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use injective_test_tube::{Account, Bank, Exchange, InjectiveTestApp, Module, RunnerResult, Wasm};
use injective_test_tube::{
Account, Bank, Exchange, InjectiveTestApp, Module, RunnerResult, SigningAccount, Wasm,
};
use std::ops::Neg;

use crate::helpers::Scaled;
Expand Down Expand Up @@ -35,36 +37,14 @@ use crate::types::{FPCoin, SwapEstimationResult};
In all tests contract is configured to self-relay trades and thus receive a 60% fee discount.
*/

#[test]
fn happy_path_two_hops_swap_eth_atom_realistic_values_self_relaying() {
let app = InjectiveTestApp::new();
pub fn happy_path_two_hops_test(app: InjectiveTestApp, owner: SigningAccount, contr_addr: String) {
let wasm = Wasm::new(&app);
let exchange = Exchange::new(&app);
let bank = Bank::new(&app);

let _signer = must_init_account_with_funds(&app, &[str_coin("1", INJ, Decimals::Eighteen)]);

let _validator = app
.get_first_validator_signing_account(INJ.to_string(), 1.2f64)
.unwrap();
let owner = must_init_account_with_funds(
&app,
&[
str_coin("1", ETH, Decimals::Eighteen),
str_coin("1", ATOM, Decimals::Six),
str_coin("1_000", USDT, Decimals::Six),
str_coin("10_000", INJ, Decimals::Eighteen),
],
);

let spot_market_1_id = launch_realistic_weth_usdt_spot_market(&exchange, &owner);
let spot_market_2_id = launch_realistic_atom_usdt_spot_market(&exchange, &owner);

let contr_addr = init_self_relaying_contract_and_get_address(
&wasm,
&owner,
&[str_coin("1_000", USDT, Decimals::Six)],
);
set_route_and_assert_success(
&wasm,
&owner,
Expand Down Expand Up @@ -227,6 +207,35 @@ fn happy_path_two_hops_swap_eth_atom_realistic_values_self_relaying() {
);
}

#[test]
fn happy_path_two_hops_swap_eth_atom_realistic_values_self_relaying() {
let app = InjectiveTestApp::new();
let wasm = Wasm::new(&app);

let _signer = must_init_account_with_funds(&app, &[str_coin("1", INJ, Decimals::Eighteen)]);

let _validator = app
.get_first_validator_signing_account(INJ.to_string(), 1.2f64)
.unwrap();
let owner = must_init_account_with_funds(
&app,
&[
str_coin("1", ETH, Decimals::Eighteen),
str_coin("1", ATOM, Decimals::Six),
str_coin("1_000", USDT, Decimals::Six),
str_coin("10_000", INJ, Decimals::Eighteen),
],
);

let contr_addr = init_self_relaying_contract_and_get_address(
&wasm,
&owner,
&[str_coin("1_000", USDT, Decimals::Six)],
);

happy_path_two_hops_test(app, owner, contr_addr);
}

#[test]
fn happy_path_two_hops_swap_inj_eth_realistic_values_self_relaying() {
let app = InjectiveTestApp::new();
Expand Down
109 changes: 109 additions & 0 deletions contracts/swap/src/testing/migration_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use crate::{
msg::{FeeRecipient, InstantiateMsg, MigrateMsg},
testing::{
integration_realistic_tests_min_quantity::happy_path_two_hops_test,
test_utils::{
must_init_account_with_funds, store_code, str_coin, Decimals, ATOM, ETH, INJ, USDT,
},
},
};

use cosmos_sdk_proto::cosmwasm::wasm::v1::{
MsgMigrateContract, MsgMigrateContractResponse, QueryContractInfoRequest,
QueryContractInfoResponse,
};
use cosmwasm_std::Addr;
use injective_test_tube::{Account, ExecuteResponse, InjectiveTestApp, Module, Runner, Wasm};

type V100InstantiateMsg = InstantiateMsg;

#[test]
#[cfg_attr(not(feature = "integration"), ignore)]
fn test_migration_v100_to_v101() {
let app = InjectiveTestApp::new();
let wasm = Wasm::new(&app);

let wasm_byte_code =
std::fs::read("../../contracts/swap/src/testing/test_artifacts/swap-contract-v100.wasm")
.unwrap();

let owner = must_init_account_with_funds(
&app,
&[
str_coin("1", ETH, Decimals::Eighteen),
str_coin("1", ATOM, Decimals::Six),
str_coin("1_000", USDT, Decimals::Six),
str_coin("10_000", INJ, Decimals::Eighteen),
],
);

let swap_v100_code_id = wasm
.store_code(&wasm_byte_code, None, &owner)
.unwrap()
.data
.code_id;

let swap_v100_address: String = wasm
.instantiate(
swap_v100_code_id,
&V100InstantiateMsg {
admin: Addr::unchecked(owner.address()),
fee_recipient: FeeRecipient::SwapContract,
},
Some(&owner.address()),
Some("swap-contract"),
&[str_coin("1_000", USDT, Decimals::Six)],
&owner,
)
.unwrap()
.data
.address;

let res: QueryContractInfoResponse = app
.query(
"/cosmwasm.wasm.v1.Query/ContractInfo",
&QueryContractInfoRequest {
address: swap_v100_address.clone(),
},
)
.unwrap();
let contract_info = res.contract_info.unwrap();

assert_eq!(res.address, swap_v100_address);
assert_eq!(contract_info.code_id, swap_v100_code_id);
assert_eq!(contract_info.creator, owner.address());
assert_eq!(contract_info.label, "swap-contract");

let swap_v110_code_id = store_code(&wasm, &owner, "swap_contract".to_string());

let _res: ExecuteResponse<MsgMigrateContractResponse> = app
.execute(
MsgMigrateContract {
sender: owner.address(),
contract: swap_v100_address.clone(),
code_id: swap_v110_code_id,
msg: serde_json_wasm::to_vec(&MigrateMsg {}).unwrap(),
},
"/cosmwasm.wasm.v1.MsgMigrateContract",
&owner,
)
.unwrap();

let res: QueryContractInfoResponse = app
.query(
"/cosmwasm.wasm.v1.Query/ContractInfo",
&QueryContractInfoRequest {
address: swap_v100_address.clone(),
},
)
.unwrap();

let contract_info = res.contract_info.unwrap();

assert_eq!(res.address, swap_v100_address);
assert_eq!(contract_info.code_id, swap_v110_code_id);
assert_eq!(contract_info.creator, owner.address());
assert_eq!(contract_info.label, "swap-contract");

happy_path_two_hops_test(app, owner, swap_v100_address);
}
1 change: 1 addition & 0 deletions contracts/swap/src/testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod config_tests;
mod integration_logic_tests;
mod integration_realistic_tests_exact_quantity;
mod integration_realistic_tests_min_quantity;
mod migration_test;
mod queries_tests;
mod storage_tests;
mod swap_tests;
Expand Down
Binary file not shown.

0 comments on commit a6e968b

Please sign in to comment.