Skip to content

Commit

Permalink
Merge pull request #970 from multiversx/no-more-add-liq-with-locked-t…
Browse files Browse the repository at this point in the history
…okens

Disable add liquidity for locked tokens
  • Loading branch information
dorin-iancu authored Nov 13, 2024
2 parents a2f0007 + cca4b51 commit 94d6506
Show file tree
Hide file tree
Showing 18 changed files with 154 additions and 45 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

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

12 changes: 12 additions & 0 deletions common/modules/disable-add-liq/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "disable-add-liq"
version = "0.0.0"
authors = ["Dorin Iancu <[email protected]>"]
edition = "2021"

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

[dependencies.multiversx-sc]
version = "=0.53.2"
features = ["esdt-token-payment-legacy-decode"]
32 changes: 32 additions & 0 deletions common/modules/disable-add-liq/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![no_std]

multiversx_sc::imports!();

pub const ADD_LIQ_ENABLED: bool = false;
pub const ADD_LIQ_DISABLED: bool = true;

#[multiversx_sc::module]
pub trait DisableAddLiqModule {
#[only_owner]
#[endpoint(enableAddLiq)]
fn enable_add_liq(&self) {
self.add_liq_disabled().set(ADD_LIQ_ENABLED);
}

#[only_owner]
#[endpoint(disableAddLiq)]
fn disable_add_liq(&self) {
self.add_liq_disabled().set(ADD_LIQ_DISABLED);
}

fn require_add_liq_enabled(&self) {
require!(
self.add_liq_disabled().get() == ADD_LIQ_ENABLED,
"Add Liquidity is disabled"
);
}

#[view(isAddLiqDisabled)]
#[storage_mapper("addLiqDisabled")]
fn add_liq_disabled(&self) -> SingleValueMapper<bool>;
}
6 changes: 3 additions & 3 deletions locked-asset/energy-factory/src/lock_options.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use common_structs::Epoch;
use common_structs::{Epoch, Percent};
use unwrappable::Unwrappable;

pub const EPOCHS_PER_MONTH: Epoch = 30;
pub const EPOCHS_PER_YEAR: Epoch = 12 * EPOCHS_PER_MONTH;
pub const MAX_PENALTY_PERCENTAGE: u64 = 10_000; // 100%
pub const MAX_PENALTY_PERCENTAGE: Percent = 10_000; // 100%

#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode, Clone, Copy, Default)]
pub struct LockOption {
pub lock_epochs: Epoch,
pub penalty_start_percentage: u64,
pub penalty_start_percentage: Percent,
}

pub const MAX_LOCK_OPTIONS: usize = 10;
Expand Down
3 changes: 3 additions & 0 deletions locked-asset/proxy_dex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ path = "../simple-lock"
[dependencies.sc_whitelist_module]
path = "../../common/modules/sc_whitelist_module"

[dependencies.disable-add-liq]
path = "../../common/modules/disable-add-liq"

