Skip to content

Commit

Permalink
Merge pull request #818 from multiversx/hooks
Browse files Browse the repository at this point in the history
hooks definition
  • Loading branch information
dorin-iancu authored Jan 29, 2024
2 parents 59b5cf6 + 0139977 commit e396169
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 4 deletions.
4 changes: 4 additions & 0 deletions common/modules/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,8 @@ pub trait UtilsModule {
"Invalid SC address"
);
}

fn require_not_empty_buffer(&self, buffer: &ManagedBuffer) {
require!(!buffer.is_empty(), "Empty buffer");
}
}
3 changes: 3 additions & 0 deletions dex/pair/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ path = "../../locked-asset/simple-lock"
[dependencies.fees-collector]
path = "../../energy-integration/fees-collector"

[dependencies.utils]
path = "../../common/modules/utils"

[dependencies.itertools]
version = "0.10.1"
default-features = false
Expand Down
8 changes: 8 additions & 0 deletions dex/pair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod fee;
mod liquidity_pool;
pub mod locking_wrapper;
pub mod pair_actions;
pub mod pair_hooks;
pub mod safe_price;
pub mod safe_price_view;

Expand Down Expand Up @@ -45,6 +46,10 @@ pub trait Pair<ContractReader>:
+ pair_actions::swap::SwapModule
+ pair_actions::views::ViewsModule
+ pair_actions::common_methods::CommonMethodsModule
+ pair_hooks::banned_address::BannedAddressModule
+ pair_hooks::change_hooks::ChangeHooksModule
+ pair_hooks::call_hook::CallHookModule
+ utils::UtilsModule
{
#[init]
fn init(
Expand Down Expand Up @@ -96,6 +101,9 @@ pub trait Pair<ContractReader>:
);
self.add_permissions_for_all(admins, Permissions::ADMIN);
};

let sc_address = self.blockchain().get_sc_address();
self.banned_addresses().add(&sc_address);
}

#[endpoint]
Expand Down
34 changes: 34 additions & 0 deletions dex/pair/src/pair_hooks/banned_address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
multiversx_sc::imports!();

#[multiversx_sc::module]
pub trait BannedAddressModule: permissions_module::PermissionsModule {
#[endpoint(addBannedAddress)]
fn add_banned_address(&self, addresses: MultiValueEncoded<ManagedAddress>) {
self.require_caller_has_owner_or_admin_permissions();

let mapper = self.banned_addresses();
for address in addresses {
mapper.add(&address);
}
}

#[endpoint(removeBannedAddress)]
fn remove_banned_address(&self, addresses: MultiValueEncoded<ManagedAddress>) {
self.require_caller_has_owner_or_admin_permissions();

let mapper = self.banned_addresses();
for address in addresses {
mapper.remove(&address);
}
}

fn require_not_banned_address(&self, address: &ManagedAddress) {
require!(
!self.banned_addresses().contains(address),
"Cannot add hook for this address"
);
}

#[storage_mapper("bannedAddresses")]
fn banned_addresses(&self) -> WhitelistMapper<ManagedAddress>;
}
47 changes: 47 additions & 0 deletions dex/pair/src/pair_hooks/call_hook.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use common_structs::PaymentsVec;

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

multiversx_sc::imports!();

#[multiversx_sc::module]
pub trait CallHookModule {
fn call_hook(
&self,
hook_type: HookType,
caller: ManagedAddress,
input_payments: PaymentsVec<Self::Api>,
args: ManagedVec<ManagedBuffer>,
) -> PaymentsVec<Self::Api> {
let hooks = self.hooks(hook_type).get();
if hooks.is_empty() {
return input_payments;
}

let mut call_args = ManagedArgBuffer::new();
call_args.push_arg(caller);

for arg in &args {
call_args.push_arg(arg);
}

let mut output_payments = input_payments;
for hook in &hooks {
let (_, back_transfers) =
ContractCallNoPayment::<_, MultiValueEncoded<ManagedBuffer>>::new(
hook.dest_address,
hook.endpoint_name,
)
.with_raw_arguments(call_args.clone())
.with_multi_token_transfer(output_payments)
.execute_on_dest_context_with_back_transfers::<MultiValueEncoded<ManagedBuffer>>();

output_payments = back_transfers.esdt_payments;
}

output_payments
}

#[storage_mapper("hooks")]
fn hooks(&self, hook_type: HookType) -> SingleValueMapper<ManagedVec<Hook<Self::Api>>>;
}
43 changes: 43 additions & 0 deletions dex/pair/src/pair_hooks/change_hooks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use super::hook_type::{Hook, HookType};

multiversx_sc::imports!();

#[multiversx_sc::module]
pub trait ChangeHooksModule:
super::call_hook::CallHookModule
+ super::banned_address::BannedAddressModule
+ permissions_module::PermissionsModule
+ utils::UtilsModule
{
#[endpoint(addHook)]
fn add_hook(&self, hook_type: HookType, to: ManagedAddress, endpoint_name: ManagedBuffer) {
self.require_caller_has_owner_or_admin_permissions();
self.require_not_banned_address(&to);
self.require_sc_address(&to);
self.require_not_empty_buffer(&endpoint_name);

self.hooks(hook_type).update(|hooks| {
hooks.push(Hook {
dest_address: to,
endpoint_name,
})
});
}

#[endpoint(removeHook)]
fn remove_hook(&self, hook_type: HookType, to: ManagedAddress, endpoint_name: ManagedBuffer) {
self.require_caller_has_owner_or_admin_permissions();

self.hooks(hook_type).update(|hooks| {
let opt_index = hooks.find(&Hook {
dest_address: to,
endpoint_name,
});

require!(opt_index.is_some(), "Item not found");

let index = unsafe { opt_index.unwrap_unchecked() };
hooks.remove(index);
})
}
}
23 changes: 23 additions & 0 deletions dex/pair/src/pair_hooks/hook_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode, Clone, Copy)]
pub enum HookType {
// can't be done, execute_on_dest does not work on init
_BeforeInitialize,
_AfterInitialize,
BeforeAddInitialLiq,
AfterAddInitialLiq,
BeforeAddLiq,
AfterAddLiq,
BeforeRemoveLiq,
AfterRemoveLiq,
BeforeSwap,
AfterSwap,
}

#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, PartialEq)]
pub struct Hook<M: ManagedTypeApi> {
pub dest_address: ManagedAddress<M>,
pub endpoint_name: ManagedBuffer<M>,
}
4 changes: 4 additions & 0 deletions dex/pair/src/pair_hooks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod banned_address;
pub mod call_hook;
pub mod change_hooks;
pub mod hook_type;
1 change: 1 addition & 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.

8 changes: 6 additions & 2 deletions dex/pair/wasm-pair-full/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 64
// Endpoints: 68
// Async Callback (empty): 1
// Total number of exported functions: 66
// Total number of exported functions: 70

#![no_std]
#![allow(internal_features)]
Expand Down Expand Up @@ -75,6 +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
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
1 change: 1 addition & 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.

1 change: 1 addition & 0 deletions dex/pair/wasm/Cargo.lock

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

8 changes: 6 additions & 2 deletions dex/pair/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 55
// Endpoints: 59
// Async Callback (empty): 1
// Total number of exported functions: 57
// Total number of exported functions: 61

#![no_std]
#![allow(internal_features)]
Expand Down Expand Up @@ -75,6 +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
)
}

Expand Down

0 comments on commit e396169

Please sign in to comment.