Skip to content

Commit

Permalink
ref: split types into many files
Browse files Browse the repository at this point in the history
  • Loading branch information
bidzyyys committed Nov 6, 2024
1 parent 10111a3 commit 274362b
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 144 deletions.
36 changes: 35 additions & 1 deletion contracts/src/uniswap/v4/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 1 addition & 4 deletions contracts/src/uniswap/v4/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
139 changes: 0 additions & 139 deletions contracts/src/uniswap/v4/types.rs

This file was deleted.

17 changes: 17 additions & 0 deletions contracts/src/uniswap/v4/types/liquidity.rs
Original file line number Diff line number Diff line change
@@ -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;
}

}
25 changes: 25 additions & 0 deletions contracts/src/uniswap/v4/types/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
35 changes: 35 additions & 0 deletions contracts/src/uniswap/v4/types/pool_key.rs
Original file line number Diff line number Diff line change
@@ -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<PoolKey> for PoolId {
fn from(value: PoolKey) -> Self {
let encoded = PoolKey::abi_encode(&value);
keccak256(encoded)
}
}
14 changes: 14 additions & 0 deletions contracts/src/uniswap/v4/types/swap_params.rs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 274362b

Please sign in to comment.