Skip to content

Commit

Permalink
Merge pull request #973 from multiversx/additional-tests-add-liq
Browse files Browse the repository at this point in the history
additional tests for add liq disabled
  • Loading branch information
dorin-iancu authored Nov 15, 2024
2 parents b2beb0d + 4f70ada commit 78df98b
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 0 deletions.
1 change: 1 addition & 0 deletions 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/pair/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ num-bigint = "0.4.2"

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

[dev-dependencies.disable-add-liq]
path = "../../common/modules/disable-add-liq"
166 changes: 166 additions & 0 deletions dex/pair/tests/pair_rs_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(deprecated)]

mod pair_setup;
use disable_add_liq::DisableAddLiqModule;
use fees_collector::{
config::ConfigModule, fees_accumulation::FeesAccumulationModule, FeesCollector,
};
Expand Down Expand Up @@ -1650,3 +1651,168 @@ fn fees_collector_pair_test() {
})
.assert_ok();
}

#[test]
fn try_add_liq_disabled_test() {
let mut pair_setup = PairSetup::new(pair::contract_obj);

pair_setup.add_liquidity(
1_001_000, 1_000_000, 1_001_000, 1_000_000, 1_000_000, 1_001_000, 1_001_000,
);

// init locking SC
let lp_address = pair_setup.pair_wrapper.address_ref().clone();
let rust_zero = rust_biguint!(0);
let locking_owner = pair_setup.b_mock.create_user_account(&rust_zero);
let locking_sc_wrapper = pair_setup.b_mock.create_sc_account(
&rust_zero,
Some(&locking_owner),
simple_lock::contract_obj,
"Some path",
);

// setup locked token
pair_setup
.b_mock
.execute_tx(&locking_owner, &locking_sc_wrapper, &rust_zero, |sc| {
sc.init();
sc.locked_token()
.set_token_id(managed_token_id!(LOCKED_TOKEN_ID));
sc.add_lp_to_whitelist(
managed_address!(&lp_address),
managed_token_id!(WEGLD_TOKEN_ID),
managed_token_id!(MEX_TOKEN_ID),
);
})
.assert_ok();

pair_setup.b_mock.set_esdt_local_roles(
locking_sc_wrapper.address_ref(),
LOCKED_TOKEN_ID,
&[
EsdtLocalRole::NftCreate,
EsdtLocalRole::NftAddQuantity,
EsdtLocalRole::NftBurn,
],
);

// setup lp proxy token
pair_setup
.b_mock
.execute_tx(&locking_owner, &locking_sc_wrapper, &rust_zero, |sc| {
sc.init();
sc.lp_proxy_token()
.set_token_id(managed_token_id!(LP_PROXY_TOKEN_ID));
})
.assert_ok();

pair_setup.b_mock.set_esdt_local_roles(
locking_sc_wrapper.address_ref(),
LP_PROXY_TOKEN_ID,
&[
EsdtLocalRole::NftCreate,
EsdtLocalRole::NftAddQuantity,
EsdtLocalRole::NftBurn,
],
);

pair_setup.b_mock.set_block_epoch(5);

DebugApi::dummy();

// lock some tokens first
pair_setup
.b_mock
.execute_esdt_transfer(
&pair_setup.user_address,
&locking_sc_wrapper,
WEGLD_TOKEN_ID,
0,
&rust_biguint!(1_000_000),
|sc| {
sc.lock_tokens_endpoint(10, OptionalValue::None);
},
)
.assert_ok();

pair_setup.b_mock.check_nft_balance(
&pair_setup.user_address,
LOCKED_TOKEN_ID,
1,
&rust_biguint!(1_000_000),
Some(&LockedTokenAttributes::<DebugApi> {
original_token_id: managed_token_id_wrapped!(WEGLD_TOKEN_ID),
original_token_nonce: 0,
unlock_epoch: 10,
}),
);

pair_setup
.b_mock
.execute_esdt_transfer(
&pair_setup.user_address,
&locking_sc_wrapper,
MEX_TOKEN_ID,
0,
&rust_biguint!(2_000_000),
|sc| {
sc.lock_tokens_endpoint(15, OptionalValue::None);
},
)
.assert_ok();

pair_setup.b_mock.check_nft_balance(
&pair_setup.user_address,
LOCKED_TOKEN_ID,
2,
&rust_biguint!(2_000_000),
Some(&LockedTokenAttributes::<DebugApi> {
original_token_id: managed_token_id_wrapped!(MEX_TOKEN_ID),
original_token_nonce: 0,
unlock_epoch: 15,
}),
);

pair_setup.b_mock.set_block_epoch(5);

// disable add liquidity
pair_setup
.b_mock
.execute_tx(
&pair_setup.owner_address,
&locking_sc_wrapper,
&rust_zero,
|sc| {
sc.disable_add_liq();
},
)
.assert_ok();

// try add liquidity through simple-lock SC - one locked (WEGLD) token, one unlocked (MEX)
let transfers = [
TxTokenTransfer {
token_identifier: LOCKED_TOKEN_ID.to_vec(),
nonce: 1,
value: rust_biguint!(500_000),
},
TxTokenTransfer {
token_identifier: MEX_TOKEN_ID.to_vec(),
nonce: 0,
value: rust_biguint!(500_000),
},
];

pair_setup
.b_mock
.execute_esdt_multi_transfer(
&pair_setup.user_address,
&locking_sc_wrapper,
&transfers[..],
|sc| {
let _ = sc
.add_liquidity_locked_token(managed_biguint!(1), managed_biguint!(1))
.into_tuple();
},
)
.assert_user_error("Add Liquidity is disabled");
}
54 changes: 54 additions & 0 deletions locked-asset/proxy_dex/tests/proxy_lp_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod proxy_dex_test_setup;

use disable_add_liq::DisableAddLiqModule;
use energy_factory::{energy::EnergyModule, SimpleLockEnergy};
use energy_query::Energy;
use multiversx_sc::{
Expand Down Expand Up @@ -1402,3 +1403,56 @@ fn increase_proxy_lp_legacy_token_energy() {
)
.assert_error(4, "Invalid payment");
}

#[test]
fn add_liq_disabled_test() {
let mut setup = ProxySetup::new(
proxy_dex::contract_obj,
pair::contract_obj,
farm_with_locked_rewards::contract_obj,
energy_factory::contract_obj,
);
let first_user = setup.first_user.clone();
let locked_token_amount = rust_biguint!(1_000_000_000);
let other_token_amount = rust_biguint!(500_000_000);

// disable add liquidity
setup
.b_mock
.execute_tx(
&setup.owner,
&setup.proxy_wrapper,
&rust_biguint!(0),
|sc| {
sc.disable_add_liq();
},
)
.assert_ok();

// set the price to 1 EGLD = 2 MEX
let payments = vec![
TxTokenTransfer {
token_identifier: LOCKED_TOKEN_ID.to_vec(),
nonce: 1,
value: locked_token_amount.clone(),
},
TxTokenTransfer {
token_identifier: WEGLD_TOKEN_ID.to_vec(),
nonce: 0,
value: other_token_amount.clone(),
},
];

// try add liquidity
let pair_addr = setup.pair_wrapper.address_ref().clone();
setup
.b_mock
.execute_esdt_multi_transfer(&first_user, &setup.proxy_wrapper, &payments, |sc| {
sc.add_liquidity_proxy(
managed_address!(&pair_addr),
managed_biguint!(locked_token_amount.to_u64().unwrap()),
managed_biguint!(other_token_amount.to_u64().unwrap()),
);
})
.assert_user_error("Add Liquidity is disabled");
}

0 comments on commit 78df98b

Please sign in to comment.