Skip to content

Commit

Permalink
Merge pull request #503 from ElrondNetwork/cache_fees_to_energy_contract
Browse files Browse the repository at this point in the history
Cache fees to energy contract
  • Loading branch information
sasurobert authored Oct 20, 2022
2 parents 20cc115 + 793bdb9 commit d486eb9
Show file tree
Hide file tree
Showing 14 changed files with 511 additions and 129 deletions.
1 change: 1 addition & 0 deletions common/common_structs/src/alias_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{LockedAssetTokenAttributesEx, UnlockSchedule};

pub type Nonce = u64;
pub type Epoch = u64;
pub type Week = usize;
pub type PaymentsVec<M> = ManagedVec<M, EsdtTokenPayment<M>>;
pub type UnlockPeriod<M> = UnlockSchedule<M>;
pub type OldLockedTokenAttributes<M> = LockedAssetTokenAttributesEx<M>;
13 changes: 13 additions & 0 deletions common/common_structs/src/wrapper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ impl<M: ManagedTypeApi> TokenPair<M> {
}
}

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, TypeAbi)]
pub struct NonceAmountPair<M: ManagedTypeApi> {
pub nonce: u64,
pub amount: BigUint<M>,
}

impl<M: ManagedTypeApi> NonceAmountPair<M> {
#[inline]
pub fn new(nonce: u64, amount: BigUint<M>) -> Self {
NonceAmountPair { nonce, amount }
}
}

#[derive(
TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone, Debug,
)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
elrond_wasm::imports!();

use common_types::TokenAmountPairsVec;
use common_types::PaymentsVec;
use week_timekeeping::Week;

use crate::{events, ClaimProgress};
Expand All @@ -18,7 +18,7 @@ pub trait WeeklyRewardsSplittingTraitsModule {
&self,
module: &Self::WeeklyRewardsSplittingMod,
week: Week,
) -> TokenAmountPairsVec<<Self::WeeklyRewardsSplittingMod as ContractBase>::Api> {
) -> PaymentsVec<<Self::WeeklyRewardsSplittingMod as ContractBase>::Api> {
let total_rewards_mapper = module.total_rewards_for_week(week);
if total_rewards_mapper.is_empty() {
let total_rewards = self.collect_rewards_for_week(module, week);
Expand All @@ -34,7 +34,7 @@ pub trait WeeklyRewardsSplittingTraitsModule {
&self,
module: &Self::WeeklyRewardsSplittingMod,
week: Week,
) -> TokenAmountPairsVec<<Self::WeeklyRewardsSplittingMod as ContractBase>::Api>;
) -> PaymentsVec<<Self::WeeklyRewardsSplittingMod as ContractBase>::Api>;

fn get_claim_progress_mapper(
&self,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
elrond_wasm::imports!();

use common_types::{TokenAmountPair, Week};
use common_types::Week;
use energy_query::Energy;
use week_timekeeping::EPOCHS_IN_WEEK;

Expand Down Expand Up @@ -73,7 +73,7 @@ pub trait WeeklyRewardsGlobalInfo: crate::events::WeeklyRewardsSplittingEventsMo
fn total_rewards_for_week(
&self,
week: Week,
) -> SingleValueMapper<ManagedVec<TokenAmountPair<Self::Api>>>;
) -> SingleValueMapper<ManagedVec<EsdtTokenPayment<Self::Api>>>;

#[view(getTotalEnergyForWeek)]
#[storage_mapper("totalEnergyForWeek")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod events;
pub mod global_info;

use base_impl::WeeklyRewardsSplittingTraitsModule;
use common_types::{PaymentsVec, TokenAmountPairsVec};
use common_types::PaymentsVec;
use energy_query::Energy;
use week_timekeeping::{Week, EPOCHS_IN_WEEK};

Expand Down Expand Up @@ -138,7 +138,7 @@ pub trait WeeklyRewardsSplittingModule:
&self,
week: Week,
energy_amount: &BigUint,
total_rewards: &TokenAmountPairsVec<Self::Api>,
total_rewards: &PaymentsVec<Self::Api>,
) -> PaymentsVec<Self::Api> {
let mut user_rewards = ManagedVec::new();
if energy_amount == &0 {
Expand All @@ -149,7 +149,11 @@ pub trait WeeklyRewardsSplittingModule:
for weekly_reward in total_rewards {
let reward_amount = weekly_reward.amount * energy_amount / &total_energy;
if reward_amount > 0 {
user_rewards.push(EsdtTokenPayment::new(weekly_reward.token, 0, reward_amount));
user_rewards.push(EsdtTokenPayment::new(
weekly_reward.token_identifier,
0,
reward_amount,
));
}
}

Expand Down
6 changes: 3 additions & 3 deletions energy-integration/farm-boosted-yields/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ elrond_wasm::derive_imports!();

use core::marker::PhantomData;

use common_types::{Nonce, TokenAmountPair, TokenAmountPairsVec};
use common_types::{Nonce, PaymentsVec};
use week_timekeeping::Week;
use weekly_rewards_splitting::{base_impl::WeeklyRewardsSplittingTraitsModule, ClaimProgress};

Expand Down Expand Up @@ -121,13 +121,13 @@ where
&self,
module: &Self::WeeklyRewardsSplittingMod,
week: Week,
) -> TokenAmountPairsVec<<Self::WeeklyRewardsSplittingMod as ContractBase>::Api> {
) -> PaymentsVec<<Self::WeeklyRewardsSplittingMod as ContractBase>::Api> {
let reward_token_id = module.reward_token_id().get();
let rewards_mapper = module.accumulated_rewards_for_week(week);
let total_rewards = rewards_mapper.get();
rewards_mapper.clear();

ManagedVec::from_single_item(TokenAmountPair::new(reward_token_id, total_rewards))
ManagedVec::from_single_item(EsdtTokenPayment::new(reward_token_id, 0, total_rewards))
}

fn get_claim_progress_mapper(
Expand Down
54 changes: 45 additions & 9 deletions energy-integration/fees-collector/src/fees_accumulation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
elrond_wasm::imports!();
elrond_wasm::derive_imports!();

use common_types::TokenAmountPair;
use week_timekeeping::Week;

#[elrond_wasm::module]
Expand All @@ -21,32 +20,47 @@ pub trait FeesAccumulationModule:
"Only known contracts can deposit"
);

let (payment_token, payment_amount) = self.call_value().single_fungible_esdt();
let payment = self.call_value().single_esdt();
require!(
self.known_tokens().contains(&payment_token),
self.known_tokens().contains(&payment.token_identifier),
"Invalid payment token"
);

let current_week = self.get_current_week();
self.accumulated_fees(current_week, &payment_token)
.update(|amt| *amt += &payment_amount);
if payment.token_nonce == 0 {
self.accumulated_fees(current_week, &payment.token_identifier)
.update(|amt| *amt += &payment.amount);
} else {
self.accumulated_locked_fees(current_week, &payment.token_identifier)
.update(|locked_fees| locked_fees.push(payment.clone()));
}

self.emit_deposit_swap_fees_event(caller, current_week, payment_token, payment_amount);
self.emit_deposit_swap_fees_event(
caller,
current_week,
payment.token_identifier,
payment.amount,
);
}

