Skip to content

Commit

Permalink
Merge pull request #824 from multiversx/revert-hooks
Browse files Browse the repository at this point in the history
rip hooks
  • Loading branch information
dorin-iancu authored Feb 6, 2024
2 parents 34e75dc + b51868b commit e227609
Show file tree
Hide file tree
Showing 21 changed files with 26 additions and 1,026 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ members = [
"dex/pair-mock",
"dex/pair-mock/meta",

"dex/sample-hooks/pair-hooks-sample",
"dex/sample-hooks/pair-hooks-sample/meta",

"energy-integration/energy-factory-mock",
"energy-integration/energy-factory-mock/meta",
"energy-integration/energy-update",
Expand Down
7 changes: 0 additions & 7 deletions dex/pair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ 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 @@ -46,9 +45,6 @@ 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]
Expand Down Expand Up @@ -101,9 +97,6 @@ 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
36 changes: 4 additions & 32 deletions dex/pair/src/pair_actions/add_liq.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
contexts::add_liquidity::AddLiquidityContext, pair_hooks::hook_type::HookType, 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,
contexts::add_liquidity::AddLiquidityContext, 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,
};

use super::common_result_types::AddLiquidityResultType;
Expand All @@ -21,9 +21,6 @@ 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
+ utils::UtilsModule
{
#[payable("*")]
Expand Down Expand Up @@ -65,24 +62,6 @@ pub trait AddLiquidityModule:
ERROR_INITIAL_LIQUIDITY_NOT_ADDED
);

let mut payments_vec = ManagedVec::new();
payments_vec.push(first_payment);
payments_vec.push(second_payment);

let mut args = ManagedVec::new();
self.encode_arg_to_vec(&first_token_amount_min, &mut args);
self.encode_arg_to_vec(&second_token_amount_min, &mut args);

let (hook_type_before, hook_type_after) = if storage_cache.lp_token_supply == 0 {
(HookType::BeforeAddInitialLiq, HookType::AfterAddInitialLiq)
} else {
(HookType::BeforeAddLiq, HookType::AfterAddLiq)
};
let payments_after_hook =
self.call_hook(hook_type_before, caller.clone(), payments_vec, args);
let first_payment = payments_after_hook.get(0);
let second_payment = payments_after_hook.get(1);

self.update_safe_price(
&storage_cache.first_token_reserve,
&storage_cache.second_token_reserve,
Expand Down Expand Up @@ -124,18 +103,11 @@ pub trait AddLiquidityModule:
self.send()
.esdt_local_mint(&storage_cache.lp_token_id, 0, &add_liq_context.liq_added);

let mut lp_payment = EsdtTokenPayment::new(
let lp_payment = EsdtTokenPayment::new(
storage_cache.lp_token_id.clone(),
0,
add_liq_context.liq_added.clone(),
);
let lp_payment_after_hook = self.call_hook(
hook_type_after,
caller.clone(),
ManagedVec::from_single_item(lp_payment),
ManagedVec::new(),
);
lp_payment = lp_payment_after_hook.get(0);

let mut output_payments =
self.build_add_liq_output_payments(&storage_cache, &add_liq_context);
Expand Down
29 changes: 3 additions & 26 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, StorageCache, ERROR_ACTIVE,
ERROR_BAD_PAYMENT_TOKENS, ERROR_INITIAL_LIQUIDITY_ALREADY_ADDED,
};

use super::common_result_types::AddLiquidityResultType;
Expand All @@ -22,9 +22,6 @@ 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
+ utils::UtilsModule
{
#[payable("*")]
Expand Down Expand Up @@ -58,19 +55,6 @@ pub trait InitialLiquidityModule:
ERROR_INITIAL_LIQUIDITY_ALREADY_ADDED
);

let mut payments_vec = ManagedVec::new();
payments_vec.push(first_payment);
payments_vec.push(second_payment);

let payments_after_hook = self.call_hook(
HookType::BeforeAddInitialLiq,
caller.clone(),
payments_vec,
ManagedVec::new(),
);
let first_payment = payments_after_hook.get(0);
let second_payment = payments_after_hook.get(1);

let first_token_optimal_amount = &first_payment.amount;
let second_token_optimal_amount = &second_payment.amount;
let liq_added = self.pool_add_initial_liquidity(
Expand All @@ -82,15 +66,8 @@ pub trait InitialLiquidityModule:
self.send()
.esdt_local_mint(&storage_cache.lp_token_id, 0, &liq_added);

let mut lp_payment =
let lp_payment =
EsdtTokenPayment::new(storage_cache.lp_token_id.clone(), 0, liq_added.clone());
let lp_payment_after_hook = self.call_hook(
HookType::AfterAddInitialLiq,
caller.clone(),
ManagedVec::from_single_item(lp_payment),
ManagedVec::new(),
);
lp_payment = lp_payment_after_hook.get(0);

self.send()
.direct_non_zero_esdt_payment(&caller, &lp_payment);
Expand Down
36 changes: 7 additions & 29 deletions dex/pair/src/pair_actions/remove_liq.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{
contexts::remove_liquidity::RemoveLiquidityContext, pair_hooks::hook_type::HookType,
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,
contexts::remove_liquidity::RemoveLiquidityContext, 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,
};

use super::common_result_types::RemoveLiquidityResultType;
Expand All @@ -23,9 +22,6 @@ 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
+ utils::UtilsModule
{
#[payable("*")]
Expand Down Expand Up @@ -67,18 +63,6 @@ pub trait RemoveLiquidityModule:
&storage_cache.second_token_reserve,
);

let mut args = ManagedVec::new();
self.encode_arg_to_vec(&first_token_amount_min, &mut args);
self.encode_arg_to_vec(&second_token_amount_min, &mut args);

let payments_after_hook = self.call_hook(
HookType::BeforeRemoveLiq,
caller.clone(),
ManagedVec::from_single_item(payment),
ManagedVec::new(),
);
let payment = payments_after_hook.get(0);

let mut remove_liq_context = RemoveLiquidityContext::new(
payment.amount,
first_token_amount_min,
Expand All @@ -99,15 +83,9 @@ 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,
caller.clone(),
output_payments,
args,
);

let first_payment_after = output_payments_after_hook.get(0);
let second_payment_after = output_payments_after_hook.get(1);
let first_payment_after = output_payments.get(0);
let second_payment_after = output_payments.get(1);
require!(
first_payment_after.amount >= remove_liq_context.first_token_amount_min,
ERROR_SLIPPAGE_ON_REMOVE
Expand All @@ -117,11 +95,11 @@ pub trait RemoveLiquidityModule:
ERROR_SLIPPAGE_ON_REMOVE
);

self.send_multiple_tokens_if_not_zero(&caller, &output_payments_after_hook);
self.send_multiple_tokens_if_not_zero(&caller, &output_payments);

self.emit_remove_liquidity_event(&storage_cache, remove_liq_context);

self.build_remove_liq_results(output_payments_after_hook)
self.build_remove_liq_results(output_payments)
}

#[payable("*")]
Expand Down
53 changes: 8 additions & 45 deletions dex/pair/src/pair_actions/swap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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,
ERROR_SLIPPAGE_EXCEEDED, ERROR_SWAP_NOT_ENABLED, ERROR_ZERO_AMOUNT,
contexts::swap::SwapContext, 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,
};

