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 with locked rewards references cleanup #782

Merged
merged 6 commits into from
Nov 1, 2023
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
19 changes: 8 additions & 11 deletions dex/farm-with-locked-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,12 @@ pub trait Farm:

self.migrate_old_farm_positions(&orig_caller);
let boosted_rewards = self.claim_only_boosted_payment(&orig_caller);
let boosted_rewards_payment = if boosted_rewards > 0 {
self.send_to_lock_contract_non_zero(
self.reward_token_id().get(),
boosted_rewards,
caller.clone(),
orig_caller.clone(),
)
} else {
EsdtTokenPayment::new(self.reward_token_id().get(), 0, BigUint::zero())
};
let boosted_rewards_payment = self.send_to_lock_contract_non_zero(
self.reward_token_id().get(),
boosted_rewards,
caller.clone(),
orig_caller.clone(),
);

let new_farm_token = self.enter_farm::<NoMintWrapper<Self>>(orig_caller.clone());
self.send_payment_non_zero(&caller, &new_farm_token);
Expand Down Expand Up @@ -282,7 +278,8 @@ pub trait Farm:
energy_address: ManagedAddress,
) -> EsdtTokenPayment {
if amount == 0 {
return EsdtTokenPayment::new(token_id, 0, amount);
let locked_token_id = self.get_locked_token_id();
return EsdtTokenPayment::new(locked_token_id, 0, amount);
}

self.lock_virtual(token_id, amount, destination_address, energy_address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,13 @@ where
assert_eq!(out_farm_token.token_nonce, expected_farm_token_nonce);
assert_eq!(out_farm_token.amount, managed_biguint!(farm_token_amount));

assert_eq!(
out_reward_token.token_identifier,
managed_token_id!(LOCKED_REWARD_TOKEN_ID)
);
if out_reward_token.amount > 0 {
assert_eq!(
out_reward_token.token_identifier,
managed_token_id!(LOCKED_REWARD_TOKEN_ID)
);
assert_eq!(out_reward_token.token_nonce, 1);
} else {
assert_eq!(
out_reward_token.token_identifier,
managed_token_id!(REWARD_TOKEN_ID)
);
assert_eq!(out_reward_token.token_nonce, 0);
}

Expand Down
5 changes: 3 additions & 2 deletions dex/farm-with-locked-rewards/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 66
// Endpoints: 67
// Async Callback: 1
// Total number of exported functions: 68
// Total number of exported functions: 69

#![no_std]

Expand All @@ -22,6 +22,7 @@ multiversx_sc_wasm_adapter::endpoints! {
farm_with_locked_rewards
(
init => init
upgrade => upgrade
enterFarm => enter_farm_endpoint
claimRewards => claim_rewards_endpoint
exitFarm => exit_farm_endpoint
Expand Down
14 changes: 4 additions & 10 deletions dex/farm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,8 @@ pub trait Farm:

self.migrate_old_farm_positions(&orig_caller);
let boosted_rewards = self.claim_only_boosted_payment(&orig_caller);
let boosted_rewards_payment = if boosted_rewards > 0 {
EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards)
} else {
EsdtTokenPayment::new(self.reward_token_id().get(), 0, BigUint::zero())
};
let boosted_rewards_payment =
EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards);

let new_farm_token = self.enter_farm::<Wrapper<Self>>(orig_caller.clone());
self.send_payment_non_zero(&caller, &new_farm_token);
Expand Down Expand Up @@ -186,11 +183,8 @@ pub trait Farm:
self.migrate_old_farm_positions(&orig_caller);

let boosted_rewards = self.claim_only_boosted_payment(&orig_caller);
let boosted_rewards_payment = if boosted_rewards > 0 {
EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards)
} else {
EsdtTokenPayment::new(self.reward_token_id().get(), 0, BigUint::zero())
};
let boosted_rewards_payment =
EsdtTokenPayment::new(self.reward_token_id().get(), 0, boosted_rewards);

let merged_farm_token = self.merge_farm_tokens::<Wrapper<Self>>();
self.send_payment_non_zero(&caller, &merged_farm_token);
Expand Down
15 changes: 15 additions & 0 deletions farm-staking/farm-staking-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ version = "=0.43.3"
[dependencies.farm]
path = "../../dex/farm"

[dependencies.farm-with-locked-rewards]
path = "../../dex/farm-with-locked-rewards"

[dependencies.farm-staking]
path = "../farm-staking"

Expand Down Expand Up @@ -57,6 +60,9 @@ path = "../../common/traits/unwrappable"
[dependencies.sc_whitelist_module]
path = "../../common/modules/sc_whitelist_module"

[dependencies.energy-query]
path = "../../energy-integration/common-modules/energy-query"

[dev-dependencies]
num-bigint = "0.4.2"

Expand All @@ -68,3 +74,12 @@ path = "../../common/modules/farm/farm_token"

[dev-dependencies.farm-boosted-yields]
path = "../../energy-integration/farm-boosted-yields"

[dev-dependencies.energy-factory]
path = "../../locked-asset/energy-factory"

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

[dev-dependencies.locking_module]
path = "../../common/modules/locking_module"
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
multiversx_sc::imports!();

use farm::{
base_functions::ClaimRewardsResultType, EnterFarmResultType, ExitFarmWithPartialPosResultType,
ProxyTrait as _,
base_functions::{ClaimRewardsResultType, DoubleMultiPayment},
EnterFarmResultType, ExitFarmWithPartialPosResultType,
};
use farm_staking::{
claim_stake_farm_rewards::ProxyTrait as _, stake_farm::ProxyTrait as _,
unstake_farm::ProxyTrait as _,
};
use farm_with_locked_rewards::ProxyTrait as _;
use pair::safe_price_view::ProxyTrait as _;

use crate::result_types::*;
Expand All @@ -17,7 +18,7 @@ pub type SafePriceResult<Api> = MultiValue2<EsdtTokenPayment<Api>, EsdtTokenPaym

#[multiversx_sc::module]
pub trait ExternalContractsInteractionsModule:
crate::lp_farm_token::LpFarmTokenModule + utils::UtilsModule
crate::lp_farm_token::LpFarmTokenModule + utils::UtilsModule + energy_query::EnergyQueryModule
{
// lp farm

Expand Down Expand Up @@ -66,19 +67,21 @@ pub trait ExternalContractsInteractionsModule:
fn merge_lp_farm_tokens(
&self,
orig_caller: ManagedAddress,
base_lp_token: EsdtTokenPayment,
mut additional_lp_tokens: PaymentsVec<Self::Api>,
) -> EsdtTokenPayment {
if additional_lp_tokens.is_empty() {
return base_lp_token;
base_lp_farm_token: EsdtTokenPayment,
mut additional_lp_farm_tokens: PaymentsVec<Self::Api>,
) -> DoubleMultiPayment<Self::Api> {
if additional_lp_farm_tokens.is_empty() {
let locked_token_id = self.get_locked_token_id();
let rewards_payment = EsdtTokenPayment::new(locked_token_id, 0, BigUint::zero());
return (base_lp_farm_token, rewards_payment).into();
}

additional_lp_tokens.push(base_lp_token);
additional_lp_farm_tokens.push(base_lp_farm_token);

let lp_farm_address = self.lp_farm_address().get();
self.lp_farm_proxy_obj(lp_farm_address)
.merge_farm_tokens_endpoint(orig_caller)
.with_multi_token_transfer(additional_lp_tokens)
.with_multi_token_transfer(additional_lp_farm_tokens)
.execute_on_dest_context()
}

Expand Down Expand Up @@ -216,7 +219,10 @@ pub trait ExternalContractsInteractionsModule:
fn staking_farm_proxy_obj(&self, sc_address: ManagedAddress) -> farm_staking::Proxy<Self::Api>;

#[proxy]
fn lp_farm_proxy_obj(&self, sc_address: ManagedAddress) -> farm::Proxy<Self::Api>;
fn lp_farm_proxy_obj(
&self,
sc_address: ManagedAddress,
) -> farm_with_locked_rewards::Proxy<Self::Api>;

#[proxy]
fn pair_proxy_obj(&self, sc_address: ManagedAddress) -> pair::Proxy<Self::Api>;
Expand Down
5 changes: 5 additions & 0 deletions farm-staking/farm-staking-proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub trait FarmStakingProxy:
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ utils::UtilsModule
+ token_send::TokenSendModule
+ energy_query::EnergyQueryModule
+ sc_whitelist_module::SCWhitelistModule
+ proxy_actions::stake::ProxyStakeModule
+ proxy_actions::claim::ProxyClaimModule
Expand All @@ -24,6 +25,7 @@ pub trait FarmStakingProxy:
#[init]
fn init(
&self,
energy_factory_address: ManagedAddress,
lp_farm_address: ManagedAddress,
staking_farm_address: ManagedAddress,
pair_address: ManagedAddress,
Expand All @@ -32,6 +34,7 @@ pub trait FarmStakingProxy:
staking_farm_token_id: TokenIdentifier,
lp_token_id: TokenIdentifier,
) {
self.require_sc_address(&energy_factory_address);
self.require_sc_address(&lp_farm_address);
self.require_sc_address(&staking_farm_address);
self.require_sc_address(&pair_address);
Expand All @@ -41,6 +44,8 @@ pub trait FarmStakingProxy:
self.require_valid_token_id(&staking_farm_token_id);
self.require_valid_token_id(&lp_token_id);

self.energy_factory_address()
.set_if_empty(&energy_factory_address);
self.lp_farm_address().set_if_empty(&lp_farm_address);
self.staking_farm_address()
.set_if_empty(&staking_farm_address);
Expand Down
1 change: 1 addition & 0 deletions farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub trait ProxyClaimModule:
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ utils::UtilsModule
+ token_send::TokenSendModule
+ energy_query::EnergyQueryModule
+ sc_whitelist_module::SCWhitelistModule
{
#[payable("*")]
Expand Down
16 changes: 10 additions & 6 deletions farm-staking/farm-staking-proxy/src/proxy_actions/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub trait ProxyStakeModule:
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ utils::UtilsModule
+ token_send::TokenSendModule
+ energy_query::EnergyQueryModule
+ sc_whitelist_module::SCWhitelistModule
{
#[payable("*")]
Expand Down Expand Up @@ -67,11 +68,13 @@ pub trait ProxyStakeModule:
);
let received_staking_farm_token = staking_farm_enter_result.received_staking_farm_token;

let merged_lp_farm_tokens = self.merge_lp_farm_tokens(
orig_caller,
lp_farm_token_payment,
additional_lp_farm_tokens,
);
let (merged_lp_farm_tokens, lp_farm_boosted_rewards) = self
.merge_lp_farm_tokens(
orig_caller,
lp_farm_token_payment,
additional_lp_farm_tokens,
)
.into_tuple();

let new_attributes = DualYieldTokenAttributes {
lp_farm_token_nonce: merged_lp_farm_tokens.token_nonce,
Expand All @@ -83,7 +86,8 @@ pub trait ProxyStakeModule:
self.create_dual_yield_tokens(&dual_yield_token_mapper, &new_attributes);
let output_payments = StakeProxyResult {
dual_yield_tokens: new_dual_yield_tokens,
boosted_rewards: staking_farm_enter_result.boosted_rewards,
staking_boosted_rewards: staking_farm_enter_result.boosted_rewards,
lp_farm_boosted_rewards,
};

output_payments.send_and_return(self, &caller)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub trait ProxyUnstakeModule:
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ utils::UtilsModule
+ token_send::TokenSendModule
+ energy_query::EnergyQueryModule
+ sc_whitelist_module::SCWhitelistModule
{
#[payable("*")]
Expand Down
6 changes: 4 additions & 2 deletions farm-staking/farm-staking-proxy/src/result_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ pub struct PairRemoveLiquidityResult<M: ManagedTypeApi> {
#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct StakeProxyResult<M: ManagedTypeApi> {
pub dual_yield_tokens: EsdtTokenPayment<M>,
pub boosted_rewards: EsdtTokenPayment<M>,
pub staking_boosted_rewards: EsdtTokenPayment<M>,
pub lp_farm_boosted_rewards: EsdtTokenPayment<M>,
}

impl<M: ManagedTypeApi> StakeProxyResult<M> {
Expand All @@ -54,7 +55,8 @@ impl<M: ManagedTypeApi> StakeProxyResult<M> {
to: &ManagedAddress<M>,
) -> Self {
sc.send_payment_non_zero(to, &self.dual_yield_tokens);
sc.send_payment_non_zero(to, &self.boosted_rewards);
sc.send_payment_non_zero(to, &self.staking_boosted_rewards);
sc.send_payment_non_zero(to, &self.lp_farm_boosted_rewards);

self
}
Expand Down
10 changes: 10 additions & 0 deletions farm-staking/farm-staking-proxy/tests/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ 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 BOOSTED_YIELDS_PERCENTAGE: u64 = 2_500; // 25%

// Proxy constants

pub static PROXY_WASM_PATH: &str = "farm-staking-proxy/output/farm-staking-proxy";
pub static DUAL_YIELD_TOKEN_ID: &[u8] = b"DYIELD-abcdef";

// Energy factory constants

pub const EPOCHS_IN_YEAR: u64 = 360;
pub static MEX_TOKEN_ID: &[u8] = b"MEX-123456";
pub static LOCKED_TOKEN_ID: &[u8] = b"LOCKED-123456";
pub static LEGACY_LOCKED_TOKEN_ID: &[u8] = b"LEGACY-123456";
pub static LOCK_OPTIONS: &[u64] = &[EPOCHS_IN_YEAR, 5 * EPOCHS_IN_YEAR, 10 * EPOCHS_IN_YEAR]; // 1, 5 or 10 years
pub static PENALTY_PERCENTAGES: &[u64] = &[4_000, 6_000, 8_000];
Loading