Skip to content

Commit

Permalink
feat: add hook utilities for permission handling and validation
Browse files Browse the repository at this point in the history
Introduces `HookOptions` and `HookPermissions` enums to manage hooks and their permissions. Implements utility functions to check individual permissions such as `has_initialize_permissions`, `has_liquidity_permissions`, and more. Also updates the utils module to include the new hook utilities.
  • Loading branch information
shuhuiluo committed Dec 25, 2024
1 parent a86c558 commit ebb8654
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/utils/hook.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use alloy_primitives::Address;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum HookOptions {
AfterRemoveLiquidityReturnsDelta = 0,
AfterAddLiquidityReturnsDelta = 1,
AfterSwapReturnsDelta = 2,
BeforeSwapReturnsDelta = 3,
AfterDonate = 4,
BeforeDonate = 5,
AfterSwap = 6,
BeforeSwap = 7,
AfterRemoveLiquidity = 8,
BeforeRemoveLiquidity = 9,
AfterAddLiquidity = 10,
BeforeAddLiquidity = 11,
AfterInitialize = 12,
BeforeInitialize = 13,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct HookPermissions {
pub after_remove_liquidity_returns_delta: bool,
pub after_add_liquidity_returns_delta: bool,
pub after_swap_returns_delta: bool,
pub before_swap_returns_delta: bool,
pub after_donate: bool,
pub before_donate: bool,
pub after_swap: bool,
pub before_swap: bool,
pub after_remove_liquidity: bool,
pub before_remove_liquidity: bool,
pub after_add_liquidity: bool,
pub before_add_liquidity: bool,
pub after_initialize: bool,
pub before_initialize: bool,
}

pub fn permissions(address: Address) -> HookPermissions {
HookPermissions {
before_initialize: has_permission(address, HookOptions::BeforeInitialize),
after_initialize: has_permission(address, HookOptions::AfterInitialize),
before_add_liquidity: has_permission(address, HookOptions::BeforeAddLiquidity),
after_add_liquidity: has_permission(address, HookOptions::AfterAddLiquidity),
before_remove_liquidity: has_permission(address, HookOptions::BeforeRemoveLiquidity),
after_remove_liquidity: has_permission(address, HookOptions::AfterRemoveLiquidity),
before_swap: has_permission(address, HookOptions::BeforeSwap),
after_swap: has_permission(address, HookOptions::AfterSwap),
before_donate: has_permission(address, HookOptions::BeforeDonate),
after_donate: has_permission(address, HookOptions::AfterDonate),
before_swap_returns_delta: has_permission(address, HookOptions::BeforeSwapReturnsDelta),
after_swap_returns_delta: has_permission(address, HookOptions::AfterSwapReturnsDelta),
after_add_liquidity_returns_delta: has_permission(
address,
HookOptions::AfterAddLiquidityReturnsDelta,
),
after_remove_liquidity_returns_delta: has_permission(
address,
HookOptions::AfterRemoveLiquidityReturnsDelta,
),
}
}

pub fn has_permission(address: Address, hook_option: HookOptions) -> bool {
let mask = (address.0 .0[18] as u64) << 8 | (address.0 .0[19] as u64);
let hook_flag_index = hook_option as u64;
mask & (1 << hook_flag_index) != 0
}

pub fn has_initialize_permissions(address: Address) -> bool {
has_permission(address, HookOptions::BeforeInitialize)
|| has_permission(address, HookOptions::AfterInitialize)
}

pub fn has_liquidity_permissions(address: Address) -> bool {
has_permission(address, HookOptions::BeforeAddLiquidity)
|| has_permission(address, HookOptions::AfterAddLiquidity)
|| has_permission(address, HookOptions::BeforeRemoveLiquidity)
|| has_permission(address, HookOptions::AfterRemoveLiquidity)
}

pub fn has_swap_permissions(address: Address) -> bool {
// this implicitly encapsulates swap delta permissions
has_permission(address, HookOptions::BeforeSwap)
|| has_permission(address, HookOptions::AfterSwap)
}

pub fn has_donate_permissions(address: Address) -> bool {
has_permission(address, HookOptions::BeforeDonate)
|| has_permission(address, HookOptions::AfterDonate)
}
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod currency_map;
pub mod encode_route_to_path;
pub mod hook;
pub mod path_currency;
pub mod price_tick_conversions;
pub mod sorts_before;
Expand All @@ -9,6 +10,7 @@ pub mod v4_position_planner;

pub use currency_map::*;
pub use encode_route_to_path::*;
pub use hook::*;
pub use path_currency::*;
pub use price_tick_conversions::*;
pub use sorts_before::*;
Expand Down

0 comments on commit ebb8654

Please sign in to comment.