fn collect_accumulated_fees_for_week(
&self,
week: Week,
) -> ManagedVec<TokenAmountPair<Self::Api>> {
) -> ManagedVec<EsdtTokenPayment<Self::Api>> {
let mut results = ManagedVec::new();
let all_tokens = self.all_tokens().get();
for token in &all_tokens {
let opt_accumulated_fees = self.get_and_clear_acccumulated_fees(week, &token);
if let Some(accumulated_fees) = opt_accumulated_fees {
results.push(TokenAmountPair::new(token, accumulated_fees));
results.push(EsdtTokenPayment::new(token.clone(), 0, accumulated_fees));
}
}

let opt_accumulated_locked_fees =
self.get_and_clear_acccumulated_locked_fees(week, &token);
if let Some(accumulated_locked_fees) = opt_accumulated_locked_fees {
results.append_vec(accumulated_locked_fees);
}
}
results
}

Expand All @@ -59,7 +73,21 @@ pub trait FeesAccumulationModule:
let value = mapper.get();
if value > 0 {
mapper.clear();
Some(value)
} else {
None
}
}

fn get_and_clear_acccumulated_locked_fees(
&self,
week: Week,
token: &TokenIdentifier,
) -> Option<ManagedVec<EsdtTokenPayment<Self::Api>>> {
let mapper = self.accumulated_locked_fees(week, token);
let value = mapper.get();
if !value.is_empty() {
mapper.clear();
Some(value)
} else {
None
Expand All @@ -69,4 +97,12 @@ pub trait FeesAccumulationModule:
#[view(getAccumulatedFees)]
#[storage_mapper("accumulatedFees")]
fn accumulated_fees(&self, week: Week, token: &TokenIdentifier) -> SingleValueMapper<BigUint>;

#[view(getAccumulatedLockedFees)]
#[storage_mapper("accumulatedLockedFees")]
fn accumulated_locked_fees(
&self,
week: Week,
token: &TokenIdentifier<Self::Api>,
) -> SingleValueMapper<ManagedVec<EsdtTokenPayment<Self::Api>>>;
}
6 changes: 3 additions & 3 deletions energy-integration/fees-collector/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

elrond_wasm::imports!();

use common_types::{PaymentsVec, TokenAmountPair, TokenAmountPairsVec, Week};
use common_types::{PaymentsVec, Week};
use core::marker::PhantomData;
use energy_query::Energy;
use weekly_rewards_splitting::base_impl::WeeklyRewardsSplittingTraitsModule;
Expand Down Expand Up @@ -124,13 +124,13 @@ where
&self,
module: &Self::WeeklyRewardsSplittingMod,
week: Week,
) -> TokenAmountPairsVec<<Self::WeeklyRewardsSplittingMod as ContractBase>::Api> {
) -> PaymentsVec<<Self::WeeklyRewardsSplittingMod as ContractBase>::Api> {
let mut results = ManagedVec::new();
let all_tokens = module.all_tokens().get();
for token in &all_tokens {
let opt_accumulated_fees = module.get_and_clear_acccumulated_fees(week, &token);
if let Some(accumulated_fees) = opt_accumulated_fees {
results.push(TokenAmountPair::new(token, accumulated_fees));
results.push(EsdtTokenPayment::new(token, 0, accumulated_fees));
}
}

Expand Down
58 changes: 31 additions & 27 deletions energy-integration/fees-collector/tests/fees_collector_rust_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
mod fees_collector_test_setup;

use common_types::TokenAmountPair;
use elrond_wasm::{
elrond_codec::multi_types::OptionalValue,
types::{BigInt, ManagedVec, MultiValueEncoded, OperationCompletionStatus},
types::{BigInt, EsdtTokenPayment, ManagedVec, MultiValueEncoded, OperationCompletionStatus},
};
use elrond_wasm_debug::{managed_address, managed_biguint, managed_token_id, rust_biguint};
use elrond_wasm_modules::pause::PauseModule;
Expand Down Expand Up @@ -321,15 +320,16 @@ fn claim_second_week_test() {
.b_mock
.execute_query(&fc_setup.fc_wrapper, |sc| {
let mut expected_total_rewards = ManagedVec::new();
expected_total_rewards.push(TokenAmountPair {
token: managed_token_id!(FIRST_TOKEN_ID),
amount: managed_biguint!(USER_BALANCE),
});
expected_total_rewards.push(TokenAmountPair {
token: managed_token_id!(SECOND_TOKEN_ID),
amount: managed_biguint!(USER_BALANCE / 2),
});

expected_total_rewards.push(EsdtTokenPayment::new(
managed_token_id!(FIRST_TOKEN_ID),
0,
managed_biguint!(USER_BALANCE),
));
expected_total_rewards.push(EsdtTokenPayment::new(
managed_token_id!(SECOND_TOKEN_ID),
0,
managed_biguint!(USER_BALANCE / 2),
));
assert_eq!(expected_total_rewards, sc.total_rewards_for_week(1).get());
})
.assert_ok();
Expand Down Expand Up @@ -366,14 +366,16 @@ fn claim_second_week_test() {
);

let mut expected_total_rewards = ManagedVec::new();
expected_total_rewards.push(TokenAmountPair {
token: managed_token_id!(FIRST_TOKEN_ID),
amount: managed_biguint!(USER_BALANCE),
});
expected_total_rewards.push(TokenAmountPair {
token: managed_token_id!(SECOND_TOKEN_ID),
amount: managed_biguint!(USER_BALANCE / 2),
});
expected_total_rewards.push(EsdtTokenPayment::new(
managed_token_id!(FIRST_TOKEN_ID),
0,
managed_biguint!(USER_BALANCE),
));
expected_total_rewards.push(EsdtTokenPayment::new(
managed_token_id!(SECOND_TOKEN_ID),
0,
managed_biguint!(USER_BALANCE / 2),
));
assert_eq!(sc.total_rewards_for_week(1).get(), expected_total_rewards);

// first user's new energy is added to week 2
Expand Down Expand Up @@ -426,14 +428,16 @@ fn claim_second_week_test() {
.b_mock
.execute_query(&fc_setup.fc_wrapper, |sc| {
let mut expected_total_rewards = ManagedVec::new();
expected_total_rewards.push(TokenAmountPair {
token: managed_token_id!(FIRST_TOKEN_ID),
amount: managed_biguint!(USER_BALANCE),
});
expected_total_rewards.push(TokenAmountPair {
token: managed_token_id!(SECOND_TOKEN_ID),
amount: managed_biguint!(USER_BALANCE / 2),
});
expected_total_rewards.push(EsdtTokenPayment::new(
managed_token_id!(FIRST_TOKEN_ID),
0,
managed_biguint!(USER_BALANCE),
));
expected_total_rewards.push(EsdtTokenPayment::new(
managed_token_id!(SECOND_TOKEN_ID),
0,
managed_biguint!(USER_BALANCE / 2),
));
assert_eq!(sc.total_rewards_for_week(1).get(), expected_total_rewards);

// first user's new energy is added to week 2
Expand Down
Loading

0 comments on commit d486eb9

Please sign in to comment.