diff --git a/common/modules/banned_addresses/Cargo.toml b/common/modules/banned_addresses/Cargo.toml new file mode 100644 index 000000000..149d2dbcf --- /dev/null +++ b/common/modules/banned_addresses/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "banned_addresses" +version = "0.0.0" +authors = ["Dorin Iancu "] +edition = "2021" + +[lib] +path = "src/lib.rs" + +[dependencies.multiversx-sc] +version = "=0.46.1" +features = ["esdt-token-payment-legacy-decode"] + +[dependencies.permissions_module] +path = "../permissions_module" diff --git a/dex/pair/src/pair_hooks/banned_address.rs b/common/modules/banned_addresses/src/lib.rs similarity index 98% rename from dex/pair/src/pair_hooks/banned_address.rs rename to common/modules/banned_addresses/src/lib.rs index e806b5d0c..7b8356d10 100644 --- a/dex/pair/src/pair_hooks/banned_address.rs +++ b/common/modules/banned_addresses/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + multiversx_sc::imports!(); #[multiversx_sc::module] diff --git a/dex/pair/Cargo.toml b/dex/pair/Cargo.toml index 457fb8d8f..2cb51d6a7 100644 --- a/dex/pair/Cargo.toml +++ b/dex/pair/Cargo.toml @@ -32,6 +32,9 @@ path = "../../energy-integration/fees-collector" [dependencies.utils] path = "../../common/modules/utils" +[dependencies.banned_addresses] +path = "../../common/modules/banned_addresses" + [dependencies.itertools] version = "0.10.1" default-features = false diff --git a/dex/pair/src/lib.rs b/dex/pair/src/lib.rs index b02e8e392..8bb3c36e7 100644 --- a/dex/pair/src/lib.rs +++ b/dex/pair/src/lib.rs @@ -46,9 +46,9 @@ pub trait Pair: + pair_actions::swap::SwapModule + pair_actions::views::ViewsModule + pair_actions::common_methods::CommonMethodsModule - + crate::pair_hooks::banned_address::BannedAddressModule - + crate::pair_hooks::change_hooks::ChangeHooksModule - + crate::pair_hooks::call_hook::CallHookModule + + pair_hooks::change_hooks::ChangeHooksModule + + pair_hooks::call_hook::CallHookModule + + banned_addresses::BannedAddressModule + utils::UtilsModule { #[init] diff --git a/dex/pair/src/pair_actions/add_liq.rs b/dex/pair/src/pair_actions/add_liq.rs index cba08c172..9d5ae41bc 100644 --- a/dex/pair/src/pair_actions/add_liq.rs +++ b/dex/pair/src/pair_actions/add_liq.rs @@ -1,6 +1,6 @@ use crate::{ - contexts::add_liquidity::AddLiquidityContext, pair_hooks::hook_type::HookType, StorageCache, - ERROR_BAD_PAYMENT_TOKENS, ERROR_INITIAL_LIQUIDITY_NOT_ADDED, ERROR_INVALID_ARGS, + contexts::add_liquidity::AddLiquidityContext, pair_hooks::hook_type::PairHookType, + StorageCache, ERROR_BAD_PAYMENT_TOKENS, ERROR_INITIAL_LIQUIDITY_NOT_ADDED, ERROR_INVALID_ARGS, ERROR_K_INVARIANT_FAILED, ERROR_LP_TOKEN_NOT_ISSUED, ERROR_NOT_ACTIVE, }; @@ -21,9 +21,9 @@ pub trait AddLiquidityModule: + permissions_module::PermissionsModule + pausable::PausableModule + super::common_methods::CommonMethodsModule - + crate::pair_hooks::banned_address::BannedAddressModule + crate::pair_hooks::change_hooks::ChangeHooksModule + crate::pair_hooks::call_hook::CallHookModule + + banned_addresses::BannedAddressModule + utils::UtilsModule { #[payable("*")] @@ -72,12 +72,15 @@ pub trait AddLiquidityModule: let mut args = ManagedVec::new(); let (hook_type_before, hook_type_after) = if storage_cache.lp_token_supply == 0 { - (HookType::BeforeAddInitialLiq, HookType::AfterAddInitialLiq) + ( + PairHookType::BeforeAddInitialLiq, + PairHookType::AfterAddInitialLiq, + ) } else { self.encode_arg_to_vec(&first_token_amount_min, &mut args); self.encode_arg_to_vec(&second_token_amount_min, &mut args); - (HookType::BeforeAddLiq, HookType::AfterAddLiq) + (PairHookType::BeforeAddLiq, PairHookType::AfterAddLiq) }; let payments_after_hook = self.call_hook(hook_type_before, caller.clone(), payments_vec, args); diff --git a/dex/pair/src/pair_actions/initial_liq.rs b/dex/pair/src/pair_actions/initial_liq.rs index 25a03db24..f62a0e654 100644 --- a/dex/pair/src/pair_actions/initial_liq.rs +++ b/dex/pair/src/pair_actions/initial_liq.rs @@ -2,8 +2,8 @@ use common_errors::ERROR_PERMISSION_DENIED; use pausable::State; use crate::{ - contexts::add_liquidity::AddLiquidityContext, pair_hooks::hook_type::HookType, StorageCache, - ERROR_ACTIVE, ERROR_BAD_PAYMENT_TOKENS, ERROR_INITIAL_LIQUIDITY_ALREADY_ADDED, + contexts::add_liquidity::AddLiquidityContext, pair_hooks::hook_type::PairHookType, + StorageCache, ERROR_ACTIVE, ERROR_BAD_PAYMENT_TOKENS, ERROR_INITIAL_LIQUIDITY_ALREADY_ADDED, }; use super::common_result_types::AddLiquidityResultType; @@ -22,9 +22,9 @@ pub trait InitialLiquidityModule: + permissions_module::PermissionsModule + pausable::PausableModule + super::common_methods::CommonMethodsModule - + crate::pair_hooks::banned_address::BannedAddressModule + crate::pair_hooks::change_hooks::ChangeHooksModule + crate::pair_hooks::call_hook::CallHookModule + + banned_addresses::BannedAddressModule + utils::UtilsModule { #[payable("*")] @@ -63,7 +63,7 @@ pub trait InitialLiquidityModule: payments_vec.push(second_payment); let payments_after_hook = self.call_hook( - HookType::BeforeAddInitialLiq, + PairHookType::BeforeAddInitialLiq, caller.clone(), payments_vec, ManagedVec::new(), @@ -85,7 +85,7 @@ pub trait InitialLiquidityModule: let mut lp_payment = EsdtTokenPayment::new(storage_cache.lp_token_id.clone(), 0, liq_added.clone()); let lp_payment_after_hook = self.call_hook( - HookType::AfterAddInitialLiq, + PairHookType::AfterAddInitialLiq, caller.clone(), ManagedVec::from_single_item(lp_payment), ManagedVec::new(), diff --git a/dex/pair/src/pair_actions/remove_liq.rs b/dex/pair/src/pair_actions/remove_liq.rs index 0a850488a..535fd68f5 100644 --- a/dex/pair/src/pair_actions/remove_liq.rs +++ b/dex/pair/src/pair_actions/remove_liq.rs @@ -1,5 +1,5 @@ use crate::{ - contexts::remove_liquidity::RemoveLiquidityContext, pair_hooks::hook_type::HookType, + contexts::remove_liquidity::RemoveLiquidityContext, pair_hooks::hook_type::PairHookType, StorageCache, SwapTokensOrder, ERROR_BAD_PAYMENT_TOKENS, ERROR_INVALID_ARGS, ERROR_K_INVARIANT_FAILED, ERROR_LP_TOKEN_NOT_ISSUED, ERROR_NOT_ACTIVE, ERROR_NOT_WHITELISTED, ERROR_SLIPPAGE_ON_REMOVE, @@ -23,9 +23,9 @@ pub trait RemoveLiquidityModule: + permissions_module::PermissionsModule + pausable::PausableModule + super::common_methods::CommonMethodsModule - + crate::pair_hooks::banned_address::BannedAddressModule + crate::pair_hooks::change_hooks::ChangeHooksModule + crate::pair_hooks::call_hook::CallHookModule + + banned_addresses::BannedAddressModule + utils::UtilsModule { #[payable("*")] @@ -72,7 +72,7 @@ pub trait RemoveLiquidityModule: self.encode_arg_to_vec(&second_token_amount_min, &mut args); let payments_after_hook = self.call_hook( - HookType::BeforeRemoveLiq, + PairHookType::BeforeRemoveLiq, caller.clone(), ManagedVec::from_single_item(payment), ManagedVec::new(), @@ -100,7 +100,7 @@ pub trait RemoveLiquidityModule: let output_payments = self.build_remove_liq_output_payments(&storage_cache, &remove_liq_context); let output_payments_after_hook = self.call_hook( - HookType::AfterRemoveLiq, + PairHookType::AfterRemoveLiq, caller.clone(), output_payments, args, diff --git a/dex/pair/src/pair_actions/swap.rs b/dex/pair/src/pair_actions/swap.rs index dbf52911e..7381602e2 100644 --- a/dex/pair/src/pair_actions/swap.rs +++ b/dex/pair/src/pair_actions/swap.rs @@ -1,6 +1,6 @@ use crate::{ - contexts::swap::SwapContext, pair_hooks::hook_type::HookType, StorageCache, ERROR_INVALID_ARGS, - ERROR_K_INVARIANT_FAILED, ERROR_NOT_ENOUGH_RESERVE, ERROR_NOT_WHITELISTED, + contexts::swap::SwapContext, pair_hooks::hook_type::PairHookType, StorageCache, + ERROR_INVALID_ARGS, ERROR_K_INVARIANT_FAILED, ERROR_NOT_ENOUGH_RESERVE, ERROR_NOT_WHITELISTED, ERROR_SLIPPAGE_EXCEEDED, ERROR_SWAP_NOT_ENABLED, ERROR_ZERO_AMOUNT, }; @@ -29,9 +29,9 @@ pub trait SwapModule: + permissions_module::PermissionsModule + pausable::PausableModule + super::common_methods::CommonMethodsModule - + crate::pair_hooks::banned_address::BannedAddressModule + crate::pair_hooks::change_hooks::ChangeHooksModule + crate::pair_hooks::call_hook::CallHookModule + + banned_addresses::BannedAddressModule + utils::UtilsModule { #[payable("*")] @@ -129,7 +129,7 @@ pub trait SwapModule: self.encode_arg_to_vec(&SwapType::FixedInput, &mut args); let payments_after_hook = self.call_hook( - HookType::BeforeSwap, + PairHookType::BeforeSwap, caller.clone(), ManagedVec::from_single_item(payment), args.clone(), @@ -165,8 +165,12 @@ pub trait SwapModule: let caller = self.blockchain().get_caller(); let output_payments = self.build_swap_output_payments(&swap_context); - let output_payments_after_hook = - self.call_hook(HookType::AfterSwap, caller.clone(), output_payments, args); + let output_payments_after_hook = self.call_hook( + PairHookType::AfterSwap, + caller.clone(), + output_payments, + args, + ); require!( output_payments_after_hook.get(0).amount >= swap_context.output_token_amount, @@ -217,7 +221,7 @@ pub trait SwapModule: self.encode_arg_to_vec(&SwapType::FixedOutput, &mut args); let payments_after_hook = self.call_hook( - HookType::BeforeSwap, + PairHookType::BeforeSwap, caller.clone(), ManagedVec::from_single_item(payment), args.clone(), @@ -253,8 +257,12 @@ pub trait SwapModule: let caller = self.blockchain().get_caller(); let output_payments = self.build_swap_output_payments(&swap_context); - let output_payments_after_hook = - self.call_hook(HookType::AfterSwap, caller.clone(), output_payments, args); + let output_payments_after_hook = self.call_hook( + PairHookType::AfterSwap, + caller.clone(), + output_payments, + args, + ); self.send_multiple_tokens_if_not_zero(&caller, &output_payments_after_hook); diff --git a/dex/pair/src/pair_hooks/call_hook.rs b/dex/pair/src/pair_hooks/call_hook.rs index c9a5906d4..c91266844 100644 --- a/dex/pair/src/pair_hooks/call_hook.rs +++ b/dex/pair/src/pair_hooks/call_hook.rs @@ -1,6 +1,6 @@ use common_structs::PaymentsVec; -use super::hook_type::{Hook, HookType}; +use super::hook_type::{Hook, PairHookType}; multiversx_sc::imports!(); @@ -8,7 +8,7 @@ multiversx_sc::imports!(); pub trait CallHookModule { fn call_hook( &self, - hook_type: HookType, + hook_type: PairHookType, caller: ManagedAddress, input_payments: PaymentsVec, args: ManagedVec, @@ -66,5 +66,5 @@ pub trait CallHookModule { } #[storage_mapper("hooks")] - fn hooks(&self, hook_type: HookType) -> SingleValueMapper>>; + fn hooks(&self, hook_type: PairHookType) -> SingleValueMapper>>; } diff --git a/dex/pair/src/pair_hooks/change_hooks.rs b/dex/pair/src/pair_hooks/change_hooks.rs index 39d444c9c..9ce4f7111 100644 --- a/dex/pair/src/pair_hooks/change_hooks.rs +++ b/dex/pair/src/pair_hooks/change_hooks.rs @@ -1,16 +1,16 @@ -use super::hook_type::{Hook, HookType}; +use super::hook_type::{Hook, PairHookType}; multiversx_sc::imports!(); #[multiversx_sc::module] pub trait ChangeHooksModule: super::call_hook::CallHookModule - + super::banned_address::BannedAddressModule + + banned_addresses::BannedAddressModule + permissions_module::PermissionsModule + utils::UtilsModule { #[endpoint(addHook)] - fn add_hook(&self, hook_type: HookType, to: ManagedAddress, endpoint_name: ManagedBuffer) { + fn add_hook(&self, hook_type: PairHookType, to: ManagedAddress, endpoint_name: ManagedBuffer) { self.require_caller_has_owner_or_admin_permissions(); self.require_not_banned_address(&to); self.require_sc_address(&to); @@ -25,7 +25,12 @@ pub trait ChangeHooksModule: } #[endpoint(removeHook)] - fn remove_hook(&self, hook_type: HookType, to: ManagedAddress, endpoint_name: ManagedBuffer) { + fn remove_hook( + &self, + hook_type: PairHookType, + to: ManagedAddress, + endpoint_name: ManagedBuffer, + ) { self.require_caller_has_owner_or_admin_permissions(); self.hooks(hook_type).update(|hooks| { diff --git a/dex/pair/src/pair_hooks/hook_type.rs b/dex/pair/src/pair_hooks/hook_type.rs index 63c7ca67e..04cacedb5 100644 --- a/dex/pair/src/pair_hooks/hook_type.rs +++ b/dex/pair/src/pair_hooks/hook_type.rs @@ -4,7 +4,7 @@ multiversx_sc::imports!(); multiversx_sc::derive_imports!(); #[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode, Clone, Copy)] -pub enum HookType { +pub enum PairHookType { // can't be done, execute_on_dest does not work on init _BeforeInitialize, _AfterInitialize, diff --git a/dex/pair/src/pair_hooks/mod.rs b/dex/pair/src/pair_hooks/mod.rs index db189828b..4441862d0 100644 --- a/dex/pair/src/pair_hooks/mod.rs +++ b/dex/pair/src/pair_hooks/mod.rs @@ -1,4 +1,3 @@ -pub mod banned_address; pub mod call_hook; pub mod change_hooks; pub mod hook_type; diff --git a/dex/pair/wasm-pair-full/Cargo.lock b/dex/pair/wasm-pair-full/Cargo.lock index 77c4e33e1..bd9f43d99 100644 --- a/dex/pair/wasm-pair-full/Cargo.lock +++ b/dex/pair/wasm-pair-full/Cargo.lock @@ -14,6 +14,14 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "banned_addresses" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "permissions_module", +] + [[package]] name = "bitflags" version = "2.4.1" @@ -247,6 +255,7 @@ dependencies = [ name = "pair" version = "0.0.0" dependencies = [ + "banned_addresses", "common_errors", "common_structs", "fees-collector", diff --git a/dex/pair/wasm-pair-full/src/lib.rs b/dex/pair/wasm-pair-full/src/lib.rs index de0788bb7..f9e43af4e 100644 --- a/dex/pair/wasm-pair-full/src/lib.rs +++ b/dex/pair/wasm-pair-full/src/lib.rs @@ -75,10 +75,10 @@ multiversx_sc_wasm_adapter::endpoints! { getAmountOut => get_amount_out_view getAmountIn => get_amount_in_view getEquivalent => get_equivalent - addBannedAddress => add_banned_address - removeBannedAddress => remove_banned_address addHook => add_hook removeHook => remove_hook + addBannedAddress => add_banned_address + removeBannedAddress => remove_banned_address getLpTokensSafePriceByDefaultOffset => get_lp_tokens_safe_price_by_default_offset getLpTokensSafePriceByRoundOffset => get_lp_tokens_safe_price_by_round_offset getLpTokensSafePriceByTimestampOffset => get_lp_tokens_safe_price_by_timestamp_offset diff --git a/dex/pair/wasm-safe-price-view/Cargo.lock b/dex/pair/wasm-safe-price-view/Cargo.lock index 48471aad0..9c0bdd3ca 100644 --- a/dex/pair/wasm-safe-price-view/Cargo.lock +++ b/dex/pair/wasm-safe-price-view/Cargo.lock @@ -14,6 +14,14 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "banned_addresses" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "permissions_module", +] + [[package]] name = "bitflags" version = "2.4.1" @@ -247,6 +255,7 @@ dependencies = [ name = "pair" version = "0.0.0" dependencies = [ + "banned_addresses", "common_errors", "common_structs", "fees-collector", diff --git a/dex/pair/wasm/Cargo.lock b/dex/pair/wasm/Cargo.lock index c1f2406db..34434c55b 100644 --- a/dex/pair/wasm/Cargo.lock +++ b/dex/pair/wasm/Cargo.lock @@ -14,6 +14,14 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "banned_addresses" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "permissions_module", +] + [[package]] name = "bitflags" version = "2.4.1" @@ -247,6 +255,7 @@ dependencies = [ name = "pair" version = "0.0.0" dependencies = [ + "banned_addresses", "common_errors", "common_structs", "fees-collector", diff --git a/dex/pair/wasm/src/lib.rs b/dex/pair/wasm/src/lib.rs index 88e3bc52e..ce29b60ec 100644 --- a/dex/pair/wasm/src/lib.rs +++ b/dex/pair/wasm/src/lib.rs @@ -75,10 +75,10 @@ multiversx_sc_wasm_adapter::endpoints! { getAmountOut => get_amount_out_view getAmountIn => get_amount_in_view getEquivalent => get_equivalent - addBannedAddress => add_banned_address - removeBannedAddress => remove_banned_address addHook => add_hook removeHook => remove_hook + addBannedAddress => add_banned_address + removeBannedAddress => remove_banned_address ) }