[dev-dependencies]
num-bigint = "0.4.2"
num-traits = "0.2"
Expand Down
1 change: 1 addition & 0 deletions locked-asset/proxy_dex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub trait ProxyDexImpl:
+ utils::UtilsModule
+ legacy_token_decode_module::LegacyTokenDecodeModule
+ sc_whitelist_module::SCWhitelistModule
+ disable_add_liq::DisableAddLiqModule
{
#[init]
fn init(
Expand Down
61 changes: 38 additions & 23 deletions locked-asset/proxy_dex/src/pair_interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ use pair::pair_actions::{
remove_liq::ProxyTrait as _,
};

pub struct CallRemoveLiqArgs<M: ManagedTypeApi> {
pub pair_address: ManagedAddress<M>,
pub lp_token_id: TokenIdentifier<M>,
pub lp_token_amount: BigUint<M>,
pub first_token_amount_min: BigUint<M>,
pub second_token_amount_min: BigUint<M>,
}

pub struct CallAddLiqArgs<M: ManagedTypeApi> {
pub pair_address: ManagedAddress<M>,
pub first_token_id: TokenIdentifier<M>,
pub first_token_amount_desired: BigUint<M>,
pub first_token_amount_min: BigUint<M>,
pub second_token_id: TokenIdentifier<M>,
pub second_token_amount_desired: BigUint<M>,
pub second_token_amount_min: BigUint<M>,
}

pub struct AddLiquidityResultWrapper<M: ManagedTypeApi> {
pub lp_tokens_received: EsdtTokenPayment<M>,
pub first_token_leftover: EsdtTokenPayment<M>,
Expand All @@ -21,32 +39,33 @@ pub struct RemoveLiqudityResultWrapper<M: ManagedTypeApi> {
pub trait PairInteractionsModule {
fn call_add_liquidity(
&self,
pair_address: ManagedAddress,
first_token_id: TokenIdentifier,
first_token_amount_desired: BigUint,
first_token_amount_min: BigUint,
second_token_id: TokenIdentifier,
second_token_amount_desired: BigUint,
second_token_amount_min: BigUint,
args: CallAddLiqArgs<Self::Api>,
) -> AddLiquidityResultWrapper<Self::Api> {
let first_payment =
EsdtTokenPayment::new(first_token_id, 0, first_token_amount_desired.clone());
let second_payment =
EsdtTokenPayment::new(second_token_id, 0, second_token_amount_desired.clone());
let first_payment = EsdtTokenPayment::new(
args.first_token_id,
0,
args.first_token_amount_desired.clone(),
);
let second_payment = EsdtTokenPayment::new(
args.second_token_id,
0,
args.second_token_amount_desired.clone(),
);

let mut all_token_payments = ManagedVec::new();
all_token_payments.push(first_payment);
all_token_payments.push(second_payment);

let raw_result: AddLiquidityResultType<Self::Api> = self
.pair_contract_proxy(pair_address)
.add_liquidity(first_token_amount_min, second_token_amount_min)
.pair_contract_proxy(args.pair_address)
.add_liquidity(args.first_token_amount_min, args.second_token_amount_min)
.with_multi_token_transfer(all_token_payments)
.execute_on_dest_context();
let (lp_tokens_received, first_tokens_used, second_tokens_used) = raw_result.into_tuple();
let first_token_leftover_amount = &first_token_amount_desired - &first_tokens_used.amount;
let first_token_leftover_amount =
&args.first_token_amount_desired - &first_tokens_used.amount;
let second_token_leftover_amount =
&second_token_amount_desired - &second_tokens_used.amount;
&args.second_token_amount_desired - &second_tokens_used.amount;

let first_token_leftover = EsdtTokenPayment::new(
first_tokens_used.token_identifier,
Expand All @@ -68,16 +87,12 @@ pub trait PairInteractionsModule {

fn call_remove_liquidity(
&self,
pair_address: ManagedAddress,
lp_token_id: TokenIdentifier,
lp_token_amount: BigUint,
first_token_amount_min: BigUint,
second_token_amount_min: BigUint,
args: CallRemoveLiqArgs<Self::Api>,
) -> RemoveLiqudityResultWrapper<Self::Api> {
let raw_result: RemoveLiquidityResultType<Self::Api> = self
.pair_contract_proxy(pair_address)
.remove_liquidity(first_token_amount_min, second_token_amount_min)
.with_esdt_transfer((lp_token_id, 0, lp_token_amount))
.pair_contract_proxy(args.pair_address)
.remove_liquidity(args.first_token_amount_min, args.second_token_amount_min)
.with_esdt_transfer((args.lp_token_id, 0, args.lp_token_amount))
.execute_on_dest_context();
let (first_token_received, second_token_received) = raw_result.into_tuple();

Expand Down
1 change: 1 addition & 0 deletions locked-asset/proxy_dex/src/proxy_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub trait ProxyFarmModule:
+ utils::UtilsModule
+ legacy_token_decode_module::LegacyTokenDecodeModule
+ sc_whitelist_module::SCWhitelistModule
+ disable_add_liq::DisableAddLiqModule
{
#[payable("*")]
#[endpoint(enterFarmProxy)]
Expand Down
33 changes: 18 additions & 15 deletions locked-asset/proxy_dex/src/proxy_pair.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#![allow(clippy::too_many_arguments)]
#![allow(clippy::comparison_chain)]
#![allow(clippy::vec_init_then_push)]

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use crate::wrapped_lp_attributes::{WrappedLpToken, WrappedLpTokenAttributes};
use crate::{
pair_interactions::{CallAddLiqArgs, CallRemoveLiqArgs},
wrapped_lp_attributes::{WrappedLpToken, WrappedLpTokenAttributes},
};
use common_structs::Epoch;
use fixed_supply_token::FixedSupplyToken;

Expand All @@ -22,6 +23,7 @@ pub trait ProxyPairModule:
+ token_send::TokenSendModule
+ utils::UtilsModule
+ legacy_token_decode_module::LegacyTokenDecodeModule
+ disable_add_liq::DisableAddLiqModule
{
#[payable("*")]
#[endpoint(addLiquidityProxy)]
Expand All @@ -33,6 +35,7 @@ pub trait ProxyPairModule:
) -> MultiValueEncoded<EsdtTokenPayment> {
self.require_is_intermediated_pair(&pair_address);
self.require_wrapped_lp_token_id_not_empty();
self.require_add_liq_enabled();

let caller = self.blockchain().get_caller();
let mut payments = self.get_non_empty_payments();
Expand All @@ -49,15 +52,15 @@ pub trait ProxyPairModule:
self.get_underlying_token(first_payment.token_identifier.clone());
let second_unlocked_token_id =
self.get_underlying_token(second_payment.token_identifier.clone());
let add_liq_result = self.call_add_liquidity(
pair_address.clone(),
first_unlocked_token_id,
first_payment.amount.clone(),
let add_liq_result = self.call_add_liquidity(CallAddLiqArgs {
pair_address: pair_address.clone(),
first_token_id: first_unlocked_token_id,
first_token_amount_desired: first_payment.amount.clone(),
first_token_amount_min,
second_unlocked_token_id,
second_payment.amount.clone(),
second_token_id: second_unlocked_token_id,
second_token_amount_desired: second_payment.amount.clone(),
second_token_amount_min,
);
});

let mut locked_token_used = input_token_refs.locked_token_ref.clone();
locked_token_used.amount = if input_token_refs.locked_token_ref.token_identifier
Expand Down Expand Up @@ -170,13 +173,13 @@ pub trait ProxyPairModule:
let attributes: WrappedLpTokenAttributes<Self::Api> =
self.get_attributes_as_part_of_fixed_supply(&input_payment, &wrapped_lp_mapper);

let remove_liq_result = self.call_remove_liquidity(
pair_address.clone(),
attributes.lp_token_id.clone(),
attributes.lp_token_amount.clone(),
let remove_liq_result = self.call_remove_liquidity(CallRemoveLiqArgs {
pair_address: pair_address.clone(),
lp_token_id: attributes.lp_token_id.clone(),
lp_token_amount: attributes.lp_token_amount.clone(),
first_token_amount_min,
second_token_amount_min,
);
});
let received_token_refs = self.require_exactly_one_base_asset(
&remove_liq_result.first_token_received,
&remove_liq_result.second_token_received,
Expand Down
9 changes: 9 additions & 0 deletions locked-asset/proxy_dex/wasm/Cargo.lock

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

7 changes: 5 additions & 2 deletions locked-asset/proxy_dex/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

// Init: 1
// Upgrade: 1
// Endpoints: 30
// Endpoints: 33
// Async Callback: 1
// Total number of exported functions: 33
// Total number of exported functions: 36

#![no_std]

Expand Down Expand Up @@ -50,6 +50,9 @@ multiversx_sc_wasm_adapter::endpoints! {
addSCAddressToWhitelist => add_sc_address_to_whitelist
removeSCAddressFromWhitelist => remove_sc_address_from_whitelist
isSCAddressWhitelisted => is_sc_address_whitelisted
enableAddLiq => enable_add_liq
disableAddLiq => disable_add_liq
isAddLiqDisabled => add_liq_disabled
)
}

Expand Down
3 changes: 3 additions & 0 deletions locked-asset/simple-lock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ version = "=0.53.2"
[dependencies.common_structs]
path = "../../common/common_structs"

[dependencies.disable-add-liq]
path = "../../common/modules/disable-add-liq"

[dev-dependencies]
num-bigint = "0.4.2"
num-traits = "0.2"
Expand Down
1 change: 1 addition & 0 deletions locked-asset/simple-lock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub trait SimpleLock:
+ lp_interactions::LpInteractionsModule
+ farm_interactions::FarmInteractionsModule
+ token_attributes::TokenAttributesModule
+ disable_add_liq::DisableAddLiqModule
{
#[init]
fn init(&self) {}
Expand Down
1 change: 1 addition & 0 deletions locked-asset/simple-lock/src/lp_interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct RemoveLiquidityResultWrapper<M: ManagedTypeApi> {
// This avoids circular dependency
mod lp_proxy {
multiversx_sc::imports!();

use super::{AddLiquidityResultType, RemoveLiquidityResultType};

#[multiversx_sc::proxy]
Expand Down
1 change: 1 addition & 0 deletions locked-asset/simple-lock/src/proxy_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub trait ProxyFarmModule:
+ crate::proxy_lp::ProxyLpModule
+ crate::token_attributes::TokenAttributesModule
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ disable_add_liq::DisableAddLiqModule
{
#[only_owner]
#[payable("EGLD")]
Expand Down
3 changes: 3 additions & 0 deletions locked-asset/simple-lock/src/proxy_lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub trait ProxyLpModule:
+ crate::lp_interactions::LpInteractionsModule
+ crate::token_attributes::TokenAttributesModule
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ disable_add_liq::DisableAddLiqModule
{
#[only_owner]
#[payable("EGLD")]
Expand Down Expand Up @@ -136,6 +137,8 @@ pub trait ProxyLpModule:
first_token_amount_min: BigUint,
second_token_amount_min: BigUint,
) -> AddLiquidityThroughProxyResultType<Self::Api> {
self.require_add_liq_enabled();

let [first_payment, second_payment] = self.call_value().multi_esdt();
let (mut first_payment_unlocked_wrapper, mut second_payment_unlocked_wrapper) =
self.unlock_lp_payments(first_payment, second_payment);
Expand Down
Loading

0 comments on commit 94d6506

Please sign in to comment.