Skip to content

Commit

Permalink
Farm Staking Proxy Legacy SC
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Jun 23, 2024
1 parent 4320a90 commit c2a140a
Show file tree
Hide file tree
Showing 15 changed files with 924 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ members = [

"legacy-contracts/simple-lock-legacy",
"legacy-contracts/simple-lock-legacy/meta",
"legacy-contracts/farm-staking-proxy-legacy",
"legacy-contracts/farm-staking-proxy-legacy/meta",

"locked-asset/",
"locked-asset/distribution",
Expand Down
7 changes: 7 additions & 0 deletions legacy-contracts/farm-staking-proxy-legacy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
/target/
*/target/

# The erdpy output
output
30 changes: 30 additions & 0 deletions legacy-contracts/farm-staking-proxy-legacy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "farm-staking-proxy-legacy"
version = "0.0.0"
authors = ["MultiversX <contact@multiversx.com>"]
edition = "2021"
publish = false

[lib]
path = "src/lib.rs"

[dependencies.multiversx-sc]
version = "=0.50.4"
features = ["esdt-token-payment-legacy-decode"]

[dependencies.multiversx-sc-modules]
version = "=0.50.4"

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

[dependencies.common_structs]
path = "../../common/common_structs"

[dev-dependencies]
num-bigint = "0.4.2"
num-traits = "0.2"
hex = "0.4"

[dev-dependencies.multiversx-sc-scenario]
version = "=0.50.4"
13 changes: 13 additions & 0 deletions legacy-contracts/farm-staking-proxy-legacy/meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "farm-staking-proxy-legacy-meta"
version = "0.0.0"
edition = "2021"
publish = false
authors = ["MultiversX <contact@multiversx.com>"]

[dependencies.farm-staking-proxy-legacy]
path = ".."

[dependencies.multiversx-sc-meta]
version = "0.50.4"
default-features = false
3 changes: 3 additions & 0 deletions legacy-contracts/farm-staking-proxy-legacy/meta/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
multiversx_sc_meta::cli_main::<farm_staking_proxy_legacy::AbiProvider>();
}
3 changes: 3 additions & 0 deletions legacy-contracts/farm-staking-proxy-legacy/multiversx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"language": "rust"
}
108 changes: 108 additions & 0 deletions legacy-contracts/farm-staking-proxy-legacy/src/dual_yield_token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[derive(TypeAbi, TopEncode, TopDecode, PartialEq, Debug)]
pub struct DualYieldTokenAttributes<M: ManagedTypeApi> {
pub lp_farm_token_nonce: u64,
pub lp_farm_token_amount: BigUint<M>,
pub staking_farm_token_nonce: u64,
pub staking_farm_token_amount: BigUint<M>,
}

impl<M: ManagedTypeApi> DualYieldTokenAttributes<M> {
/// dual yield tokens are always created with an amount equal to staking_farm_token_amount,
/// so we just return this field instead of duplicating
#[inline]
pub fn get_total_dual_yield_tokens_for_position(&self) -> &BigUint<M> {
&self.staking_farm_token_amount
}
}

#[multiversx_sc::module]
pub trait DualYieldTokenModule: token_merge_helper::TokenMergeHelperModule {
#[only_owner]
#[endpoint(setTransferRoleDualYieldToken)]
fn set_transfer_role_dual_yield_token(&self, opt_address: OptionalValue<ManagedAddress>) {
let address = self.resolve_address(opt_address);
let token_id = self.dual_yield_token_id().get();
let roles = [EsdtLocalRole::Transfer];

self.send()
.esdt_system_sc_proxy()
.set_special_roles(&address, &token_id, roles.iter().cloned())
.async_call_and_exit()
}

fn resolve_address(&self, opt_address: OptionalValue<ManagedAddress>) -> ManagedAddress {
match opt_address {
OptionalValue::Some(addr) => addr,
OptionalValue::None => self.blockchain().get_sc_address(),
}
}

fn require_dual_yield_token(&self, token_id: &TokenIdentifier) {
let dual_yield_token_id = self.dual_yield_token_id().get();
require!(token_id == &dual_yield_token_id, "Invalid payment token");
}

fn require_all_payments_dual_yield_tokens(
&self,
payments: &ManagedVec<EsdtTokenPayment<Self::Api>>,
) {
if payments.is_empty() {
return;
}

let dual_yield_token_id = self.dual_yield_token_id().get();
for p in payments {
require!(
p.token_identifier == dual_yield_token_id,
"Invalid payment token"
);
}
}

fn burn_dual_yield_tokens(&self, sft_nonce: u64, amount: &BigUint) {
let dual_yield_token_id = self.dual_yield_token_id().get();
self.send()
.esdt_local_burn(&dual_yield_token_id, sft_nonce, amount);
}

fn get_dual_yield_token_attributes(
&self,
dual_yield_token_nonce: u64,
) -> DualYieldTokenAttributes<Self::Api> {
let own_sc_address = self.blockchain().get_sc_address();
let dual_yield_token_id = self.dual_yield_token_id().get();
let token_info = self.blockchain().get_esdt_token_data(
&own_sc_address,
&dual_yield_token_id,
dual_yield_token_nonce,
);

token_info.decode_attributes()
}

fn get_lp_farm_token_amount_equivalent(
&self,
attributes: &DualYieldTokenAttributes<Self::Api>,
amount: &BigUint,
) -> BigUint {
self.rule_of_three_non_zero_result(
amount,
attributes.get_total_dual_yield_tokens_for_position(),
&attributes.lp_farm_token_amount,
)
}

#[inline]
fn get_staking_farm_token_amount_equivalent(&self, amount: &BigUint) -> BigUint {
// since staking_farm_token_amount is equal to the total dual yield tokens,
// we simply return the amount
amount.clone()
}

#[view(getDualYieldTokenId)]
#[storage_mapper("dualYieldTokenId")]
fn dual_yield_token_id(&self) -> SingleValueMapper<TokenIdentifier>;
}
Loading

0 comments on commit c2a140a

Please sign in to comment.