From 274362b67c690bdff31b8fe4b1dd4264f66fde25 Mon Sep 17 00:00:00 2001 From: Daniel Bigos Date: Wed, 6 Nov 2024 02:10:03 +0100 Subject: [PATCH] ref: split types into many files --- contracts/src/uniswap/v4/hooks.rs | 36 ++++- contracts/src/uniswap/v4/mod.rs | 5 +- contracts/src/uniswap/v4/types.rs | 139 ------------------ contracts/src/uniswap/v4/types/liquidity.rs | 17 +++ contracts/src/uniswap/v4/types/mod.rs | 25 ++++ contracts/src/uniswap/v4/types/pool_key.rs | 35 +++++ contracts/src/uniswap/v4/types/swap_params.rs | 14 ++ 7 files changed, 127 insertions(+), 144 deletions(-) delete mode 100644 contracts/src/uniswap/v4/types.rs create mode 100644 contracts/src/uniswap/v4/types/liquidity.rs create mode 100644 contracts/src/uniswap/v4/types/mod.rs create mode 100644 contracts/src/uniswap/v4/types/pool_key.rs create mode 100644 contracts/src/uniswap/v4/types/swap_params.rs diff --git a/contracts/src/uniswap/v4/hooks.rs b/contracts/src/uniswap/v4/hooks.rs index 99aa8b25c..fc2bb2710 100644 --- a/contracts/src/uniswap/v4/hooks.rs +++ b/contracts/src/uniswap/v4/hooks.rs @@ -15,11 +15,45 @@ use alloy_primitives::{Address, Bytes, FixedBytes, I128, U160, U256}; use alloy_sol_types::sol; use stylus_sdk::stylus_proc::SolidityError; -use crate::uniswap::v4::{ +use crate::uniswap::v4::types::{ BalanceDelta, BeforeSwapDelta, ModifyLiquidityParams, PoolKey, SwapParams, I24, U24, }; +sol! { + /// Struct representing hook's permissions. + struct Permissions { + /// Indicates whether `[`IHooks::before_initialize`]` hook is available. + bool beforeInitialize; + /// Indicates whether `[`IHooks::after_initialize`]` hook is available. + bool afterInitialize; + /// Indicates whether `[`IHooks::before_add_liquidity`]` hook is available. + bool beforeAddLiquidity; + /// Indicates whether `[`IHooks::after_add_liquidity`]` hook is available. + bool afterAddLiquidity; + /// Indicates whether `[`IHooks::before_remove_liquidity`]` hook is available. + bool beforeRemoveLiquidity; + /// Indicates whether `[`IHooks::after_remove_liquidity`]` hook is available. + bool afterRemoveLiquidity; + /// Indicates whether `[`IHooks::before_swap`]` hook is available. + bool beforeSwap; + /// Indicates whether `[`IHooks::after_swap`]` hook is available. + bool afterSwap; + /// Indicates whether `[`IHooks::before_donate`]` hook is available. + bool beforeDonate; + /// Indicates whether `[`IHooks::after_donate`]` hook is available. + bool afterDonate; + /// Indicates whether the hook is available. + bool beforeSwapReturnDelta; + /// Indicates whether the hook is available. + bool afterSwapReturnDelta; + /// Indicates whether the hook is available. + bool afterAddLiquidityReturnDelta; + /// Indicates whether the hook is available. + bool afterRemoveLiquidityReturnDelta; + } +} + sol! { /// Emitted when a hook is not implemented. #[derive(Debug)] diff --git a/contracts/src/uniswap/v4/mod.rs b/contracts/src/uniswap/v4/mod.rs index 00067ffac..73c0da5b5 100644 --- a/contracts/src/uniswap/v4/mod.rs +++ b/contracts/src/uniswap/v4/mod.rs @@ -3,7 +3,4 @@ pub mod hooks; pub mod types; pub use hooks::IHooks; -pub use types::{ - BalanceDelta, BeforeSwapDelta, Currency, ModifyLiquidityParams, PoolKey, - SwapParams, I24, U24, -}; +pub use types::*; diff --git a/contracts/src/uniswap/v4/types.rs b/contracts/src/uniswap/v4/types.rs deleted file mode 100644 index b473dd650..000000000 --- a/contracts/src/uniswap/v4/types.rs +++ /dev/null @@ -1,139 +0,0 @@ -//! Module with data types for Uniswap V4 Hooks. -use alloy_primitives::{Address, Signed, Uint, I256}; -use alloy_sol_types::sol; - -// TODO: newer alloy's versions have `I24`. -pub type I24 = Signed<24, 1>; -// TODO: newer alloy's versions have `U24`. -pub type U24 = Uint<24, 1>; - -pub type BalanceDelta = I256; -pub type BeforeSwapDelta = I256; - -pub type Currency = Address; - -sol! { - struct ModifyLiquidityParams { - /// the lower and upper tick of the position - int24 tickLower; - int24 tickUpper; - /// how to modify the liquidity - int256 liquidityDelta; - //// a value to set if you want unique liquidity positions at the same range - bytes32 salt; - } - -} - -sol! { - struct SwapParams { - /// Whether to swap token0 for token1 or vice versa - bool zeroForOne; - /// The desired input amount if negative (exactIn), or the desired output amount if positive (exactOut) - int256 amountSpecified; - /// The sqrt price at which, if reached, the swap will stop executing - uint160 sqrtPriceLimitX96; - } -} - -sol! { - /// Returns the key for identifying a pool. - struct PoolKey { - /// The lower currency of the pool, sorted numerically. - // TODO - address currency0; - /// The higher currency of the pool, sorted numerically. - // TODO - address currency1; - /// The pool LP fee, capped at 1_000_000. - /// If the highest bit is 1, the pool has a dynamic fee and - /// must be exactly equal to 0x800000. - uint24 fee; - /// Ticks that involve positions must be a multiple of tick spacing - int24 tickSpacing; - /* - /// @notice The hooks of the pool - IHooks hooks; - */ - } -} - -pub use interface::IHooks; -#[allow(missing_docs)] -mod interface { - stylus_sdk::stylus_proc::sol_interface! { - interface IHooks { - /* - function beforeInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96) external returns (bytes4); - - function afterInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96, int24 tick) - external - returns (bytes4); - - function beforeAddLiquidity( - address sender, - PoolKey calldata key, - IPoolManager.ModifyLiquidityParams calldata params, - bytes calldata hookData - ) external returns (bytes4); - - function afterAddLiquidity( - address sender, - PoolKey calldata key, - IPoolManager.ModifyLiquidityParams calldata params, - BalanceDelta delta, - BalanceDelta feesAccrued, - bytes calldata hookData - ) external returns (bytes4, BalanceDelta); - - function beforeRemoveLiquidity( - address sender, - PoolKey calldata key, - IPoolManager.ModifyLiquidityParams calldata params, - bytes calldata hookData - ) external returns (bytes4); - - function afterRemoveLiquidity( - address sender, - PoolKey calldata key, - IPoolManager.ModifyLiquidityParams calldata params, - BalanceDelta delta, - BalanceDelta feesAccrued, - bytes calldata hookData - ) external returns (bytes4, BalanceDelta); - - function beforeSwap( - address sender, - PoolKey calldata key, - IPoolManager.SwapParams calldata params, - bytes calldata hookData - ) external returns (bytes4, BeforeSwapDelta, uint24); - - function afterSwap( - address sender, - PoolKey calldata key, - IPoolManager.SwapParams calldata params, - BalanceDelta delta, - bytes calldata hookData - ) external returns (bytes4, int128); - - function beforeDonate( - address sender, - PoolKey calldata key, - uint256 amount0, - uint256 amount1, - bytes calldata hookData - ) external returns (bytes4); - - function afterDonate( - address sender, - PoolKey calldata key, - uint256 amount0, - uint256 amount1, - bytes calldata hookData - ) external returns (bytes4); - */ - } - - } -} diff --git a/contracts/src/uniswap/v4/types/liquidity.rs b/contracts/src/uniswap/v4/types/liquidity.rs new file mode 100644 index 000000000..ced997ff1 --- /dev/null +++ b/contracts/src/uniswap/v4/types/liquidity.rs @@ -0,0 +1,17 @@ +use alloy_sol_types::sol; + +sol! { + /// Struct representing modify liquidity operation parameters. + struct ModifyLiquidityParams { + /// The lower tick of the position. + int24 tickLower; + /// The upper tick of the position. + int24 tickUpper; + /// How to modify the liquidity. + int256 liquidityDelta; + /// A value to set if you want unique liquidity positions + /// at the same range. + bytes32 salt; + } + +} diff --git a/contracts/src/uniswap/v4/types/mod.rs b/contracts/src/uniswap/v4/types/mod.rs new file mode 100644 index 000000000..d09ae0003 --- /dev/null +++ b/contracts/src/uniswap/v4/types/mod.rs @@ -0,0 +1,25 @@ +//! Module with data types for Uniswap V4 Hooks. +use alloy_primitives::{Signed, Uint, B256, I256}; + +mod liquidity; +mod pool_key; +mod swap_params; + +pub use liquidity::ModifyLiquidityParams; +pub use pool_key::{Currency, PoolKey}; +pub use swap_params::SwapParams; + +/// Type representing Id of a Pool. +pub type PoolId = B256; + +/// Type representing signed 24-bits integer. +pub type I24 = Signed<24, 1>; + +/// Type representing unsigned 24-bits integer. +pub type U24 = Uint<24, 1>; + +/// Type representing balance delta. +pub type BalanceDelta = I256; + +/// Type representing before swap delta. +pub type BeforeSwapDelta = I256; diff --git a/contracts/src/uniswap/v4/types/pool_key.rs b/contracts/src/uniswap/v4/types/pool_key.rs new file mode 100644 index 000000000..597147999 --- /dev/null +++ b/contracts/src/uniswap/v4/types/pool_key.rs @@ -0,0 +1,35 @@ +use alloy_primitives::keccak256; +use alloy_sol_types::{sol, SolValue}; + +use super::PoolId; + +sol! { + /// The currency data type. + type Currency is address; + + /// Returns the key for identifying a pool. + struct PoolKey { + /// The lower currency of the pool, sorted numerically. + Currency currency0; + /// The higher currency of the pool, sorted numerically. + Currency currency1; + /// The pool LP fee, capped at 1_000_000. + /// If the highest bit is 1, the pool has a dynamic fee and + /// must be exactly equal to 0x800000. + uint24 fee; + /// Ticks that involve positions must be a multiple of tick spacing. + int24 tickSpacing; + /* + // TODO + // The hooks of the pool + // IHooks hooks; + */ + } +} + +impl From for PoolId { + fn from(value: PoolKey) -> Self { + let encoded = PoolKey::abi_encode(&value); + keccak256(encoded) + } +} diff --git a/contracts/src/uniswap/v4/types/swap_params.rs b/contracts/src/uniswap/v4/types/swap_params.rs new file mode 100644 index 000000000..b92f6b9a4 --- /dev/null +++ b/contracts/src/uniswap/v4/types/swap_params.rs @@ -0,0 +1,14 @@ +use alloy_sol_types::sol; + +sol! { + /// Struct representing swap parameters. + struct SwapParams { + /// Whether to swap token0 for token1 or vice versa. + bool zeroForOne; + /// The desired input amount if negative (exactIn), + /// or the desired output amount if positive (exactOut). + int256 amountSpecified; + /// The sqrt price at which, if reached, the swap will stop executing. + uint160 sqrtPriceLimitX96; + } +}