Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Dec 11, 2024
1 parent 1e583c9 commit 8497b70
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 36 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions dex/router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ path = "../../locked-asset/simple-lock"

[dev-dependencies.multiversx-sc-scenario]
version = "=0.53.2"

[dev-dependencies.fees-collector]
path = "../../energy-integration/fees-collector"
50 changes: 21 additions & 29 deletions dex/router/tests/router_setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ where
RouterObjBuilder: 'static + Copy + Fn() -> router::ContractObj<DebugApi>,
PairObjBuilder: 'static + Copy + Fn() -> pair::ContractObj<DebugApi>,
{
pub blockchain_wrapper: BlockchainStateWrapper,
pub b_mock: BlockchainStateWrapper,
pub owner_address: Address,
pub user_address: Address,
pub router_wrapper: ContractObjWrapper<router::ContractObj<DebugApi>, RouterObjBuilder>,
Expand All @@ -57,31 +57,23 @@ where
{
pub fn new(router_builder: RouterObjBuilder, pair_builder: PairObjBuilder) -> Self {
let rust_zero = rust_biguint!(0u64);
let mut blockchain_wrapper = BlockchainStateWrapper::new();
let owner_addr = blockchain_wrapper.create_user_account(&rust_zero);
let mut b_mock = BlockchainStateWrapper::new();
let owner_addr = b_mock.create_user_account(&rust_zero);

let router_wrapper = blockchain_wrapper.create_sc_account(
let router_wrapper = b_mock.create_sc_account(
&rust_zero,
Some(&owner_addr),
router_builder,
ROUTER_WASM_PATH,
);

let mex_pair_wrapper = blockchain_wrapper.create_sc_account(
&rust_zero,
Some(&owner_addr),
pair_builder,
PAIR_WASM_PATH,
);
let mex_pair_wrapper =
b_mock.create_sc_account(&rust_zero, Some(&owner_addr), pair_builder, PAIR_WASM_PATH);

let usdc_pair_wrapper = blockchain_wrapper.create_sc_account(
&rust_zero,
Some(&owner_addr),
pair_builder,
PAIR_WASM_PATH,
);
let usdc_pair_wrapper =
b_mock.create_sc_account(&rust_zero, Some(&owner_addr), pair_builder, PAIR_WASM_PATH);

blockchain_wrapper
b_mock
.execute_tx(&owner_addr, &mex_pair_wrapper, &rust_zero, |sc| {
let first_token_id = managed_token_id!(WEGLD_TOKEN_ID);
let second_token_id = managed_token_id!(MEX_TOKEN_ID);
Expand All @@ -108,7 +100,7 @@ where
})
.assert_ok();

blockchain_wrapper
b_mock
.execute_tx(&owner_addr, &usdc_pair_wrapper, &rust_zero, |sc| {
let first_token_id = managed_token_id!(WEGLD_TOKEN_ID);
let second_token_id = managed_token_id!(USDC_TOKEN_ID);
Expand All @@ -135,7 +127,7 @@ where
})
.assert_ok();

blockchain_wrapper
b_mock
.execute_tx(&owner_addr, &router_wrapper, &rust_zero, |sc| {
sc.init(OptionalValue::None);

Expand All @@ -157,38 +149,38 @@ where
.assert_ok();

let lp_token_roles = [EsdtLocalRole::Mint, EsdtLocalRole::Burn];
blockchain_wrapper.set_esdt_local_roles(
b_mock.set_esdt_local_roles(
mex_pair_wrapper.address_ref(),
LPMEX_TOKEN_ID,
&lp_token_roles[..],
);

let lp_token_roles = [EsdtLocalRole::Mint, EsdtLocalRole::Burn];
blockchain_wrapper.set_esdt_local_roles(
b_mock.set_esdt_local_roles(
usdc_pair_wrapper.address_ref(),
LPUSDC_TOKEN_ID,
&lp_token_roles[..],
);

let user_addr = blockchain_wrapper.create_user_account(&rust_biguint!(100_000_000));
blockchain_wrapper.set_esdt_balance(
let user_addr = b_mock.create_user_account(&rust_biguint!(100_000_000));
b_mock.set_esdt_balance(
&user_addr,
WEGLD_TOKEN_ID,
&rust_biguint!(USER_TOTAL_WEGLD_TOKENS),
);
blockchain_wrapper.set_esdt_balance(
b_mock.set_esdt_balance(
&user_addr,
MEX_TOKEN_ID,
&rust_biguint!(USER_TOTAL_MEX_TOKENS),
);
blockchain_wrapper.set_esdt_balance(
b_mock.set_esdt_balance(
&user_addr,
USDC_TOKEN_ID,
&rust_biguint!(USER_TOTAL_USDC_TOKENS),
);

RouterSetup {
blockchain_wrapper,
b_mock,
owner_address: owner_addr,
user_address: user_addr,
router_wrapper,
Expand All @@ -211,7 +203,7 @@ where
},
];

self.blockchain_wrapper
self.b_mock
.execute_esdt_multi_transfer(
&self.user_address,
&self.mex_pair_wrapper,
Expand All @@ -238,7 +230,7 @@ where
},
];

self.blockchain_wrapper
self.b_mock
.execute_esdt_multi_transfer(
&self.user_address,
&self.usdc_pair_wrapper,
Expand All @@ -261,7 +253,7 @@ where
) {
let payment_amount_big = rust_biguint!(payment_amount);

self.blockchain_wrapper
self.b_mock
.execute_esdt_transfer(
&self.user_address,
&self.router_wrapper,
Expand Down
80 changes: 74 additions & 6 deletions dex/router/tests/router_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(deprecated)]

mod router_setup;
use fees_collector::{fees_accumulation::FeesAccumulationModule, FeesCollector};
use multiversx_sc::{
codec::multi_types::OptionalValue,
storage::mappers::StorageTokenWrapper,
Expand Down Expand Up @@ -131,17 +132,17 @@ fn test_multi_pair_swap() {

router_setup.add_liquidity();

router_setup.blockchain_wrapper.check_esdt_balance(
router_setup.b_mock.check_esdt_balance(
&router_setup.user_address,
WEGLD_TOKEN_ID,
&rust_biguint!(5_000_000_000),
);
router_setup.blockchain_wrapper.check_esdt_balance(
router_setup.b_mock.check_esdt_balance(
&router_setup.user_address,
MEX_TOKEN_ID,
&rust_biguint!(5_000_000_000),
);
router_setup.blockchain_wrapper.check_esdt_balance(
router_setup.b_mock.check_esdt_balance(
&router_setup.user_address,
USDC_TOKEN_ID,
&rust_biguint!(5_000_000_000),
Expand All @@ -164,17 +165,17 @@ fn test_multi_pair_swap() {

router_setup.multi_pair_swap(MEX_TOKEN_ID, 100_000, &ops);

router_setup.blockchain_wrapper.check_esdt_balance(
router_setup.b_mock.check_esdt_balance(
&router_setup.user_address,
WEGLD_TOKEN_ID,
&rust_biguint!(5_000_000_000), //unchanged
);
router_setup.blockchain_wrapper.check_esdt_balance(
router_setup.b_mock.check_esdt_balance(
&router_setup.user_address,
MEX_TOKEN_ID,
&rust_biguint!(4_999_900_000), //spent 100_000
);
router_setup.blockchain_wrapper.check_esdt_balance(
router_setup.b_mock.check_esdt_balance(
&router_setup.user_address,
USDC_TOKEN_ID,
&rust_biguint!(5_000_082_909), //gained 82_909
Expand Down Expand Up @@ -558,3 +559,70 @@ fn user_enable_pair_swaps_fail_test() {
}),
);
}

#[test]
fn fees_collector_base_token_feature_test() {
let mut setup = RouterSetup::new(router::contract_obj, pair::contract_obj);

setup.add_liquidity();

let fc_wrapper = setup.b_mock.create_sc_account(
&rust_biguint!(0),
Some(&setup.owner_address),
fees_collector::contract_obj,
"fees collector path",
);

let router_address = setup.router_wrapper.address_ref().clone();
setup
.b_mock
.execute_tx(&setup.owner_address, &fc_wrapper, &rust_biguint!(0), |sc| {
sc.init(
managed_token_id!(b"LOCKED-123456"), // unused
managed_address!(&router_address), // unused
managed_address!(&router_address),
managed_token_id!(WEGLD_TOKEN_ID),
MultiValueEncoded::new(),
);

let mut tokens = MultiValueEncoded::new();
tokens.push(managed_token_id!(WEGLD_TOKEN_ID));
tokens.push(managed_token_id!(USDC_TOKEN_ID));
tokens.push(managed_token_id!(CUSTOM_TOKEN_ID));

// must use qualified syntax, otherwise, you get complaints of multiple "config" modules
fees_collector::config::ConfigModule::add_known_tokens(&sc, tokens);

let _ = fees_collector::config::ConfigModule::known_contracts(&sc)
.insert(managed_address!(&setup.owner_address));
})
.assert_ok();

// try deposit USDC
setup
.b_mock
.set_esdt_balance(&setup.owner_address, USDC_TOKEN_ID, &rust_biguint!(1_000));

setup
.b_mock
.execute_esdt_transfer(
&setup.owner_address,
&fc_wrapper,
USDC_TOKEN_ID,
0,
&rust_biguint!(1_000),
|sc| {
sc.deposit_swap_fees();

// check fees were accumulate for WEGLD instead of USDC
assert!(sc
.accumulated_fees(1, &managed_token_id!(USDC_TOKEN_ID))
.is_empty());

assert!(!sc
.accumulated_fees(1, &managed_token_id!(WEGLD_TOKEN_ID))
.is_empty());
},
)
.assert_ok();
}

0 comments on commit 8497b70

Please sign in to comment.