Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Farm staking NFT impl (part 3) #832

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions farm-staking/farm-staking-nft/src/custom_rewards.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use common_structs::{Epoch, FarmToken, Nonce, PaymentsVec};
use common_structs::{Epoch, Nonce, PaymentsVec};
use contexts::storage_cache::StorageCache;

use crate::token_attributes::StakingFarmNftTokenAttributes;
use crate::token_attributes::{
PartialStakingFarmNftTokenAttributes, StakingFarmNftTokenAttributes,
};

pub const MAX_PERCENT: u64 = 10_000;
pub const BLOCKS_IN_YEAR: u64 = 31_536_000 / 6; // seconds_in_year / 6_seconds_per_block
Expand Down Expand Up @@ -113,10 +115,10 @@ pub trait CustomRewardsModule:
fn calculate_base_farm_rewards(
&self,
farm_token_amount: &BigUint,
token_attributes: &StakingFarmNftTokenAttributes<Self::Api>,
token_attributes: &PartialStakingFarmNftTokenAttributes<Self::Api>,
storage_cache: &StorageCache<Self>,
) -> BigUint {
let token_rps = token_attributes.get_reward_per_share();
let token_rps = token_attributes.reward_per_share.clone();
if storage_cache.reward_per_share > token_rps {
let rps_diff = &storage_cache.reward_per_share - &token_rps;
farm_token_amount * &rps_diff / &storage_cache.division_safety_constant
Expand Down Expand Up @@ -200,7 +202,7 @@ pub trait CustomRewardsModule:
&self,
caller: &ManagedAddress,
farm_token_amount: &BigUint,
token_attributes: &StakingFarmNftTokenAttributes<Self::Api>,
token_attributes: &PartialStakingFarmNftTokenAttributes<Self::Api>,
storage_cache: &StorageCache<Self>,
) -> BigUint {
let base_farm_reward =
Expand All @@ -215,8 +217,8 @@ pub trait CustomRewardsModule:
caller: ManagedAddress,
farming_token_amount: BigUint,
current_reward_per_share: BigUint,
) -> StakingFarmNftTokenAttributes<Self::Api> {
StakingFarmNftTokenAttributes {
) -> PartialStakingFarmNftTokenAttributes<Self::Api> {
PartialStakingFarmNftTokenAttributes {
reward_per_share: current_reward_per_share,
compounded_reward: BigUint::zero(),
current_farm_amount: farming_token_amount,
Expand All @@ -228,10 +230,10 @@ pub trait CustomRewardsModule:
fn create_claim_rewards_initial_attributes(
&self,
caller: ManagedAddress,
first_token_attributes: StakingFarmNftTokenAttributes<Self::Api>,
first_token_attributes: PartialStakingFarmNftTokenAttributes<Self::Api>,
current_reward_per_share: BigUint,
) -> StakingFarmNftTokenAttributes<Self::Api> {
StakingFarmNftTokenAttributes {
) -> PartialStakingFarmNftTokenAttributes<Self::Api> {
PartialStakingFarmNftTokenAttributes {
reward_per_share: current_reward_per_share,
compounded_reward: first_token_attributes.compounded_reward,
current_farm_amount: first_token_attributes.current_farm_amount,
Expand All @@ -243,13 +245,13 @@ pub trait CustomRewardsModule:
fn create_compound_rewards_initial_attributes(
&self,
caller: ManagedAddress,
first_token_attributes: StakingFarmNftTokenAttributes<Self::Api>,
first_token_attributes: PartialStakingFarmNftTokenAttributes<Self::Api>,
current_reward_per_share: BigUint,
reward: &BigUint,
) -> StakingFarmNftTokenAttributes<Self::Api> {
) -> PartialStakingFarmNftTokenAttributes<Self::Api> {
let new_pos_compounded_reward = first_token_attributes.compounded_reward + reward;
let new_pos_current_farm_amount = first_token_attributes.current_farm_amount + reward;
StakingFarmNftTokenAttributes {
PartialStakingFarmNftTokenAttributes {
reward_per_share: current_reward_per_share,
compounded_reward: new_pos_compounded_reward,
current_farm_amount: new_pos_current_farm_amount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
multiversx_sc::imports!();

use common_structs::{PaymentAttributesPair, PaymentsVec};
use contexts::{claim_rewards_context::ClaimRewardsContext, storage_cache::StorageCache};
use farm::base_functions::ClaimRewardsResultType;
use farm_base_impl::claim_rewards::InternalClaimRewardsResult;
use fixed_supply_token::FixedSupplyToken;

use crate::{farm_hooks::hook_type::FarmHookType, token_attributes::StakingFarmNftTokenAttributes};
use contexts::{
claim_rewards_context::ClaimRewardsContext,
storage_cache::{FarmContracTraitBounds, StorageCache},
};

use crate::{
farm_hooks::hook_type::FarmHookType,
result_types::ClaimRewardsResultType,
token_attributes::{PartialStakingFarmNftTokenAttributes, StakingFarmNftTokenAttributes},
};

pub struct InternalClaimRewardsResult<'a, C>
where
C: FarmContracTraitBounds,
{
pub context: ClaimRewardsContext<C::Api, StakingFarmNftTokenAttributes<C::Api>>,
pub storage_cache: StorageCache<'a, C>,
pub rewards: EsdtTokenPayment<C::Api>,
pub new_farm_token: PaymentAttributesPair<C::Api, PartialStakingFarmNftTokenAttributes<C::Api>>,
pub created_with_merge: bool,
}

#[multiversx_sc::module]
pub trait ClaimStakeFarmRewardsModule:
Expand Down Expand Up @@ -35,6 +50,7 @@ pub trait ClaimStakeFarmRewardsModule:
+ banned_addresses::BannedAddressModule
+ crate::farm_hooks::change_hooks::ChangeHooksModule
+ crate::farm_hooks::call_hook::CallHookModule
+ crate::token_info::TokenInfoModule
{
#[payable("*")]
#[endpoint(claimRewards)]
Expand All @@ -60,7 +76,6 @@ pub trait ClaimStakeFarmRewardsModule:
output_payments.push(virtual_farm_token.payment);
self.push_if_non_zero_payment(&mut output_payments, claim_result.rewards.clone());

// TODO: Fix attributes
let mut output_payments_after_hook = self.call_hook(
FarmHookType::AfterClaimRewards,
caller.clone(),
Expand All @@ -74,23 +89,26 @@ pub trait ClaimStakeFarmRewardsModule:
self.send_payment_non_zero(&caller, &virtual_farm_token.payment);
self.send_payment_non_zero(&caller, &claim_result.rewards);

self.emit_claim_rewards_event(
&caller,
claim_result.context,
virtual_farm_token.clone(),
claim_result.rewards.clone(),
claim_result.created_with_merge,
claim_result.storage_cache,
);

(virtual_farm_token.payment, claim_result.rewards).into()
// self.emit_claim_rewards_event(
// &caller,
// claim_result.context,
// virtual_farm_token.clone(),
// claim_result.rewards.clone(),
// claim_result.created_with_merge,
// claim_result.storage_cache,
// );

ClaimRewardsResultType {
new_farm_token: virtual_farm_token.payment,
rewards: claim_result.rewards,
}
}

fn claim_rewards_base(
&self,
caller: ManagedAddress,
payments: PaymentsVec<Self::Api>,
) -> InternalClaimRewardsResult<Self, StakingFarmNftTokenAttributes<Self::Api>> {
) -> InternalClaimRewardsResult<Self> {
let mut claim_result = self.claim_rewards_base_no_farm_token_mint(caller, payments);
let virtual_farm_token_payment = &claim_result.new_farm_token.payment;
let minted_farm_token_nonce = self.send().esdt_nft_create_compact(
Expand All @@ -107,7 +125,7 @@ pub trait ClaimStakeFarmRewardsModule:
&self,
caller: ManagedAddress,
payments: PaymentsVec<Self::Api>,
) -> InternalClaimRewardsResult<Self, StakingFarmNftTokenAttributes<Self::Api>> {
) -> InternalClaimRewardsResult<Self> {
let mut storage_cache = StorageCache::new(self);
self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id);

Expand All @@ -121,11 +139,10 @@ pub trait ClaimStakeFarmRewardsModule:
self.generate_aggregated_rewards(&mut storage_cache);

let farm_token_amount = &claim_rewards_context.first_farm_token.payment.amount;
let token_attributes = claim_rewards_context
.first_farm_token
.attributes
.clone()
.into_part(farm_token_amount);
let token_attributes = self.into_part(
claim_rewards_context.first_farm_token.attributes.clone(),
&claim_rewards_context.first_farm_token.payment,
);

let reward = self.calculate_rewards(
&caller,
Expand All @@ -143,7 +160,7 @@ pub trait ClaimStakeFarmRewardsModule:
token_attributes,
storage_cache.reward_per_share.clone(),
);
let new_token_attributes = self.merge_attributes_from_payments(
let new_token_attributes = self.merge_attributes_from_payments_nft(
base_attributes,
&claim_rewards_context.additional_payments,
&farm_token_mapper,
Expand All @@ -152,7 +169,7 @@ pub trait ClaimStakeFarmRewardsModule:
payment: EsdtTokenPayment::new(
storage_cache.farm_token_id.clone(),
0,
new_token_attributes.get_total_supply(),
new_token_attributes.current_farm_amount.clone(),
),
attributes: new_token_attributes,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
use common_errors::ERROR_DIFFERENT_TOKEN_IDS;
use common_structs::PaymentsVec;
use contexts::{claim_rewards_context::CompoundRewardsContext, storage_cache::StorageCache};
use farm_base_impl::compound_rewards::InternalCompoundRewardsResult;
use fixed_supply_token::FixedSupplyToken;

use crate::{farm_hooks::hook_type::FarmHookType, token_attributes::StakingFarmNftTokenAttributes};
use common_structs::{PaymentAttributesPair, PaymentsVec};
use contexts::{
claim_rewards_context::CompoundRewardsContext,
storage_cache::{FarmContracTraitBounds, StorageCache},
};

use crate::{
farm_hooks::hook_type::FarmHookType,
result_types::CompoundRewardsResultType,
token_attributes::{PartialStakingFarmNftTokenAttributes, StakingFarmNftTokenAttributes},
};

multiversx_sc::imports!();

pub struct InternalCompoundRewardsResult<'a, C>
where
C: FarmContracTraitBounds,
{
pub context: CompoundRewardsContext<C::Api, StakingFarmNftTokenAttributes<C::Api>>,
pub storage_cache: StorageCache<'a, C>,
pub new_farm_token: PaymentAttributesPair<C::Api, PartialStakingFarmNftTokenAttributes<C::Api>>,
pub compounded_rewards: BigUint<C::Api>,
pub created_with_merge: bool,
}

#[multiversx_sc::module]
pub trait CompoundStakeFarmRewardsModule:
crate::custom_rewards::CustomRewardsModule
Expand Down Expand Up @@ -35,10 +51,11 @@ pub trait CompoundStakeFarmRewardsModule:
+ banned_addresses::BannedAddressModule
+ crate::farm_hooks::change_hooks::ChangeHooksModule
+ crate::farm_hooks::call_hook::CallHookModule
+ crate::token_info::TokenInfoModule
{
#[payable("*")]
#[endpoint(compoundRewards)]
fn compound_rewards(&self) -> EsdtTokenPayment {
fn compound_rewards(&self) -> CompoundRewardsResultType<Self::Api> {
let caller = self.blockchain().get_caller();
let payments = self.get_non_empty_payments();
let payments_after_hook = self.call_hook(
Expand Down Expand Up @@ -67,23 +84,23 @@ pub trait CompoundStakeFarmRewardsModule:

self.set_farm_supply_for_current_week(&compound_result.storage_cache.farm_token_supply);

self.emit_compound_rewards_event(
&caller,
compound_result.context,
compound_result.new_farm_token,
compound_result.compounded_rewards,
compound_result.created_with_merge,
compound_result.storage_cache,
);
// self.emit_compound_rewards_event(
// &caller,
// compound_result.context,
// compound_result.new_farm_token,
// compound_result.compounded_rewards,
// compound_result.created_with_merge,
// compound_result.storage_cache,
// );

new_farm_token
CompoundRewardsResultType { new_farm_token }
}

fn compound_rewards_base(
&self,
caller: ManagedAddress,
payments: PaymentsVec<Self::Api>,
) -> InternalCompoundRewardsResult<Self, StakingFarmNftTokenAttributes<Self::Api>> {
) -> InternalCompoundRewardsResult<Self> {
let mut storage_cache = StorageCache::new(self);
self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id);
require!(
Expand All @@ -101,11 +118,10 @@ pub trait CompoundStakeFarmRewardsModule:
self.generate_aggregated_rewards(&mut storage_cache);

let farm_token_amount = &compound_rewards_context.first_farm_token.payment.amount;
let token_attributes = compound_rewards_context
.first_farm_token
.attributes
.clone()
.into_part(farm_token_amount);
let token_attributes = self.into_part(
compound_rewards_context.first_farm_token.attributes.clone(),
&compound_rewards_context.first_farm_token.payment,
);

let reward = self.calculate_rewards(
&caller,
Expand All @@ -125,7 +141,7 @@ pub trait CompoundStakeFarmRewardsModule:
storage_cache.reward_per_share.clone(),
&reward,
);
let new_farm_token = self.merge_and_create_token(
let new_farm_token = self.merge_and_create_token_nft(
base_attributes,
&compound_rewards_context.additional_payments,
&farm_token_mapper,
Expand Down
22 changes: 14 additions & 8 deletions farm-staking/farm-staking-nft/src/farm_actions/stake_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ multiversx_sc::imports!();

use common_structs::{PaymentAttributesPair, PaymentsVec};
use contexts::{enter_farm_context::EnterFarmContext, storage_cache::StorageCache};
use farm::EnterFarmResultType;
use farm_base_impl::enter_farm::InternalEnterFarmResult;
use fixed_supply_token::FixedSupplyToken;

use crate::{farm_hooks::hook_type::FarmHookType, token_attributes::StakingFarmNftTokenAttributes};
use crate::{
farm_hooks::hook_type::FarmHookType, result_types::EnterFarmResultType,
token_attributes::PartialStakingFarmNftTokenAttributes,
};

#[multiversx_sc::module]
pub trait StakeFarmModule:
Expand Down Expand Up @@ -35,6 +36,7 @@ pub trait StakeFarmModule:
+ banned_addresses::BannedAddressModule
+ crate::farm_hooks::change_hooks::ChangeHooksModule
+ crate::farm_hooks::call_hook::CallHookModule
+ crate::token_info::TokenInfoModule
{
#[payable("*")]
#[endpoint(stakeFarm)]
Expand Down Expand Up @@ -83,8 +85,9 @@ pub trait StakeFarmModule:
attributes
.farming_token_parts
.append_vec(all_farming_tokens);
let attr_full = attributes.clone().into_full();

let new_farm_token = farm_token_mapper.nft_create(new_farm_token.amount, &attributes);
let new_farm_token = farm_token_mapper.nft_create(new_farm_token.amount, &attr_full);

let mut output_payments = ManagedVec::new();
output_payments.push(new_farm_token);
Expand Down Expand Up @@ -118,14 +121,17 @@ pub trait StakeFarmModule:
enter_result.storage_cache,
);

(new_farm_token, boosted_rewards_payment).into()
EnterFarmResultType {
new_farm_token,
boosted_rewards_payment,
}
}

fn enter_farm_base_no_token_create(
&self,
caller: ManagedAddress,
payments: PaymentsVec<Self::Api>,
) -> InternalEnterFarmResult<Self, StakingFarmNftTokenAttributes<Self::Api>> {
) -> InternalEnterFarmResult<Self, PartialStakingFarmNftTokenAttributes<Self::Api>> {
let mut storage_cache = StorageCache::new(self);
self.validate_contract_state(storage_cache.contract_state, &storage_cache.farm_token_id);

Expand All @@ -152,7 +158,7 @@ pub trait StakeFarmModule:
enter_farm_context.farming_token_payment.amount.clone(),
storage_cache.reward_per_share.clone(),
);
let new_token_attributes = self.merge_attributes_from_payments(
let new_token_attributes = self.merge_attributes_from_payments_nft(
base_attributes,
&enter_farm_context.additional_farm_tokens,
&farm_token_mapper,
Expand All @@ -161,7 +167,7 @@ pub trait StakeFarmModule:
payment: EsdtTokenPayment::new(
storage_cache.farm_token_id.clone(),
0,
new_token_attributes.get_total_supply(),
new_token_attributes.current_farm_amount.clone(),
),
attributes: new_token_attributes,
};
Expand Down
Loading
Loading