diff --git a/Cargo.lock b/Cargo.lock index 724f6ca9d..0d49fa708 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -342,6 +342,13 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "distribution-tests" version = "0.0.0" @@ -1371,6 +1378,7 @@ version = "0.0.0" dependencies = [ "common_errors", "common_structs", + "disable-add-liq", "fees-collector", "itertools 0.10.5", "multiversx-sc", @@ -1587,6 +1595,7 @@ version = "0.0.0" dependencies = [ "common_structs", "config", + "disable-add-liq", "energy-factory", "energy-query", "farm", @@ -1822,6 +1831,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "hex", "multiversx-sc", "multiversx-sc-modules", diff --git a/common/modules/disable-add-liq/Cargo.toml b/common/modules/disable-add-liq/Cargo.toml new file mode 100644 index 000000000..13194c5ca --- /dev/null +++ b/common/modules/disable-add-liq/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "disable-add-liq" +version = "0.0.0" +authors = ["Dorin Iancu "] +edition = "2021" + +[lib] +path = "src/lib.rs" + +[dependencies.multiversx-sc] +version = "=0.53.2" +features = ["esdt-token-payment-legacy-decode"] diff --git a/common/modules/disable-add-liq/src/lib.rs b/common/modules/disable-add-liq/src/lib.rs new file mode 100644 index 000000000..63bf5e6e5 --- /dev/null +++ b/common/modules/disable-add-liq/src/lib.rs @@ -0,0 +1,32 @@ +#![no_std] + +multiversx_sc::imports!(); + +pub const ADD_LIQ_ENABLED: bool = true; +pub const ADD_LIQ_DISABLED: bool = false; + +#[multiversx_sc::module] +pub trait DisableAddLiqModule { + #[only_owner] + #[endpoint(enableAddLiq)] + fn enable_add_liq(&self) { + self.add_liq_enabled().set(ADD_LIQ_ENABLED); + } + + #[only_owner] + #[endpoint(disableAddLiq)] + fn disable_add_liq(&self) { + self.add_liq_enabled().set(ADD_LIQ_DISABLED); + } + + fn require_add_liq_enabled(&self) { + require!( + self.add_liq_enabled().get() == ADD_LIQ_ENABLED, + "Add Liquidity is disabled" + ); + } + + #[view(isAddLiqEnabled)] + #[storage_mapper("addLiqEnabled")] + fn add_liq_enabled(&self) -> SingleValueMapper; +} diff --git a/dex/farm-with-locked-rewards/wasm/Cargo.lock b/dex/farm-with-locked-rewards/wasm/Cargo.lock index 897ce8c8e..6aa2b48bf 100644 --- a/dex/farm-with-locked-rewards/wasm/Cargo.lock +++ b/dex/farm-with-locked-rewards/wasm/Cargo.lock @@ -74,6 +74,13 @@ dependencies = [ "token_send", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "either" version = "1.13.0" @@ -488,6 +495,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/dex/farm-with-locked-rewards/wasm/src/lib.rs b/dex/farm-with-locked-rewards/wasm/src/lib.rs index e8aa3099f..1aff7cb49 100644 --- a/dex/farm-with-locked-rewards/wasm/src/lib.rs +++ b/dex/farm-with-locked-rewards/wasm/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 66 +// Endpoints: 65 // Async Callback: 1 -// Total number of exported functions: 69 +// Total number of exported functions: 68 #![no_std] @@ -71,7 +71,6 @@ multiversx_sc_wasm_adapter::endpoints! { getAccumulatedRewardsForWeek => accumulated_rewards_for_week getFarmSupplyForWeek => farm_supply_for_week getRemainingBoostedRewardsToDistribute => remaining_boosted_rewards_to_distribute - getUndistributedBoostedRewards => undistributed_boosted_rewards setBoostedYieldsFactors => set_boosted_yields_factors getBoostedYieldsFactors => get_boosted_yields_factors getCurrentWeek => get_current_week diff --git a/dex/farm/tests/farm_multi_user_test.rs b/dex/farm/tests/farm_multi_user_test.rs index 03315b184..210e3b799 100644 --- a/dex/farm/tests/farm_multi_user_test.rs +++ b/dex/farm/tests/farm_multi_user_test.rs @@ -690,12 +690,14 @@ fn farm_multiple_claim_weeks_with_collect_undistributed_rewards_test() { farm_setup.check_error_collect_undistributed_boosted_rewards( "Current week must be higher than the week offset", + 1, + 4, ); // advance to week 6 farm_setup.b_mock.set_block_epoch(36); - farm_setup.collect_undistributed_boosted_rewards(); + farm_setup.collect_undistributed_boosted_rewards(1, 1); farm_setup.check_undistributed_boosted_rewards(1); farm_setup.check_remaining_boosted_rewards_to_distribute(1, 0); farm_setup.check_remaining_boosted_rewards_to_distribute(2, 1); @@ -704,7 +706,7 @@ fn farm_multiple_claim_weeks_with_collect_undistributed_rewards_test() { // advance to week 8 farm_setup.b_mock.set_block_epoch(50); - farm_setup.collect_undistributed_boosted_rewards(); + farm_setup.collect_undistributed_boosted_rewards(1, 3); farm_setup.check_undistributed_boosted_rewards(3); farm_setup.check_remaining_boosted_rewards_to_distribute(1, 0); @@ -966,7 +968,7 @@ fn farm_claim_with_minimum_tokens() { let remaining_boosted_yields_rewards = total_boosted_yields_rewards - first_boosted_amt - second_boosted_amt; farm_setup.check_undistributed_boosted_rewards(0); - farm_setup.collect_undistributed_boosted_rewards(); + farm_setup.collect_undistributed_boosted_rewards(1, 1); farm_setup.check_undistributed_boosted_rewards(remaining_boosted_yields_rewards); farm_setup.check_remaining_boosted_rewards_to_distribute(1, 0); farm_setup.check_remaining_boosted_rewards_to_distribute(2, 0); diff --git a/dex/farm/tests/farm_setup/multi_user_farm_setup.rs b/dex/farm/tests/farm_setup/multi_user_farm_setup.rs index cb12a2a1e..b681b2166 100644 --- a/dex/farm/tests/farm_setup/multi_user_farm_setup.rs +++ b/dex/farm/tests/farm_setup/multi_user_farm_setup.rs @@ -24,9 +24,11 @@ use farm_boosted_yields::FarmBoostedYieldsModule; use farm_token::FarmTokenModule; use pausable::{PausableModule, State}; use sc_whitelist_module::SCWhitelistModule; -use week_timekeeping::Epoch; +use week_timekeeping::{Epoch, Week}; use weekly_rewards_splitting::update_claim_progress_energy::UpdateClaimProgressEnergyModule; +use super::single_user_farm_setup::MEX_TOKEN_ID; + pub static REWARD_TOKEN_ID: &[u8] = b"REW-123456"; pub static FARMING_TOKEN_ID: &[u8] = b"LPTOK-123456"; pub static FARM_TOKEN_ID: &[u8] = b"FARM-123456"; @@ -65,6 +67,7 @@ where pub first_user: Address, pub second_user: Address, pub third_user: Address, + pub undistributed_rew_dest: Address, pub last_farm_token_nonce: u64, pub farm_wrapper: ContractObjWrapper, FarmObjBuilder>, pub energy_factory_wrapper: @@ -91,6 +94,7 @@ where let first_user = b_mock.create_user_account(&rust_zero); let second_user = b_mock.create_user_account(&rust_zero); let third_user = b_mock.create_user_account(&rust_zero); + let undistributed_rew_dest = b_mock.create_user_account(&rust_zero); let farm_wrapper = b_mock.create_sc_account(&rust_zero, Some(&owner), farm_builder, "farm.wasm"); let energy_factory_wrapper = b_mock.create_sc_account( @@ -102,6 +106,20 @@ where let eu_wrapper = b_mock.create_sc_account(&rust_zero, Some(&owner), eu_builder, "energy update mock"); + b_mock + .execute_tx(&owner, &energy_factory_wrapper, &rust_zero, |sc| { + sc.init(); + sc.base_asset_token_id() + .set(managed_token_id!(MEX_TOKEN_ID)); + }) + .assert_ok(); + + b_mock.set_esdt_local_roles( + energy_factory_wrapper.address_ref(), + MEX_TOKEN_ID, + &[EsdtLocalRole::Mint], + ); + b_mock .execute_tx(&owner, &eu_wrapper, &rust_zero, |sc| { sc.init(); @@ -185,6 +203,7 @@ where first_user, second_user, third_user, + undistributed_rew_dest, last_farm_token_nonce: 0, farm_wrapper, energy_factory_wrapper, @@ -651,18 +670,33 @@ where .assert_ok(); } - pub fn check_error_collect_undistributed_boosted_rewards(&mut self, expected_message: &str) { + pub fn check_error_collect_undistributed_boosted_rewards( + &mut self, + expected_message: &str, + start_week: Week, + end_week: Week, + ) { + let dest_address = self.undistributed_rew_dest.clone(); self.b_mock .execute_tx(&self.owner, &self.farm_wrapper, &rust_biguint!(0), |sc| { - sc.collect_undistributed_boosted_rewards(); + sc.collect_undistributed_boosted_rewards( + start_week, + end_week, + managed_address!(&dest_address), + ); }) .assert_error(4, expected_message) } - pub fn collect_undistributed_boosted_rewards(&mut self) { + pub fn collect_undistributed_boosted_rewards(&mut self, start_week: Week, end_week: Week) { + let dest_address = self.undistributed_rew_dest.clone(); self.b_mock .execute_tx(&self.owner, &self.farm_wrapper, &rust_biguint!(0), |sc| { - sc.collect_undistributed_boosted_rewards(); + sc.collect_undistributed_boosted_rewards( + start_week, + end_week, + managed_address!(&dest_address), + ); }) .assert_ok(); } @@ -683,12 +717,11 @@ where } pub fn check_undistributed_boosted_rewards(&mut self, expected_amount: u64) { - self.b_mock - .execute_query(&self.farm_wrapper, |sc| { - let result_managed = sc.undistributed_boosted_rewards().get(); - assert_eq!(result_managed, managed_biguint!(expected_amount)); - }) - .assert_ok(); + self.b_mock.check_esdt_balance( + &self.undistributed_rew_dest, + MEX_TOKEN_ID, + &rust_biguint!(expected_amount), + ); } pub fn check_farm_token_supply(&mut self, expected_farm_token_supply: u64) { diff --git a/dex/farm/wasm/Cargo.lock b/dex/farm/wasm/Cargo.lock index c3dea8cdc..92b4a7933 100644 --- a/dex/farm/wasm/Cargo.lock +++ b/dex/farm/wasm/Cargo.lock @@ -74,6 +74,13 @@ dependencies = [ "token_send", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "either" version = "1.13.0" @@ -458,6 +465,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/dex/farm/wasm/src/lib.rs b/dex/farm/wasm/src/lib.rs index 525d76239..312ed3f3b 100644 --- a/dex/farm/wasm/src/lib.rs +++ b/dex/farm/wasm/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 63 +// Endpoints: 62 // Async Callback: 1 -// Total number of exported functions: 66 +// Total number of exported functions: 65 #![no_std] @@ -68,7 +68,6 @@ multiversx_sc_wasm_adapter::endpoints! { getAccumulatedRewardsForWeek => accumulated_rewards_for_week getFarmSupplyForWeek => farm_supply_for_week getRemainingBoostedRewardsToDistribute => remaining_boosted_rewards_to_distribute - getUndistributedBoostedRewards => undistributed_boosted_rewards setBoostedYieldsFactors => set_boosted_yields_factors getBoostedYieldsFactors => get_boosted_yields_factors getCurrentWeek => get_current_week 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/src/lib.rs b/dex/pair/src/lib.rs index e67010d67..76c3c1f5b 100644 --- a/dex/pair/src/lib.rs +++ b/dex/pair/src/lib.rs @@ -124,3 +124,4 @@ pub trait Pair: self.lp_token_identifier().set(&token_identifier); } } + diff --git a/dex/pair/tests/pair_rs_test.rs b/dex/pair/tests/pair_rs_test.rs index bd82750d8..39edfb746 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, ADD_LIQ_ENABLED}; use fees_collector::{ config::ConfigModule, fees_accumulation::FeesAccumulationModule, FeesCollector, }; @@ -1292,6 +1293,8 @@ fn add_liquidity_through_simple_lock_proxy() { .b_mock .execute_tx(&locking_owner, &locking_sc_wrapper, &rust_zero, |sc| { sc.init(); + sc.add_liq_enabled().set(ADD_LIQ_ENABLED); + sc.locked_token() .set_token_id(managed_token_id!(LOCKED_TOKEN_ID)); sc.add_lp_to_whitelist( @@ -1650,3 +1653,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 = [ + 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/dex/pair/wasm-pair-full/Cargo.lock b/dex/pair/wasm-pair-full/Cargo.lock index 50c1aa77e..fe9af7730 100644 --- a/dex/pair/wasm-pair-full/Cargo.lock +++ b/dex/pair/wasm-pair-full/Cargo.lock @@ -45,6 +45,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "either" version = "1.13.0" @@ -327,6 +334,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/dex/pair/wasm-safe-price-view/Cargo.lock b/dex/pair/wasm-safe-price-view/Cargo.lock index e71c9ac82..47b6d15f3 100644 --- a/dex/pair/wasm-safe-price-view/Cargo.lock +++ b/dex/pair/wasm-safe-price-view/Cargo.lock @@ -45,6 +45,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "either" version = "1.13.0" @@ -327,6 +334,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/dex/pair/wasm/Cargo.lock b/dex/pair/wasm/Cargo.lock index 1b9f258d3..22f253c6a 100644 --- a/dex/pair/wasm/Cargo.lock +++ b/dex/pair/wasm/Cargo.lock @@ -45,6 +45,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "either" version = "1.13.0" @@ -327,6 +334,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/dex/price-discovery/wasm/Cargo.lock b/dex/price-discovery/wasm/Cargo.lock index 4fbe6c51b..62c383266 100644 --- a/dex/price-discovery/wasm/Cargo.lock +++ b/dex/price-discovery/wasm/Cargo.lock @@ -38,6 +38,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "endian-type" version = "0.1.2" @@ -262,6 +269,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/dex/proxy-deployer/wasm/Cargo.lock b/dex/proxy-deployer/wasm/Cargo.lock index 8d87420e6..88813977d 100644 --- a/dex/proxy-deployer/wasm/Cargo.lock +++ b/dex/proxy-deployer/wasm/Cargo.lock @@ -74,6 +74,13 @@ dependencies = [ "token_send", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "either" version = "1.13.0" @@ -469,6 +476,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/dex/router/wasm/Cargo.lock b/dex/router/wasm/Cargo.lock index a8e9d5976..8c965d9a5 100644 --- a/dex/router/wasm/Cargo.lock +++ b/dex/router/wasm/Cargo.lock @@ -45,6 +45,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "either" version = "1.13.0" @@ -339,6 +346,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/energy-integration/common-modules/weekly-rewards-splitting/src/lib.rs b/energy-integration/common-modules/weekly-rewards-splitting/src/lib.rs index 456bf60d2..e1d39978d 100644 --- a/energy-integration/common-modules/weekly-rewards-splitting/src/lib.rs +++ b/energy-integration/common-modules/weekly-rewards-splitting/src/lib.rs @@ -3,7 +3,7 @@ multiversx_sc::imports!(); multiversx_sc::derive_imports!(); -pub const USER_MAX_CLAIM_WEEKS: usize = 4; +pub const USER_MAX_CLAIM_WEEKS: Week = 4; pub mod base_impl; pub mod events; diff --git a/energy-integration/energy-factory-mock/src/lib.rs b/energy-integration/energy-factory-mock/src/lib.rs index d3354e67e..2b2693b78 100644 --- a/energy-integration/energy-factory-mock/src/lib.rs +++ b/energy-integration/energy-factory-mock/src/lib.rs @@ -52,9 +52,23 @@ pub trait EnergyFactoryMock { self.user_energy(&user).set(&energy); } + #[endpoint(transferUnlockedToken)] + fn transfer_unlocked_token(&self, dest: ManagedAddress, amount: BigUint) { + require!(amount != 0, "Invalid amount"); + + let base_asset_token_id = self.base_asset_token_id().get(); + self.send() + .esdt_local_mint(&base_asset_token_id, 0, &amount); + self.send() + .direct_esdt(&dest, &base_asset_token_id, 0, &amount); + } + #[storage_mapper("userEnergy")] fn user_energy(&self, user: &ManagedAddress) -> SingleValueMapper>; #[storage_mapper("lockedTokenId")] fn locked_token(&self) -> NonFungibleTokenMapper; + + #[storage_mapper("baseAssetTokenId")] + fn base_asset_token_id(&self) -> SingleValueMapper; } diff --git a/energy-integration/energy-factory-mock/wasm/Cargo.lock b/energy-integration/energy-factory-mock/wasm/Cargo.lock index 4e08004ac..0937f217e 100644 --- a/energy-integration/energy-factory-mock/wasm/Cargo.lock +++ b/energy-integration/energy-factory-mock/wasm/Cargo.lock @@ -38,6 +38,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "endian-type" version = "0.1.2" @@ -253,6 +260,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/energy-integration/energy-factory-mock/wasm/src/lib.rs b/energy-integration/energy-factory-mock/wasm/src/lib.rs index 400849b5a..9af0d54c3 100644 --- a/energy-integration/energy-factory-mock/wasm/src/lib.rs +++ b/energy-integration/energy-factory-mock/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 4 +// Endpoints: 5 // Async Callback (empty): 1 -// Total number of exported functions: 6 +// Total number of exported functions: 7 #![no_std] @@ -22,6 +22,7 @@ multiversx_sc_wasm_adapter::endpoints! { getEnergyAmountForUser => get_energy_amount_for_user getEnergyEntryForUser => get_energy_entry_for_user setUserEnergyAfterLockedTokenTransfer => set_user_energy_after_locked_token_transfer + transferUnlockedToken => transfer_unlocked_token ) } diff --git a/energy-integration/energy-update/wasm/Cargo.lock b/energy-integration/energy-update/wasm/Cargo.lock index e36d78e0b..68376f8b9 100644 --- a/energy-integration/energy-update/wasm/Cargo.lock +++ b/energy-integration/energy-update/wasm/Cargo.lock @@ -74,6 +74,13 @@ dependencies = [ "token_send", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "either" version = "1.13.0" @@ -468,6 +475,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/energy-integration/farm-boosted-yields/src/lib.rs b/energy-integration/farm-boosted-yields/src/lib.rs index 739907b2c..f3f088f96 100644 --- a/energy-integration/farm-boosted-yields/src/lib.rs +++ b/energy-integration/farm-boosted-yields/src/lib.rs @@ -30,6 +30,16 @@ impl SplitReward { } } +mod energy_factory_proxy_send_rew { + multiversx_sc::imports!(); + + #[multiversx_sc::proxy] + pub trait EnergyFactorySendRewProxy { + #[endpoint(transferUnlockedToken)] + fn transfer_unlocked_token(&self, dest: ManagedAddress, amount: BigUint); + } +} + #[multiversx_sc::module] pub trait FarmBoostedYieldsModule: boosted_yields_factors::BoostedYieldsFactorsModule @@ -44,31 +54,41 @@ pub trait FarmBoostedYieldsModule: + weekly_rewards_splitting::update_claim_progress_energy::UpdateClaimProgressEnergyModule + energy_query::EnergyQueryModule { + #[only_owner] #[endpoint(collectUndistributedBoostedRewards)] - fn collect_undistributed_boosted_rewards(&self) { - self.require_caller_has_admin_permissions(); - - let collect_rewards_offset = USER_MAX_CLAIM_WEEKS + 1usize; + fn collect_undistributed_boosted_rewards( + &self, + start_week: Week, + end_week: Week, + dest_address: ManagedAddress, + ) { + let collect_rewards_offset = USER_MAX_CLAIM_WEEKS + 1; let current_week = self.get_current_week(); require!( current_week > collect_rewards_offset, "Current week must be higher than the week offset" ); + require!(start_week <= end_week, "Invalid week numbers"); + require!( + end_week <= current_week - collect_rewards_offset, + "Invalid end week" + ); - let last_collect_week_mapper = self.last_undistributed_boosted_rewards_collect_week(); - let first_collect_week = last_collect_week_mapper.get() + 1; - let last_collect_week = current_week - collect_rewards_offset; - if first_collect_week > last_collect_week { - return; + let mut total_rewards = BigUint::zero(); + for week in start_week..=end_week { + let rewards_to_distribute = self.remaining_boosted_rewards_to_distribute(week).take(); + total_rewards += rewards_to_distribute; } - for week in first_collect_week..=last_collect_week { - let rewards_to_distribute = self.remaining_boosted_rewards_to_distribute(week).take(); - self.undistributed_boosted_rewards() - .update(|total_amount| *total_amount += rewards_to_distribute); + if total_rewards == 0 { + return; } - last_collect_week_mapper.set(last_collect_week); + let energy_factory = self.energy_factory_address().get(); + let _: () = self + .energy_factory_send_rew_proxy_obj(energy_factory) + .transfer_unlocked_token(dest_address, total_rewards) + .execute_on_dest_context(); } fn take_reward_slice(&self, full_reward: BigUint) -> SplitReward { @@ -150,12 +170,11 @@ pub trait FarmBoostedYieldsModule: #[storage_mapper("remainingBoostedRewardsToDistribute")] fn remaining_boosted_rewards_to_distribute(&self, week: Week) -> SingleValueMapper; - #[storage_mapper("lastUndistributedBoostedRewardsCollectWeek")] - fn last_undistributed_boosted_rewards_collect_week(&self) -> SingleValueMapper; - - #[view(getUndistributedBoostedRewards)] - #[storage_mapper("undistributedBoostedRewards")] - fn undistributed_boosted_rewards(&self) -> SingleValueMapper; + #[proxy] + fn energy_factory_send_rew_proxy_obj( + &self, + sc_address: ManagedAddress, + ) -> energy_factory_proxy_send_rew::Proxy; } pub struct FarmBoostedYieldsWrapper { diff --git a/energy-integration/fees-collector/wasm/Cargo.lock b/energy-integration/fees-collector/wasm/Cargo.lock index d962ee257..4e67ac4bf 100644 --- a/energy-integration/fees-collector/wasm/Cargo.lock +++ b/energy-integration/fees-collector/wasm/Cargo.lock @@ -45,6 +45,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "endian-type" version = "0.1.2" @@ -279,6 +286,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/energy-integration/governance-v2/wasm/Cargo.lock b/energy-integration/governance-v2/wasm/Cargo.lock index 5853e1198..1bc423aba 100644 --- a/energy-integration/governance-v2/wasm/Cargo.lock +++ b/energy-integration/governance-v2/wasm/Cargo.lock @@ -45,6 +45,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "endian-type" version = "0.1.2" @@ -299,6 +306,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/farm-staking/farm-staking-proxy/wasm/Cargo.lock b/farm-staking/farm-staking-proxy/wasm/Cargo.lock index 1f79bbe16..10299c347 100644 --- a/farm-staking/farm-staking-proxy/wasm/Cargo.lock +++ b/farm-staking/farm-staking-proxy/wasm/Cargo.lock @@ -74,6 +74,13 @@ dependencies = [ "token_send", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "either" version = "1.13.0" @@ -549,6 +556,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/farm-staking/farm-staking/wasm/Cargo.lock b/farm-staking/farm-staking/wasm/Cargo.lock index 465de9d30..95d2f2a15 100644 --- a/farm-staking/farm-staking/wasm/Cargo.lock +++ b/farm-staking/farm-staking/wasm/Cargo.lock @@ -74,6 +74,13 @@ dependencies = [ "token_send", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "either" version = "1.13.0" @@ -489,6 +496,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/farm-staking/farm-staking/wasm/src/lib.rs b/farm-staking/farm-staking/wasm/src/lib.rs index 9489b1d7e..7e687cca5 100644 --- a/farm-staking/farm-staking/wasm/src/lib.rs +++ b/farm-staking/farm-staking/wasm/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 69 +// Endpoints: 68 // Async Callback: 1 -// Total number of exported functions: 72 +// Total number of exported functions: 71 #![no_std] @@ -74,7 +74,6 @@ multiversx_sc_wasm_adapter::endpoints! { getAccumulatedRewardsForWeek => accumulated_rewards_for_week getFarmSupplyForWeek => farm_supply_for_week getRemainingBoostedRewardsToDistribute => remaining_boosted_rewards_to_distribute - getUndistributedBoostedRewards => undistributed_boosted_rewards setBoostedYieldsFactors => set_boosted_yields_factors getBoostedYieldsFactors => get_boosted_yields_factors getCurrentWeek => get_current_week diff --git a/legacy-contracts/factory-legacy/wasm/Cargo.lock b/legacy-contracts/factory-legacy/wasm/Cargo.lock index f9d78eed7..9a2de5fca 100644 --- a/legacy-contracts/factory-legacy/wasm/Cargo.lock +++ b/legacy-contracts/factory-legacy/wasm/Cargo.lock @@ -38,6 +38,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "endian-type" version = "0.1.2" @@ -249,6 +256,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/legacy-contracts/farm-staking-proxy-v13/wasm/Cargo.lock b/legacy-contracts/farm-staking-proxy-v13/wasm/Cargo.lock index ff771930d..64e6817aa 100644 --- a/legacy-contracts/farm-staking-proxy-v13/wasm/Cargo.lock +++ b/legacy-contracts/farm-staking-proxy-v13/wasm/Cargo.lock @@ -74,6 +74,13 @@ dependencies = [ "token_send", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "either" version = "1.13.0" @@ -509,6 +516,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/legacy-contracts/proxy-dex-legacy/wasm/Cargo.lock b/legacy-contracts/proxy-dex-legacy/wasm/Cargo.lock index d45260ddb..290b6eda0 100644 --- a/legacy-contracts/proxy-dex-legacy/wasm/Cargo.lock +++ b/legacy-contracts/proxy-dex-legacy/wasm/Cargo.lock @@ -45,6 +45,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "either" version = "1.13.0" @@ -360,6 +367,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/locked-asset/energy-factory/src/lib.rs b/locked-asset/energy-factory/src/lib.rs index a8cd96be4..6f076537c 100644 --- a/locked-asset/energy-factory/src/lib.rs +++ b/locked-asset/energy-factory/src/lib.rs @@ -14,6 +14,7 @@ pub mod penalty; pub mod token_merging; pub mod token_whitelist; pub mod unlock_with_penalty; +pub mod unlocked_token_transfer; pub mod unstake; pub mod virtual_lock; @@ -47,6 +48,7 @@ pub trait SimpleLockEnergy: + virtual_lock::VirtualLockModule + sc_whitelist_module::SCWhitelistModule + locked_token_transfer::LockedTokenTransferModule + + unlocked_token_transfer::UnlockedTokenTransferModule + legacy_token_decode_module::LegacyTokenDecodeModule { /// Args: @@ -82,11 +84,10 @@ pub trait SimpleLockEnergy: self.min_migrated_token_locked_period() .set(min_migrated_token_locked_period); - self.base_asset_token_id() - .set_if_empty(&base_asset_token_id); - self.legacy_locked_token_id().set_if_empty(&legacy_token_id); + self.base_asset_token_id().set(&base_asset_token_id); + self.legacy_locked_token_id().set(&legacy_token_id); self.old_locked_asset_factory_address() - .set_if_empty(&old_locked_asset_factory_address); + .set(&old_locked_asset_factory_address); self.set_paused(true); } diff --git a/locked-asset/energy-factory/src/lock_options.rs b/locked-asset/energy-factory/src/lock_options.rs index 2237d2f92..107ea971c 100644 --- a/locked-asset/energy-factory/src/lock_options.rs +++ b/locked-asset/energy-factory/src/lock_options.rs @@ -1,17 +1,17 @@ multiversx_sc::imports!(); multiversx_sc::derive_imports!(); -use common_structs::Epoch; +use common_structs::{Epoch, Percent}; use unwrappable::Unwrappable; pub const EPOCHS_PER_MONTH: Epoch = 30; pub const EPOCHS_PER_YEAR: Epoch = 12 * EPOCHS_PER_MONTH; -pub const MAX_PENALTY_PERCENTAGE: u64 = 10_000; // 100% +pub const MAX_PENALTY_PERCENTAGE: Percent = 10_000; // 100% #[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode, Clone, Copy, Default)] pub struct LockOption { pub lock_epochs: Epoch, - pub penalty_start_percentage: u64, + pub penalty_start_percentage: Percent, } pub const MAX_LOCK_OPTIONS: usize = 10; diff --git a/locked-asset/energy-factory/src/locked_token_transfer.rs b/locked-asset/energy-factory/src/locked_token_transfer.rs index 8e9397e48..d37d6f6cd 100644 --- a/locked-asset/energy-factory/src/locked_token_transfer.rs +++ b/locked-asset/energy-factory/src/locked_token_transfer.rs @@ -15,6 +15,7 @@ pub trait LockedTokenTransferModule: let mapper = self.token_transfer_whitelist(); for sc_addr in sc_addresses { self.require_sc_address(&sc_addr); + mapper.add(&sc_addr); } } @@ -38,6 +39,7 @@ pub trait LockedTokenTransferModule: energy: Energy, ) { self.require_not_paused(); + let caller = self.blockchain().get_caller(); self.token_transfer_whitelist().require_whitelisted(&caller); diff --git a/locked-asset/energy-factory/src/unlock_with_penalty.rs b/locked-asset/energy-factory/src/unlock_with_penalty.rs index 102565ed5..d14ff3e79 100644 --- a/locked-asset/energy-factory/src/unlock_with_penalty.rs +++ b/locked-asset/energy-factory/src/unlock_with_penalty.rs @@ -10,7 +10,7 @@ use crate::{energy::Energy, lock_options::MAX_PENALTY_PERCENTAGE}; pub static TOKEN_CAN_BE_UNLOCKED_ALREADY_ERR_MSG: &[u8] = b"Token can be unlocked already"; pub struct LockReduceResult { - pub new_lock_epochs: u64, + pub new_lock_epochs: Epoch, pub unlocked_tokens: EgldOrEsdtTokenPayment, pub energy: Energy, } @@ -39,6 +39,7 @@ pub trait UnlockWithPenaltyModule: #[endpoint(unlockEarly)] fn unlock_early(&self) { self.require_not_paused(); + let caller = self.blockchain().get_caller(); let payment = self.call_value().single_esdt(); let reduce_result = self.reduce_lock_period_common(&caller, payment.clone(), None); diff --git a/locked-asset/energy-factory/src/unlocked_token_transfer.rs b/locked-asset/energy-factory/src/unlocked_token_transfer.rs new file mode 100644 index 000000000..c2269b0cd --- /dev/null +++ b/locked-asset/energy-factory/src/unlocked_token_transfer.rs @@ -0,0 +1,55 @@ +multiversx_sc::imports!(); + +#[multiversx_sc::module] +pub trait UnlockedTokenTransferModule: + utils::UtilsModule + + multiversx_sc_modules::pause::PauseModule + + crate::token_whitelist::TokenWhitelistModule +{ + #[only_owner] + #[endpoint(addToUnlockedTokenTransferWhitelist)] + fn add_to_unlocked_token_transfer_whitelist( + &self, + sc_addresses: MultiValueEncoded, + ) { + let mapper = self.unlocked_token_transfer_whitelist(); + for sc_addr in sc_addresses { + self.require_sc_address(&sc_addr); + + mapper.add(&sc_addr); + } + } + + #[only_owner] + #[endpoint(removeFromUnlockedTokenTransferWhitelist)] + fn remove_from_unlocked_token_transfer_whitelist( + &self, + sc_addresses: MultiValueEncoded, + ) { + let mapper = self.unlocked_token_transfer_whitelist(); + for sc_addr in sc_addresses { + mapper.remove(&sc_addr); + } + } + + #[endpoint(transferUnlockedToken)] + fn transfer_unlocked_token(&self, dest: ManagedAddress, amount: BigUint) { + self.require_not_paused(); + require!(amount != 0, "Invalid amount"); + + let caller = self.blockchain().get_caller(); + require!( + self.unlocked_token_transfer_whitelist().contains(&caller), + "May not call this endpoint" + ); + + let base_asset_token_id = self.base_asset_token_id().get(); + self.send() + .esdt_local_mint(&base_asset_token_id, 0, &amount); + self.send() + .direct_esdt(&dest, &base_asset_token_id, 0, &amount); + } + + #[storage_mapper("ulkTokenTransfWhitelist")] + fn unlocked_token_transfer_whitelist(&self) -> WhitelistMapper; +} diff --git a/locked-asset/energy-factory/wasm/Cargo.lock b/locked-asset/energy-factory/wasm/Cargo.lock index 93a271e50..c539f6b4b 100644 --- a/locked-asset/energy-factory/wasm/Cargo.lock +++ b/locked-asset/energy-factory/wasm/Cargo.lock @@ -38,6 +38,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "endian-type" version = "0.1.2" @@ -237,6 +244,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/locked-asset/energy-factory/wasm/src/lib.rs b/locked-asset/energy-factory/wasm/src/lib.rs index 181f50f1a..feff62338 100644 --- a/locked-asset/energy-factory/wasm/src/lib.rs +++ b/locked-asset/energy-factory/wasm/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 34 +// Endpoints: 37 // Async Callback: 1 -// Total number of exported functions: 37 +// Total number of exported functions: 40 #![no_std] @@ -54,6 +54,9 @@ multiversx_sc_wasm_adapter::endpoints! { addToTokenTransferWhitelist => add_to_token_transfer_whitelist removeFromTokenTransferWhitelist => remove_from_token_transfer_whitelist setUserEnergyAfterLockedTokenTransfer => set_user_energy_after_locked_token_transfer + addToUnlockedTokenTransferWhitelist => add_to_unlocked_token_transfer_whitelist + removeFromUnlockedTokenTransferWhitelist => remove_from_unlocked_token_transfer_whitelist + transferUnlockedToken => transfer_unlocked_token ) } diff --git a/locked-asset/lkmex-transfer/wasm/Cargo.lock b/locked-asset/lkmex-transfer/wasm/Cargo.lock index d269860db..e10425062 100644 --- a/locked-asset/lkmex-transfer/wasm/Cargo.lock +++ b/locked-asset/lkmex-transfer/wasm/Cargo.lock @@ -38,6 +38,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "endian-type" version = "0.1.2" @@ -268,6 +275,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/locked-asset/locked-token-wrapper/wasm/Cargo.lock b/locked-asset/locked-token-wrapper/wasm/Cargo.lock index 98b8ba9bc..6ee87c898 100644 --- a/locked-asset/locked-token-wrapper/wasm/Cargo.lock +++ b/locked-asset/locked-token-wrapper/wasm/Cargo.lock @@ -38,6 +38,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "endian-type" version = "0.1.2" @@ -292,6 +299,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/locked-asset/proxy_dex/Cargo.toml b/locked-asset/proxy_dex/Cargo.toml index e5023a79d..e1d220f10 100644 --- a/locked-asset/proxy_dex/Cargo.toml +++ b/locked-asset/proxy_dex/Cargo.toml @@ -63,6 +63,9 @@ path = "../simple-lock" [dependencies.sc_whitelist_module] path = "../../common/modules/sc_whitelist_module" +[dependencies.disable-add-liq] +path = "../../common/modules/disable-add-liq" + [dev-dependencies] num-bigint = "0.4.2" num-traits = "0.2" diff --git a/locked-asset/proxy_dex/src/lib.rs b/locked-asset/proxy_dex/src/lib.rs index 047b087e8..7ec982ecc 100644 --- a/locked-asset/proxy_dex/src/lib.rs +++ b/locked-asset/proxy_dex/src/lib.rs @@ -36,6 +36,7 @@ pub trait ProxyDexImpl: + utils::UtilsModule + legacy_token_decode_module::LegacyTokenDecodeModule + sc_whitelist_module::SCWhitelistModule + + disable_add_liq::DisableAddLiqModule { #[init] fn init( @@ -48,12 +49,9 @@ pub trait ProxyDexImpl: self.require_sc_address(&old_factory_address); self.require_sc_address(&energy_factory_address); - self.old_locked_token_id() - .set_if_empty(&old_locked_token_id); - self.old_factory_address() - .set_if_empty(&old_factory_address); - self.energy_factory_address() - .set_if_empty(&energy_factory_address); + self.old_locked_token_id().set(&old_locked_token_id); + self.old_factory_address().set(&old_factory_address); + self.energy_factory_address().set(&energy_factory_address); } #[upgrade] diff --git a/locked-asset/proxy_dex/src/pair_interactions.rs b/locked-asset/proxy_dex/src/pair_interactions.rs index 7e959a4e1..f04d0d6ac 100644 --- a/locked-asset/proxy_dex/src/pair_interactions.rs +++ b/locked-asset/proxy_dex/src/pair_interactions.rs @@ -6,6 +6,24 @@ use pair::pair_actions::{ remove_liq::ProxyTrait as _, }; +pub struct CallRemoveLiqArgs { + pub pair_address: ManagedAddress, + pub lp_token_id: TokenIdentifier, + pub lp_token_amount: BigUint, + pub first_token_amount_min: BigUint, + pub second_token_amount_min: BigUint, +} + +pub struct CallAddLiqArgs { + pub pair_address: ManagedAddress, + pub first_token_id: TokenIdentifier, + pub first_token_amount_desired: BigUint, + pub first_token_amount_min: BigUint, + pub second_token_id: TokenIdentifier, + pub second_token_amount_desired: BigUint, + pub second_token_amount_min: BigUint, +} + pub struct AddLiquidityResultWrapper { pub lp_tokens_received: EsdtTokenPayment, pub first_token_leftover: EsdtTokenPayment, @@ -21,32 +39,33 @@ pub struct RemoveLiqudityResultWrapper { pub trait PairInteractionsModule { fn call_add_liquidity( &self, - pair_address: ManagedAddress, - first_token_id: TokenIdentifier, - first_token_amount_desired: BigUint, - first_token_amount_min: BigUint, - second_token_id: TokenIdentifier, - second_token_amount_desired: BigUint, - second_token_amount_min: BigUint, + args: CallAddLiqArgs, ) -> AddLiquidityResultWrapper { - let first_payment = - EsdtTokenPayment::new(first_token_id, 0, first_token_amount_desired.clone()); - let second_payment = - EsdtTokenPayment::new(second_token_id, 0, second_token_amount_desired.clone()); + let first_payment = EsdtTokenPayment::new( + args.first_token_id, + 0, + args.first_token_amount_desired.clone(), + ); + let second_payment = EsdtTokenPayment::new( + args.second_token_id, + 0, + args.second_token_amount_desired.clone(), + ); let mut all_token_payments = ManagedVec::new(); all_token_payments.push(first_payment); all_token_payments.push(second_payment); let raw_result: AddLiquidityResultType = self - .pair_contract_proxy(pair_address) - .add_liquidity(first_token_amount_min, second_token_amount_min) + .pair_contract_proxy(args.pair_address) + .add_liquidity(args.first_token_amount_min, args.second_token_amount_min) .with_multi_token_transfer(all_token_payments) .execute_on_dest_context(); let (lp_tokens_received, first_tokens_used, second_tokens_used) = raw_result.into_tuple(); - let first_token_leftover_amount = &first_token_amount_desired - &first_tokens_used.amount; + let first_token_leftover_amount = + &args.first_token_amount_desired - &first_tokens_used.amount; let second_token_leftover_amount = - &second_token_amount_desired - &second_tokens_used.amount; + &args.second_token_amount_desired - &second_tokens_used.amount; let first_token_leftover = EsdtTokenPayment::new( first_tokens_used.token_identifier, @@ -68,16 +87,12 @@ pub trait PairInteractionsModule { fn call_remove_liquidity( &self, - pair_address: ManagedAddress, - lp_token_id: TokenIdentifier, - lp_token_amount: BigUint, - first_token_amount_min: BigUint, - second_token_amount_min: BigUint, + args: CallRemoveLiqArgs, ) -> RemoveLiqudityResultWrapper { let raw_result: RemoveLiquidityResultType = self - .pair_contract_proxy(pair_address) - .remove_liquidity(first_token_amount_min, second_token_amount_min) - .with_esdt_transfer((lp_token_id, 0, lp_token_amount)) + .pair_contract_proxy(args.pair_address) + .remove_liquidity(args.first_token_amount_min, args.second_token_amount_min) + .with_esdt_transfer((args.lp_token_id, 0, args.lp_token_amount)) .execute_on_dest_context(); let (first_token_received, second_token_received) = raw_result.into_tuple(); diff --git a/locked-asset/proxy_dex/src/proxy_farm.rs b/locked-asset/proxy_dex/src/proxy_farm.rs index b90f00f69..9532463d3 100644 --- a/locked-asset/proxy_dex/src/proxy_farm.rs +++ b/locked-asset/proxy_dex/src/proxy_farm.rs @@ -39,6 +39,7 @@ pub trait ProxyFarmModule: + utils::UtilsModule + legacy_token_decode_module::LegacyTokenDecodeModule + sc_whitelist_module::SCWhitelistModule + + disable_add_liq::DisableAddLiqModule { #[payable("*")] #[endpoint(enterFarmProxy)] diff --git a/locked-asset/proxy_dex/src/proxy_pair.rs b/locked-asset/proxy_dex/src/proxy_pair.rs index 79542f06a..e17397d9d 100644 --- a/locked-asset/proxy_dex/src/proxy_pair.rs +++ b/locked-asset/proxy_dex/src/proxy_pair.rs @@ -1,11 +1,12 @@ #![allow(clippy::too_many_arguments)] -#![allow(clippy::comparison_chain)] -#![allow(clippy::vec_init_then_push)] multiversx_sc::imports!(); multiversx_sc::derive_imports!(); -use crate::wrapped_lp_attributes::{WrappedLpToken, WrappedLpTokenAttributes}; +use crate::{ + pair_interactions::{CallAddLiqArgs, CallRemoveLiqArgs}, + wrapped_lp_attributes::{WrappedLpToken, WrappedLpTokenAttributes}, +}; use common_structs::Epoch; use fixed_supply_token::FixedSupplyToken; @@ -22,6 +23,7 @@ pub trait ProxyPairModule: + token_send::TokenSendModule + utils::UtilsModule + legacy_token_decode_module::LegacyTokenDecodeModule + + disable_add_liq::DisableAddLiqModule { #[payable("*")] #[endpoint(addLiquidityProxy)] @@ -33,6 +35,7 @@ pub trait ProxyPairModule: ) -> MultiValueEncoded { self.require_is_intermediated_pair(&pair_address); self.require_wrapped_lp_token_id_not_empty(); + self.require_add_liq_enabled(); let caller = self.blockchain().get_caller(); let mut payments = self.get_non_empty_payments(); @@ -49,15 +52,15 @@ pub trait ProxyPairModule: self.get_underlying_token(first_payment.token_identifier.clone()); let second_unlocked_token_id = self.get_underlying_token(second_payment.token_identifier.clone()); - let add_liq_result = self.call_add_liquidity( - pair_address.clone(), - first_unlocked_token_id, - first_payment.amount.clone(), + let add_liq_result = self.call_add_liquidity(CallAddLiqArgs { + pair_address: pair_address.clone(), + first_token_id: first_unlocked_token_id, + first_token_amount_desired: first_payment.amount.clone(), first_token_amount_min, - second_unlocked_token_id, - second_payment.amount.clone(), + second_token_id: second_unlocked_token_id, + second_token_amount_desired: second_payment.amount.clone(), second_token_amount_min, - ); + }); let mut locked_token_used = input_token_refs.locked_token_ref.clone(); locked_token_used.amount = if input_token_refs.locked_token_ref.token_identifier @@ -170,13 +173,13 @@ pub trait ProxyPairModule: let attributes: WrappedLpTokenAttributes = self.get_attributes_as_part_of_fixed_supply(&input_payment, &wrapped_lp_mapper); - let remove_liq_result = self.call_remove_liquidity( - pair_address.clone(), - attributes.lp_token_id.clone(), - attributes.lp_token_amount.clone(), + let remove_liq_result = self.call_remove_liquidity(CallRemoveLiqArgs { + pair_address: pair_address.clone(), + lp_token_id: attributes.lp_token_id.clone(), + lp_token_amount: attributes.lp_token_amount.clone(), first_token_amount_min, second_token_amount_min, - ); + }); let received_token_refs = self.require_exactly_one_base_asset( &remove_liq_result.first_token_received, &remove_liq_result.second_token_received, diff --git a/locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs b/locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs index 73dd9d9c9..90db558e1 100644 --- a/locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs +++ b/locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs @@ -3,6 +3,7 @@ use common_structs::{LockedAssetTokenAttributesEx, UnlockMilestoneEx, UnlockScheduleEx}; use config::ConfigModule; +use disable_add_liq::{DisableAddLiqModule, ADD_LIQ_ENABLED}; use energy_factory::{locked_token_transfer::LockedTokenTransferModule, SimpleLockEnergy}; use energy_query::EnergyQueryModule; use farm_boosted_yields::boosted_yields_factors::BoostedYieldsFactorsModule; @@ -455,6 +456,8 @@ where managed_address!(simple_lock_addr), ); + sc.add_liq_enabled().set(ADD_LIQ_ENABLED); + sc.wrapped_lp_token() .set_token_id(managed_token_id!(WRAPPED_LP_TOKEN_ID)); sc.wrapped_farm_token() 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"); +} diff --git a/locked-asset/proxy_dex/wasm/Cargo.lock b/locked-asset/proxy_dex/wasm/Cargo.lock index 3330a21d1..e5731cf54 100644 --- a/locked-asset/proxy_dex/wasm/Cargo.lock +++ b/locked-asset/proxy_dex/wasm/Cargo.lock @@ -74,6 +74,13 @@ dependencies = [ "token_send", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "either" version = "1.13.0" @@ -438,6 +445,7 @@ name = "proxy_dex" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "energy-factory", "energy-query", "farm", @@ -512,6 +520,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/locked-asset/proxy_dex/wasm/src/lib.rs b/locked-asset/proxy_dex/wasm/src/lib.rs index c04d97990..575932340 100644 --- a/locked-asset/proxy_dex/wasm/src/lib.rs +++ b/locked-asset/proxy_dex/wasm/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 30 +// Endpoints: 33 // Async Callback: 1 -// Total number of exported functions: 33 +// Total number of exported functions: 36 #![no_std] @@ -50,6 +50,9 @@ multiversx_sc_wasm_adapter::endpoints! { addSCAddressToWhitelist => add_sc_address_to_whitelist removeSCAddressFromWhitelist => remove_sc_address_from_whitelist isSCAddressWhitelisted => is_sc_address_whitelisted + enableAddLiq => enable_add_liq + disableAddLiq => disable_add_liq + isAddLiqEnabled => add_liq_enabled ) } diff --git a/locked-asset/simple-lock/Cargo.toml b/locked-asset/simple-lock/Cargo.toml index b867f2aa3..6d9409e17 100644 --- a/locked-asset/simple-lock/Cargo.toml +++ b/locked-asset/simple-lock/Cargo.toml @@ -18,6 +18,9 @@ version = "=0.53.2" [dependencies.common_structs] path = "../../common/common_structs" +[dependencies.disable-add-liq] +path = "../../common/modules/disable-add-liq" + [dev-dependencies] num-bigint = "0.4.2" num-traits = "0.2" diff --git a/locked-asset/simple-lock/src/lib.rs b/locked-asset/simple-lock/src/lib.rs index fb59b792c..a4fb10a5e 100644 --- a/locked-asset/simple-lock/src/lib.rs +++ b/locked-asset/simple-lock/src/lib.rs @@ -21,6 +21,7 @@ pub trait SimpleLock: + lp_interactions::LpInteractionsModule + farm_interactions::FarmInteractionsModule + token_attributes::TokenAttributesModule + + disable_add_liq::DisableAddLiqModule { #[init] fn init(&self) {} diff --git a/locked-asset/simple-lock/src/lp_interactions.rs b/locked-asset/simple-lock/src/lp_interactions.rs index 4229cca53..6a6fa1968 100644 --- a/locked-asset/simple-lock/src/lp_interactions.rs +++ b/locked-asset/simple-lock/src/lp_interactions.rs @@ -23,6 +23,7 @@ pub struct RemoveLiquidityResultWrapper { // This avoids circular dependency mod lp_proxy { multiversx_sc::imports!(); + use super::{AddLiquidityResultType, RemoveLiquidityResultType}; #[multiversx_sc::proxy] diff --git a/locked-asset/simple-lock/src/proxy_farm.rs b/locked-asset/simple-lock/src/proxy_farm.rs index 9defead0c..4f10bc3e9 100644 --- a/locked-asset/simple-lock/src/proxy_farm.rs +++ b/locked-asset/simple-lock/src/proxy_farm.rs @@ -34,6 +34,7 @@ pub trait ProxyFarmModule: + crate::proxy_lp::ProxyLpModule + crate::token_attributes::TokenAttributesModule + multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule + + disable_add_liq::DisableAddLiqModule { #[only_owner] #[payable("EGLD")] diff --git a/locked-asset/simple-lock/src/proxy_lp.rs b/locked-asset/simple-lock/src/proxy_lp.rs index ed16b65d1..86461ccfb 100644 --- a/locked-asset/simple-lock/src/proxy_lp.rs +++ b/locked-asset/simple-lock/src/proxy_lp.rs @@ -24,6 +24,7 @@ pub trait ProxyLpModule: + crate::lp_interactions::LpInteractionsModule + crate::token_attributes::TokenAttributesModule + multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule + + disable_add_liq::DisableAddLiqModule { #[only_owner] #[payable("EGLD")] @@ -136,6 +137,8 @@ pub trait ProxyLpModule: first_token_amount_min: BigUint, second_token_amount_min: BigUint, ) -> AddLiquidityThroughProxyResultType { + self.require_add_liq_enabled(); + let [first_payment, second_payment] = self.call_value().multi_esdt(); let (mut first_payment_unlocked_wrapper, mut second_payment_unlocked_wrapper) = self.unlock_lp_payments(first_payment, second_payment); diff --git a/locked-asset/simple-lock/wasm/Cargo.lock b/locked-asset/simple-lock/wasm/Cargo.lock index 8e5ac6f77..e24df1557 100644 --- a/locked-asset/simple-lock/wasm/Cargo.lock +++ b/locked-asset/simple-lock/wasm/Cargo.lock @@ -31,6 +31,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "endian-type" version = "0.1.2" @@ -189,6 +196,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/locked-asset/simple-lock/wasm/src/lib.rs b/locked-asset/simple-lock/wasm/src/lib.rs index eeafb6cb1..09c1ec3ad 100644 --- a/locked-asset/simple-lock/wasm/src/lib.rs +++ b/locked-asset/simple-lock/wasm/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 19 +// Endpoints: 22 // Async Callback: 1 -// Total number of exported functions: 22 +// Total number of exported functions: 25 #![no_std] @@ -39,6 +39,9 @@ multiversx_sc_wasm_adapter::endpoints! { farmClaimRewardsLockedToken => farm_claim_rewards_locked_token getKnownFarms => known_farms getFarmProxyTokenId => farm_proxy_token + enableAddLiq => enable_add_liq + disableAddLiq => disable_add_liq + isAddLiqEnabled => add_liq_enabled ) } diff --git a/locked-asset/token-unstake/src/events.rs b/locked-asset/token-unstake/src/events.rs index 7c4bdccc0..7779f4ad6 100644 --- a/locked-asset/token-unstake/src/events.rs +++ b/locked-asset/token-unstake/src/events.rs @@ -1,3 +1,5 @@ +use common_structs::Epoch; + use crate::tokens_per_user::UnstakePair; multiversx_sc::imports!(); @@ -24,7 +26,7 @@ pub trait EventsModule { &self, #[indexed] caller: &ManagedAddress, #[indexed] block: u64, - #[indexed] epoch: u64, + #[indexed] epoch: Epoch, #[indexed] timestamp: u64, data: ManagedVec>, ); diff --git a/locked-asset/token-unstake/src/fees_handler.rs b/locked-asset/token-unstake/src/fees_handler.rs index 803ef5345..1de93362f 100644 --- a/locked-asset/token-unstake/src/fees_handler.rs +++ b/locked-asset/token-unstake/src/fees_handler.rs @@ -1,6 +1,8 @@ multiversx_sc::imports!(); -pub const MAX_PENALTY_PERCENTAGE: u64 = 10_000; +pub const MAX_PENALTY_PERCENTAGE: Percent = 10_000; + +use common_structs::Percent; use crate::{events, tokens_per_user::UnstakePair}; @@ -22,6 +24,14 @@ pub trait FeesHandlerModule: + utils::UtilsModule + events::EventsModule { + #[only_owner] + #[endpoint(setFeesBurnPercent)] + fn set_fees_burn_percent(&self, percent: Percent) { + require!(percent <= MAX_PENALTY_PERCENTAGE, "Invalid percent"); + + self.fees_burn_percentage().set(percent); + } + #[payable("*")] #[endpoint(depositUserTokens)] fn deposit_user_tokens(&self, user: ManagedAddress) { @@ -103,7 +113,7 @@ pub trait FeesHandlerModule: #[view(getFeesBurnPercentage)] #[storage_mapper("feesBurnPercentage")] - fn fees_burn_percentage(&self) -> SingleValueMapper; + fn fees_burn_percentage(&self) -> SingleValueMapper; #[view(getFeesCollectorAddress)] #[storage_mapper("feesCollectorAddress")] diff --git a/locked-asset/token-unstake/src/lib.rs b/locked-asset/token-unstake/src/lib.rs index 985e91be9..f51713dbb 100644 --- a/locked-asset/token-unstake/src/lib.rs +++ b/locked-asset/token-unstake/src/lib.rs @@ -8,7 +8,7 @@ pub mod fees_handler; pub mod tokens_per_user; pub mod unbond_tokens; -use crate::fees_handler::MAX_PENALTY_PERCENTAGE; +use common_structs::{Epoch, Percent}; #[multiversx_sc::contract] pub trait TokenUnstakeModule: @@ -24,22 +24,19 @@ pub trait TokenUnstakeModule: #[init] fn init( &self, - unbond_epochs: u64, + unbond_epochs: Epoch, energy_factory_address: ManagedAddress, - fees_burn_percentage: u64, + fees_burn_percentage: Percent, fees_collector_address: ManagedAddress, ) { self.require_sc_address(&energy_factory_address); self.require_sc_address(&fees_collector_address); - require!( - fees_burn_percentage <= MAX_PENALTY_PERCENTAGE, - "Invalid percentage" - ); + + self.set_fees_burn_percent(fees_burn_percentage); self.unbond_epochs().set(unbond_epochs); - self.energy_factory_address().set(&energy_factory_address); - self.fees_collector_address().set(&fees_collector_address); - self.fees_burn_percentage().set(fees_burn_percentage); + self.energy_factory_address().set(energy_factory_address); + self.fees_collector_address().set(fees_collector_address); } #[upgrade] diff --git a/locked-asset/token-unstake/src/tokens_per_user.rs b/locked-asset/token-unstake/src/tokens_per_user.rs index 81eb1a8ab..34961da37 100644 --- a/locked-asset/token-unstake/src/tokens_per_user.rs +++ b/locked-asset/token-unstake/src/tokens_per_user.rs @@ -1,3 +1,5 @@ +use common_structs::Epoch; + multiversx_sc::imports!(); multiversx_sc::derive_imports!(); @@ -13,7 +15,7 @@ multiversx_sc::derive_imports!(); Debug, )] pub struct UnstakePair { - pub unlock_epoch: u64, + pub unlock_epoch: Epoch, pub locked_tokens: EsdtTokenPayment, pub unlocked_tokens: EsdtTokenPayment, } @@ -22,7 +24,7 @@ pub struct UnstakePair { pub trait TokensPerUserModule { #[view(getUnbondEpochs)] #[storage_mapper("unbondEpochs")] - fn unbond_epochs(&self) -> SingleValueMapper; + fn unbond_epochs(&self) -> SingleValueMapper; #[view(getUnlockedTokensForUser)] #[storage_mapper("unlockedTokensForUser")] diff --git a/locked-asset/token-unstake/src/unbond_tokens.rs b/locked-asset/token-unstake/src/unbond_tokens.rs index 20aeb1843..021d596e7 100644 --- a/locked-asset/token-unstake/src/unbond_tokens.rs +++ b/locked-asset/token-unstake/src/unbond_tokens.rs @@ -1,4 +1,6 @@ -use crate::events; +use common_structs::PaymentsVec; + +use crate::{events, tokens_per_user::UnstakePair}; multiversx_sc::imports!(); @@ -16,7 +18,8 @@ pub trait UnbondTokensModule: let current_epoch = self.blockchain().get_block_epoch(); let mut output_payments = ManagedVec::new(); let mut penalty_tokens = ManagedVec::::new(); - self.unlocked_tokens_for_user(&caller) + let new_unlocked_tokens = self + .unlocked_tokens_for_user(&caller) .update(|user_entries| { while !user_entries.is_empty() { let entry = user_entries.get(0); @@ -24,31 +27,13 @@ pub trait UnbondTokensModule: break; } - let locked_tokens = entry.locked_tokens; - let unlocked_tokens = entry.unlocked_tokens; - - // we only burn the tokens that are not unlocked - // the rest are sent back as penalty - let locked_tokens_burn_amount = unlocked_tokens.amount.clone(); - self.send().esdt_local_burn( - &locked_tokens.token_identifier, - locked_tokens.token_nonce, - &locked_tokens_burn_amount, - ); - - let penalty_amount = &locked_tokens.amount - &unlocked_tokens.amount; - if penalty_amount > 0 { - let penalty = EsdtTokenPayment::new( - locked_tokens.token_identifier, - locked_tokens.token_nonce, - penalty_amount, - ); - penalty_tokens.push(penalty); - } + self.handle_single_unbond_entry(&entry, &mut penalty_tokens); - output_payments.push(unlocked_tokens); + output_payments.push(entry.unlocked_tokens); user_entries.remove(0); } + + (*user_entries).clone() }); require!(!output_payments.is_empty(), "Nothing to unbond"); @@ -58,10 +43,38 @@ pub trait UnbondTokensModule: } self.send().direct_multi(&caller, &output_payments); - - let new_unlocked_tokens = self.unlocked_tokens_for_user(&caller).get(); self.emit_unlocked_tokens_event(&caller, new_unlocked_tokens); output_payments.into() } + + fn handle_single_unbond_entry( + &self, + entry: &UnstakePair, + penalty_tokens: &mut PaymentsVec, + ) { + let locked_tokens = &entry.locked_tokens; + let unlocked_tokens = &entry.unlocked_tokens; + + // we only burn the tokens that are not unlocked + // the rest are sent back as penalty + let locked_tokens_burn_amount = &unlocked_tokens.amount; + self.send().esdt_local_burn( + &locked_tokens.token_identifier, + locked_tokens.token_nonce, + locked_tokens_burn_amount, + ); + + let penalty_amount = &locked_tokens.amount - &unlocked_tokens.amount; + if penalty_amount == 0 { + return; + } + + let penalty = EsdtTokenPayment::new( + locked_tokens.token_identifier.clone(), + locked_tokens.token_nonce, + penalty_amount, + ); + penalty_tokens.push(penalty); + } } diff --git a/locked-asset/token-unstake/wasm/Cargo.lock b/locked-asset/token-unstake/wasm/Cargo.lock index 788eb7478..486861f02 100644 --- a/locked-asset/token-unstake/wasm/Cargo.lock +++ b/locked-asset/token-unstake/wasm/Cargo.lock @@ -45,6 +45,13 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "endian-type" version = "0.1.2" @@ -244,6 +251,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/locked-asset/token-unstake/wasm/src/lib.rs b/locked-asset/token-unstake/wasm/src/lib.rs index ea93ce423..4745ad92f 100644 --- a/locked-asset/token-unstake/wasm/src/lib.rs +++ b/locked-asset/token-unstake/wasm/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 10 +// Endpoints: 11 // Async Callback (empty): 1 -// Total number of exported functions: 13 +// Total number of exported functions: 14 #![no_std] @@ -24,6 +24,7 @@ multiversx_sc_wasm_adapter::endpoints! { getUnlockedTokensForUser => unlocked_tokens_for_user claimUnlockedTokens => claim_unlocked_tokens cancelUnbond => cancel_unbond + setFeesBurnPercent => set_fees_burn_percent depositUserTokens => deposit_user_tokens depositFees => deposit_fees getFeesBurnPercentage => fees_burn_percentage