From 5258ca5e2ab17cdcbde65a3daa9f378ade219782 Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Thu, 14 Nov 2024 12:40:52 +0200 Subject: [PATCH 1/2] additional tests for add liq disabled --- Cargo.lock | 1 + dex/pair/Cargo.toml | 3 + dex/pair/tests/pair_rs_test.rs | 166 ++++++++++++++++++ locked-asset/proxy_dex/tests/proxy_lp_test.rs | 54 ++++++ 4 files changed, 224 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index ffba89587..0d49fa708 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1378,6 +1378,7 @@ version = "0.0.0" dependencies = [ "common_errors", "common_structs", + "disable-add-liq", "fees-collector", "itertools 0.10.5", "multiversx-sc", diff --git a/dex/pair/Cargo.toml b/dex/pair/Cargo.toml index 7f913c316..6e784fa7e 100644 --- a/dex/pair/Cargo.toml +++ b/dex/pair/Cargo.toml @@ -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" diff --git a/dex/pair/tests/pair_rs_test.rs b/dex/pair/tests/pair_rs_test.rs index bd82750d8..63a5c380d 100644 --- a/dex/pair/tests/pair_rs_test.rs +++ b/dex/pair/tests/pair_rs_test.rs @@ -1,6 +1,7 @@ #![allow(deprecated)] mod pair_setup; +use disable_add_liq::DisableAddLiqModule; use fees_collector::{ config::ConfigModule, fees_accumulation::FeesAccumulationModule, FeesCollector, }; @@ -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:: { + 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:: { + 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 = vec![ + 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"); +} diff --git a/locked-asset/proxy_dex/tests/proxy_lp_test.rs b/locked-asset/proxy_dex/tests/proxy_lp_test.rs index 5d524ba3a..2b6972f68 100644 --- a/locked-asset/proxy_dex/tests/proxy_lp_test.rs +++ b/locked-asset/proxy_dex/tests/proxy_lp_test.rs @@ -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::{ @@ -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"); +} From 4f70ada88a5ef58fc509b330d98a5a2c066d3685 Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Thu, 14 Nov 2024 12:43:19 +0200 Subject: [PATCH 2/2] evil clippy --- dex/pair/tests/pair_rs_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dex/pair/tests/pair_rs_test.rs b/dex/pair/tests/pair_rs_test.rs index 63a5c380d..01ee97bc5 100644 --- a/dex/pair/tests/pair_rs_test.rs +++ b/dex/pair/tests/pair_rs_test.rs @@ -1789,7 +1789,7 @@ fn try_add_liq_disabled_test() { .assert_ok(); // try add liquidity through simple-lock SC - one locked (WEGLD) token, one unlocked (MEX) - let transfers = vec![ + let transfers = [ TxTokenTransfer { token_identifier: LOCKED_TOKEN_ID.to_vec(), nonce: 1,