Skip to content

Commit

Permalink
common module
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Feb 6, 2024
1 parent c6b3f06 commit 04890fe
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 39 deletions.
15 changes: 15 additions & 0 deletions common/modules/banned_addresses/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "banned_addresses"
version = "0.0.0"
authors = ["Dorin Iancu <[email protected]>"]
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"
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![no_std]

multiversx_sc::imports!();

#[multiversx_sc::module]
Expand Down
3 changes: 3 additions & 0 deletions dex/pair/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions dex/pair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ pub trait Pair<ContractReader>:
+ 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]
Expand Down
13 changes: 8 additions & 5 deletions dex/pair/src/pair_actions/add_liq.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand All @@ -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("*")]
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions dex/pair/src/pair_actions/initial_liq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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("*")]
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
8 changes: 4 additions & 4 deletions dex/pair/src/pair_actions/remove_liq.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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("*")]
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 17 additions & 9 deletions dex/pair/src/pair_actions/swap.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down Expand Up @@ -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("*")]
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions dex/pair/src/pair_hooks/call_hook.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use common_structs::PaymentsVec;

use super::hook_type::{Hook, HookType};
use super::hook_type::{Hook, PairHookType};

multiversx_sc::imports!();

#[multiversx_sc::module]
pub trait CallHookModule {
fn call_hook(
&self,
hook_type: HookType,
hook_type: PairHookType,
caller: ManagedAddress,
input_payments: PaymentsVec<Self::Api>,
args: ManagedVec<ManagedBuffer>,
Expand Down Expand Up @@ -66,5 +66,5 @@ pub trait CallHookModule {
}

#[storage_mapper("hooks")]
fn hooks(&self, hook_type: HookType) -> SingleValueMapper<ManagedVec<Hook<Self::Api>>>;
fn hooks(&self, hook_type: PairHookType) -> SingleValueMapper<ManagedVec<Hook<Self::Api>>>;
}
13 changes: 9 additions & 4 deletions dex/pair/src/pair_hooks/change_hooks.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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| {
Expand Down
2 changes: 1 addition & 1 deletion dex/pair/src/pair_hooks/hook_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion dex/pair/src/pair_hooks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod banned_address;
pub mod call_hook;
pub mod change_hooks;
pub mod hook_type;
9 changes: 9 additions & 0 deletions dex/pair/wasm-pair-full/Cargo.lock

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

4 changes: 2 additions & 2 deletions dex/pair/wasm-pair-full/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions dex/pair/wasm-safe-price-view/Cargo.lock

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

9 changes: 9 additions & 0 deletions dex/pair/wasm/Cargo.lock

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

4 changes: 2 additions & 2 deletions dex/pair/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down

0 comments on commit 04890fe

Please sign in to comment.