Nov 26, 2023

Uniswap 4.0

Notable features:

  • A time-weighted average market maker
  • Singleton Design
  • Hooks
  • Non Upgradeable
  • Onchain limit orders
  • Depositing out-of-range liquidity into lending protocols
  • Autocompounded LP fees back into the LP positions
  • Better Gas optimization
  • Customizable
  • Developer friendly
  • Super Generic

Uniswap V3 Context

V3: Screenshot from a great talk given by Uniswap team. https://www.youtube.com/watch?v=iILNV4YFGdk V3: Screenshot from a great talk given by Uniswap team. https://www.youtube.com/watch?v=iILNV4YFGdk V3: Screenshot from a great talk given by Uniswap team. https://www.youtube.com/watch?v=iILNV4YFGdk

Uniswap V4

V4: Screenshot from a great talk given by Uniswap team. https://www.youtube.com/watch?v=iILNV4YFGdk V4: Screenshot from a great talk given by Uniswap team. https://www.youtube.com/watch?v=iILNV4YFGdk V4: Screenshot from a great talk given by Uniswap team. https://www.youtube.com/watch?v=iILNV4YFGdk V4: Screenshot from a great talk given by Uniswap team. https://www.youtube.com/watch?v=iILNV4YFGdk V4: Screenshot from a great talk given by Uniswap team. https://www.youtube.com/watch?v=iILNV4YFGdk V4: Screenshot from a great talk given by Uniswap team. https://www.youtube.com/watch?v=iILNV4YFGdk V4: Screenshot from a great talk given by Uniswap team. https://www.youtube.com/watch?v=iILNV4YFGdk V4: Screenshot from a great talk given by Uniswap team. https://www.youtube.com/watch?v=iILNV4YFGdk V4: Screenshot from a great talk given by Uniswap team. https://www.youtube.com/watch?v=iILNV4YFGdk V4: Diagram Drawn via Audit Smart

PoolManager Contract:

Contract Inheritance:

  • Inherits from IPoolManager, Fees, NoDelegateCall, and Claims.

Constants:

  • MAX_TICK_SPACING and MIN_TICK_SPACING used for valid tick spacing in pools.

State Variables:

  • currencyDelta: Tracks net currency amounts for lockers.
  • reservesOf: Tracks currency reserves.
  • pools: Mapping of pool IDs to their states.

Constructor:

  • Initializes with a gas limit for the controller, passed to Fees.

Pool Management Functions:

  • _getPool: Private function to retrieve pool state.
  • initialize: Sets up a pool with parameters, checks validity, and calls hook functions if defined.
  • lock: Allows locker actions, settles currency balances at lock end.
  • modifyPosition: Adjusts liquidity, handles fees for LPs.
  • swap: Executes token swaps within a pool, updates balances and fees.
  • donate: Adds liquidity to a pool without receiving tokens.
  • take, settle, mint, burn: Manage pool reserves and balances.

Fee Management:

  • Functions to update protocol, hook, and swap fees.

External Storage Loading:

  • Functions extsload read storage slots directly for off-chain use.

Locking Mechanism:

  • Manages and queries the locking mechanism to ensure atomic actions.

Fallback Function:

  • receive function to accept native tokens for relevant pools.

Security Considerations:

  • Uses noDelegateCall modifier to prevent delegate calls.
  • Locking mechanism prevents reentrancy with onlyByLocker modifier.
  • Uses unchecked blocks for arithmetic (be cautious of overflow/underflow).
  • Hooks and callbacks add complexity; return values are checked for expected selectors.
  • initialize prevents invalid pool configurations.
  • Emits events for transparency and off-chain monitoring.

State-Changing Functions in PoolManager Contract

  • initialize: Initializes a pool, potentially changing state variables.
  • lock: Allows locker actions, settles currency balances at the end, altering state.
  • _accountDelta: Potentially modifies account currency deltas.
  • _accountPoolBalanceDelta: Alters account pool balance deltas, affecting state.
  • modifyPosition: Changes liquidity provider positions, adjusts liquidity, handles fees.
  • swap: Executes token swaps within a pool, updating balances and fees, affecting state.
  • donate: Adds liquidity to a pool without receiving tokens, potentially altering state.
  • take, settle, mint, burn: Manage pool reserves and balances, changing the state.
  • setProtocolFees: Updates protocol fees, potentially changing contract behavior.
  • setHookFees: Alters hook fees, potentially impacting transaction fees.
  • updateDynamicSwapFee: Updates dynamic swap fees for trades, affecting state.

Additional State Change:

  • receive Function: Receives native tokens (e.g., ETH), changing the contract's balance.
V4: Diagram Drawn via Audit Smart

Fees Contract Overview

