-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #818 from multiversx/hooks
hooks definition
- Loading branch information
Showing
13 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters