Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Nov 3, 2024
1 parent e825a19 commit 5ef4b70
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 204 deletions.
65 changes: 29 additions & 36 deletions farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,26 @@ pub trait ProxyClaimModule:
&self,
opt_orig_caller: OptionalValue<ManagedAddress>,
) -> ClaimDualYieldResult<Self::Api> {
let payment = self.call_value().single_esdt();
let dual_yield_token_mapper = self.dual_yield_token();
dual_yield_token_mapper.require_same_token(&payment.token_identifier);

let caller = self.blockchain().get_caller();
let attributes: DualYieldTokenAttributes<Self::Api> =
self.get_attributes_as_part_of_fixed_supply(&payment, &dual_yield_token_mapper);
let internal_claim_result = self.claim_dual_yield(
&caller,
opt_orig_caller,
attributes.staking_farm_token_amount.clone(),
attributes,
);
let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller);

let new_dual_yield_tokens = self.create_dual_yield_tokens(
&dual_yield_token_mapper,
&internal_claim_result.new_dual_yield_attributes,
);
let claim_result = ClaimDualYieldResult {
lp_farm_rewards: internal_claim_result.lp_farm_rewards,
staking_farm_rewards: internal_claim_result.staking_farm_rewards,
new_dual_yield_tokens,
};
let payment = self.call_value().single_esdt();

dual_yield_token_mapper.nft_burn(payment.token_nonce, &payment.amount);
let claim_result = self.claim_dual_yield_common(orig_caller, &payment);

claim_result.send_and_return(self, &caller)
}

