Skip to content

Commit

Permalink
some more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Oct 24, 2024
1 parent 561268b commit 7bcbc0e
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion dex/farm-with-locked-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub trait Farm:
}

#[upgrade]
fn upgrade(&self) {
fn upgrade(&self, timestamp_oracle_address: ManagedAddress) {
if self.first_week_start_epoch().is_empty() {
let current_epoch = self.blockchain().get_block_epoch();
self.first_week_start_epoch().set(current_epoch);
Expand All @@ -80,6 +80,8 @@ pub trait Farm:
// Farm position migration code
let farm_token_mapper = self.farm_token();
self.try_set_farm_position_migration_nonce(farm_token_mapper);

self.set_timestamp_oracle_address(timestamp_oracle_address);
}

#[payable("*")]
Expand All @@ -104,6 +106,8 @@ pub trait Farm:
self.send_payment_non_zero(&caller, &new_farm_token);
self.update_energy_and_progress(&orig_caller);

self.update_start_of_epoch_timestamp();

(new_farm_token, boosted_rewards_payment).into()
}

Expand All @@ -128,6 +132,8 @@ pub trait Farm:
orig_caller,
);

self.update_start_of_epoch_timestamp();

(claim_rewards_result.new_farm_token, locked_rewards_payment).into()
}

Expand Down Expand Up @@ -158,6 +164,8 @@ pub trait Farm:

self.clear_user_energy_if_needed(&orig_caller);

self.update_start_of_epoch_timestamp();

(exit_farm_result.farming_tokens, locked_rewards_payment).into()
}

Expand All @@ -183,6 +191,8 @@ pub trait Farm:
orig_caller,
);

self.update_start_of_epoch_timestamp();

(merged_farm_token, locked_rewards_payment).into()
}

Expand Down Expand Up @@ -225,6 +235,8 @@ pub trait Farm:
let boosted_rewards = self.claim_only_boosted_payment(user);
self.set_farm_supply_for_current_week(&storage_cache.farm_token_supply);

// Don't need to call update here too, the internal functions call it already

self.send_to_lock_contract_non_zero(
self.reward_token_id().get(),
boosted_rewards,
Expand All @@ -237,18 +249,24 @@ pub trait Farm:
fn start_produce_rewards_endpoint(&self) {
self.require_caller_has_admin_permissions();
self.start_produce_rewards();

self.update_start_of_epoch_timestamp();
}

#[endpoint(endProduceRewards)]
fn end_produce_rewards_endpoint(&self) {
self.require_caller_has_admin_permissions();
self.end_produce_rewards::<NoMintWrapper<Self>>();

self.update_start_of_epoch_timestamp();
}

#[endpoint(setPerBlockRewardAmount)]
fn set_per_block_rewards_endpoint(&self, per_block_amount: BigUint) {
self.require_caller_has_admin_permissions();
self.set_per_block_rewards::<NoMintWrapper<Self>>(per_block_amount);

self.update_start_of_epoch_timestamp();
}

#[endpoint(setBoostedYieldsRewardsPercentage)]
Expand All @@ -260,6 +278,8 @@ pub trait Farm:
NoMintWrapper::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

self.boosted_yields_rewards_percentage().set(percentage);

self.update_start_of_epoch_timestamp();
}

#[view(calculateRewardsForGivenPosition)]
Expand Down
3 changes: 3 additions & 0 deletions dex/farm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ path = "../../energy-integration/energy-factory-mock"

[dev-dependencies.simple-lock]
path = "../../locked-asset/simple-lock"

[dev-dependencies.timestamp-oracle]
path = "../../energy-integration/timestamp-oracle"
24 changes: 23 additions & 1 deletion dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub trait Farm:
}