use super::common_result_types::{SwapTokensFixedInputResultType, SwapTokensFixedOutputResultType};
Expand Down Expand Up @@ -29,9 +29,6 @@ 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
+ utils::UtilsModule
{
#[payable("*")]
Expand Down Expand Up @@ -102,7 +99,6 @@ pub trait SwapModule:
require!(amount_out_min > 0, ERROR_INVALID_ARGS);

let mut storage_cache = StorageCache::new(self);
let caller = self.blockchain().get_caller();
let payment = self.call_value().single_esdt();
let swap_tokens_order =
storage_cache.get_swap_tokens_order(&payment.token_identifier, &token_out);
Expand All @@ -125,20 +121,6 @@ pub trait SwapModule:
&storage_cache.second_token_reserve,
);

let mut args = ManagedVec::new();
self.encode_arg_to_vec(&SwapType::FixedInput, &mut args);

let payments_after_hook = self.call_hook(
HookType::BeforeSwap,
caller.clone(),
ManagedVec::from_single_item(payment),
args.clone(),
);
let payment = payments_after_hook.get(0);

self.encode_arg_to_vec(&token_out, &mut args);
self.encode_arg_to_vec(&amount_out_min, &mut args);

let mut swap_context = SwapContext::new(
payment.token_identifier,
payment.amount,
Expand All @@ -165,19 +147,17 @@ 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);

require!(
output_payments_after_hook.get(0).amount >= swap_context.output_token_amount,
output_payments.get(0).amount >= swap_context.output_token_amount,
ERROR_SLIPPAGE_EXCEEDED
);

self.send_multiple_tokens_if_not_zero(&caller, &output_payments_after_hook);
self.send_multiple_tokens_if_not_zero(&caller, &output_payments);

self.emit_swap_event(&storage_cache, swap_context);

self.build_swap_fixed_input_results(output_payments_after_hook)
self.build_swap_fixed_input_results(output_payments)
}

#[payable("*")]
Expand All @@ -190,7 +170,6 @@ pub trait SwapModule:
require!(amount_out > 0, ERROR_INVALID_ARGS);

let mut storage_cache = StorageCache::new(self);
let caller = self.blockchain().get_caller();
let payment = self.call_value().single_esdt();
let swap_tokens_order =
storage_cache.get_swap_tokens_order(&payment.token_identifier, &token_out);
Expand All @@ -213,20 +192,6 @@ pub trait SwapModule:
&storage_cache.second_token_reserve,
);

let mut args = ManagedVec::new();
self.encode_arg_to_vec(&SwapType::FixedOutput, &mut args);

let payments_after_hook = self.call_hook(
HookType::BeforeSwap,
caller.clone(),
ManagedVec::from_single_item(payment),
args.clone(),
);
let payment = payments_after_hook.get(0);

self.encode_arg_to_vec(&token_out, &mut args);
self.encode_arg_to_vec(&amount_out, &mut args);

let mut swap_context = SwapContext::new(
payment.token_identifier,
payment.amount,
Expand All @@ -253,14 +218,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);

self.send_multiple_tokens_if_not_zero(&caller, &output_payments_after_hook);
self.send_multiple_tokens_if_not_zero(&caller, &output_payments);

self.emit_swap_event(&storage_cache, swap_context);

self.build_swap_fixed_output_results(output_payments_after_hook)
self.build_swap_fixed_output_results(output_payments)
}

fn perform_swap_fixed_input(
Expand Down
34 changes: 0 additions & 34 deletions dex/pair/src/pair_hooks/banned_address.rs

This file was deleted.

Loading

0 comments on commit e227609

Please sign in to comment.