Skip to content

Commit

Permalink
remove supply fix for merge endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Aug 2, 2024
1 parent d43a154 commit ba5aec9
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 262 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use multiversx_sc::{
types::{Address, BigInt, EsdtLocalRole, MultiValueEncoded},
};
use multiversx_sc_scenario::{
imports::TxTokenTransfer,
managed_address, managed_biguint, managed_token_id, rust_biguint,
whitebox_legacy::{BlockchainStateWrapper, ContractObjWrapper},
DebugApi,
Expand Down Expand Up @@ -50,11 +49,6 @@ pub const EPOCHS_IN_YEAR: u64 = 360;
pub static LOCK_OPTIONS: &[u64] = &[EPOCHS_IN_YEAR, 2 * EPOCHS_IN_YEAR, 4 * EPOCHS_IN_YEAR];
pub static PENALTY_PERCENTAGES: &[u64] = &[4_000, 6_000, 8_000];

pub struct NonceAmountPair {
pub nonce: u64,
pub amount: u64,
}

pub struct RawFarmTokenAttributes {
pub reward_per_share_bytes: Vec<u8>,
pub entering_epoch: Epoch,
Expand Down Expand Up @@ -415,37 +409,6 @@ where
result
}

pub fn merge_farm_tokens(&mut self, user: &Address, farm_tokens: Vec<NonceAmountPair>) {
self.last_farm_token_nonce += 1;
let mut expected_farm_token_amount = 0;
let mut payments = Vec::new();
for farm_token in farm_tokens {
expected_farm_token_amount += farm_token.amount;
payments.push(TxTokenTransfer {
token_identifier: FARM_TOKEN_ID.to_vec(),
nonce: farm_token.nonce,
value: rust_biguint!(farm_token.amount),
});
}

self.b_mock
.execute_esdt_multi_transfer(user, &self.farm_wrapper, &payments, |sc| {
let (out_farm_token, _boosted_rewards) = sc
.merge_farm_tokens_endpoint(OptionalValue::None)
.into_tuple();
assert_eq!(
out_farm_token.token_identifier,
managed_token_id!(FARM_TOKEN_ID)
);
assert_eq!(out_farm_token.token_nonce, self.last_farm_token_nonce);
assert_eq!(
out_farm_token.amount,
managed_biguint!(expected_farm_token_amount)
);
})
.assert_ok();
}

pub fn exit_farm(&mut self, user: &Address, farm_token_nonce: u64, exit_farm_amount: u64) {
self.b_mock
.execute_esdt_transfer(
Expand Down
103 changes: 0 additions & 103 deletions dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(deprecated)]

use common_structs::FarmTokenAttributes;
use farm_with_locked_rewards_setup::NonceAmountPair;
use multiversx_sc::codec::Empty;
use multiversx_sc_scenario::{managed_address, managed_biguint, rust_biguint, DebugApi};
use simple_lock::locked_token::LockedTokenAttributes;
Expand Down Expand Up @@ -466,105 +465,3 @@ fn claim_rewards_per_week_test() {
None,
);
}

#[test]
fn merge_farm_position_per_week_test() {
DebugApi::dummy();
let mut farm_setup = FarmSetup::new(
farm_with_locked_rewards::contract_obj,
energy_factory::contract_obj,
);

farm_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);
farm_setup.set_boosted_yields_factors();
farm_setup.b_mock.set_block_epoch(2);

let temp_user = farm_setup.third_user.clone();

// first user enter farm
let farm_in_amount = 10_000_000;
let first_user = farm_setup.first_user.clone();
farm_setup.set_user_energy(&first_user, 1_000, 2, 1);

// User creates 3 different positions
farm_setup.enter_farm(&first_user, farm_in_amount);
farm_setup.enter_farm(&first_user, farm_in_amount);
farm_setup.enter_farm(&first_user, farm_in_amount);

farm_setup.check_farm_token_supply(farm_in_amount * 3);
farm_setup.check_farm_rps(0u64);

farm_setup.b_mock.set_block_nonce(10);
farm_setup.b_mock.set_block_epoch(6);
farm_setup.set_user_energy(&first_user, 1_000, 6, 1);
farm_setup.set_user_energy(&temp_user, 1, 6, 1);
farm_setup.enter_farm(&temp_user, 1);
farm_setup.exit_farm(&temp_user, 4, 1);

farm_setup.check_farm_rps(250_000_000u64);
let boosted_rewards_per_week = 2_500;

// advance 1 week
farm_setup.set_user_energy(&first_user, 1_000, 13, 1);
farm_setup.b_mock.set_block_nonce(20);
farm_setup.b_mock.set_block_epoch(13);

let mut payments = vec![
NonceAmountPair {
nonce: 1,
amount: farm_in_amount,
},
NonceAmountPair {
nonce: 2,
amount: farm_in_amount,
},
];

farm_setup.merge_farm_tokens(&first_user, payments);

farm_setup.check_farm_token_supply(farm_in_amount * 3);
farm_setup.check_farm_rps(500_000_000u64);
farm_setup.b_mock.check_nft_balance::<Empty>(
&first_user,
LOCKED_REWARD_TOKEN_ID,
1,
&rust_biguint!(boosted_rewards_per_week),
None,
);

// advance 1 week
farm_setup.set_user_energy(&first_user, 1_000, 15, 1);
farm_setup.b_mock.set_block_nonce(30);
farm_setup.b_mock.set_block_epoch(15);

payments = vec![
NonceAmountPair {
nonce: 3,
amount: farm_in_amount,
},
NonceAmountPair {
nonce: 5,
amount: farm_in_amount * 2,
},
];

farm_setup.merge_farm_tokens(&first_user, payments);

farm_setup.check_farm_token_supply(farm_in_amount * 3);
farm_setup.check_farm_rps(750_000_000u64);

farm_setup.b_mock.check_nft_balance::<Empty>(
&first_user,
FARM_TOKEN_ID,
6,
&rust_biguint!(farm_in_amount * 3),
None,
);
farm_setup.b_mock.check_nft_balance::<Empty>(
&first_user,
LOCKED_REWARD_TOKEN_ID,
1,
&rust_biguint!(boosted_rewards_per_week * 2),
None,
);
}
8 changes: 0 additions & 8 deletions farm-staking/farm-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,6 @@ pub trait FarmStaking:

FC::check_and_update_user_farm_position(self, orig_caller, &payments);

let mut storage_cache = StorageCache::new(self);
self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id);

FC::generate_aggregated_rewards(self, &mut storage_cache);

self.set_farm_supply_for_current_week(&storage_cache.farm_token_supply);

self.merge_from_payments_and_burn(payments, &token_mapper)
}

Expand All @@ -160,7 +153,6 @@ pub trait FarmStaking:
require!(percentage <= MAX_PERCENT, "Invalid percentage");

let mut storage_cache = StorageCache::new(self);
self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id);
FarmStakingWrapper::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

self.boosted_yields_rewards_percentage().set(percentage);
Expand Down
114 changes: 0 additions & 114 deletions farm-staking/farm-staking/tests/farm_staking_energy_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1616,117 +1616,3 @@ fn claim_rewards_per_week_test() {
&rust_biguint!(user_rewards_balance),
);
}

#[test]
fn merge_farm_position_per_week_test() {
DebugApi::dummy();
let mut fs_setup =
FarmStakingSetup::new(farm_staking::contract_obj, energy_factory::contract_obj);

fs_setup.set_boosted_yields_factors();
fs_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);

let first_user = fs_setup.user_address.clone();
let farm_in_amount = 100_000_000;

fs_setup.set_user_energy(&first_user, 10_000, 0, 10);
fs_setup.stake_farm(&first_user, farm_in_amount, &[], 1, 0, 0);
fs_setup.stake_farm(&first_user, farm_in_amount, &[], 2, 0, 0);
fs_setup.stake_farm(&first_user, farm_in_amount, &[], 3, 0, 0);

fs_setup.check_farm_token_supply(farm_in_amount * 3);
fs_setup.check_farm_rps(0u64);

fs_setup.b_mock.set_block_nonce(100);
fs_setup.b_mock.set_block_epoch(6);
fs_setup.set_user_energy(&first_user, 1_000, 6, 1);

// Reset user balance
fs_setup
.b_mock
.set_esdt_balance(&first_user, FARMING_TOKEN_ID, &rust_biguint!(0));

// random user tx to collect rewards
let rand_user = fs_setup.b_mock.create_user_account(&rust_biguint!(0));
fs_setup.b_mock.set_esdt_balance(
&rand_user,
FARMING_TOKEN_ID,
&rust_biguint!(USER_TOTAL_RIDE_TOKENS),
);

fs_setup.set_user_energy(&rand_user, 1, 6, 1);
fs_setup.stake_farm(&rand_user, 10, &[], 4, 3_500_000u64, 0);
fs_setup.unstake_farm_no_checks(&rand_user, 10, 4);

let farm_rps_increase = 3_500_000u64;
let mut current_farm_rps = 0;
current_farm_rps += farm_rps_increase;
fs_setup.check_farm_rps(current_farm_rps);

// advance 1 week
fs_setup.set_user_energy(&first_user, 1_000, 13, 1);
fs_setup.b_mock.set_block_nonce(200);
fs_setup.b_mock.set_block_epoch(13);

let boosted_rewards_for_week = 350u64;

current_farm_rps += farm_rps_increase;
let mut user_rewards_balance = boosted_rewards_for_week;

let mut payments = vec![
NonceAmountPair {
nonce: 1,
amount: farm_in_amount,
},
NonceAmountPair {
nonce: 2,
amount: farm_in_amount,
},
];
fs_setup.merge_farm_tokens(&first_user, payments, 6);

fs_setup.check_farm_rps(current_farm_rps);

// advance 1 week
fs_setup.set_user_energy(&first_user, 1_000, 15, 1);
fs_setup.b_mock.set_block_nonce(300);
fs_setup.b_mock.set_block_epoch(15);

current_farm_rps += farm_rps_increase;
user_rewards_balance += boosted_rewards_for_week;

payments = vec![
NonceAmountPair {
nonce: 3,
amount: farm_in_amount,
},
NonceAmountPair {
nonce: 6,
amount: farm_in_amount * 2,
},
];
fs_setup.merge_farm_tokens(&first_user, payments, 7);

fs_setup.check_farm_rps(current_farm_rps);

fs_setup.b_mock.check_esdt_balance(
&first_user,
REWARD_TOKEN_ID,
&rust_biguint!(user_rewards_balance),
);

let expected_attributes = StakingFarmTokenAttributes::<DebugApi> {
reward_per_share: managed_biguint!(0),
compounded_reward: managed_biguint!(0),
current_farm_amount: managed_biguint!(farm_in_amount * 3),
original_owner: managed_address!(&first_user),
};

fs_setup.b_mock.check_nft_balance(
&first_user,
FARM_TOKEN_ID,
7,
&rust_biguint!(farm_in_amount * 3),
Some(&expected_attributes),
);
}

0 comments on commit ba5aec9

Please sign in to comment.