fn claim_dual_yield(
fn claim_dual_yield_common(
&self,
caller: &ManagedAddress,
opt_orig_caller: OptionalValue<ManagedAddress>,
staking_claim_amount: BigUint,
attributes: DualYieldTokenAttributes<Self::Api>,
) -> InternalClaimResult<Self::Api> {
let orig_caller = self.get_orig_caller_from_opt(caller, opt_orig_caller);
orig_caller: ManagedAddress,
payment: &EsdtTokenPayment,
) -> ClaimDualYieldResult<Self::Api> {
let dual_yield_token_mapper = self.dual_yield_token();
dual_yield_token_mapper.require_same_token(&payment.token_identifier);

let attributes: DualYieldTokenAttributes<Self::Api> =
self.get_attributes_as_part_of_fixed_supply(&payment, &dual_yield_token_mapper);

Check warning on line 47 in farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs#L47

warning: this expression creates a reference which is immediately dereferenced by the compiler --> farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs:47:57 | 47 | self.get_attributes_as_part_of_fixed_supply(&payment, &dual_yield_token_mapper); | ^^^^^^^^ help: change this to: `payment` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
Raw output
farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs:47:57:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
  --> farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs:47:57
   |
47 |             self.get_attributes_as_part_of_fixed_supply(&payment, &dual_yield_token_mapper);
   |                                                         ^^^^^^^^ help: change this to: `payment`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
   = note: `#[warn(clippy::needless_borrow)]` on by default


__END__

Check warning on line 47 in farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs#L47

warning: this expression creates a reference which is immediately dereferenced by the compiler --> farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs:47:57 | 47 | self.get_attributes_as_part_of_fixed_supply(&payment, &dual_yield_token_mapper); | ^^^^^^^^ help: change this to: `payment` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
Raw output
farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs:47:57:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
  --> farm-staking/farm-staking-proxy/src/proxy_actions/claim.rs:47:57
   |
47 |             self.get_attributes_as_part_of_fixed_supply(&payment, &dual_yield_token_mapper);
   |                                                         ^^^^^^^^ help: change this to: `payment`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
   = note: `#[warn(clippy::needless_borrow)]` on by default


__END__

let lp_tokens_in_position = self.get_lp_tokens_in_farm_position(
attributes.lp_farm_token_nonce,
Expand All @@ -81,23 +64,33 @@ pub trait ProxyClaimModule:
orig_caller,
staking_farm_token_id,
attributes.staking_farm_token_nonce,
staking_claim_amount,
attributes.staking_farm_token_amount,
new_staking_farm_value,
);

let new_lp_farm_tokens = lp_farm_claim_rewards_result.new_lp_farm_tokens;
let new_staking_farm_tokens = staking_farm_claim_rewards_result.new_staking_farm_tokens;
let new_attributes = DualYieldTokenAttributes {
let new_dual_yield_attributes = DualYieldTokenAttributes {
lp_farm_token_nonce: new_lp_farm_tokens.token_nonce,
lp_farm_token_amount: new_lp_farm_tokens.amount,
staking_farm_token_nonce: new_staking_farm_tokens.token_nonce,
staking_farm_token_amount: new_staking_farm_tokens.amount,
};

InternalClaimResult {
lp_farm_rewards: lp_farm_claim_rewards_result.lp_farm_rewards,
staking_farm_rewards: staking_farm_claim_rewards_result.staking_farm_rewards,
new_dual_yield_attributes: new_attributes,
}
let new_dual_yield_tokens =
self.create_dual_yield_tokens(&dual_yield_token_mapper, &new_dual_yield_attributes);

let lp_farm_rewards = lp_farm_claim_rewards_result.lp_farm_rewards;
let staking_farm_rewards = staking_farm_claim_rewards_result.staking_farm_rewards;

let claim_result = ClaimDualYieldResult {
lp_farm_rewards,
staking_farm_rewards,
new_dual_yield_tokens,
};

dual_yield_token_mapper.nft_burn(payment.token_nonce, &payment.amount);

claim_result
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
multiversx_sc::imports!();

use common_structs::FarmTokenAttributes;

use crate::{
dual_yield_token::DualYieldTokenAttributes,
result_types::{ClaimDualYieldResult, StakeProxyResult},
};

multiversx_sc::imports!();

#[multiversx_sc::module]
pub trait ProxyExternalInteractionsModule:
crate::dual_yield_token::DualYieldTokenModule
+ crate::external_contracts_interactions::ExternalContractsInteractionsModule
+ crate::lp_farm_token::LpFarmTokenModule
+ crate::proxy_actions::stake::ProxyStakeModule
+ crate::proxy_actions::claim::ProxyClaimModule
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ utils::UtilsModule
+ token_send::TokenSendModule
Expand All @@ -25,76 +27,9 @@ pub trait ProxyExternalInteractionsModule:
self.require_user_whitelisted(&original_owner, &caller);

let payments = self.get_non_empty_payments();
let lp_farm_token_payment = payments.get(0);
let additional_payments = payments.slice(1, payments.len()).unwrap_or_default();

self.check_stake_farm_payments(
&original_owner,
&lp_farm_token_payment,
&additional_payments,
);
let lp_farm_token_id = self.lp_farm_token_id().get();
require!(
lp_farm_token_payment.token_identifier == lp_farm_token_id,
"Invalid first payment"
);

let dual_yield_token_mapper = self.dual_yield_token();
let staking_farm_token_id = self.staking_farm_token_id().get();
let mut additional_staking_farm_tokens = ManagedVec::new();
let mut additional_lp_farm_tokens = ManagedVec::new();
for p in &additional_payments {
let attributes: DualYieldTokenAttributes<Self::Api> =
self.get_attributes_as_part_of_fixed_supply(&p, &dual_yield_token_mapper);

additional_staking_farm_tokens.push(EsdtTokenPayment::new(
staking_farm_token_id.clone(),
attributes.staking_farm_token_nonce,
attributes.staking_farm_token_amount,
));

additional_lp_farm_tokens.push(EsdtTokenPayment::new(
lp_farm_token_payment.token_identifier.clone(),
attributes.lp_farm_token_nonce,
attributes.lp_farm_token_amount,
));

dual_yield_token_mapper.nft_burn(p.token_nonce, &p.amount);
}
self.check_stake_farm_payments(&original_owner, &payments);

let lp_tokens_in_farm = self.get_lp_tokens_in_farm_position(
lp_farm_token_payment.token_nonce,
&lp_farm_token_payment.amount,
);
let staking_token_amount = self.get_lp_tokens_safe_price(lp_tokens_in_farm);
let staking_farm_enter_result = self.staking_farm_enter(
original_owner.clone(),
staking_token_amount,
additional_staking_farm_tokens,
);
let received_staking_farm_token = staking_farm_enter_result.received_staking_farm_token;

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

let new_attributes = DualYieldTokenAttributes {
lp_farm_token_nonce: merged_lp_farm_tokens.token_nonce,
lp_farm_token_amount: merged_lp_farm_tokens.amount,
staking_farm_token_nonce: received_staking_farm_token.token_nonce,
staking_farm_token_amount: received_staking_farm_token.amount,
};
let new_dual_yield_tokens =
self.create_dual_yield_tokens(&dual_yield_token_mapper, &new_attributes);
let output_payments = StakeProxyResult {
dual_yield_tokens: new_dual_yield_tokens,
staking_boosted_rewards: staking_farm_enter_result.boosted_rewards,
lp_farm_boosted_rewards,
};
let output_payments = self.stake_farm_tokens_common(original_owner.clone(), payments);

self.send_payment_non_zero(&original_owner, &output_payments.lp_farm_boosted_rewards);
self.send_payment_non_zero(&original_owner, &output_payments.staking_boosted_rewards);
Expand All @@ -106,113 +41,78 @@ pub trait ProxyExternalInteractionsModule:
#[payable("*")]
#[endpoint(claimDualYieldOnBehalf)]
fn claim_dual_yield_on_behalf(&self) -> ClaimDualYieldResult<Self::Api> {
let caller = self.blockchain().get_caller();

let payment = self.call_value().single_esdt();
let dual_yield_token_mapper = self.dual_yield_token();
dual_yield_token_mapper.require_same_token(&payment.token_identifier);

let attributes: DualYieldTokenAttributes<Self::Api> =
self.get_attributes_as_part_of_fixed_supply(&payment, &dual_yield_token_mapper);

let original_owner = self.get_farm_position_original_owner(attributes.lp_farm_token_nonce);
let caller = self.blockchain().get_caller();
let original_owner = self.get_underlying_farm_position_original_owner(&payment);
self.require_user_whitelisted(&original_owner, &caller);

let lp_tokens_in_position = self.get_lp_tokens_in_farm_position(
attributes.lp_farm_token_nonce,
&attributes.lp_farm_token_amount,
);
let new_staking_farm_value = self.get_lp_tokens_safe_price(lp_tokens_in_position);

let staking_farm_token_id = self.staking_farm_token_id().get();
let lp_farm_token_id = self.lp_farm_token_id().get();
let lp_farm_claim_rewards_result = self.lp_farm_claim_rewards(
original_owner.clone(),
lp_farm_token_id,
attributes.lp_farm_token_nonce,
attributes.lp_farm_token_amount,
);
let staking_farm_claim_rewards_result = self.staking_farm_claim_rewards(
original_owner.clone(),
staking_farm_token_id,
attributes.staking_farm_token_nonce,
attributes.staking_farm_token_amount,
new_staking_farm_value,
);

let new_lp_farm_tokens = lp_farm_claim_rewards_result.new_lp_farm_tokens;
let new_staking_farm_tokens = staking_farm_claim_rewards_result.new_staking_farm_tokens;
let new_attributes = DualYieldTokenAttributes {
lp_farm_token_nonce: new_lp_farm_tokens.token_nonce,
lp_farm_token_amount: new_lp_farm_tokens.amount,
staking_farm_token_nonce: new_staking_farm_tokens.token_nonce,
staking_farm_token_amount: new_staking_farm_tokens.amount,
};

let lp_farm_rewards = lp_farm_claim_rewards_result.lp_farm_rewards;
let staking_farm_rewards = staking_farm_claim_rewards_result.staking_farm_rewards;
let new_dual_yield_attributes = new_attributes;

let new_dual_yield_tokens =
self.create_dual_yield_tokens(&dual_yield_token_mapper, &new_dual_yield_attributes);
let claim_result = ClaimDualYieldResult {
lp_farm_rewards,
staking_farm_rewards,
new_dual_yield_tokens,
};
let claim_result = self.claim_dual_yield_common(original_owner.clone(), &payment);

self.send_payment_non_zero(&original_owner, &claim_result.lp_farm_rewards);
self.send_payment_non_zero(&original_owner, &claim_result.staking_farm_rewards);
self.send_payment_non_zero(&caller, &claim_result.new_dual_yield_tokens);

dual_yield_token_mapper.nft_burn(payment.token_nonce, &payment.amount);

claim_result
}

fn check_stake_farm_payments(
&self,
original_owner: &ManagedAddress,
first_payment: &EsdtTokenPayment,
additional_payments: &ManagedVec<EsdtTokenPayment>,
payments: &ManagedVec<EsdtTokenPayment>,
) {
let dual_yield_token_mapper = self.dual_yield_token();
let dual_yield_token_id = dual_yield_token_mapper.get_token_id();
let lp_farm_token_id = self.lp_farm_token_id().get();
let lp_farm_token_payment = payments.get(0);
let additional_payments = payments.slice(1, payments.len()).unwrap_or_default();

let lp_farm_token_id = self.lp_farm_token_id().get();
require!(
first_payment.token_identifier == lp_farm_token_id,
lp_farm_token_payment.token_identifier == lp_farm_token_id,
"Invalid first payment"
);

let attributes = self
.blockchain()
.get_token_attributes::<FarmTokenAttributes<Self::Api>>(
&lp_farm_token_payment.token_identifier,
lp_farm_token_payment.token_nonce,
);

require!(
&self.get_farm_position_original_owner(first_payment.token_nonce) == original_owner,
&attributes.original_owner == original_owner,
"Provided address is not the same as the original owner"
);

for payment in additional_payments.into_iter() {
if payment.token_identifier != dual_yield_token_id {
sc_panic!("Wrong additional payments");
}

let attributes: DualYieldTokenAttributes<Self::Api> =
dual_yield_token_mapper.get_token_attributes(payment.token_nonce);
require!(
&self.get_farm_position_original_owner(attributes.lp_farm_token_nonce)
== original_owner,
&self.get_underlying_farm_position_original_owner(&payment) == original_owner,
"Provided address is not the same as the original owner"
);
}
}

fn get_farm_position_original_owner(&self, farm_token_nonce: u64) -> ManagedAddress {
fn get_underlying_farm_position_original_owner(
&self,
payment: &EsdtTokenPayment,
) -> ManagedAddress {
let dual_yield_token_mapper = self.dual_yield_token();
dual_yield_token_mapper.require_same_token(&payment.token_identifier);

let attributes: DualYieldTokenAttributes<Self::Api> =
self.get_attributes_as_part_of_fixed_supply(&payment, &dual_yield_token_mapper);

Check warning on line 101 in farm-staking/farm-staking-proxy/src/proxy_actions/external_interaction.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] farm-staking/farm-staking-proxy/src/proxy_actions/external_interaction.rs#L101

warning: this expression creates a reference which is immediately dereferenced by the compiler --> farm-staking/farm-staking-proxy/src/proxy_actions/external_interaction.rs:101:57 | 101 | self.get_attributes_as_part_of_fixed_supply(&payment, &dual_yield_token_mapper); | ^^^^^^^^ help: change this to: `payment` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
Raw output
farm-staking/farm-staking-proxy/src/proxy_actions/external_interaction.rs:101:57:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> farm-staking/farm-staking-proxy/src/proxy_actions/external_interaction.rs:101:57
    |
101 |             self.get_attributes_as_part_of_fixed_supply(&payment, &dual_yield_token_mapper);
    |                                                         ^^^^^^^^ help: change this to: `payment`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow


__END__

Check warning on line 101 in farm-staking/farm-staking-proxy/src/proxy_actions/external_interaction.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] farm-staking/farm-staking-proxy/src/proxy_actions/external_interaction.rs#L101

warning: this expression creates a reference which is immediately dereferenced by the compiler --> farm-staking/farm-staking-proxy/src/proxy_actions/external_interaction.rs:101:57 | 101 | self.get_attributes_as_part_of_fixed_supply(&payment, &dual_yield_token_mapper); | ^^^^^^^^ help: change this to: `payment` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
Raw output
farm-staking/farm-staking-proxy/src/proxy_actions/external_interaction.rs:101:57:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> farm-staking/farm-staking-proxy/src/proxy_actions/external_interaction.rs:101:57
    |
101 |             self.get_attributes_as_part_of_fixed_supply(&payment, &dual_yield_token_mapper);
    |                                                         ^^^^^^^^ help: change this to: `payment`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow


__END__

let lp_farm_token_id = self.lp_farm_token_id().get();
let attributes = self
.blockchain()
.get_token_attributes::<FarmTokenAttributes<Self::Api>>(
&lp_farm_token_id,
farm_token_nonce,
attributes.lp_farm_token_nonce,
);

require!(
attributes.original_owner != ManagedAddress::zero(),
"Invalid original owner"
);

attributes.original_owner
}

Expand Down
17 changes: 14 additions & 3 deletions farm-staking/farm-staking-proxy/src/proxy_actions/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ pub trait ProxyStakeModule:
let caller = self.blockchain().get_caller();
let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller);
let payments = self.get_non_empty_payments();

let output_payments = self.stake_farm_tokens_common(orig_caller, payments);

output_payments.send_and_return(self, &caller)
}

fn stake_farm_tokens_common(
&self,
original_caller: ManagedAddress,
payments: ManagedVec<EsdtTokenPayment>,
) -> StakeProxyResult<Self::Api> {
let lp_farm_token_payment = payments.get(0);
let additional_payments = payments.slice(1, payments.len()).unwrap_or_default();

Expand Down Expand Up @@ -62,15 +73,15 @@ pub trait ProxyStakeModule:
);
let staking_token_amount = self.get_lp_tokens_safe_price(lp_tokens_in_farm);
let staking_farm_enter_result = self.staking_farm_enter(
orig_caller.clone(),
original_caller.clone(),
staking_token_amount,
additional_staking_farm_tokens,
);
let received_staking_farm_token = staking_farm_enter_result.received_staking_farm_token;

let (merged_lp_farm_tokens, lp_farm_boosted_rewards) = self
.merge_lp_farm_tokens(
orig_caller,
original_caller,
lp_farm_token_payment,
additional_lp_farm_tokens,
)
Expand All @@ -90,6 +101,6 @@ pub trait ProxyStakeModule:
lp_farm_boosted_rewards,
};

output_payments.send_and_return(self, &caller)
output_payments
}
}
Loading

0 comments on commit 5ef4b70

Please sign in to comment.