Skip to content

Commit

Permalink
farm with locked rewards unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Aug 2, 2024
1 parent 2285407 commit 88dd12b
Show file tree
Hide file tree
Showing 3 changed files with 320 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 All @@ -24,6 +25,7 @@ use farm_with_locked_rewards::Farm;
use locking_module::lock_with_energy_module::LockWithEnergyModule;
use multiversx_sc_modules::pause::PauseModule;
use pausable::{PausableModule, State};
use rewards::RewardsModule;
use sc_whitelist_module::SCWhitelistModule;
use simple_lock::locked_token::LockedTokenModule;
use week_timekeeping::Epoch;
Expand All @@ -48,6 +50,11 @@ 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 @@ -408,6 +415,37 @@ 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 All @@ -422,4 +460,53 @@ where
)
.assert_ok();
}

pub fn claim_boosted_rewards_for_user(
&mut self,
owner: &Address,
broker: &Address,
locked_reward_nonce: u64,
) -> u64 {
self.last_farm_token_nonce += 1;

let mut result = 0;
self.b_mock
.execute_tx(broker, &self.farm_wrapper, &rust_biguint!(0), |sc| {
let reward_payment =
sc.claim_boosted_rewards(OptionalValue::Some(managed_address!(owner)));
assert_eq!(
reward_payment.token_identifier,
managed_token_id!(LOCKED_REWARD_TOKEN_ID)
);
assert_eq!(reward_payment.token_nonce, locked_reward_nonce);

result = reward_payment.amount.to_u64().unwrap();
})
.assert_ok();

result
}

pub fn check_farm_token_supply(&mut self, expected_farm_token_supply: u64) {
let b_mock = &mut self.b_mock;
b_mock
.execute_query(&self.farm_wrapper, |sc| {
let actual_farm_supply = sc.farm_token_supply().get();
assert_eq!(
managed_biguint!(expected_farm_token_supply),
actual_farm_supply
);
})
.assert_ok();
}

pub fn check_farm_rps(&mut self, expected_amount: u64) {
let b_mock = &mut self.b_mock;
b_mock
.execute_query(&self.farm_wrapper, |sc| {
let current_rps = sc.reward_per_share().get();
assert_eq!(managed_biguint!(expected_amount), current_rps);
})
.assert_ok();
}
}
233 changes: 233 additions & 0 deletions dex/farm-with-locked-rewards/tests/farm_with_locked_rewards_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![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 @@ -335,3 +337,234 @@ fn total_farm_position_claim_with_locked_rewards_test() {
None,
);
}

#[test]
fn claim_only_boosted_rewards_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 = 100_000_000;
let first_user = farm_setup.first_user.clone();
farm_setup.set_user_energy(&first_user, 1_000, 2, 1);
farm_setup.enter_farm(&first_user, farm_in_amount);

farm_setup.check_farm_token_supply(farm_in_amount);
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, 2, 1);

farm_setup.check_farm_rps(75_000_000u64);

// 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 boosted_rewards = 2_500;
let second_week_received_reward_amt =
farm_setup.claim_boosted_rewards_for_user(&first_user, &first_user, 1);

assert_eq!(second_week_received_reward_amt, boosted_rewards);
farm_setup.check_farm_rps(150_000_000u64);

// 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);
let third_week_received_reward_amt =
farm_setup.claim_boosted_rewards_for_user(&first_user, &first_user, 1);

assert_eq!(third_week_received_reward_amt, boosted_rewards);
farm_setup.check_farm_rps(225_000_000u64);

farm_setup.b_mock.check_nft_balance::<Empty>(
&first_user,
LOCKED_REWARD_TOKEN_ID,
1,
&rust_biguint!(boosted_rewards * 2),
None,
);
}

#[test]
fn claim_rewards_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 = 100_000_000;
let first_user = farm_setup.first_user.clone();
farm_setup.set_user_energy(&first_user, 1_000, 2, 1);
farm_setup.enter_farm(&first_user, farm_in_amount);

farm_setup.check_farm_token_supply(farm_in_amount);
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, 2, 1);

farm_setup.check_farm_rps(75_000_000u64);
let base_rewards_per_week = 7_500;
let boosted_rewards_per_week = 2_500;
let total_rewards_per_week = base_rewards_per_week + boosted_rewards_per_week;

// 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 second_week_received_reward_amt = farm_setup.claim_rewards(&first_user, 1, farm_in_amount);

assert_eq!(
second_week_received_reward_amt,
total_rewards_per_week + base_rewards_per_week
);
farm_setup.check_farm_rps(150_000_000u64);

// 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);
let third_week_received_reward_amt = farm_setup.claim_rewards(&first_user, 3, farm_in_amount);

assert_eq!(third_week_received_reward_amt, total_rewards_per_week);
farm_setup.check_farm_rps(225_000_000u64);

farm_setup.b_mock.check_nft_balance::<Empty>(
&first_user,
LOCKED_REWARD_TOKEN_ID,
1,
&rust_biguint!(total_rewards_per_week * 2 + base_rewards_per_week),
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,
);
}
Loading

0 comments on commit 88dd12b

Please sign in to comment.