Constants and State Variables:

  • MIN_PROTOCOL_FEE_DENOMINATOR: Sets the minimum denominator for protocol fees.
  • MAX_SWAP_FEE: Defines the maximum allowed swap fee.
  • protocolFeesAccrued: Tracks accrued protocol fees per currency.
  • hookFeesAccrued: Tracks accrued hook fees per currency and hook address.
  • protocolFeeController: Manages protocol fees.
  • controllerGasLimit: Immutable gas limit for calls to the protocol fee controller.

Constructor:

  • Sets controllerGasLimit on contract deployment.

Fee Fetching Functions:

  • _fetchProtocolFees: Retrieves protocol fees for a pool, performs gas checks, and validates fees.
  • _fetchHookFees: Retrieves hook fees for a pool based on mask and hook conditions.
  • _fetchDynamicSwapFee: Retrieves dynamic swap fee for a pool, reverts if fee is too large.

Fee Checking Function:

  • _checkProtocolFee: Ensures fee is not below MIN_PROTOCOL_FEE_DENOMINATOR unless zero.

Protocol Fee Controller Management:

  • setProtocolFeeController: Allows owner to set the protocol fee controller.

Fee Collection Functions:

  • collectProtocolFees: Allows owner or protocol fee controller to collect accrued protocol fees for a currency.
  • collectHookFees: Allows a hook to collect accrued hook fees for a currency.

Security Considerations:

  • Uses Solidity ^0.8.x with built-in overflow checks, reducing arithmetic vulnerabilities.
  • try/catch blocks in fee-fetching functions prevent revert on external call failure, but may cause unexpected behavior.
  • controllerGasLimit must be carefully set to avoid EIP-150's 63/64 rule causing out-of-gas errors.
  • collectProtocolFees and collectHookFees have checks for authorized addresses, promoting security.
  • collectHookFees allowing recipient set to hook address for zero address could be a feature or potential vulnerability.
  • Emits an event when protocol fee controller updates, enhancing transparency.
  • Incomplete implementation of IFees interface due to abstract nature. Full implementation review necessary for security.

Overall, while the contract structure seems robust, a thorough security audit should encompass all external contracts, libraries, and system-wide checks for vulnerabilities or logical errors.

State-Changing Functions

setProtocolFeeController(IProtocolFeeController controller):

  • Sets the protocol fee controller to a new address.
  • Accessible only by the owner of the contract.

collectProtocolFees(address recipient, Currency currency, uint256 amount):

  • Collects accrued protocol fees for a given currency and transfers them to a specified recipient.
  • Accessible by the owner or the protocol fee controller.

collectHookFees(address recipient, Currency currency, uint256 amount):

  • Collects accrued hook fees for a given currency and transfers them to a specified recipient.
  • Accessible by any hook address that has accrued fees.
V4: Diagram Drawn via Audit Smart

Claims Contract Functionality

  • balances: Private mapping tracking each account's currency balance.
  • balanceOf: Public view function returning the balance of a specified currency for an account.
  • transfer: Public function allowing currency transfer between accounts after checking recipient and sender conditions. Deducts from sender and adds to recipient, emitting a Transfer event.
  • _mint: Internal function increasing currency balance for an account and emitting a Mint event, callable only within the contract or derived contracts.
  • _burn: Internal function decreasing currency balance for the message sender and emitting a Burn event, provided sender has sufficient balance.

Security Considerations:

  • transfer function lacks zero address check for the recipient but prevents sending to the contract's address, potentially a design choice.
  • Uses unchecked block in transfer function for balance subtraction after ensuring sufficient balance, optimizing gas.
  • _mint and _burn are internal, accessible only to the contract or derived contracts, preventing external minting/burning.
  • No access control for _mint function, advisable to restrict minting privileges for security.
  • Emits events for Transfer, Mint, and Burn operations, enhancing transparency.
  • The use of a custom Currency type and its associated library could impact contract security, warranting a thorough review.
  • Non-compliance with ERC-20 standard might affect compatibility with wallets/interfaces.
  • Specifies BUSL-1.1 license, impacting legal compliance and usage terms.

Overall, the contract seems a basic implementation of a multi-currency token system. However, a comprehensive security audit should cover Currency type, CurrencyLibrary, IClaims interface, and other interacting contracts. Also, context like governance mechanisms and administrative functions influencing minting/burning should be considered.

State-Changing Functions in Claims Contract:

  • transfer: Modifies the balances mapping by transferring a specified amount of currency from the sender to the recipient.
  • _mint: Increases the balance of a particular currency for a specified address in the balances mapping.
  • _burn: Decreases the balance of a specific currency for the message sender in the balances mapping.

Note:

References:

https://www.youtube.com/watch?v=iILNV4YFGdk


Thanks for reading! If you want to see future content, you can follow me on Twitter or get connected over at LinkedIn.


Support My Content

If you find my content helpful, consider supporting a humanitarian cause (building homes for elderly people in rural Terai region of Nepal) that I am planning with your donation:

Ethereum (ETH)

0xb0b1B20062DA9Dd7BaA4D5b088DF49dbe4b46fF2

Thank you for your support!