#[upgrade]
fn upgrade(&self) {
fn upgrade(&self, timestamp_oracle_address: ManagedAddress) {
if self.first_week_start_epoch().is_empty() {
let current_epoch = self.blockchain().get_block_epoch();
self.first_week_start_epoch().set(current_epoch);
Expand All @@ -82,6 +82,8 @@ pub trait Farm:
// Farm position migration code
let farm_token_mapper = self.farm_token();
self.try_set_farm_position_migration_nonce(farm_token_mapper);

self.set_timestamp_oracle_address(timestamp_oracle_address);
}

#[payable("*")]
Expand All @@ -104,6 +106,8 @@ pub trait Farm:
self.send_payment_non_zero(&caller, &boosted_rewards_payment);
self.update_energy_and_progress(&orig_caller);

self.update_start_of_epoch_timestamp();

(new_farm_token, boosted_rewards_payment).into()
}

Expand All @@ -122,6 +126,8 @@ pub trait Farm:
self.send_payment_non_zero(&caller, &claim_rewards_result.new_farm_token);
self.send_payment_non_zero(&caller, &claim_rewards_result.rewards);

self.update_start_of_epoch_timestamp();

claim_rewards_result.into()
}

Expand All @@ -140,6 +146,8 @@ pub trait Farm:
self.send_payment_non_zero(&caller, &output_farm_token_payment);
self.update_energy_and_progress(&orig_caller);

self.update_start_of_epoch_timestamp();

output_farm_token_payment
}

Expand All @@ -160,6 +168,8 @@ pub trait Farm:
self.send_payment_non_zero(&caller, &exit_farm_result.rewards);
self.clear_user_energy_if_needed(&orig_caller);

self.update_start_of_epoch_timestamp();

(exit_farm_result.farming_tokens, exit_farm_result.rewards).into()
}

Expand All @@ -182,6 +192,8 @@ pub trait Farm:
self.send_payment_non_zero(&caller, &merged_farm_token);
self.send_payment_non_zero(&caller, &boosted_rewards_payment);

self.update_start_of_epoch_timestamp();

(merged_farm_token, boosted_rewards_payment).into()
}

Expand Down Expand Up @@ -228,25 +240,33 @@ pub trait Farm:

self.send_payment_non_zero(user, &boosted_rewards_payment);

// Don't need to call update here too, the internal functions call it already

boosted_rewards_payment
}

#[endpoint(startProduceRewards)]
fn start_produce_rewards_endpoint(&self) {
self.require_caller_has_admin_permissions();
self.start_produce_rewards();

self.update_start_of_epoch_timestamp();
}

#[endpoint(endProduceRewards)]
fn end_produce_rewards_endpoint(&self) {
self.require_caller_has_admin_permissions();
self.end_produce_rewards::<Wrapper<Self>>();

self.update_start_of_epoch_timestamp();
}

#[endpoint(setPerBlockRewardAmount)]
fn set_per_block_rewards_endpoint(&self, per_block_amount: BigUint) {
self.require_caller_has_admin_permissions();
self.set_per_block_rewards::<Wrapper<Self>>(per_block_amount);

self.update_start_of_epoch_timestamp();
}

#[endpoint(setBoostedYieldsRewardsPercentage)]
Expand All @@ -258,6 +278,8 @@ pub trait Farm:
Wrapper::<Self>::generate_aggregated_rewards(self, &mut storage_cache);

self.boosted_yields_rewards_percentage().set(percentage);

self.update_start_of_epoch_timestamp();
}

#[view(calculateRewardsForGivenPosition)]
Expand Down
36 changes: 32 additions & 4 deletions dex/farm/tests/farm_setup/multi_user_farm_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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::TimestampOracle;
use week_timekeeping::Epoch;
use weekly_rewards_splitting::update_claim_progress_energy::UpdateClaimProgressEnergyModule;

Expand Down Expand Up @@ -54,11 +55,16 @@ pub struct NonceAmountPair {
pub amount: u64,
}

