From bd03a133ffdd12094c6173ee0b416af907c8f5a7 Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Thu, 21 Nov 2024 12:21:19 +0200 Subject: [PATCH] test --- .../farm/farm_base_impl/src/base_farm_init.rs | 6 +- common/modules/pausable/src/pausable.rs | 2 +- .../src/permissions_module.rs | 22 +- dex/farm/tests/farm_multi_user_test.rs | 2 +- dex/pair/src/lib.rs | 18 +- dex/router/tests/router_setup/mod.rs | 151 +++++----- dex/router/tests/router_test.rs | 269 +++++++++++++++++- locked-asset/lkmex-transfer/src/lib.rs | 2 +- 8 files changed, 368 insertions(+), 104 deletions(-) diff --git a/common/modules/farm/farm_base_impl/src/base_farm_init.rs b/common/modules/farm/farm_base_impl/src/base_farm_init.rs index 700057a3e..2e53f39c5 100644 --- a/common/modules/farm/farm_base_impl/src/base_farm_init.rs +++ b/common/modules/farm/farm_base_impl/src/base_farm_init.rs @@ -38,16 +38,16 @@ pub trait BaseFarmInitModule: self.farming_token_id().set_if_empty(&farming_token_id); if !owner.is_zero() { - self.add_permissions(owner, Permissions::OWNER | Permissions::PAUSE); + self.add_permissions(&owner, Permissions::OWNER | Permissions::PAUSE); } let caller = self.blockchain().get_caller(); if admins.is_empty() { // backwards compatibility let all_permissions = Permissions::OWNER | Permissions::ADMIN | Permissions::PAUSE; - self.add_permissions(caller, all_permissions); + self.add_permissions(&caller, all_permissions); } else { - self.add_permissions(caller, Permissions::OWNER | Permissions::PAUSE); + self.add_permissions(&caller, Permissions::OWNER | Permissions::PAUSE); self.add_permissions_for_all(admins, Permissions::ADMIN); }; } diff --git a/common/modules/pausable/src/pausable.rs b/common/modules/pausable/src/pausable.rs index 6f8813685..5e4ad8ed5 100644 --- a/common/modules/pausable/src/pausable.rs +++ b/common/modules/pausable/src/pausable.rs @@ -26,7 +26,7 @@ pub trait PausableModule: permissions_module::PermissionsModule { self.require_caller_has_owner_permissions(); for address in address_list { - self.remove_permissions(address, Permissions::PAUSE); + self.remove_permissions(&address, Permissions::PAUSE); } } diff --git a/common/modules/permissions_module/src/permissions_module.rs b/common/modules/permissions_module/src/permissions_module.rs index 38eab7fdd..d9980c6d6 100644 --- a/common/modules/permissions_module/src/permissions_module.rs +++ b/common/modules/permissions_module/src/permissions_module.rs @@ -13,36 +13,36 @@ pub trait PermissionsModule { #[endpoint(addAdmin)] fn add_admin_endpoint(&self, address: ManagedAddress) { self.require_caller_has_owner_permissions(); - self.add_permissions(address, Permissions::ADMIN); + self.add_permissions(&address, Permissions::ADMIN); } #[endpoint(removeAdmin)] fn remove_admin_endpoint(&self, address: ManagedAddress) { self.require_caller_has_owner_permissions(); - self.remove_permissions(address, Permissions::ADMIN); + self.remove_permissions(&address, Permissions::ADMIN); } #[only_owner] #[endpoint(updateOwnerOrAdmin)] fn update_owner_or_admin_endpoint(&self, previous_owner: ManagedAddress) { let caller = self.blockchain().get_caller(); - let previous_owner_permissions = self.permissions(previous_owner.clone()).get(); + let previous_owner_permissions = self.permissions(&previous_owner).get(); - self.permissions(previous_owner).clear(); - self.permissions(caller).set(previous_owner_permissions); + self.permissions(&previous_owner).clear(); + self.permissions(&caller).set(previous_owner_permissions); } - fn set_permissions(&self, address: ManagedAddress, permissions: Permissions) { + fn set_permissions(&self, address: &ManagedAddress, permissions: Permissions) { self.permissions(address).set(permissions); } - fn add_permissions(&self, address: ManagedAddress, new_permissions: Permissions) { + fn add_permissions(&self, address: &ManagedAddress, new_permissions: Permissions) { self.permissions(address).update(|permissions| { permissions.insert(new_permissions); }); } - fn remove_permissions(&self, address: ManagedAddress, permissions_to_remove: Permissions) { + fn remove_permissions(&self, address: &ManagedAddress, permissions_to_remove: Permissions) { self.permissions(address).update(|permissions| { permissions.remove(permissions_to_remove); }); @@ -54,13 +54,13 @@ pub trait PermissionsModule { permissions: Permissions, ) { for address in addresses { - self.add_permissions(address, permissions.clone()); + self.add_permissions(&address, permissions.clone()); } } fn require_caller_any_of(&self, permissions: Permissions) { let caller = self.blockchain().get_caller(); - let caller_permissions = self.permissions(caller).get(); + let caller_permissions = self.permissions(&caller).get(); require!( caller_permissions.intersects(permissions), ERROR_PERMISSION_DENIED @@ -85,5 +85,5 @@ pub trait PermissionsModule { #[view(getPermissions)] #[storage_mapper("permissions")] - fn permissions(&self, address: ManagedAddress) -> SingleValueMapper; + fn permissions(&self, address: &ManagedAddress) -> SingleValueMapper; } diff --git a/dex/farm/tests/farm_multi_user_test.rs b/dex/farm/tests/farm_multi_user_test.rs index d37bee046..72db6521c 100644 --- a/dex/farm/tests/farm_multi_user_test.rs +++ b/dex/farm/tests/farm_multi_user_test.rs @@ -254,7 +254,7 @@ fn farm_change_boosted_yields_factors_test() { BoostedYieldsConfig::new(current_week - 1, default_factors.clone()); assert_eq!(expected_config, sc.boosted_yields_config().get()); - sc.add_permissions(managed_address!(&farm_addr), Permissions::all()); + sc.add_permissions(&managed_address!(&farm_addr), Permissions::all()); sc.set_boosted_yields_factors( managed_biguint!(1u64), managed_biguint!(1u64), diff --git a/dex/pair/src/lib.rs b/dex/pair/src/lib.rs index 619a0e36b..9d4f806a0 100644 --- a/dex/pair/src/lib.rs +++ b/dex/pair/src/lib.rs @@ -79,25 +79,26 @@ pub trait Pair: self.state().set(State::Inactive); self.router_address().set(&router_address); - self.first_token_id().set_if_empty(&first_token_id); - self.second_token_id().set_if_empty(&second_token_id); - let initial_liquidity_adder_opt = if !initial_liquidity_adder.is_zero() { + self.first_token_id().set(first_token_id); + self.second_token_id().set(second_token_id); + + let opt_initial_liquidity_adder = if !initial_liquidity_adder.is_zero() { Some(initial_liquidity_adder) } else { None }; self.initial_liquidity_adder() - .set_if_empty(&initial_liquidity_adder_opt); + .set(&opt_initial_liquidity_adder); if admins.is_empty() { // backwards compatibility let all_permissions = Permissions::OWNER | Permissions::ADMIN | Permissions::PAUSE; - self.add_permissions(router_address, all_permissions.clone()); - self.add_permissions(router_owner_address, all_permissions); + self.add_permissions(&router_address, all_permissions.clone()); + self.add_permissions(&router_owner_address, all_permissions); } else { - self.add_permissions(router_address, Permissions::OWNER | Permissions::PAUSE); + self.add_permissions(&router_address, Permissions::OWNER | Permissions::PAUSE); self.add_permissions( - router_owner_address, + &router_owner_address, Permissions::OWNER | Permissions::PAUSE, ); self.add_permissions_for_all(admins, Permissions::ADMIN); @@ -124,6 +125,7 @@ pub trait Pair: token_identifier.is_valid_esdt_identifier(), ERROR_NOT_AN_ESDT ); + self.lp_token_identifier().set(&token_identifier); } } diff --git a/dex/router/tests/router_setup/mod.rs b/dex/router/tests/router_setup/mod.rs index 09b8267a3..3b7d246b1 100644 --- a/dex/router/tests/router_setup/mod.rs +++ b/dex/router/tests/router_setup/mod.rs @@ -6,13 +6,13 @@ use multiversx_sc_scenario::{ whitebox_legacy::*, DebugApi, }; -pub const PAIR_WASM_PATH: &str = "pair/output/pair.wasm"; -pub const ROUTER_WASM_PATH: &str = "router/output/router.wasm"; -pub const MEX_TOKEN_ID: &[u8] = b"MEX-abcdef"; -pub const WEGLD_TOKEN_ID: &[u8] = b"WEGLD-abcdef"; -pub const USDC_TOKEN_ID: &[u8] = b"USDC-abcdef"; -pub const LPMEX_TOKEN_ID: &[u8] = b"LPMEX-abcdef"; -pub const LPUSDC_TOKEN_ID: &[u8] = b"LPUSDC-abcdef"; +pub static PAIR_WASM_PATH: &str = "pair/output/pair.wasm"; +pub static ROUTER_WASM_PATH: &str = "router/output/router.wasm"; +pub static MEX_TOKEN_ID: &[u8] = b"MEX-abcdef"; +pub static WEGLD_TOKEN_ID: &[u8] = b"WEGLD-abcdef"; +pub static USDC_TOKEN_ID: &[u8] = b"USDC-abcdef"; +pub static LPMEX_TOKEN_ID: &[u8] = b"LPMEX-abcdef"; +pub static LPUSDC_TOKEN_ID: &[u8] = b"LPUSDC-abcdef"; pub const USER_TOTAL_MEX_TOKENS: u64 = 5_001_001_000; pub const USER_TOTAL_WEGLD_TOKENS: u64 = 5_002_002_000; @@ -36,13 +36,12 @@ use router::pair_actions::create::PairTokens; use router::pair_actions::multi_pair_swap::*; use router::*; -#[allow(dead_code)] pub struct RouterSetup where RouterObjBuilder: 'static + Copy + Fn() -> router::ContractObj, PairObjBuilder: 'static + Copy + Fn() -> pair::ContractObj, { - pub blockchain_wrapper: BlockchainStateWrapper, + pub b_mock: BlockchainStateWrapper, pub owner_address: Address, pub user_address: Address, pub router_wrapper: ContractObjWrapper, RouterObjBuilder>, @@ -57,85 +56,95 @@ 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( + let mex_pair_wrapper = b_mock.create_sc_account( &rust_zero, - Some(&owner_addr), + Some(router_wrapper.address_ref()), pair_builder, PAIR_WASM_PATH, ); - let usdc_pair_wrapper = blockchain_wrapper.create_sc_account( + let usdc_pair_wrapper = b_mock.create_sc_account( &rust_zero, - Some(&owner_addr), + Some(router_wrapper.address_ref()), pair_builder, PAIR_WASM_PATH, ); - blockchain_wrapper - .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); - let router_address = managed_address!(&owner_addr); - let router_owner_address = managed_address!(&owner_addr); - let total_fee_percent = 300u64; - let special_fee_percent = 50u64; + b_mock + .execute_tx( + router_wrapper.address_ref(), + &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); + let router_address = managed_address!(router_wrapper.address_ref()); + let router_owner_address = managed_address!(&owner_addr); + let total_fee_percent = 300u64; + let special_fee_percent = 50u64; - sc.init( - first_token_id, - second_token_id, - router_address, - router_owner_address, - total_fee_percent, - special_fee_percent, - ManagedAddress::::zero(), - MultiValueEncoded::>::new(), - ); + sc.init( + first_token_id, + second_token_id, + router_address, + router_owner_address, + total_fee_percent, + special_fee_percent, + ManagedAddress::::zero(), + MultiValueEncoded::>::new(), + ); - let lp_token_id = managed_token_id!(LPMEX_TOKEN_ID); - sc.lp_token_identifier().set(&lp_token_id); + let lp_token_id = managed_token_id!(LPMEX_TOKEN_ID); + sc.lp_token_identifier().set(&lp_token_id); - sc.state().set(State::Active); - }) + sc.state().set(State::Active); + }, + ) .assert_ok(); - blockchain_wrapper - .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); - let router_address = managed_address!(&owner_addr); - let router_owner_address = managed_address!(&owner_addr); - let total_fee_percent = 300u64; - let special_fee_percent = 50u64; + b_mock + .execute_tx( + router_wrapper.address_ref(), + &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); + let router_address = managed_address!(router_wrapper.address_ref()); + let router_owner_address = managed_address!(&owner_addr); + let total_fee_percent = 300u64; + let special_fee_percent = 50u64; - sc.init( - first_token_id, - second_token_id, - router_address, - router_owner_address, - total_fee_percent, - special_fee_percent, - ManagedAddress::::zero(), - MultiValueEncoded::>::new(), - ); + sc.init( + first_token_id, + second_token_id, + router_address, + router_owner_address, + total_fee_percent, + special_fee_percent, + ManagedAddress::::zero(), + MultiValueEncoded::>::new(), + ); - let lp_token_id = managed_token_id!(LPUSDC_TOKEN_ID); - sc.lp_token_identifier().set(&lp_token_id); + let lp_token_id = managed_token_id!(LPUSDC_TOKEN_ID); + sc.lp_token_identifier().set(&lp_token_id); - sc.state().set(State::Active); - }) + sc.state().set(State::Active); + }, + ) .assert_ok(); - blockchain_wrapper + b_mock .execute_tx(&owner_addr, &router_wrapper, &rust_zero, |sc| { sc.init(managed_token_id!(MEX_TOKEN_ID), OptionalValue::None); @@ -157,38 +166,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, @@ -211,7 +220,7 @@ where }, ]; - self.blockchain_wrapper + self.b_mock .execute_esdt_multi_transfer( &self.user_address, &self.mex_pair_wrapper, @@ -238,7 +247,7 @@ where }, ]; - self.blockchain_wrapper + self.b_mock .execute_esdt_multi_transfer( &self.user_address, &self.usdc_pair_wrapper, @@ -261,7 +270,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, diff --git a/dex/router/tests/router_test.rs b/dex/router/tests/router_test.rs index 9bb11bf62..dc5876bad 100644 --- a/dex/router/tests/router_test.rs +++ b/dex/router/tests/router_test.rs @@ -1,6 +1,7 @@ #![allow(deprecated)] mod router_setup; +use common_structs::TokenPair; use multiversx_sc::{ codec::multi_types::OptionalValue, storage::mappers::StorageTokenWrapper, @@ -10,8 +11,8 @@ use multiversx_sc::{ }, }; use pair::{ - config::ConfigModule as PairConfigModule, pair_actions::initial_liq::InitialLiquidityModule, - Pair, + config::ConfigModule as PairConfigModule, fee::storage::StorageModule, + pair_actions::initial_liq::InitialLiquidityModule, Pair, }; use pausable::{PausableModule, State}; use router::{ @@ -138,17 +139,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), @@ -171,17 +172,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 @@ -565,3 +566,255 @@ fn user_enable_pair_swaps_fail_test() { }), ); } + +#[test] +fn user_enable_swaps_and_buyback_and_burn() { + let mut setup = RouterSetup::new(router::contract_obj, pair::contract_obj); + + let current_epoch = 5; + setup.b_mock.set_block_epoch(current_epoch); + + setup.b_mock.set_esdt_balance( + &setup.user_address, + CUSTOM_TOKEN_ID, + &rust_biguint!(USER_CUSTOM_TOKEN_BALANCE), + ); + setup.b_mock.set_esdt_balance( + &setup.user_address, + WEGLD_TOKEN_ID, + &rust_biguint!(USER_USDC_BALANCE), + ); + + let new_pair_wrapper = setup.b_mock.create_sc_account( + &rust_biguint!(0), + Some(setup.router_wrapper.address_ref()), + pair::contract_obj, + PAIR_WASM_PATH, + ); + let simple_lock_wrapper = setup.b_mock.create_sc_account( + &rust_biguint!(0), + Some(&setup.owner_address), + simple_lock::contract_obj, + "simple-lock.wasm", + ); + + // setup simple-lock + setup + .b_mock + .execute_tx( + &setup.owner_address, + &simple_lock_wrapper, + &rust_biguint!(0), + |sc| { + sc.locked_token() + .set_token_id(managed_token_id!(LOCKED_TOKEN_ID)); + }, + ) + .assert_ok(); + + setup.b_mock.set_esdt_local_roles( + simple_lock_wrapper.address_ref(), + LOCKED_TOKEN_ID, + &[ + EsdtLocalRole::NftCreate, + EsdtLocalRole::NftAddQuantity, + EsdtLocalRole::NftBurn, + ], + ); + + // setup router + setup + .b_mock + .execute_tx( + &setup.owner_address, + &setup.router_wrapper, + &rust_biguint!(0), + |sc| { + sc.pair_map().insert( + PairTokens { + first_token_id: managed_token_id!(CUSTOM_TOKEN_ID), + second_token_id: managed_token_id!(WEGLD_TOKEN_ID), + }, + managed_address!(new_pair_wrapper.address_ref()), + ); + + sc.add_common_tokens_for_user_pairs(MultiValueEncoded::from(ManagedVec::from( + vec![managed_token_id!(WEGLD_TOKEN_ID)], + ))); + + sc.config_enable_by_user_parameters( + managed_token_id!(WEGLD_TOKEN_ID), + managed_token_id!(LOCKED_TOKEN_ID), + managed_biguint!(MIN_LOCKED_TOKEN_VALUE), + MIN_LOCKED_PERIOD_EPOCHS, + ) + }, + ) + .assert_ok(); + + // setup pair + let router_address = setup.router_wrapper.address_ref().clone(); + let owner_address = setup.owner_address.clone(); + let user_address = setup.user_address.clone(); + setup + .b_mock + .execute_tx( + setup.router_wrapper.address_ref(), + &new_pair_wrapper, + &rust_biguint!(0), + |sc| { + let first_token_id = managed_token_id!(CUSTOM_TOKEN_ID); + let second_token_id = managed_token_id!(WEGLD_TOKEN_ID); + let router_address = managed_address!(&router_address); + let router_owner_address = managed_address!(&owner_address); + + sc.init( + first_token_id, + second_token_id, + router_address, + router_owner_address, + 0, + 0, + managed_address!(&user_address), + MultiValueEncoded::>::new(), + ); + + assert_eq!(sc.state().get(), State::Inactive); + + sc.lp_token_identifier() + .set(&managed_token_id!(LPUSDC_TOKEN_ID)); + }, + ) + .assert_ok(); + + setup.b_mock.set_esdt_local_roles( + new_pair_wrapper.address_ref(), + LPUSDC_TOKEN_ID, + &[EsdtLocalRole::Mint, EsdtLocalRole::Burn], + ); + + // add liquidity + let payments = vec![ + TxTokenTransfer { + token_identifier: CUSTOM_TOKEN_ID.to_vec(), + nonce: 0, + value: rust_biguint!(USER_CUSTOM_TOKEN_BALANCE), + }, + TxTokenTransfer { + token_identifier: WEGLD_TOKEN_ID.to_vec(), + nonce: 0, + value: rust_biguint!(USER_USDC_BALANCE), + }, + ]; + + let user_lp_tokens_balance = 999_000u64; + setup + .b_mock + .execute_esdt_multi_transfer(&setup.user_address, &new_pair_wrapper, &payments, |sc| { + let (lp_tokens_received, _, _) = sc.add_initial_liquidity().into_tuple(); + assert_eq!( + lp_tokens_received.token_identifier, + managed_token_id!(LPUSDC_TOKEN_ID) + ); + assert_eq!( + lp_tokens_received.amount, + managed_biguint!(user_lp_tokens_balance) + ); + }) + .assert_ok(); + + // lock LP tokens + setup + .b_mock + .execute_esdt_transfer( + &setup.user_address, + &simple_lock_wrapper, + LPUSDC_TOKEN_ID, + 0, + &rust_biguint!(user_lp_tokens_balance), + |sc| { + sc.lock_tokens_endpoint( + current_epoch + MIN_LOCKED_PERIOD_EPOCHS, + OptionalValue::None, + ); + }, + ) + .assert_ok(); + + DebugApi::dummy(); + setup.b_mock.check_nft_balance( + &setup.user_address, + LOCKED_TOKEN_ID, + 1, + &rust_biguint!(user_lp_tokens_balance), + Some(&LockedTokenAttributes:: { + original_token_id: managed_token_id_wrapped!(LPUSDC_TOKEN_ID), + original_token_nonce: 0, + unlock_epoch: current_epoch + MIN_LOCKED_PERIOD_EPOCHS, + }), + ); + + // pass blocks time to update safe price + setup.b_mock.set_block_nonce(1_000_000); + + // activate swaps through router + setup + .b_mock + .execute_esdt_transfer( + &setup.user_address, + &setup.router_wrapper, + LOCKED_TOKEN_ID, + 1, + &rust_biguint!(user_lp_tokens_balance), + |sc| { + sc.set_swap_enabled_by_user(managed_address!(new_pair_wrapper.address_ref())); + }, + ) + .assert_ok(); + + // check pair state is active + setup + .b_mock + .execute_query(&new_pair_wrapper, |sc| { + assert_eq!(sc.state().get(), State::Active); + }) + .assert_ok(); + + // check user received the locked tokens back + setup.b_mock.check_nft_balance( + &setup.user_address, + LOCKED_TOKEN_ID, + 1, + &rust_biguint!(user_lp_tokens_balance), + Some(&LockedTokenAttributes:: { + original_token_id: managed_token_id_wrapped!(LPUSDC_TOKEN_ID), + original_token_nonce: 0, + unlock_epoch: current_epoch + MIN_LOCKED_PERIOD_EPOCHS, + }), + ); + + // check new pair was whitelisted in existing pair + setup + .b_mock + .execute_query(&setup.mex_pair_wrapper, |sc| { + assert!(sc + .whitelist() + .contains(&managed_address!(new_pair_wrapper.address_ref()))); + }) + .assert_ok(); + + setup + .b_mock + .execute_query(&new_pair_wrapper, |sc| { + // check existing pair was added as trusted swap in new pair + assert!(sc.trusted_swap_pair().contains_key(&TokenPair { + first_token: managed_token_id!(WEGLD_TOKEN_ID), + second_token: managed_token_id!(MEX_TOKEN_ID), + })); + + // check fee was set on + let opt_entry = sc.destination_map().get(&ManagedAddress::zero()); + assert_eq!(opt_entry, Some(managed_token_id!(MEX_TOKEN_ID))); + }) + .assert_ok(); +} diff --git a/locked-asset/lkmex-transfer/src/lib.rs b/locked-asset/lkmex-transfer/src/lib.rs index 126e740b8..a166c08de 100644 --- a/locked-asset/lkmex-transfer/src/lib.rs +++ b/locked-asset/lkmex-transfer/src/lib.rs @@ -53,7 +53,7 @@ pub trait LkmexTransfer: self.set_energy_factory_address(energy_factory_address); let caller = self.blockchain().get_caller(); - self.add_permissions(caller, Permissions::OWNER); + self.add_permissions(&caller, Permissions::OWNER); } #[upgrade]