-
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 #823 from multiversx/interface-hook
example hook impl
- Loading branch information
Showing
13 changed files
with
739 additions
and
7 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
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,21 @@ | ||
[package] | ||
name = "pair-hooks-sample" | ||
version = "0.0.0" | ||
authors = ["you"] | ||
edition = "2021" | ||
publish = false | ||
|
||
[lib] | ||
path = "src/lib.rs" | ||
|
||
[dependencies.multiversx-sc] | ||
version = "=0.46.1" | ||
|
||
[dependencies.pair] | ||
path = "../../pair" | ||
|
||
[dev-dependencies] | ||
num-bigint = "0.4.2" | ||
|
||
[dev-dependencies.multiversx-sc-scenario] | ||
version = "=0.46.1" |
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,12 @@ | ||
[package] | ||
name = "pair-hooks-sample-meta" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies.pair-hooks-sample] | ||
path = ".." | ||
|
||
[dependencies.multiversx-sc-meta] | ||
version = "=0.46.1" | ||
default-features = false |
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,3 @@ | ||
fn main() { | ||
multiversx_sc_meta::cli_main::<pair_hooks_sample::AbiProvider>(); | ||
} |
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,3 @@ | ||
{ | ||
"language": "rust" | ||
} |
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,140 @@ | ||
#![no_std] | ||
|
||
use core::marker::PhantomData; | ||
|
||
use pair::pair_hooks::hook_type::PairHook; | ||
|
||
multiversx_sc::imports!(); | ||
|
||
#[multiversx_sc::contract] | ||
pub trait PairHooksSample { | ||
#[init] | ||
fn init(&self, known_pairs: MultiValueEncoded<ManagedAddress>) { | ||
let mapper = self.known_pairs(); | ||
for pair in known_pairs { | ||
mapper.add(&pair); | ||
} | ||
} | ||
|
||
#[payable("*")] | ||
#[endpoint(beforeAddInitialLiqHook)] | ||
fn before_add_initial_liq_hook(&self, original_caller: ManagedAddress) { | ||
self.require_known_pair(); | ||
|
||
let [first_payment, second_payment] = self.call_value().multi_esdt(); | ||
Wrapper::<Self>::before_add_initial_liq( | ||
self, | ||
first_payment, | ||
second_payment, | ||
original_caller, | ||
); | ||
} | ||
|
||
#[payable("*")] | ||
#[endpoint(afterAddInitialLiqHook)] | ||
fn after_add_initial_liq_hook(&self, original_caller: ManagedAddress) { | ||
self.require_known_pair(); | ||
|
||
let lp_payment = self.call_value().single_esdt(); | ||
Wrapper::<Self>::after_add_initial_liq(self, lp_payment, original_caller); | ||
} | ||
|
||
fn require_known_pair(&self) { | ||
let caller = self.blockchain().get_caller(); | ||
require!( | ||
self.known_pairs().contains(&caller), | ||
"Only known pairs may call this endpoint" | ||
); | ||
} | ||
|
||
#[storage_mapper("knownPairs")] | ||
fn known_pairs(&self) -> WhitelistMapper<ManagedAddress>; | ||
} | ||
|
||
pub struct Wrapper<T: PairHooksSample> { | ||
_phantom: PhantomData<T>, | ||
} | ||
|
||
impl<T: PairHooksSample> PairHook for Wrapper<T> { | ||
type Sc = T; | ||
|
||
fn before_add_initial_liq( | ||
sc: &Self::Sc, | ||
first_payment: EsdtTokenPayment<<Self::Sc as ContractBase>::Api>, | ||
second_payment: EsdtTokenPayment<<Self::Sc as ContractBase>::Api>, | ||
_original_caller: ManagedAddress<<Self::Sc as ContractBase>::Api>, | ||
) { | ||
let caller = sc.blockchain().get_caller(); | ||
sc.send() | ||
.direct_non_zero_esdt_payment(&caller, &first_payment); | ||
sc.send() | ||
.direct_non_zero_esdt_payment(&caller, &second_payment); | ||
} | ||
|
||
fn after_add_initial_liq( | ||
sc: &Self::Sc, | ||
lp_payment: EsdtTokenPayment<<Self::Sc as ContractBase>::Api>, | ||
_original_caller: ManagedAddress<<Self::Sc as ContractBase>::Api>, | ||
) { | ||
let caller = sc.blockchain().get_caller(); | ||
sc.send().direct_non_zero_esdt_payment(&caller, &lp_payment); | ||
} | ||
|
||
fn before_add_liq( | ||
_sc: &Self::Sc, | ||
_first_payment: EsdtTokenPayment<<Self::Sc as ContractBase>::Api>, | ||
_second_payment: EsdtTokenPayment<<Self::Sc as ContractBase>::Api>, | ||
_original_caller: ManagedAddress<<Self::Sc as ContractBase>::Api>, | ||
_first_token_amount_min: BigUint<<Self::Sc as ContractBase>::Api>, | ||
_second_token_amount_min: BigUint<<Self::Sc as ContractBase>::Api>, | ||
) { | ||
todo!() | ||
} | ||
|
||
fn after_add_liq( | ||
_sc: &Self::Sc, | ||
_lp_payment: EsdtTokenPayment<<Self::Sc as ContractBase>::Api>, | ||
_original_caller: ManagedAddress<<Self::Sc as ContractBase>::Api>, | ||
) { | ||
todo!() | ||
} | ||
|
||
fn before_remove_liq( | ||
_sc: &Self::Sc, | ||
_lp_payment: EsdtTokenPayment<<Self::Sc as ContractBase>::Api>, | ||
_original_caller: ManagedAddress<<Self::Sc as ContractBase>::Api>, | ||
) { | ||
todo!() | ||
} | ||
|
||
fn after_remove_liq( | ||
_sc: &Self::Sc, | ||
_first_payment: EsdtTokenPayment<<Self::Sc as ContractBase>::Api>, | ||
_second_payment: EsdtTokenPayment<<Self::Sc as ContractBase>::Api>, | ||
_original_caller: ManagedAddress<<Self::Sc as ContractBase>::Api>, | ||
_first_token_amount_min: BigUint<<Self::Sc as ContractBase>::Api>, | ||
_second_token_amount_min: BigUint<<Self::Sc as ContractBase>::Api>, | ||
) { | ||
todo!() | ||
} | ||
|
||
fn before_swap( | ||
_sc: &Self::Sc, | ||
_payment: EsdtTokenPayment<<Self::Sc as ContractBase>::Api>, | ||
_original_caller: ManagedAddress<<Self::Sc as ContractBase>::Api>, | ||
_swap_type: pair::pair_actions::swap::SwapType, | ||
) { | ||
todo!() | ||
} | ||
|
||
fn after_swap( | ||
_sc: &Self::Sc, | ||
_payment: EsdtTokenPayment<<Self::Sc as ContractBase>::Api>, | ||
_original_caller: ManagedAddress<<Self::Sc as ContractBase>::Api>, | ||
_swap_type: pair::pair_actions::swap::SwapType, | ||
_token_out: TokenIdentifier<<Self::Sc as ContractBase>::Api>, | ||
_amount_out: BigUint<<Self::Sc as ContractBase>::Api>, | ||
) { | ||
todo!() | ||
} | ||
} |
Oops, something went wrong.