Skip to content

Commit

Permalink
impl fixes + some test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Oct 28, 2024
1 parent ae1b782 commit d2324ad
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 93 deletions.
11 changes: 11 additions & 0 deletions dex/farm/tests/farm_multi_user_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ fn farm_with_boosted_yields_test() {
farm_setup.set_boosted_yields_rewards_percentage(BOOSTED_YIELDS_PERCENTAGE);
farm_setup.set_boosted_yields_factors();
farm_setup.b_mock.set_block_epoch(2);
farm_setup
.b_mock
.set_block_timestamp(2 * TIMESTAMP_PER_EPOCH);

// first user enter farm
let first_farm_token_amount = 100_000_000;
Expand All @@ -147,6 +150,10 @@ fn farm_with_boosted_yields_test() {

// random tx on end of week 1, to cummulate rewards
farm_setup.b_mock.set_block_epoch(6);
farm_setup
.b_mock
.set_block_timestamp(6 * TIMESTAMP_PER_EPOCH);

farm_setup.set_user_energy(&first_user, 1_000, 6, 1);
farm_setup.set_user_energy(&second_user, 4_000, 6, 1);
farm_setup.set_user_energy(&third_user, 1, 6, 1);
Expand All @@ -155,6 +162,10 @@ fn farm_with_boosted_yields_test() {

// advance 1 week
farm_setup.b_mock.set_block_epoch(10);
farm_setup
.b_mock
.set_block_timestamp(10 * TIMESTAMP_PER_EPOCH);

farm_setup.set_user_energy(&first_user, 1_000, 10, 1);
farm_setup.set_user_energy(&second_user, 4_000, 10, 1);

Expand Down
30 changes: 25 additions & 5 deletions dex/farm/tests/farm_review_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ use multiversx_sc_scenario::rust_biguint;
#[test]
fn test_overview() {
let per_block_reward_amount = rust_biguint!(100);
let mut farm_setup = FarmRewardsDistrSetup::new(farm::contract_obj, per_block_reward_amount);
let mut farm_setup = FarmRewardsDistrSetup::new(
farm::contract_obj,
per_block_reward_amount,
timestamp_oracle::contract_obj,
);
let alice = farm_setup.new_address_with_lp_tokens(rust_biguint!(5_000));
let bob = farm_setup.new_address_with_lp_tokens(rust_biguint!(5_000));
let eve = farm_setup.new_address_with_lp_tokens(rust_biguint!(5_000));
Expand Down Expand Up @@ -63,7 +67,11 @@ fn test_overview() {
#[test]
fn test_overview_but_changes_in_per_reward_block() {
let per_block_reward_amount = rust_biguint!(100);
let mut farm_setup = FarmRewardsDistrSetup::new(farm::contract_obj, per_block_reward_amount);
let mut farm_setup = FarmRewardsDistrSetup::new(
farm::contract_obj,
per_block_reward_amount,
timestamp_oracle::contract_obj,
);
let alice = farm_setup.new_address_with_lp_tokens(rust_biguint!(5_000));
let bob = farm_setup.new_address_with_lp_tokens(rust_biguint!(5_000));
let eve = farm_setup.new_address_with_lp_tokens(rust_biguint!(5_000));
Expand Down Expand Up @@ -136,7 +144,11 @@ fn exp18(value: u64) -> RustBigUint {
#[test]
fn test_overview_realistic_numbers() {
let per_block_reward_amount = exp18(100);
let mut farm_setup = FarmRewardsDistrSetup::new(farm::contract_obj, per_block_reward_amount);
let mut farm_setup = FarmRewardsDistrSetup::new(
farm::contract_obj,
per_block_reward_amount,
timestamp_oracle::contract_obj,
);
let alice = farm_setup.new_address_with_lp_tokens(exp18(5_000));
let bob = farm_setup.new_address_with_lp_tokens(exp18(5_000));
let eve = farm_setup.new_address_with_lp_tokens(exp18(5_000));
Expand Down Expand Up @@ -206,7 +218,11 @@ fn exp21(value: u64) -> RustBigUint {
#[test]
fn test_billion_to_trillion() {
let per_block_reward_amount = exp21(100);
let mut farm_setup = FarmRewardsDistrSetup::new(farm::contract_obj, per_block_reward_amount);
let mut farm_setup = FarmRewardsDistrSetup::new(
farm::contract_obj,
per_block_reward_amount,
timestamp_oracle::contract_obj,
);
let alice = farm_setup.new_address_with_lp_tokens(exp21(5_000));
let bob = farm_setup.new_address_with_lp_tokens(exp21(5_000));
let eve = farm_setup.new_address_with_lp_tokens(exp21(5_000));
Expand Down Expand Up @@ -272,7 +288,11 @@ fn test_billion_to_trillion() {
#[test]
fn test_rv_earn_twice() {
let per_block_reward_amount = rust_biguint!(100);
let mut farm_setup = FarmRewardsDistrSetup::new(farm::contract_obj, per_block_reward_amount);
let mut farm_setup = FarmRewardsDistrSetup::new(
farm::contract_obj,
per_block_reward_amount,
timestamp_oracle::contract_obj,
);
let alice = farm_setup.new_address_with_lp_tokens(rust_biguint!(5_000));
let bob = farm_setup.new_address_with_lp_tokens(rust_biguint!(5_000));
farm_setup.step(
Expand Down
63 changes: 47 additions & 16 deletions dex/farm/tests/farm_setup/farm_rewards_distr_setup.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#![allow(dead_code)]

use farm_boosted_yields::custom_reward_logic::CustomRewardLogicModule;
use multiversx_sc::codec::multi_types::OptionalValue;
use multiversx_sc::storage::mappers::StorageTokenWrapper;
use multiversx_sc::types::{Address, BigUint, EsdtLocalRole, ManagedAddress, MultiValueEncoded};
use multiversx_sc_scenario::managed_address;
use multiversx_sc_scenario::{
managed_biguint, managed_token_id, rust_biguint, whitebox_legacy::TxTokenTransfer,
whitebox_legacy::*, DebugApi,
Expand All @@ -14,6 +16,10 @@ use farm::*;
use farm_token::FarmTokenModule;
use pausable::{PausableModule, State};
use rewards::*;
use timestamp_oracle::epoch_to_timestamp::EpochToTimestampModule;
use timestamp_oracle::TimestampOracle;

use super::multi_user_farm_setup::TIMESTAMP_PER_EPOCH;

pub const FARM_WASM_PATH: &str = "farm/output/farm.wasm";

Expand Down Expand Up @@ -50,34 +56,55 @@ impl Expected {
}
}

pub struct FarmRewardsDistrSetup<FarmObjBuilder>
pub struct FarmRewardsDistrSetup<FarmObjBuilder, TimestampOracleObjBuilder>
where
FarmObjBuilder: 'static + Copy + Fn() -> farm::ContractObj<DebugApi>,
TimestampOracleObjBuilder: 'static + Copy + Fn() -> timestamp_oracle::ContractObj<DebugApi>,
{
pub blockchain_wrapper: BlockchainStateWrapper,
pub owner_address: Address,
pub farm_wrapper: ContractObjWrapper<farm::ContractObj<DebugApi>, FarmObjBuilder>,
pub timestamp_oracle_wrapper:
ContractObjWrapper<timestamp_oracle::ContractObj<DebugApi>, TimestampOracleObjBuilder>,
}

impl<FarmObjBuilder> FarmRewardsDistrSetup<FarmObjBuilder>
impl<FarmObjBuilder, TimestampOracleObjBuilder>
FarmRewardsDistrSetup<FarmObjBuilder, TimestampOracleObjBuilder>
where
FarmObjBuilder: 'static + Copy + Fn() -> farm::ContractObj<DebugApi>,
TimestampOracleObjBuilder: 'static + Copy + Fn() -> timestamp_oracle::ContractObj<DebugApi>,
{
pub fn new(farm_builder: FarmObjBuilder, per_block_reward_amount: RustBigUint) -> Self {
pub fn new(
farm_builder: FarmObjBuilder,
per_block_reward_amount: RustBigUint,
timestamp_oracle_builder: TimestampOracleObjBuilder,
) -> Self {
let rust_zero = rust_biguint!(0u64);
let mut blockchain_wrapper = BlockchainStateWrapper::new();
let owner_addr = blockchain_wrapper.create_user_account(&rust_zero);
let farm_wrapper = blockchain_wrapper.create_sc_account(
let mut b_mock = BlockchainStateWrapper::new();
let owner = b_mock.create_user_account(&rust_zero);
let farm_wrapper =
b_mock.create_sc_account(&rust_zero, Some(&owner), farm_builder, FARM_WASM_PATH);

let timestamp_oracle_wrapper = b_mock.create_sc_account(
&rust_zero,
Some(&owner_addr),
farm_builder,
FARM_WASM_PATH,
Some(&owner),
timestamp_oracle_builder,
"timestamp oracle",
);
b_mock
.execute_tx(&owner, &timestamp_oracle_wrapper, &rust_zero, |sc| {
sc.init(0);

for i in 0..=21 {
sc.set_start_timestamp_for_epoch(i, i * TIMESTAMP_PER_EPOCH + 1);
}
})
.assert_ok();

// init farm contract

blockchain_wrapper
.execute_tx(&owner_addr, &farm_wrapper, &rust_zero, |sc| {
b_mock
.execute_tx(&owner, &farm_wrapper, &rust_zero, |sc| {
let reward_token_id = managed_token_id!(MEX_TOKEN_ID);
let farming_token_id = managed_token_id!(LP_TOKEN_ID);
let division_safety_constant = managed_biguint!(DIVISION_SAFETY_CONSTANT);
Expand All @@ -98,6 +125,9 @@ where

sc.state().set(State::Active);
sc.produce_rewards_enabled().set(true);
sc.set_timestamp_oracle_address(managed_address!(
timestamp_oracle_wrapper.address_ref()
));
})
.assert_ok();

Expand All @@ -106,30 +136,31 @@ where
EsdtLocalRole::NftAddQuantity,
EsdtLocalRole::NftBurn,
];
blockchain_wrapper.set_esdt_local_roles(
b_mock.set_esdt_local_roles(
farm_wrapper.address_ref(),
FARM_TOKEN_ID,
&farm_token_roles[..],
);

let farming_token_roles = [EsdtLocalRole::Burn];
blockchain_wrapper.set_esdt_local_roles(
b_mock.set_esdt_local_roles(
farm_wrapper.address_ref(),
LP_TOKEN_ID,
&farming_token_roles[..],
);

let reward_token_roles = [EsdtLocalRole::Mint];
blockchain_wrapper.set_esdt_local_roles(
b_mock.set_esdt_local_roles(
farm_wrapper.address_ref(),
MEX_TOKEN_ID,
&reward_token_roles[..],
);

FarmRewardsDistrSetup {
blockchain_wrapper,
owner_address: owner_addr,
blockchain_wrapper: b_mock,
owner_address: owner,
farm_wrapper,
timestamp_oracle_wrapper,
}
}

Expand Down
11 changes: 10 additions & 1 deletion dex/farm/tests/farm_setup/multi_user_farm_setup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(dead_code)]

use common_structs::FarmTokenAttributes;
use common_structs::{FarmTokenAttributes, Timestamp};
use config::ConfigModule;
use farm_boosted_yields::custom_reward_logic::CustomRewardLogicModule;
use multiversx_sc::codec::multi_types::OptionalValue;
Expand All @@ -23,6 +23,7 @@ use farm_boosted_yields::boosted_yields_factors::BoostedYieldsFactorsModule;
use farm_token::FarmTokenModule;
use pausable::{PausableModule, State};
use sc_whitelist_module::SCWhitelistModule;
use timestamp_oracle::epoch_to_timestamp::EpochToTimestampModule;
use timestamp_oracle::TimestampOracle;
use week_timekeeping::Epoch;
use weekly_rewards_splitting::update_claim_progress_energy::UpdateClaimProgressEnergyModule;
Expand All @@ -40,6 +41,7 @@ pub const USER_REWARDS_ENERGY_CONST: u64 = 3;
pub const USER_REWARDS_FARM_CONST: u64 = 2;
pub const MIN_ENERGY_AMOUNT_FOR_BOOSTED_YIELDS: u64 = 1;
pub const MIN_FARM_AMOUNT_FOR_BOOSTED_YIELDS: u64 = 1;
pub const TIMESTAMP_PER_EPOCH: Timestamp = 1 * 24 * 60 * 60;

Check warning on line 44 in dex/farm/tests/farm_setup/multi_user_farm_setup.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] dex/farm/tests/farm_setup/multi_user_farm_setup.rs#L44

warning: this operation has no effect --> dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44 | 44 | pub const TIMESTAMP_PER_EPOCH: Timestamp = 1 * 24 * 60 * 60; | ^^^^^^ help: consider reducing it to: `24` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op = note: `#[warn(clippy::identity_op)]` on by default
Raw output
dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44:w:warning: this operation has no effect
  --> dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44
   |
44 | pub const TIMESTAMP_PER_EPOCH: Timestamp = 1 * 24 * 60 * 60;
   |                                            ^^^^^^ help: consider reducing it to: `24`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op
   = note: `#[warn(clippy::identity_op)]` on by default


__END__

Check warning on line 44 in dex/farm/tests/farm_setup/multi_user_farm_setup.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] dex/farm/tests/farm_setup/multi_user_farm_setup.rs#L44

warning: this operation has no effect --> dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44 | 44 | pub const TIMESTAMP_PER_EPOCH: Timestamp = 1 * 24 * 60 * 60; | ^^^^^^ help: consider reducing it to: `24` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op = note: `#[warn(clippy::identity_op)]` on by default
Raw output
dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44:w:warning: this operation has no effect
  --> dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44
   |
44 | pub const TIMESTAMP_PER_EPOCH: Timestamp = 1 * 24 * 60 * 60;
   |                                            ^^^^^^ help: consider reducing it to: `24`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op
   = note: `#[warn(clippy::identity_op)]` on by default


__END__

Check warning on line 44 in dex/farm/tests/farm_setup/multi_user_farm_setup.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] dex/farm/tests/farm_setup/multi_user_farm_setup.rs#L44

warning: this operation has no effect --> dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44 | 44 | pub const TIMESTAMP_PER_EPOCH: Timestamp = 1 * 24 * 60 * 60; | ^^^^^^ help: consider reducing it to: `24` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op = note: `#[warn(clippy::identity_op)]` on by default
Raw output
dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44:w:warning: this operation has no effect
  --> dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44
   |
44 | pub const TIMESTAMP_PER_EPOCH: Timestamp = 1 * 24 * 60 * 60;
   |                                            ^^^^^^ help: consider reducing it to: `24`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op
   = note: `#[warn(clippy::identity_op)]` on by default


__END__

Check warning on line 44 in dex/farm/tests/farm_setup/multi_user_farm_setup.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] dex/farm/tests/farm_setup/multi_user_farm_setup.rs#L44

warning: this operation has no effect --> dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44 | 44 | pub const TIMESTAMP_PER_EPOCH: Timestamp = 1 * 24 * 60 * 60; | ^^^^^^ help: consider reducing it to: `24` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op = note: `#[warn(clippy::identity_op)]` on by default
Raw output
dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44:w:warning: this operation has no effect
  --> dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44
   |
44 | pub const TIMESTAMP_PER_EPOCH: Timestamp = 1 * 24 * 60 * 60;
   |                                            ^^^^^^ help: consider reducing it to: `24`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op
   = note: `#[warn(clippy::identity_op)]` on by default


__END__

Check warning on line 44 in dex/farm/tests/farm_setup/multi_user_farm_setup.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] dex/farm/tests/farm_setup/multi_user_farm_setup.rs#L44

warning: this operation has no effect --> dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44 | 44 | pub const TIMESTAMP_PER_EPOCH: Timestamp = 1 * 24 * 60 * 60; | ^^^^^^ help: consider reducing it to: `24` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op = note: `#[warn(clippy::identity_op)]` on by default
Raw output
dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44:w:warning: this operation has no effect
  --> dex/farm/tests/farm_setup/multi_user_farm_setup.rs:44:44
   |
44 | pub const TIMESTAMP_PER_EPOCH: Timestamp = 1 * 24 * 60 * 60;
   |                                            ^^^^^^ help: consider reducing it to: `24`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op
   = note: `#[warn(clippy::identity_op)]` on by default


__END__

pub struct RawFarmTokenAttributes {
pub reward_per_share_bytes: Vec<u8>,
Expand Down Expand Up @@ -131,6 +133,10 @@ where
b_mock
.execute_tx(&owner, &timestamp_oracle_wrapper, &rust_zero, |sc| {
sc.init(0);

for i in 0..=21 {
sc.set_start_timestamp_for_epoch(i, i * TIMESTAMP_PER_EPOCH + 1);
}
})
.assert_ok();

Expand Down Expand Up @@ -159,6 +165,9 @@ where
sc.set_energy_factory_address(managed_address!(
energy_factory_wrapper.address_ref()
));
sc.set_timestamp_oracle_address(managed_address!(
timestamp_oracle_wrapper.address_ref()
));
})
.assert_ok();

Expand Down
Loading

0 comments on commit d2324ad

Please sign in to comment.