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 Proxy Legacy SC #928

Merged
merged 5 commits into from
Jul 3, 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
49 changes: 49 additions & 0 deletions Cargo.lock

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

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

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

"locked-asset/",
"locked-asset/distribution",
Expand Down
7 changes: 7 additions & 0 deletions legacy-contracts/farm-staking-proxy-v-13/.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
60 changes: 60 additions & 0 deletions legacy-contracts/farm-staking-proxy-v-13/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[package]
name = "farm-staking-proxy-v-13"
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"

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

[dependencies.pair]
path = "../../dex/pair"

[dependencies.farm-v-13]
path = "../farm-v-13"

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

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

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

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

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

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

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

[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-v-13/meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "farm-staking-proxy-v-13-meta"
version = "0.0.0"
edition = "2021"
publish = false
authors = ["MultiversX <contact@multiversx.com>"]

[dependencies.farm-staking-proxy-v-13]
path = ".."

[dependencies.multiversx-sc-meta]
version = "0.50.4"
default-features = false
3 changes: 3 additions & 0 deletions legacy-contracts/farm-staking-proxy-v-13/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_v_13::AbiProvider>();
}
3 changes: 3 additions & 0 deletions legacy-contracts/farm-staking-proxy-v-13/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-v-13/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
Loading