From fb9f86f9b511f1bc20cefe729a140a7afa7a0616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Sat, 10 Dec 2022 18:08:10 +0200 Subject: [PATCH 01/12] Update owner or admin endpoint --- .../permissions_module/src/permissions_module.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/common/modules/permissions_module/src/permissions_module.rs b/common/modules/permissions_module/src/permissions_module.rs index 104d4608f..0a37b4ec8 100644 --- a/common/modules/permissions_module/src/permissions_module.rs +++ b/common/modules/permissions_module/src/permissions_module.rs @@ -22,6 +22,16 @@ pub trait PermissionsModule { 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(); + + self.permissions(previous_owner).clear(); + self.permissions(caller).set(previous_owner_permissions); + } + fn set_permissions(&self, address: ManagedAddress, permissions: Permissions) { self.permissions(address).set(permissions); } From bf3920436f7659d0e80ba81e06da1e238a22b30c Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Sat, 10 Dec 2022 22:38:15 +0200 Subject: [PATCH 02/12] impl --- common/common_structs/src/alias_types.rs | 3 +- .../common_structs/src/locked_token_types.rs | 73 ++++++++++ locked-asset/energy-factory/src/migration.rs | 16 ++- .../tests/energy_factory_setup/mod.rs | 1 + .../energy-factory/tests/old_tokens_test.rs | 132 +++++++++++++++++- 5 files changed, 216 insertions(+), 9 deletions(-) diff --git a/common/common_structs/src/alias_types.rs b/common/common_structs/src/alias_types.rs index 33ec155ca..e0538f13a 100644 --- a/common/common_structs/src/alias_types.rs +++ b/common/common_structs/src/alias_types.rs @@ -1,6 +1,6 @@ elrond_wasm::imports!(); -use crate::{LockedAssetTokenAttributesEx, UnlockSchedule}; +use crate::{InitialLockedAssetTokenAttributesEx, LockedAssetTokenAttributesEx, UnlockSchedule}; pub type Nonce = u64; pub type Epoch = u64; @@ -9,3 +9,4 @@ pub type Percent = u64; pub type PaymentsVec = ManagedVec>; pub type UnlockPeriod = UnlockSchedule; pub type OldLockedTokenAttributes = LockedAssetTokenAttributesEx; +pub type InitialOldLockedTokenAttributes = InitialLockedAssetTokenAttributesEx; diff --git a/common/common_structs/src/locked_token_types.rs b/common/common_structs/src/locked_token_types.rs index 0cf58755f..e23139d1a 100644 --- a/common/common_structs/src/locked_token_types.rs +++ b/common/common_structs/src/locked_token_types.rs @@ -6,6 +6,9 @@ use crate::{Epoch, EpochAmountPair}; pub const MAX_MILESTONES_IN_SCHEDULE: usize = 64; pub const PERCENTAGE_TOTAL_EX: u64 = 100_000u64; +pub const LOCKED_TOKEN_ACTIVATION_NONCE: u64 = 2_286_815u64; +pub const UPDATE_PERCENTAGE_CONST: u64 = 1_000u64; + #[derive( ManagedVecItem, TopEncode, @@ -40,6 +43,23 @@ pub struct UnlockMilestoneEx { pub unlock_percent: u64, } +#[derive( + ManagedVecItem, + TopEncode, + TopDecode, + PartialEq, + TypeAbi, + NestedEncode, + NestedDecode, + Clone, + Copy, + Debug, +)] +pub struct InitialUnlockMilestoneEx { + pub unlock_epoch: u64, + pub unlock_percent: u8, +} + #[derive( TopEncode, TopDecode, NestedEncode, NestedDecode, Clone, ManagedVecItem, TypeAbi, Debug, )] @@ -68,6 +88,21 @@ pub struct UnlockScheduleEx { pub unlock_milestones: ManagedVec, } +#[derive( + TopEncode, + TopDecode, + NestedEncode, + NestedDecode, + Clone, + ManagedVecItem, + TypeAbi, + PartialEq, + Debug, +)] +pub struct InitialUnlockScheduleEx { + pub unlock_milestones: ManagedVec, +} + impl UnlockScheduleEx { pub fn get_total_percent(&self) -> u64 { let mut total = 0; @@ -149,6 +184,44 @@ pub struct LockedAssetTokenAttributesEx { pub is_merged: bool, } +#[derive( + ManagedVecItem, + TopEncode, + TopDecode, + NestedEncode, + NestedDecode, + TypeAbi, + Clone, + PartialEq, + Debug, +)] +pub struct InitialLockedAssetTokenAttributesEx { + pub unlock_schedule: InitialUnlockScheduleEx, + pub is_merged: bool, +} + +impl InitialLockedAssetTokenAttributesEx { + pub fn migrate_to_new_attributes(&self) -> LockedAssetTokenAttributesEx { + let mut updated_unlock_milestones: ManagedVec = ManagedVec::new(); + for unlock_milestone in self.unlock_schedule.unlock_milestones.into_iter() { + let updated_milestone = UnlockMilestoneEx { + unlock_epoch: unlock_milestone.unlock_epoch, + unlock_percent: u64::from(unlock_milestone.unlock_percent) + * UPDATE_PERCENTAGE_CONST, + }; + updated_unlock_milestones.push(updated_milestone); + } + let updated_unlock_schedule = UnlockScheduleEx { + unlock_milestones: updated_unlock_milestones, + }; + + LockedAssetTokenAttributesEx { + unlock_schedule: updated_unlock_schedule, + is_merged: self.is_merged, + } + } +} + impl LockedAssetTokenAttributesEx { pub fn get_unlock_amounts_per_epoch( &self, diff --git a/locked-asset/energy-factory/src/migration.rs b/locked-asset/energy-factory/src/migration.rs index 0f97aef69..a3204c17f 100644 --- a/locked-asset/energy-factory/src/migration.rs +++ b/locked-asset/energy-factory/src/migration.rs @@ -1,7 +1,10 @@ elrond_wasm::imports!(); use crate::energy::Energy; -use common_structs::{Epoch, OldLockedTokenAttributes, UnlockEpochAmountPairs}; +use common_structs::{ + Epoch, InitialOldLockedTokenAttributes, OldLockedTokenAttributes, UnlockEpochAmountPairs, + LOCKED_TOKEN_ACTIVATION_NONCE, +}; use math::safe_sub; use simple_lock::error_messages::INVALID_PAYMENTS_ERR_MSG; use unwrappable::Unwrappable; @@ -105,7 +108,16 @@ pub trait SimpleLockMigrationModule: energy: &mut Energy, ) -> EsdtTokenPayment { let attributes: OldLockedTokenAttributes = - self.get_token_attributes(&payment.token_identifier, payment.token_nonce); + if payment.token_nonce < LOCKED_TOKEN_ACTIVATION_NONCE { + let initial_attributes: InitialOldLockedTokenAttributes = + self.get_token_attributes(&payment.token_identifier, payment.token_nonce); + initial_attributes.migrate_to_new_attributes() + } else { + let updated_attributes: OldLockedTokenAttributes = + self.get_token_attributes(&payment.token_identifier, payment.token_nonce); + updated_attributes + }; + self.send().esdt_local_burn( &payment.token_identifier, payment.token_nonce, diff --git a/locked-asset/energy-factory/tests/energy_factory_setup/mod.rs b/locked-asset/energy-factory/tests/energy_factory_setup/mod.rs index 7a7ba1c39..97fcefdbb 100644 --- a/locked-asset/energy-factory/tests/energy_factory_setup/mod.rs +++ b/locked-asset/energy-factory/tests/energy_factory_setup/mod.rs @@ -25,6 +25,7 @@ use unbond_sc_mock::*; pub const EPOCHS_IN_YEAR: u64 = 360; pub const EPOCHS_IN_WEEK: u64 = 7; pub const USER_BALANCE: u64 = 1_000_000_000_000_000_000; +pub const FIRST_UPDATED_BLOCK_NONCE: u64 = 2_286_815; // first nonce for updated legacy attributes pub static BASE_ASSET_TOKEN_ID: &[u8] = b"MEX-123456"; pub static LOCKED_TOKEN_ID: &[u8] = b"LOCKED-123456"; diff --git a/locked-asset/energy-factory/tests/old_tokens_test.rs b/locked-asset/energy-factory/tests/old_tokens_test.rs index 324a3f708..52ade8155 100644 --- a/locked-asset/energy-factory/tests/old_tokens_test.rs +++ b/locked-asset/energy-factory/tests/old_tokens_test.rs @@ -1,6 +1,9 @@ mod energy_factory_setup; -use common_structs::{LockedAssetTokenAttributesEx, UnlockMilestoneEx, UnlockScheduleEx}; +use common_structs::{ + InitialLockedAssetTokenAttributesEx, InitialUnlockMilestoneEx, InitialUnlockScheduleEx, + LockedAssetTokenAttributesEx, UnlockMilestoneEx, UnlockScheduleEx, +}; use elrond_wasm::types::{BigInt, ManagedVec, MultiValueEncoded}; use elrond_wasm_modules::pause::PauseModule; use energy_factory::{ @@ -43,7 +46,7 @@ fn extend_lock_period_old_token_test() { setup.b_mock.set_nft_balance( &first_user, LEGACY_LOCKED_TOKEN_ID, - 1, + FIRST_UPDATED_BLOCK_NONCE, &rust_biguint!(USER_BALANCE), &old_token_attributes, ); @@ -87,7 +90,7 @@ fn extend_lock_period_old_token_test() { &first_user, &setup.sc_wrapper, LEGACY_LOCKED_TOKEN_ID, - 1, + FIRST_UPDATED_BLOCK_NONCE, &rust_biguint!(USER_BALANCE), |sc| { let _ = sc.migrate_old_tokens(); @@ -160,7 +163,7 @@ fn min_period_migrated_token_test() { setup.b_mock.set_nft_balance( &first_user, LEGACY_LOCKED_TOKEN_ID, - 1, + FIRST_UPDATED_BLOCK_NONCE, &rust_biguint!(USER_BALANCE), &old_token_attributes, ); @@ -207,7 +210,7 @@ fn min_period_migrated_token_test() { &first_user, &setup.sc_wrapper, LEGACY_LOCKED_TOKEN_ID, - 1, + FIRST_UPDATED_BLOCK_NONCE, &rust_biguint!(USER_BALANCE), |sc| { let _ = sc.migrate_old_tokens(); @@ -268,7 +271,7 @@ fn min_period_migrated_token_test2() { setup.b_mock.set_nft_balance( &first_user, LEGACY_LOCKED_TOKEN_ID, - 1, + FIRST_UPDATED_BLOCK_NONCE, &rust_biguint!(USER_BALANCE), &old_token_attributes, ); @@ -319,6 +322,111 @@ fn min_period_migrated_token_test2() { // rounded up to next month -> 1 + 432 = to_start_month(1440 + 234 * 4) => 2376 => 2400 let new_unlock_epoch = to_start_of_month(current_epoch + new_lock_epochs.to_u64().unwrap() * 4) + 30; + setup + .b_mock + .execute_esdt_transfer( + &first_user, + &setup.sc_wrapper, + LEGACY_LOCKED_TOKEN_ID, + FIRST_UPDATED_BLOCK_NONCE, + &rust_biguint!(USER_BALANCE), + |sc| { + let _ = sc.migrate_old_tokens(); + }, + ) + .assert_ok(); + + setup.b_mock.check_nft_balance( + &first_user, + LOCKED_TOKEN_ID, + 1, + &rust_biguint!(USER_BALANCE), + Some(&LockedTokenAttributes:: { + original_token_id: managed_token_id_wrapped!(BASE_ASSET_TOKEN_ID), + original_token_nonce: 0, + unlock_epoch: new_unlock_epoch, + }), + ); + + let mut energy_increase = + managed_biguint!(40_000) * USER_BALANCE * (new_unlock_epoch - first_unlock_epoch) + / 100_000u32; + energy_increase += + managed_biguint!(60_000) * USER_BALANCE * (new_unlock_epoch - second_unlock_epoch) + / 100_000u32; + user_energy_amount += energy_increase; + + let actual_energy_after = setup.get_user_energy(&first_user); + assert_eq!(to_rust_biguint(user_energy_amount), actual_energy_after); +} + +#[test] +fn check_initial_old_unlock_schedule_decode_test() { + let _ = DebugApi::dummy(); + let rust_zero = rust_biguint!(0); + let mut setup = SimpleLockEnergySetup::new(energy_factory::contract_obj); + + let current_epoch = 1; + setup.b_mock.set_block_epoch(current_epoch); + + let first_unlock_epoch = 91; // 3 months + let second_unlock_epoch = 121; // 9 months + let mut unlock_milestones = ManagedVec::::new(); + unlock_milestones.push(InitialUnlockMilestoneEx { + unlock_percent: 40u8, + unlock_epoch: first_unlock_epoch, + }); + unlock_milestones.push(InitialUnlockMilestoneEx { + unlock_percent: 60u8, + unlock_epoch: second_unlock_epoch, + }); + let old_token_attributes = InitialLockedAssetTokenAttributesEx { + is_merged: false, + unlock_schedule: InitialUnlockScheduleEx { unlock_milestones }, + }; + + let first_user = setup.first_user.clone(); + setup.b_mock.set_nft_balance( + &first_user, + LEGACY_LOCKED_TOKEN_ID, + 1, // nonce < FIRST_UPDATED_BLOCK_NONCE + &rust_biguint!(USER_BALANCE), + &old_token_attributes, + ); + + let mut user_energy_amount = managed_biguint!(0); + user_energy_amount += + managed_biguint!(40_000) * USER_BALANCE * (first_unlock_epoch - current_epoch) / 100_000u32; + user_energy_amount += + managed_biguint!(60_000) * USER_BALANCE * (second_unlock_epoch - current_epoch) + / 100_000u32; + + setup + .b_mock + .execute_tx(&setup.owner, &setup.sc_wrapper, &rust_zero, |sc| { + sc.set_paused(true); + let mut users_energy = MultiValueEncoded::new(); + let user_energy = ( + managed_address!(&first_user), + managed_biguint!(USER_BALANCE), + BigInt::from(user_energy_amount.clone()), + ) + .into(); + users_energy.push(user_energy); + sc.set_energy_for_old_tokens(users_energy); + + let expected_energy = Energy::new( + BigInt::from(user_energy_amount.clone()), + 1, + managed_biguint!(USER_BALANCE), + ); + let actual_energy = sc.user_energy(&managed_address!(&first_user)).get(); + assert_eq!(expected_energy, actual_energy); + + sc.set_paused(false); + }) + .assert_ok(); + setup .b_mock .execute_esdt_transfer( @@ -333,6 +441,18 @@ fn min_period_migrated_token_test2() { ) .assert_ok(); + // (40% * x * 90 + 60% * x * 120) / x = 36 + 72 = 108 + let new_lock_epochs: elrond_wasm::types::BigUint = + (managed_biguint!(40_000) * USER_BALANCE / 100_000u32 + * (first_unlock_epoch - current_epoch) + + managed_biguint!(60_000) * USER_BALANCE / 100_000u32 + * (second_unlock_epoch - current_epoch)) + / USER_BALANCE; + // rounded up to next month -> 1 + 432 = to_start_month(433) => 420 => 450 + let new_unlock_epoch = + to_start_of_month(current_epoch + new_lock_epochs.to_u64().unwrap() * 4) + 30; + assert_eq!(new_unlock_epoch, 450); + setup.b_mock.check_nft_balance( &first_user, LOCKED_TOKEN_ID, From a053cacb082ca20a035805c6b44f64765b8f0368 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Sat, 10 Dec 2022 23:18:35 +0200 Subject: [PATCH 03/12] fix after review --- common/common_structs/src/alias_types.rs | 4 +- .../common_structs/src/locked_token_types.rs | 53 +------------------ .../energy-factory/tests/old_tokens_test.rs | 14 ++--- 3 files changed, 11 insertions(+), 60 deletions(-) diff --git a/common/common_structs/src/alias_types.rs b/common/common_structs/src/alias_types.rs index e0538f13a..69e7fecc6 100644 --- a/common/common_structs/src/alias_types.rs +++ b/common/common_structs/src/alias_types.rs @@ -1,6 +1,6 @@ elrond_wasm::imports!(); -use crate::{InitialLockedAssetTokenAttributesEx, LockedAssetTokenAttributesEx, UnlockSchedule}; +use crate::{LockedAssetTokenAttributes, LockedAssetTokenAttributesEx, UnlockSchedule}; pub type Nonce = u64; pub type Epoch = u64; @@ -9,4 +9,4 @@ pub type Percent = u64; pub type PaymentsVec = ManagedVec>; pub type UnlockPeriod = UnlockSchedule; pub type OldLockedTokenAttributes = LockedAssetTokenAttributesEx; -pub type InitialOldLockedTokenAttributes = InitialLockedAssetTokenAttributesEx; +pub type InitialOldLockedTokenAttributes = LockedAssetTokenAttributes; diff --git a/common/common_structs/src/locked_token_types.rs b/common/common_structs/src/locked_token_types.rs index e23139d1a..a0d1c3545 100644 --- a/common/common_structs/src/locked_token_types.rs +++ b/common/common_structs/src/locked_token_types.rs @@ -43,23 +43,6 @@ pub struct UnlockMilestoneEx { pub unlock_percent: u64, } -#[derive( - ManagedVecItem, - TopEncode, - TopDecode, - PartialEq, - TypeAbi, - NestedEncode, - NestedDecode, - Clone, - Copy, - Debug, -)] -pub struct InitialUnlockMilestoneEx { - pub unlock_epoch: u64, - pub unlock_percent: u8, -} - #[derive( TopEncode, TopDecode, NestedEncode, NestedDecode, Clone, ManagedVecItem, TypeAbi, Debug, )] @@ -88,21 +71,6 @@ pub struct UnlockScheduleEx { pub unlock_milestones: ManagedVec, } -#[derive( - TopEncode, - TopDecode, - NestedEncode, - NestedDecode, - Clone, - ManagedVecItem, - TypeAbi, - PartialEq, - Debug, -)] -pub struct InitialUnlockScheduleEx { - pub unlock_milestones: ManagedVec, -} - impl UnlockScheduleEx { pub fn get_total_percent(&self) -> u64 { let mut total = 0; @@ -184,30 +152,13 @@ pub struct LockedAssetTokenAttributesEx { pub is_merged: bool, } -#[derive( - ManagedVecItem, - TopEncode, - TopDecode, - NestedEncode, - NestedDecode, - TypeAbi, - Clone, - PartialEq, - Debug, -)] -pub struct InitialLockedAssetTokenAttributesEx { - pub unlock_schedule: InitialUnlockScheduleEx, - pub is_merged: bool, -} - -impl InitialLockedAssetTokenAttributesEx { +impl LockedAssetTokenAttributes { pub fn migrate_to_new_attributes(&self) -> LockedAssetTokenAttributesEx { let mut updated_unlock_milestones: ManagedVec = ManagedVec::new(); for unlock_milestone in self.unlock_schedule.unlock_milestones.into_iter() { let updated_milestone = UnlockMilestoneEx { unlock_epoch: unlock_milestone.unlock_epoch, - unlock_percent: u64::from(unlock_milestone.unlock_percent) - * UPDATE_PERCENTAGE_CONST, + unlock_percent: unlock_milestone.unlock_percent as u64 * UPDATE_PERCENTAGE_CONST, }; updated_unlock_milestones.push(updated_milestone); } diff --git a/locked-asset/energy-factory/tests/old_tokens_test.rs b/locked-asset/energy-factory/tests/old_tokens_test.rs index 52ade8155..ff99cdaaf 100644 --- a/locked-asset/energy-factory/tests/old_tokens_test.rs +++ b/locked-asset/energy-factory/tests/old_tokens_test.rs @@ -1,8 +1,8 @@ mod energy_factory_setup; use common_structs::{ - InitialLockedAssetTokenAttributesEx, InitialUnlockMilestoneEx, InitialUnlockScheduleEx, - LockedAssetTokenAttributesEx, UnlockMilestoneEx, UnlockScheduleEx, + LockedAssetTokenAttributes, LockedAssetTokenAttributesEx, UnlockMilestone, UnlockMilestoneEx, + UnlockSchedule, UnlockScheduleEx, }; use elrond_wasm::types::{BigInt, ManagedVec, MultiValueEncoded}; use elrond_wasm_modules::pause::PauseModule; @@ -371,18 +371,18 @@ fn check_initial_old_unlock_schedule_decode_test() { let first_unlock_epoch = 91; // 3 months let second_unlock_epoch = 121; // 9 months - let mut unlock_milestones = ManagedVec::::new(); - unlock_milestones.push(InitialUnlockMilestoneEx { + let mut unlock_milestones = ManagedVec::::new(); + unlock_milestones.push(UnlockMilestone { unlock_percent: 40u8, unlock_epoch: first_unlock_epoch, }); - unlock_milestones.push(InitialUnlockMilestoneEx { + unlock_milestones.push(UnlockMilestone { unlock_percent: 60u8, unlock_epoch: second_unlock_epoch, }); - let old_token_attributes = InitialLockedAssetTokenAttributesEx { + let old_token_attributes = LockedAssetTokenAttributes { is_merged: false, - unlock_schedule: InitialUnlockScheduleEx { unlock_milestones }, + unlock_schedule: UnlockSchedule { unlock_milestones }, }; let first_user = setup.first_user.clone(); From 4115cf1ff650189c75728db8dee3d9bb5f91c7ee Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Sat, 10 Dec 2022 23:49:37 +0200 Subject: [PATCH 04/12] release yml fix --- .github/workflows/release.yml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a57075c57..34e0c6e32 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -22,12 +22,6 @@ permissions: jobs: build: runs-on: ubuntu-latest -<<<<<<< HEAD -======= - # See: https://docs.github.com/en/actions/using-jobs/running-jobs-in-a-container - container: - image: ${{ github.event.inputs.image }} ->>>>>>> main steps: - name: Checkout uses: actions/checkout@v2 @@ -71,15 +65,9 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | -<<<<<<< HEAD echo "Built using Docker image: **elrondnetwork/build-contract-rust:${{ github.event.inputs.image_tag }}**." >> notes.txt echo "" >> notes.txt echo "## Codehash (blake2b):" >> notes.txt -======= - echo "Built using Docker image: **${{ github.event.inputs.image }}**." >> notes.txt - echo "" >> notes.txt - echo "## Codehashes (SHA 256):" >> notes.txt ->>>>>>> main echo "" >> notes.txt for i in $(find ./assets -type f -name *.wasm); do From 661d2018d274c9026facf679f66e1a59684df708 Mon Sep 17 00:00:00 2001 From: "dorin.iancu" Date: Mon, 12 Dec 2022 10:47:25 +0200 Subject: [PATCH 05/12] fix farm staking reserve --- .../farm-staking/src/base_impl_wrapper.rs | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/farm-staking/farm-staking/src/base_impl_wrapper.rs b/farm-staking/farm-staking/src/base_impl_wrapper.rs index 5ac6187e6..938a47e53 100644 --- a/farm-staking/farm-staking/src/base_impl_wrapper.rs +++ b/farm-staking/farm-staking/src/base_impl_wrapper.rs @@ -2,6 +2,7 @@ elrond_wasm::imports!(); use core::marker::PhantomData; +use contexts::storage_cache::StorageCache; use farm_base_impl::base_traits_impl::FarmContract; use crate::token_attributes::StakingFarmTokenAttributes; @@ -43,21 +44,47 @@ where let current_block_nonce = sc.blockchain().get_block_nonce(); let last_reward_nonce = sc.last_reward_block_nonce().get(); - if current_block_nonce > last_reward_nonce { - let extra_rewards_unbounded = - Self::calculate_per_block_rewards(sc, current_block_nonce, last_reward_nonce); + if current_block_nonce <= last_reward_nonce { + return BigUint::zero(); + } + + let extra_rewards_unbounded = + Self::calculate_per_block_rewards(sc, current_block_nonce, last_reward_nonce); + + let farm_token_supply = sc.farm_token_supply().get(); + let extra_rewards_apr_bounded_per_block = sc.get_amount_apr_bounded(&farm_token_supply); - let farm_token_supply = sc.farm_token_supply().get(); - let extra_rewards_apr_bounded_per_block = sc.get_amount_apr_bounded(&farm_token_supply); + let block_nonce_diff = current_block_nonce - last_reward_nonce; + let extra_rewards_apr_bounded = extra_rewards_apr_bounded_per_block * block_nonce_diff; - let block_nonce_diff = current_block_nonce - last_reward_nonce; - let extra_rewards_apr_bounded = extra_rewards_apr_bounded_per_block * block_nonce_diff; + sc.last_reward_block_nonce().set(current_block_nonce); + + core::cmp::min(extra_rewards_unbounded, extra_rewards_apr_bounded) + } + + fn generate_aggregated_rewards( + sc: &Self::FarmSc, + storage_cache: &mut StorageCache, + ) { + let accumulated_rewards_mapper = sc.accumulated_rewards(); + let mut accumulated_rewards = accumulated_rewards_mapper.get(); + let reward_capacity = sc.reward_capacity().get(); + let remaining_rewards = &reward_capacity - &accumulated_rewards; + + let mut total_reward = Self::mint_per_block_rewards(sc, &storage_cache.reward_token_id); + total_reward = core::cmp::min(total_reward, remaining_rewards); + if total_reward == 0 { + return; + } - sc.last_reward_block_nonce().set(current_block_nonce); + storage_cache.reward_reserve += &total_reward; + accumulated_rewards += &total_reward; + accumulated_rewards_mapper.set(&accumulated_rewards); - core::cmp::min(extra_rewards_unbounded, extra_rewards_apr_bounded) - } else { - BigUint::zero() + if storage_cache.farm_token_supply > 0 { + let increase = (&total_reward * &storage_cache.division_safety_constant) + / &storage_cache.farm_token_supply; + storage_cache.reward_per_share += &increase; } } From 9a67e7fb2368ecae51265ac7e56c5cc149a1f261 Mon Sep 17 00:00:00 2001 From: "dorin.iancu" Date: Mon, 12 Dec 2022 11:14:45 +0200 Subject: [PATCH 06/12] farm staking init for upgrade --- farm-staking/farm-staking/src/lib.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/farm-staking/farm-staking/src/lib.rs b/farm-staking/farm-staking/src/lib.rs index ad66e4f3e..e5cc88871 100644 --- a/farm-staking/farm-staking/src/lib.rs +++ b/farm-staking/farm-staking/src/lib.rs @@ -5,6 +5,7 @@ #![feature(trait_alias)] use base_impl_wrapper::FarmStakingWrapper; +use common_structs::Nonce; use contexts::storage_cache::StorageCache; use farm_base_impl::base_traits_impl::FarmContract; use fixed_supply_token::FixedSupplyToken; @@ -54,6 +55,7 @@ pub trait FarmStaking: division_safety_constant: BigUint, max_apr: BigUint, min_unbond_epochs: u64, + upgrade_block: Nonce, owner: ManagedAddress, admins: MultiValueEncoded, ) { @@ -70,6 +72,23 @@ pub trait FarmStaking: self.max_annual_percentage_rewards().set(&max_apr); self.try_set_min_unbond_epochs(min_unbond_epochs); + + let per_block_reward = self.per_block_reward_amount().get(); + let current_block_nonce = self.blockchain().get_block_epoch(); + let block_nonce_diff = current_block_nonce - upgrade_block; + let rewards_since_upgrade = per_block_reward * block_nonce_diff; + + let accumulated_rewards_before = self.accumulated_rewards().update(|acc| { + let before = (*acc).clone(); + *acc += &rewards_since_upgrade; + + let capacity = self.reward_capacity().get(); + *acc = core::cmp::min((*acc).clone(), capacity); + + before + }); + self.reward_reserve() + .update(|r| *r += accumulated_rewards_before); } #[payable("*")] From 7b524d59fee49d616347cac6c4381b91abe80aa1 Mon Sep 17 00:00:00 2001 From: "dorin.iancu" Date: Mon, 12 Dec 2022 12:27:23 +0200 Subject: [PATCH 07/12] fix wrong api function call --- farm-staking/farm-staking/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/farm-staking/farm-staking/src/lib.rs b/farm-staking/farm-staking/src/lib.rs index e5cc88871..32f5d472e 100644 --- a/farm-staking/farm-staking/src/lib.rs +++ b/farm-staking/farm-staking/src/lib.rs @@ -74,7 +74,7 @@ pub trait FarmStaking: self.try_set_min_unbond_epochs(min_unbond_epochs); let per_block_reward = self.per_block_reward_amount().get(); - let current_block_nonce = self.blockchain().get_block_epoch(); + let current_block_nonce = self.blockchain().get_block_nonce(); let block_nonce_diff = current_block_nonce - upgrade_block; let rewards_since_upgrade = per_block_reward * block_nonce_diff; From 2069d2784cfd42af15da0524783ed2cca76943a4 Mon Sep 17 00:00:00 2001 From: "dorin.iancu" Date: Mon, 12 Dec 2022 13:43:13 +0200 Subject: [PATCH 08/12] fix test --- farm-staking/farm-staking/tests/farm_staking_test.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/farm-staking/farm-staking/tests/farm_staking_test.rs b/farm-staking/farm-staking/tests/farm_staking_test.rs index cd2ce89c7..08dbb9bb6 100644 --- a/farm-staking/farm-staking/tests/farm_staking_test.rs +++ b/farm-staking/farm-staking/tests/farm_staking_test.rs @@ -68,6 +68,7 @@ where division_safety_constant, managed_biguint!(MAX_APR), MIN_UNBOND_EPOCHS, + 0, ManagedAddress::::zero(), MultiValueEncoded::new(), ); From e4d8b9dec309cdf1f534c5913c6ebc4c4b2c48a4 Mon Sep 17 00:00:00 2001 From: "dorin.iancu" Date: Mon, 12 Dec 2022 13:47:46 +0200 Subject: [PATCH 09/12] fix another test --- .../tests/staking_farm_with_lp_staking_contract_setup/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_setup/mod.rs b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_setup/mod.rs index c4f828a62..bb27ef30e 100644 --- a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_setup/mod.rs +++ b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_setup/mod.rs @@ -42,6 +42,7 @@ where div_const, max_apr, UNBOND_EPOCHS, + 0, ManagedAddress::::zero(), MultiValueEncoded::new(), ); From bbeaaf357c7067bd875ef4567b5966d03673675a Mon Sep 17 00:00:00 2001 From: "dorin.iancu" Date: Mon, 12 Dec 2022 16:15:22 +0200 Subject: [PATCH 10/12] fix typo --- energy-integration/fees-collector/src/fees_accumulation.rs | 2 +- energy-integration/fees-collector/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/energy-integration/fees-collector/src/fees_accumulation.rs b/energy-integration/fees-collector/src/fees_accumulation.rs index 7dde2f31c..d834f9a19 100644 --- a/energy-integration/fees-collector/src/fees_accumulation.rs +++ b/energy-integration/fees-collector/src/fees_accumulation.rs @@ -46,7 +46,7 @@ pub trait FeesAccumulationModule: self.emit_deposit_swap_fees_event(caller, current_week, payment); } - fn get_and_clear_acccumulated_fees( + fn get_and_clear_accumulated_fees( &self, week: Week, token: &TokenIdentifier, diff --git a/energy-integration/fees-collector/src/lib.rs b/energy-integration/fees-collector/src/lib.rs index 96463a8d1..47ed6cdcc 100644 --- a/energy-integration/fees-collector/src/lib.rs +++ b/energy-integration/fees-collector/src/lib.rs @@ -122,7 +122,7 @@ where let mut results = ManagedVec::new(); let all_tokens = sc.all_tokens().get(); for token in &all_tokens { - let opt_accumulated_fees = sc.get_and_clear_acccumulated_fees(week, &token); + let opt_accumulated_fees = sc.get_and_clear_accumulated_fees(week, &token); if let Some(accumulated_fees) = opt_accumulated_fees { results.push(EsdtTokenPayment::new(token, 0, accumulated_fees)); } From 40bee88047289408d67aeba374fd23daeebfa25e Mon Sep 17 00:00:00 2001 From: "dorin.iancu" Date: Mon, 12 Dec 2022 16:35:33 +0200 Subject: [PATCH 11/12] pair fixes --- dex/pair/src/bot_protection.rs | 198 ------------------------------- dex/pair/src/config.rs | 14 --- dex/pair/src/lib.rs | 31 +---- dex/pair/src/liquidity_pool.rs | 11 +- dex/pair/tests/pair_rs_test.rs | 37 ------ dex/pair/tests/pair_setup/mod.rs | 48 -------- 6 files changed, 8 insertions(+), 331 deletions(-) delete mode 100644 dex/pair/src/bot_protection.rs diff --git a/dex/pair/src/bot_protection.rs b/dex/pair/src/bot_protection.rs deleted file mode 100644 index 45fcd12b3..000000000 --- a/dex/pair/src/bot_protection.rs +++ /dev/null @@ -1,198 +0,0 @@ -elrond_wasm::imports!(); -elrond_wasm::derive_imports!(); - -use crate::config; -use crate::contexts::base::StorageCache; -use crate::contexts::swap::SwapContext; - -const PERCENT_MAX: u64 = 100_000; - -#[derive(TopEncode, TopDecode, TypeAbi)] -pub struct BPConfig { - protect_stop_block: u64, - volume_percent: u64, - max_num_actions_per_address: u64, -} - -#[elrond_wasm::module] -pub trait BPModule: - config::ConfigModule - + token_send::TokenSendModule - + permissions_module::PermissionsModule - + pausable::PausableModule -{ - fn require_can_proceed_swap( - &self, - ctx: &SwapContext, - storage_cache: &StorageCache, - ) { - if self.bp_swap_config().is_empty() { - return; - } - - let caller = self.blockchain().get_caller(); - let bp_config = self.bp_swap_config().get(); - let current_block = self.blockchain().get_block_nonce(); - if bp_config.protect_stop_block < current_block { - self.num_swaps_by_address(&caller).clear(); - return; - } - - let reserve_in = storage_cache.get_reserve_in(ctx.swap_tokens_order); - let reserve_out = storage_cache.get_reserve_out(ctx.swap_tokens_order); - if *reserve_in == 0 && *reserve_out == 0 { - return; - } - - let num_swaps = self.num_swaps_by_address(&caller).get(); - require!( - num_swaps < bp_config.max_num_actions_per_address, - "too many swaps by address" - ); - - let amount_in_percent = &ctx.final_input_amount * PERCENT_MAX / reserve_in; - require!( - amount_in_percent < bp_config.volume_percent, - "swap amount in too large" - ); - - let amount_out_percent = &ctx.final_output_amount * PERCENT_MAX / reserve_out; - require!( - amount_out_percent < bp_config.volume_percent, - "swap amount out too large" - ); - - self.num_swaps_by_address(&caller).set(num_swaps + 1); - } - - fn require_can_proceed_remove(&self, lp_token_supply: &BigUint, liquidity_removed: &BigUint) { - if self.bp_remove_config().is_empty() { - return; - } - - let caller = self.blockchain().get_caller(); - let bp_config = self.bp_remove_config().get(); - let current_block = self.blockchain().get_block_nonce(); - if bp_config.protect_stop_block < current_block { - self.num_removes_by_address(&caller).clear(); - return; - } - if lp_token_supply == &0u64 { - return; - } - - let num_removes = self.num_removes_by_address(&caller).get(); - require!( - num_removes < bp_config.max_num_actions_per_address, - "too many removes by address" - ); - - let percent_removed = liquidity_removed * PERCENT_MAX / lp_token_supply; - require!( - percent_removed < bp_config.volume_percent, - "remove liquidity too large" - ); - - self.num_removes_by_address(&caller).set(num_removes + 1); - } - - fn require_can_proceed_add(&self, lp_token_supply: &BigUint, liquidity_added: &BigUint) { - if self.bp_add_config().is_empty() { - return; - } - - let caller = self.blockchain().get_caller(); - let bp_config = self.bp_add_config().get(); - let current_block = self.blockchain().get_block_nonce(); - if bp_config.protect_stop_block < current_block { - self.num_adds_by_address(&caller).clear(); - return; - } - if lp_token_supply == &0 { - return; - } - - let num_adds = self.num_adds_by_address(&caller).get(); - require!( - num_adds < bp_config.max_num_actions_per_address, - "too many adds by address" - ); - - let percent_added = liquidity_added * PERCENT_MAX / lp_token_supply; - require!( - percent_added < bp_config.volume_percent, - "add liquidity too large" - ); - - self.num_adds_by_address(&caller).set(num_adds + 1); - } - - #[endpoint(setBPSwapConfig)] - fn set_bp_swap_config( - &self, - protect_stop_block: u64, - volume_percent: u64, - max_num_actions_per_address: u64, - ) { - self.require_caller_has_owner_permissions(); - self.bp_swap_config().set(&BPConfig { - protect_stop_block, - volume_percent, - max_num_actions_per_address, - }); - } - - #[endpoint(setBPRemoveConfig)] - fn set_bp_remove_config( - &self, - protect_stop_block: u64, - volume_percent: u64, - max_num_actions_per_address: u64, - ) { - self.require_caller_has_owner_permissions(); - self.bp_remove_config().set(&BPConfig { - protect_stop_block, - volume_percent, - max_num_actions_per_address, - }); - } - - #[endpoint(setBPAddConfig)] - fn set_bp_add_config( - &self, - protect_stop_block: u64, - volume_percent: u64, - max_num_actions_per_address: u64, - ) { - self.require_caller_has_owner_permissions(); - self.bp_add_config().set(&BPConfig { - protect_stop_block, - volume_percent, - max_num_actions_per_address, - }); - } - - #[view(getBPSwapConfig)] - #[storage_mapper("bp_swap_config")] - fn bp_swap_config(&self) -> SingleValueMapper; - - #[view(getNumSwapsByAddress)] - #[storage_mapper("num_swaps_by_address")] - fn num_swaps_by_address(&self, address: &ManagedAddress) -> SingleValueMapper; - - #[view(getBPRemoveConfig)] - #[storage_mapper("bp_remove_config")] - fn bp_remove_config(&self) -> SingleValueMapper; - - #[view(getNumRemovesByAddress)] - #[storage_mapper("num_removes_by_address")] - fn num_removes_by_address(&self, address: &ManagedAddress) -> SingleValueMapper; - - #[view(getBPAddConfig)] - #[storage_mapper("bp_add_config")] - fn bp_add_config(&self) -> SingleValueMapper; - - #[view(getNumAddsByAddress)] - #[storage_mapper("num_adds_by_address")] - fn num_adds_by_address(&self, address: &ManagedAddress) -> SingleValueMapper; -} diff --git a/dex/pair/src/config.rs b/dex/pair/src/config.rs index 962095d47..448707c33 100644 --- a/dex/pair/src/config.rs +++ b/dex/pair/src/config.rs @@ -12,12 +12,6 @@ pub const MAX_FEE_PERCENTAGE: u64 = 5_000; pub trait ConfigModule: token_send::TokenSendModule + permissions_module::PermissionsModule + pausable::PausableModule { - #[endpoint(setExternSwapGasLimit)] - fn set_extern_swap_gas_limit(&self, gas_limit: u64) { - self.require_caller_has_owner_permissions(); - self.extern_swap_gas_limit().set(gas_limit); - } - #[endpoint(setStateActiveNoSwaps)] fn set_state_active_no_swaps(&self) { self.require_caller_has_owner_permissions(); @@ -56,14 +50,6 @@ pub trait ConfigModule: #[storage_mapper("router_address")] fn router_address(&self) -> SingleValueMapper; - #[view(getRouterOwnerManagedAddress)] - #[storage_mapper("router_owner_address")] - fn router_owner_address(&self) -> SingleValueMapper; - - #[view(getExternSwapGasLimit)] - #[storage_mapper("extern_swap_gas_limit")] - fn extern_swap_gas_limit(&self) -> SingleValueMapper; - #[storage_mapper("lpTokenIdentifier")] fn lp_token_identifier(&self) -> SingleValueMapper; diff --git a/dex/pair/src/lib.rs b/dex/pair/src/lib.rs index e00ab0f6c..0670aab91 100644 --- a/dex/pair/src/lib.rs +++ b/dex/pair/src/lib.rs @@ -1,13 +1,9 @@ #![no_std] -#![allow(clippy::vec_init_then_push)] elrond_wasm::imports!(); elrond_wasm::derive_imports!(); -const DEFAULT_EXTERN_SWAP_GAS_LIMIT: u64 = 50000000; - mod amm; -pub mod bot_protection; pub mod config; mod contexts; pub mod errors; @@ -47,7 +43,6 @@ pub trait Pair: + token_send::TokenSendModule + events::EventsModule + safe_price::SafePriceModule - + bot_protection::BPModule + contexts::output_builder::OutputBuilderModule + locking_wrapper::LockingWrapperModule + permissions_module::PermissionsModule @@ -71,19 +66,18 @@ pub trait Pair: ERROR_NOT_AN_ESDT ); require!(first_token_id != second_token_id, ERROR_SAME_TOKENS); + let lp_token_id = self.lp_token_identifier().get(); require!(first_token_id != lp_token_id, ERROR_POOL_TOKEN_IS_PLT); require!(second_token_id != lp_token_id, ERROR_POOL_TOKEN_IS_PLT); - self.set_fee_percents(total_fee_percent, special_fee_percent); + self.set_fee_percents(total_fee_percent, special_fee_percent); self.state().set(State::Inactive); - self.extern_swap_gas_limit() - .set_if_empty(DEFAULT_EXTERN_SWAP_GAS_LIMIT); self.router_address().set(&router_address); - self.router_owner_address().set(&router_owner_address); - self.first_token_id().set(&first_token_id); - self.second_token_id().set(&second_token_id); + 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() { Some(initial_liquidity_adder) } else { @@ -231,7 +225,7 @@ pub trait Pair: ); self.set_optimal_amounts(&mut add_liq_context, &storage_cache); - let liq_added = if storage_cache.lp_token_supply == 0u64 { + add_liq_context.liq_added = if storage_cache.lp_token_supply == 0u64 { self.pool_add_initial_liquidity( &add_liq_context.first_token_optimal_amount, &add_liq_context.second_token_optimal_amount, @@ -244,8 +238,6 @@ pub trait Pair: &mut storage_cache, ) }; - self.require_can_proceed_add(&storage_cache.lp_token_supply, &liq_added); - add_liq_context.liq_added = liq_added; let new_k = self.calculate_k_constant( &storage_cache.first_token_reserve, @@ -311,10 +303,6 @@ pub trait Pair: second_token_amount_min, ); self.pool_remove_liquidity(&mut remove_liq_context, &mut storage_cache); - self.require_can_proceed_remove( - &storage_cache.lp_token_supply, - &remove_liq_context.lp_token_payment_amount, - ); let new_k = self.calculate_k_constant( &storage_cache.first_token_reserve, @@ -361,10 +349,6 @@ pub trait Pair: let mut remove_liq_context = RemoveLiquidityContext::new(payment.amount, BigUint::from(1u64), BigUint::from(1u64)); self.pool_remove_liquidity(&mut remove_liq_context, &mut storage_cache); - self.require_can_proceed_remove( - &storage_cache.lp_token_supply, - &remove_liq_context.lp_token_payment_amount, - ); self.burn( &storage_cache.lp_token_id, @@ -435,7 +419,6 @@ pub trait Pair: require!(amount_out > 0u64, ERROR_ZERO_AMOUNT); swap_context.final_output_amount = amount_out; - self.require_can_proceed_swap(&swap_context, &storage_cache); let new_k = self.calculate_k_constant( &storage_cache.first_token_reserve, @@ -490,7 +473,6 @@ pub trait Pair: swap_tokens_order, ); self.perform_swap_fixed_input(&mut swap_context, &mut storage_cache); - self.require_can_proceed_swap(&swap_context, &storage_cache); let new_k = self.calculate_k_constant( &storage_cache.first_token_reserve, @@ -555,7 +537,6 @@ pub trait Pair: swap_tokens_order, ); self.perform_swap_fixed_output(&mut swap_context, &mut storage_cache); - self.require_can_proceed_swap(&swap_context, &storage_cache); let new_k = self.calculate_k_constant( &storage_cache.first_token_reserve, diff --git a/dex/pair/src/liquidity_pool.rs b/dex/pair/src/liquidity_pool.rs index 551f719ef..46adab005 100644 --- a/dex/pair/src/liquidity_pool.rs +++ b/dex/pair/src/liquidity_pool.rs @@ -47,7 +47,8 @@ pub trait LiquidityPoolModule: second_token_optimal_amount: &BigUint, storage_cache: &mut StorageCache, ) -> BigUint { - let liquidity = self.biguint_min(first_token_optimal_amount, second_token_optimal_amount); + let liquidity = + core::cmp::min(first_token_optimal_amount, second_token_optimal_amount).clone(); let minimum_liquidity = BigUint::from(MINIMUM_LIQUIDITY); require!(liquidity > minimum_liquidity, ERROR_FIRST_LIQUDITY); @@ -214,12 +215,4 @@ pub trait LiquidityPoolModule: amount_out } - - fn biguint_min(&self, a: &BigUint, b: &BigUint) -> BigUint { - if a < b { - a.clone() - } else { - b.clone() - } - } } diff --git a/dex/pair/tests/pair_rs_test.rs b/dex/pair/tests/pair_rs_test.rs index 0610e4355..60bdbdbfa 100644 --- a/dex/pair/tests/pair_rs_test.rs +++ b/dex/pair/tests/pair_rs_test.rs @@ -115,43 +115,6 @@ fn test_safe_price() { pair_setup.check_future_safe_state(110, 110, 1, 1_011_000, 991_130, 1_011_000, 991_130); } -#[test] -fn test_swap_protect() { - 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, - ); - - let protect_until_block = 10; - let max_volume_percent = 10_000; - let max_num_swaps = 2; - pair_setup.set_swap_protect(protect_until_block, max_volume_percent, max_num_swaps); - - pair_setup.swap_fixed_input_expect_error( - WEGLD_TOKEN_ID, - 500_000, - MEX_TOKEN_ID, - 1, - "swap amount in too large", - ); - - pair_setup.swap_fixed_input(WEGLD_TOKEN_ID, 1_000, MEX_TOKEN_ID, 1, 996); - pair_setup.swap_fixed_input(WEGLD_TOKEN_ID, 1_000, MEX_TOKEN_ID, 1, 994); - - pair_setup.swap_fixed_input_expect_error( - WEGLD_TOKEN_ID, - 1_000, - MEX_TOKEN_ID, - 1, - "too many swaps by address", - ); - - pair_setup.b_mock.set_block_nonce(protect_until_block + 1); - - pair_setup.swap_fixed_input(WEGLD_TOKEN_ID, 500_000, MEX_TOKEN_ID, 1, 331_672); -} - #[test] fn test_locked_asset() { let mut pair_setup = PairSetup::new(pair::contract_obj); diff --git a/dex/pair/tests/pair_setup/mod.rs b/dex/pair/tests/pair_setup/mod.rs index 42cfdac88..ae17a50a3 100644 --- a/dex/pair/tests/pair_setup/mod.rs +++ b/dex/pair/tests/pair_setup/mod.rs @@ -17,7 +17,6 @@ pub const LP_PROXY_TOKEN_ID: &[u8] = b"LPPROXY-abcdef"; pub const USER_TOTAL_MEX_TOKENS: u64 = 5_000_000_000; pub const USER_TOTAL_WEGLD_TOKENS: u64 = 5_000_000_000; -use pair::bot_protection::*; use pair::config::ConfigModule as PairConfigModule; use pair::safe_price::*; use pair::*; @@ -174,31 +173,6 @@ where .assert_ok(); } - pub fn swap_fixed_input_expect_error( - &mut self, - payment_token_id: &[u8], - payment_amount: u64, - desired_token_id: &[u8], - desired_amount_min: u64, - expected_message: &str, - ) { - self.b_mock - .execute_esdt_transfer( - &self.user_address, - &self.pair_wrapper, - payment_token_id, - 0, - &rust_biguint!(payment_amount), - |sc| { - sc.swap_tokens_fixed_input( - managed_token_id!(desired_token_id), - managed_biguint!(desired_amount_min), - ); - }, - ) - .assert_user_error(expected_message); - } - pub fn swap_fixed_output( &mut self, payment_token_id: &[u8], @@ -265,28 +239,6 @@ where ); } - pub fn set_swap_protect( - &mut self, - protect_stop_block: u64, - volume_percent: u64, - max_num_actions_per_address: u64, - ) { - self.b_mock - .execute_tx( - &self.owner_address, - &self.pair_wrapper, - &rust_biguint!(0), - |sc| { - sc.set_bp_swap_config( - protect_stop_block, - volume_percent, - max_num_actions_per_address, - ); - }, - ) - .assert_ok(); - } - #[allow(clippy::too_many_arguments)] pub fn check_current_safe_state( &mut self, From 641ce50fe013da0b3eff4850305bad22cdf98038 Mon Sep 17 00:00:00 2001 From: "dorin.iancu" Date: Tue, 13 Dec 2022 10:26:34 +0200 Subject: [PATCH 12/12] revert init changes --- .../mod.rs | 1 - farm-staking/farm-staking/src/lib.rs | 19 ------------------- .../farm-staking/tests/farm_staking_test.rs | 1 - 3 files changed, 21 deletions(-) diff --git a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_setup/mod.rs b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_setup/mod.rs index 5ee746765..4cdaaae5f 100644 --- a/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_setup/mod.rs +++ b/farm-staking/farm-staking-proxy/tests/staking_farm_with_lp_staking_contract_setup/mod.rs @@ -42,7 +42,6 @@ where div_const, max_apr, UNBOND_EPOCHS, - 0, ManagedAddress::::zero(), MultiValueEncoded::new(), ); diff --git a/farm-staking/farm-staking/src/lib.rs b/farm-staking/farm-staking/src/lib.rs index 32f5d472e..ad66e4f3e 100644 --- a/farm-staking/farm-staking/src/lib.rs +++ b/farm-staking/farm-staking/src/lib.rs @@ -5,7 +5,6 @@ #![feature(trait_alias)] use base_impl_wrapper::FarmStakingWrapper; -use common_structs::Nonce; use contexts::storage_cache::StorageCache; use farm_base_impl::base_traits_impl::FarmContract; use fixed_supply_token::FixedSupplyToken; @@ -55,7 +54,6 @@ pub trait FarmStaking: division_safety_constant: BigUint, max_apr: BigUint, min_unbond_epochs: u64, - upgrade_block: Nonce, owner: ManagedAddress, admins: MultiValueEncoded, ) { @@ -72,23 +70,6 @@ pub trait FarmStaking: self.max_annual_percentage_rewards().set(&max_apr); self.try_set_min_unbond_epochs(min_unbond_epochs); - - let per_block_reward = self.per_block_reward_amount().get(); - let current_block_nonce = self.blockchain().get_block_nonce(); - let block_nonce_diff = current_block_nonce - upgrade_block; - let rewards_since_upgrade = per_block_reward * block_nonce_diff; - - let accumulated_rewards_before = self.accumulated_rewards().update(|acc| { - let before = (*acc).clone(); - *acc += &rewards_since_upgrade; - - let capacity = self.reward_capacity().get(); - *acc = core::cmp::min((*acc).clone(), capacity); - - before - }); - self.reward_reserve() - .update(|r| *r += accumulated_rewards_before); } #[payable("*")] diff --git a/farm-staking/farm-staking/tests/farm_staking_test.rs b/farm-staking/farm-staking/tests/farm_staking_test.rs index 9aa1c20cf..a21dd05b7 100644 --- a/farm-staking/farm-staking/tests/farm_staking_test.rs +++ b/farm-staking/farm-staking/tests/farm_staking_test.rs @@ -68,7 +68,6 @@ where division_safety_constant, managed_biguint!(MAX_APR), MIN_UNBOND_EPOCHS, - 0, ManagedAddress::::zero(), MultiValueEncoded::new(), );