Skip to content

Commit

Permalink
Merge pull request #781 from multiversx/opt-caller-arg
Browse files Browse the repository at this point in the history
opt caller arg
  • Loading branch information
dorin-iancu authored Oct 10, 2023
2 parents 9534cc4 + cd4d21b commit 720dae7
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 57 deletions.
6 changes: 3 additions & 3 deletions locked-asset/proxy_dex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ path = "../../energy-integration/common-modules/energy-query"
[dependencies.simple-lock]
path = "../simple-lock"

[dependencies.sc_whitelist_module]
path = "../../common/modules/sc_whitelist_module"

[dev-dependencies]
num-bigint = "0.4.2"
num-traits = "0.2"
Expand All @@ -82,8 +85,5 @@ path = "../../common/modules/farm/farm_token"
[dev-dependencies.rewards]
path = "../../common/modules/farm/rewards"

[dev-dependencies.sc_whitelist_module]
path = "../../common/modules/sc_whitelist_module"

[dev-dependencies.farm-boosted-yields]
path = "../../energy-integration/farm-boosted-yields"
12 changes: 6 additions & 6 deletions locked-asset/proxy_dex/src/farm_interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ pub struct ExitFarmResultWrapper<M: ManagedTypeApi> {
pub trait FarmInteractionsModule {
fn call_enter_farm(
&self,
user: ManagedAddress,
farm_address: ManagedAddress,
farming_token_id: TokenIdentifier,
farming_token_amount: BigUint,
) -> EnterFarmResultWrapper<Self::Api> {
let original_caller = self.blockchain().get_caller();
let enter_farm_result: EnterFarmResultType<Self::Api> = self
.farm_contract_proxy(farm_address)
.enter_farm_endpoint(original_caller)
.enter_farm_endpoint(user)
.with_esdt_transfer((farming_token_id, 0, farming_token_amount))
.execute_on_dest_context();

Expand All @@ -40,13 +40,13 @@ pub trait FarmInteractionsModule {

fn call_exit_farm(
&self,
user: ManagedAddress,
farm_address: ManagedAddress,
farm_token: EsdtTokenPayment,
) -> ExitFarmResultWrapper<Self::Api> {
let original_caller = self.blockchain().get_caller();
let raw_result: ExitFarmWithPartialPosResultType<Self::Api> = self
.farm_contract_proxy(farm_address)
.exit_farm_endpoint(original_caller)
.exit_farm_endpoint(user)
.with_esdt_transfer(farm_token)
.execute_on_dest_context();
let (farming_tokens, reward_tokens) = raw_result.into_tuple();
Expand All @@ -59,13 +59,13 @@ pub trait FarmInteractionsModule {

fn call_claim_rewards_farm(
&self,
user: ManagedAddress,
farm_address: ManagedAddress,
farm_token: EsdtTokenPayment,
) -> ClaimRewardsResultWrapper<Self::Api> {
let original_caller = self.blockchain().get_caller();
let raw_result: ClaimRewardsResultType<Self::Api> = self
.farm_contract_proxy(farm_address)
.claim_rewards_endpoint(original_caller)
.claim_rewards_endpoint(user)
.with_esdt_transfer(farm_token)
.execute_on_dest_context();
let (new_farm_token, rewards) = raw_result.into_tuple();
Expand Down
5 changes: 3 additions & 2 deletions locked-asset/proxy_dex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod pair_interactions;
pub mod proxy_common;
pub mod proxy_farm;
pub mod proxy_pair;
pub mod sc_whitelist;
pub mod other_sc_whitelist;
pub mod wrapped_farm_attributes;
pub mod wrapped_farm_token_merge;
pub mod wrapped_lp_attributes;
Expand All @@ -22,7 +22,7 @@ pub mod wrapped_lp_token_merge;
#[multiversx_sc::contract]
pub trait ProxyDexImpl:
proxy_common::ProxyCommonModule
+ sc_whitelist::ScWhitelistModule
+ crate::other_sc_whitelist::OtherScWhitelistModule
+ proxy_pair::ProxyPairModule
+ pair_interactions::PairInteractionsModule
+ proxy_farm::ProxyFarmModule
Expand All @@ -37,6 +37,7 @@ pub trait ProxyDexImpl:
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ utils::UtilsModule
+ legacy_token_decode_module::LegacyTokenDecodeModule
+ sc_whitelist_module::SCWhitelistModule
{
#[init]
fn init(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
multiversx_sc::imports!();

#[multiversx_sc::module]
pub trait ScWhitelistModule {
pub trait OtherScWhitelistModule {
#[only_owner]
#[endpoint(addPairToIntermediate)]
fn add_pair_to_intermediate(&self, pair_address: ManagedAddress) {
Expand Down
84 changes: 59 additions & 25 deletions locked-asset/proxy_dex/src/proxy_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ use crate::{
wrapped_lp_attributes::WrappedLpTokenAttributes,
};

pub struct FarmingFarmTokenPair<M: ManagedTypeApi> {
pub struct EnterFarmResult<M: ManagedTypeApi> {
pub farming_token: EsdtTokenPayment<M>,
pub farm_token: EsdtTokenPayment<M>,
pub rewards: EsdtTokenPayment<M>,
}

pub type EnterFarmProxyResultType<M> = MultiValue2<EsdtTokenPayment<M>, EsdtTokenPayment<M>>;
pub type ExitFarmProxyResultType<M> = MultiValue2<EsdtTokenPayment<M>, EsdtTokenPayment<M>>;
pub type ClaimRewardsFarmProxyResultType<M> = MultiValue2<EsdtTokenPayment<M>, EsdtTokenPayment<M>>;

#[multiversx_sc::module]
pub trait ProxyFarmModule:
crate::proxy_common::ProxyCommonModule
+ crate::sc_whitelist::ScWhitelistModule
+ crate::other_sc_whitelist::OtherScWhitelistModule
+ crate::proxy_pair::ProxyPairModule
+ crate::pair_interactions::PairInteractionsModule
+ crate::farm_interactions::FarmInteractionsModule
Expand All @@ -36,30 +38,44 @@ pub trait ProxyFarmModule:
+ crate::events::EventsModule
+ utils::UtilsModule
+ legacy_token_decode_module::LegacyTokenDecodeModule
+ sc_whitelist_module::SCWhitelistModule
{
#[payable("*")]
#[endpoint(enterFarmProxy)]
fn enter_farm_proxy_endpoint(&self, farm_address: ManagedAddress) -> EsdtTokenPayment {
fn enter_farm_proxy_endpoint(
&self,
farm_address: ManagedAddress,
opt_original_caller: OptionalValue<ManagedAddress>,
) -> EnterFarmProxyResultType<Self::Api> {
self.require_is_intermediated_farm(&farm_address);
self.require_wrapped_farm_token_id_not_empty();
self.require_wrapped_lp_token_id_not_empty();

let caller = self.blockchain().get_caller();
let original_caller = self.get_orig_caller_from_opt(&caller, opt_original_caller);

let mut payments = self.get_non_empty_payments();
let proxy_farming_token = self.pop_first_payment(&mut payments);

let wrapped_lp_token_id = self.wrapped_lp_token().get_token_id();
let farm_farming_token_pair = if self.is_locked_token(&proxy_farming_token.token_identifier)
{
self.enter_farm_locked_token(farm_address.clone(), proxy_farming_token.clone())
let enter_result = if self.is_locked_token(&proxy_farming_token.token_identifier) {
self.enter_farm_locked_token(
original_caller.clone(),
farm_address.clone(),
proxy_farming_token.clone(),
)
} else if proxy_farming_token.token_identifier == wrapped_lp_token_id {
self.enter_farm_wrapped_lp(farm_address.clone(), proxy_farming_token.clone())
self.enter_farm_wrapped_lp(
original_caller.clone(),
farm_address.clone(),
proxy_farming_token.clone(),
)
} else {
sc_panic!(INVALID_PAYMENTS_ERR_MSG)
};

let new_token_attributes = WrappedFarmTokenAttributes {
farm_token: farm_farming_token_pair.farm_token,
farm_token: enter_result.farm_token,
proxy_farming_token,
};

Expand All @@ -72,7 +88,7 @@ pub trait ProxyFarmModule:
self.send().esdt_local_burn_multi(&payments);

self.merge_wrapped_farm_tokens_with_virtual_pos(
&caller,
&original_caller,
farm_address.clone(),
wrapped_lp_tokens,
new_token_attributes,
Expand All @@ -89,46 +105,51 @@ pub trait ProxyFarmModule:
};

self.send_payment_non_zero(&caller, &new_wrapped_farm_token.payment);
self.send_payment_non_zero(&caller, &enter_result.rewards);

self.emit_enter_farm_proxy_event(
&caller,
&original_caller,
&farm_address,
farm_farming_token_pair.farming_token,
enter_result.farming_token,
new_wrapped_farm_token.payment.clone(),
new_wrapped_farm_token.attributes,
token_merge_requested,
);

new_wrapped_farm_token.payment
(new_wrapped_farm_token.payment, enter_result.rewards).into()
}

fn enter_farm_locked_token(
&self,
user: ManagedAddress,
farm_address: ManagedAddress,
locked_token: EsdtTokenPayment,
) -> FarmingFarmTokenPair<Self::Api> {
) -> EnterFarmResult<Self::Api> {
let asset_token_id = self.get_base_token_id();
self.send()
.esdt_local_mint(&asset_token_id, 0, &locked_token.amount);

let minted_asset_tokens = EsdtTokenPayment::new(asset_token_id, 0, locked_token.amount);
let enter_result = self.call_enter_farm(
user,
farm_address,
minted_asset_tokens.token_identifier.clone(),
minted_asset_tokens.amount.clone(),
);

FarmingFarmTokenPair {
EnterFarmResult {
farming_token: minted_asset_tokens,
farm_token: enter_result.farm_token,
rewards: enter_result.reward_token,
}
}

fn enter_farm_wrapped_lp(
&self,
user: ManagedAddress,
farm_address: ManagedAddress,
wrapped_lp_token: EsdtTokenPayment,
) -> FarmingFarmTokenPair<Self::Api> {
) -> EnterFarmResult<Self::Api> {
let wrapped_lp_token_mapper = self.wrapped_lp_token();
let wrapped_lp_attributes: WrappedLpTokenAttributes<Self::Api> = self
.get_attributes_as_part_of_fixed_supply(&wrapped_lp_token, &wrapped_lp_token_mapper);
Expand All @@ -139,20 +160,26 @@ pub trait ProxyFarmModule:
wrapped_lp_attributes.lp_token_amount,
);
let enter_result = self.call_enter_farm(
user,
farm_address,
farming_token.token_identifier.clone(),
farming_token.amount.clone(),
);

FarmingFarmTokenPair {
EnterFarmResult {
farming_token,
farm_token: enter_result.farm_token,
rewards: enter_result.reward_token,
}
}

#[payable("*")]
#[endpoint(exitFarmProxy)]
fn exit_farm_proxy(&self, farm_address: ManagedAddress) -> ExitFarmProxyResultType<Self::Api> {
fn exit_farm_proxy(
&self,
farm_address: ManagedAddress,
opt_original_caller: OptionalValue<ManagedAddress>,
) -> ExitFarmProxyResultType<Self::Api> {
self.require_is_intermediated_farm(&farm_address);
self.require_wrapped_farm_token_id_not_empty();
self.require_wrapped_lp_token_id_not_empty();
Expand All @@ -166,10 +193,13 @@ pub trait ProxyFarmModule:
.get_token_attributes(&payment.token_identifier, payment.token_nonce);

let wrapped_farm_attributes_for_exit: WrappedFarmTokenAttributes<Self::Api> =
full_wrapped_farm_attributes
.clone()
.into_part(&payment.amount);
full_wrapped_farm_attributes.into_part(&payment.amount);

let caller = self.blockchain().get_caller();
let original_caller = self.get_orig_caller_from_opt(&caller, opt_original_caller);

let exit_result = self.call_exit_farm(
original_caller.clone(),
farm_address.clone(),
wrapped_farm_attributes_for_exit.farm_token.clone(),
);
Expand All @@ -181,10 +211,9 @@ pub trait ProxyFarmModule:
attributes: wrapped_farm_attributes_for_exit.clone(),
};

let caller = self.blockchain().get_caller();
let initial_proxy_farming_tokens = self
.handle_farm_penalty_and_get_output_proxy_farming_token(
&caller,
&original_caller,
wrapped_farm_tokens_for_initial_tokens,
exit_result.farming_tokens.amount,
);
Expand All @@ -195,7 +224,7 @@ pub trait ProxyFarmModule:
wrapped_farm_token_mapper.nft_burn(payment.token_nonce, &payment.amount);

self.emit_exit_farm_proxy_event(
&caller,
&original_caller,
&farm_address,
payment,
wrapped_farm_attributes_for_exit,
Expand Down Expand Up @@ -262,6 +291,7 @@ pub trait ProxyFarmModule:
fn claim_rewards_proxy(
&self,
farm_address: ManagedAddress,
opt_original_caller: OptionalValue<ManagedAddress>,
) -> ClaimRewardsFarmProxyResultType<Self::Api> {
self.require_is_intermediated_farm(&farm_address);
self.require_wrapped_farm_token_id_not_empty();
Expand All @@ -273,7 +303,12 @@ pub trait ProxyFarmModule:

let wrapped_farm_attributes: WrappedFarmTokenAttributes<Self::Api> =
self.get_attributes_as_part_of_fixed_supply(&payment, &wrapped_farm_token_mapper);

let caller = self.blockchain().get_caller();
let original_caller = self.get_orig_caller_from_opt(&caller, opt_original_caller);

let claim_result = self.call_claim_rewards_farm(
original_caller.clone(),
farm_address.clone(),
wrapped_farm_attributes.farm_token.clone(),
);
Expand All @@ -286,12 +321,11 @@ pub trait ProxyFarmModule:
let new_wrapped_token =
wrapped_farm_token_mapper.nft_create(new_token_amount, &new_wrapped_farm_attributes);

let caller = self.blockchain().get_caller();
self.send_payment_non_zero(&caller, &new_wrapped_token);
self.send_payment_non_zero(&caller, &claim_result.rewards);

self.emit_claim_rewards_farm_proxy_event(
&caller,
&original_caller,
&farm_address,
payment,
wrapped_farm_attributes,
Expand Down
4 changes: 2 additions & 2 deletions locked-asset/proxy_dex/src/proxy_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use fixed_supply_token::FixedSupplyToken;
#[multiversx_sc::module]
pub trait ProxyPairModule:
crate::proxy_common::ProxyCommonModule
+ crate::sc_whitelist::ScWhitelistModule
+ crate::other_sc_whitelist::OtherScWhitelistModule
+ crate::pair_interactions::PairInteractionsModule
+ crate::wrapped_lp_token_merge::WrappedLpTokenMerge
+ crate::energy_update::EnergyUpdateModule
+ crate::events::EventsModule
+ energy_query::EnergyQueryModule
+ token_merge_helper::TokenMergeHelperModule
+ token_send::TokenSendModule
+ crate::events::EventsModule
+ utils::UtilsModule
+ legacy_token_decode_module::LegacyTokenDecodeModule
{
Expand Down
2 changes: 1 addition & 1 deletion locked-asset/proxy_dex/src/wrapped_farm_token_merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use fixed_supply_token::FixedSupplyToken;
pub trait WrappedFarmTokenMerge:
token_merge_helper::TokenMergeHelperModule
+ token_send::TokenSendModule
+ crate::sc_whitelist::ScWhitelistModule
+ crate::other_sc_whitelist::OtherScWhitelistModule
+ crate::proxy_common::ProxyCommonModule
+ crate::wrapped_lp_token_merge::WrappedLpTokenMerge
+ utils::UtilsModule
Expand Down
2 changes: 1 addition & 1 deletion locked-asset/proxy_dex/tests/proxy_dex_test_setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use multiversx_sc_scenario::{
};
use pair::{config::ConfigModule as OtherConfigModule, Pair};
use pausable::{PausableModule, State};
use proxy_dex::{proxy_common::ProxyCommonModule, sc_whitelist::ScWhitelistModule, ProxyDexImpl};
use proxy_dex::{proxy_common::ProxyCommonModule, other_sc_whitelist::OtherScWhitelistModule, ProxyDexImpl};
use sc_whitelist_module::SCWhitelistModule;
use simple_lock::locked_token::{LockedTokenAttributes, LockedTokenModule};

Expand Down
Loading

0 comments on commit 720dae7

Please sign in to comment.