diff --git a/Cargo.lock b/Cargo.lock index 724f6ca9d..ffba89587 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -342,6 +342,13 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "distribution-tests" version = "0.0.0" @@ -1587,6 +1594,7 @@ version = "0.0.0" dependencies = [ "common_structs", "config", + "disable-add-liq", "energy-factory", "energy-query", "farm", @@ -1822,6 +1830,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "hex", "multiversx-sc", "multiversx-sc-modules", diff --git a/common/modules/disable-add-liq/Cargo.toml b/common/modules/disable-add-liq/Cargo.toml new file mode 100644 index 000000000..13194c5ca --- /dev/null +++ b/common/modules/disable-add-liq/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "disable-add-liq" +version = "0.0.0" +authors = ["Dorin Iancu "] +edition = "2021" + +[lib] +path = "src/lib.rs" + +[dependencies.multiversx-sc] +version = "=0.53.2" +features = ["esdt-token-payment-legacy-decode"] diff --git a/common/modules/disable-add-liq/src/lib.rs b/common/modules/disable-add-liq/src/lib.rs new file mode 100644 index 000000000..47fba58c5 --- /dev/null +++ b/common/modules/disable-add-liq/src/lib.rs @@ -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; +} diff --git a/locked-asset/energy-factory/src/lock_options.rs b/locked-asset/energy-factory/src/lock_options.rs index 2237d2f92..107ea971c 100644 --- a/locked-asset/energy-factory/src/lock_options.rs +++ b/locked-asset/energy-factory/src/lock_options.rs @@ -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; diff --git a/locked-asset/proxy_dex/Cargo.toml b/locked-asset/proxy_dex/Cargo.toml index e5023a79d..e1d220f10 100644 --- a/locked-asset/proxy_dex/Cargo.toml +++ b/locked-asset/proxy_dex/Cargo.toml @@ -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" diff --git a/locked-asset/proxy_dex/src/lib.rs b/locked-asset/proxy_dex/src/lib.rs index 047b087e8..ee5672a04 100644 --- a/locked-asset/proxy_dex/src/lib.rs +++ b/locked-asset/proxy_dex/src/lib.rs @@ -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( diff --git a/locked-asset/proxy_dex/src/pair_interactions.rs b/locked-asset/proxy_dex/src/pair_interactions.rs index 7e959a4e1..f04d0d6ac 100644 --- a/locked-asset/proxy_dex/src/pair_interactions.rs +++ b/locked-asset/proxy_dex/src/pair_interactions.rs @@ -6,6 +6,24 @@ use pair::pair_actions::{ remove_liq::ProxyTrait as _, }; +pub struct CallRemoveLiqArgs { + pub pair_address: ManagedAddress, + pub lp_token_id: TokenIdentifier, + pub lp_token_amount: BigUint, + pub first_token_amount_min: BigUint, + pub second_token_amount_min: BigUint, +} + +pub struct CallAddLiqArgs { + pub pair_address: ManagedAddress, + pub first_token_id: TokenIdentifier, + pub first_token_amount_desired: BigUint, + pub first_token_amount_min: BigUint, + pub second_token_id: TokenIdentifier, + pub second_token_amount_desired: BigUint, + pub second_token_amount_min: BigUint, +} + pub struct AddLiquidityResultWrapper { pub lp_tokens_received: EsdtTokenPayment, pub first_token_leftover: EsdtTokenPayment, @@ -21,32 +39,33 @@ pub struct RemoveLiqudityResultWrapper { 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, ) -> AddLiquidityResultWrapper { - 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 - .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, @@ -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, ) -> RemoveLiqudityResultWrapper { let raw_result: RemoveLiquidityResultType = 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(); diff --git a/locked-asset/proxy_dex/src/proxy_farm.rs b/locked-asset/proxy_dex/src/proxy_farm.rs index b90f00f69..9532463d3 100644 --- a/locked-asset/proxy_dex/src/proxy_farm.rs +++ b/locked-asset/proxy_dex/src/proxy_farm.rs @@ -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)] diff --git a/locked-asset/proxy_dex/src/proxy_pair.rs b/locked-asset/proxy_dex/src/proxy_pair.rs index 79542f06a..e17397d9d 100644 --- a/locked-asset/proxy_dex/src/proxy_pair.rs +++ b/locked-asset/proxy_dex/src/proxy_pair.rs @@ -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; @@ -22,6 +23,7 @@ pub trait ProxyPairModule: + token_send::TokenSendModule + utils::UtilsModule + legacy_token_decode_module::LegacyTokenDecodeModule + + disable_add_liq::DisableAddLiqModule { #[payable("*")] #[endpoint(addLiquidityProxy)] @@ -33,6 +35,7 @@ pub trait ProxyPairModule: ) -> MultiValueEncoded { 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(); @@ -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 @@ -170,13 +173,13 @@ pub trait ProxyPairModule: let attributes: WrappedLpTokenAttributes = 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, diff --git a/locked-asset/proxy_dex/wasm/Cargo.lock b/locked-asset/proxy_dex/wasm/Cargo.lock index 3330a21d1..78557ecb7 100644 --- a/locked-asset/proxy_dex/wasm/Cargo.lock +++ b/locked-asset/proxy_dex/wasm/Cargo.lock @@ -74,6 +74,14 @@ dependencies = [ "token_send", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "common_structs", + "multiversx-sc", +] + [[package]] name = "either" version = "1.13.0" @@ -438,6 +446,7 @@ name = "proxy_dex" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "energy-factory", "energy-query", "farm", diff --git a/locked-asset/proxy_dex/wasm/src/lib.rs b/locked-asset/proxy_dex/wasm/src/lib.rs index c04d97990..e5b1b3038 100644 --- a/locked-asset/proxy_dex/wasm/src/lib.rs +++ b/locked-asset/proxy_dex/wasm/src/lib.rs @@ -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] @@ -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 ) } diff --git a/locked-asset/simple-lock/Cargo.toml b/locked-asset/simple-lock/Cargo.toml index b867f2aa3..6d9409e17 100644 --- a/locked-asset/simple-lock/Cargo.toml +++ b/locked-asset/simple-lock/Cargo.toml @@ -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" diff --git a/locked-asset/simple-lock/src/lib.rs b/locked-asset/simple-lock/src/lib.rs index fb59b792c..a4fb10a5e 100644 --- a/locked-asset/simple-lock/src/lib.rs +++ b/locked-asset/simple-lock/src/lib.rs @@ -21,6 +21,7 @@ pub trait SimpleLock: + lp_interactions::LpInteractionsModule + farm_interactions::FarmInteractionsModule + token_attributes::TokenAttributesModule + + disable_add_liq::DisableAddLiqModule { #[init] fn init(&self) {} diff --git a/locked-asset/simple-lock/src/lp_interactions.rs b/locked-asset/simple-lock/src/lp_interactions.rs index 4229cca53..6a6fa1968 100644 --- a/locked-asset/simple-lock/src/lp_interactions.rs +++ b/locked-asset/simple-lock/src/lp_interactions.rs @@ -23,6 +23,7 @@ pub struct RemoveLiquidityResultWrapper { // This avoids circular dependency mod lp_proxy { multiversx_sc::imports!(); + use super::{AddLiquidityResultType, RemoveLiquidityResultType}; #[multiversx_sc::proxy] diff --git a/locked-asset/simple-lock/src/proxy_farm.rs b/locked-asset/simple-lock/src/proxy_farm.rs index 9defead0c..4f10bc3e9 100644 --- a/locked-asset/simple-lock/src/proxy_farm.rs +++ b/locked-asset/simple-lock/src/proxy_farm.rs @@ -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")] diff --git a/locked-asset/simple-lock/src/proxy_lp.rs b/locked-asset/simple-lock/src/proxy_lp.rs index ed16b65d1..86461ccfb 100644 --- a/locked-asset/simple-lock/src/proxy_lp.rs +++ b/locked-asset/simple-lock/src/proxy_lp.rs @@ -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")] @@ -136,6 +137,8 @@ pub trait ProxyLpModule: first_token_amount_min: BigUint, second_token_amount_min: BigUint, ) -> AddLiquidityThroughProxyResultType { + 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); diff --git a/locked-asset/simple-lock/wasm/Cargo.lock b/locked-asset/simple-lock/wasm/Cargo.lock index 8e5ac6f77..45c1b2963 100644 --- a/locked-asset/simple-lock/wasm/Cargo.lock +++ b/locked-asset/simple-lock/wasm/Cargo.lock @@ -31,6 +31,14 @@ dependencies = [ "unwrappable", ] +[[package]] +name = "disable-add-liq" +version = "0.0.0" +dependencies = [ + "common_structs", + "multiversx-sc", +] + [[package]] name = "endian-type" version = "0.1.2" @@ -189,6 +197,7 @@ name = "simple-lock" version = "0.0.0" dependencies = [ "common_structs", + "disable-add-liq", "multiversx-sc", "multiversx-sc-modules", ] diff --git a/locked-asset/simple-lock/wasm/src/lib.rs b/locked-asset/simple-lock/wasm/src/lib.rs index eeafb6cb1..6c74ebe77 100644 --- a/locked-asset/simple-lock/wasm/src/lib.rs +++ b/locked-asset/simple-lock/wasm/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 19 +// Endpoints: 22 // Async Callback: 1 -// Total number of exported functions: 22 +// Total number of exported functions: 25 #![no_std] @@ -39,6 +39,9 @@ multiversx_sc_wasm_adapter::endpoints! { farmClaimRewardsLockedToken => farm_claim_rewards_locked_token getKnownFarms => known_farms getFarmProxyTokenId => farm_proxy_token + enableAddLiq => enable_add_liq + disableAddLiq => disable_add_liq + isAddLiqDisabled => add_liq_disabled ) }