pub struct MultiUserFarmSetup<FarmObjBuilder, EnergyFactoryBuilder, EnergyUpdateObjBuilder>
where
pub struct MultiUserFarmSetup<
FarmObjBuilder,
EnergyFactoryBuilder,
EnergyUpdateObjBuilder,
TimestampOracleObjBuilder,
> where
FarmObjBuilder: 'static + Copy + Fn() -> farm::ContractObj<DebugApi>,
EnergyFactoryBuilder: 'static + Copy + Fn() -> energy_factory_mock::ContractObj<DebugApi>,
EnergyUpdateObjBuilder: 'static + Copy + Fn() -> energy_update::ContractObj<DebugApi>,
TimestampOracleObjBuilder: 'static + Copy + Fn() -> timestamp_oracle::ContractObj<DebugApi>,
{
pub b_mock: BlockchainStateWrapper,
pub owner: Address,
Expand All @@ -71,19 +77,28 @@ where
ContractObjWrapper<energy_factory_mock::ContractObj<DebugApi>, EnergyFactoryBuilder>,
pub eu_wrapper:
ContractObjWrapper<energy_update::ContractObj<DebugApi>, EnergyUpdateObjBuilder>,
pub timestamp_oracle_wrapper:
ContractObjWrapper<timestamp_oracle::ContractObj<DebugApi>, TimestampOracleObjBuilder>,
}

impl<FarmObjBuilder, EnergyFactoryBuilder, EnergyUpdateObjBuilder>
MultiUserFarmSetup<FarmObjBuilder, EnergyFactoryBuilder, EnergyUpdateObjBuilder>
impl<FarmObjBuilder, EnergyFactoryBuilder, EnergyUpdateObjBuilder, TimestampOracleObjBuilder>
MultiUserFarmSetup<
FarmObjBuilder,
EnergyFactoryBuilder,
EnergyUpdateObjBuilder,
TimestampOracleObjBuilder,
>
where
FarmObjBuilder: 'static + Copy + Fn() -> farm::ContractObj<DebugApi>,
EnergyFactoryBuilder: 'static + Copy + Fn() -> energy_factory_mock::ContractObj<DebugApi>,
EnergyUpdateObjBuilder: 'static + Copy + Fn() -> energy_update::ContractObj<DebugApi>,
TimestampOracleObjBuilder: 'static + Copy + Fn() -> timestamp_oracle::ContractObj<DebugApi>,
{
pub fn new(
farm_builder: FarmObjBuilder,
energy_factory_builder: EnergyFactoryBuilder,
eu_builder: EnergyUpdateObjBuilder,
timestamp_oracle_builder: TimestampOracleObjBuilder,
) -> Self {
let rust_zero = rust_biguint!(0);
let mut b_mock = BlockchainStateWrapper::new();
Expand All @@ -108,6 +123,18 @@ where
})
.assert_ok();

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

b_mock
.execute_tx(&owner, &farm_wrapper, &rust_zero, |sc| {
let reward_token_id = managed_token_id!(REWARD_TOKEN_ID);
Expand Down Expand Up @@ -187,6 +214,7 @@ where
farm_wrapper,
energy_factory_wrapper,
eu_wrapper,
timestamp_oracle_wrapper,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ pub trait CustomRewardLogicModule:
+ energy_query::EnergyQueryModule
+ utils::UtilsModule
{
#[only_owner]
#[endpoint(setTimestampOracleAddress)]
fn set_timestamp_oracle_address(&self, sc_address: ManagedAddress) {
self.require_caller_has_admin_permissions();
self.require_sc_address(&sc_address);

self.timestamp_oracle_address().set(sc_address);
}
Expand Down Expand Up @@ -161,6 +162,11 @@ pub trait CustomRewardLogicModule:
*user_reward = interpolated_reward;
}

#[inline]
fn update_start_of_epoch_timestamp(&self) {
let _ = self.get_start_of_epoch_timestamp();
}

fn get_start_of_epoch_timestamp(&self) -> Timestamp {
let timestamp_oracle_addr = self.timestamp_oracle_address().get();
self.timestamp_oracle_proxy_obj(timestamp_oracle_addr)
Expand Down
2 changes: 2 additions & 0 deletions farm-staking/farm-staking/src/claim_stake_farm_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ pub trait ClaimStakeFarmRewardsModule:
claim_result.storage_cache,
);

self.update_start_of_epoch_timestamp();

(virtual_farm_token.payment, claim_result.rewards).into()
}
}
2 changes: 2 additions & 0 deletions farm-staking/farm-staking/src/compound_stake_farm_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub trait CompoundStakeFarmRewardsModule:
compound_result.storage_cache,
);

self.update_start_of_epoch_timestamp();

new_farm_token
}
}
Loading

0 comments on commit 7bcbc0e

Please sign in to comment.