- Introduction
- Key Features
- Technical Architecture
- Dynamic Fees Algorithm
- Local Development Setup
- Running Tests
The Dynamic LP Assurance Hook is an advanced Uniswap V4 hook designed to protect liquidity providers (LPs) by dynamically calculating and collecting insurance fees, while also enabling flash loan functionalities. It ensures LPs are compensated for impermanent loss (IL) and that fees for swaps and flash loans are optimally calculated based on current market and pool conditions.
-
Dynamic Insurance Fee Calculation
- Adapts to trade size, pool liquidity, and other risk factors.
- Compensates LPs for impermanent loss during liquidity withdrawal.
- Offloads computationally intensive fee calculations to Stylus contracts for cost and execution efficiency.
-
Flash Loan Functionality
- Facilitates flash loans on idle liquidity aggregated across multiple pools.
- Dynamically adjusts flash loan fees based on utilization, liquidity, and trade size.
- Offloads fee computation to Stylus contracts for performance gains.
-
Efficient Fee Distribution
- Distributes insurance and flash loan fees proportionally among LPs based on pool contributions.
- On each swap (
beforeSwap
), callscalculateInsuranceFee
(Stylus contract). - Dynamically determines insurance fees based on pool conditions and trade parameters.
- Updates internal accounting (
poolDataMap
,tokenDataMap
) without direct token transfers.
- The
flashLoan
function executes loans using liquidity from multiple pools. - Calls
flashFee
(Stylus contract) to compute fees based on utilization and liquidity. - Ensures repayment plus fees, then distributes collected fees proportionally among LPs.
- The
beforeRemoveLiquidity
function calculates IL compensation for the withdrawing LP.
This is implemented in the Arbitrum Stylus Contract Stylus Repository.
Formula:
InsuranceFee = TradeSize Γ VolumeMultiplier Γ ILMultiplier Γ SizeMultiplier
- TradeSize: Size of the swap or liquidity movement.
- VolumeMultiplier: Increases or reduces fees based on total volume.
Example: 0.1 + (0.9 Γ TotalVolume) / (TotalVolume + 1) - ILMultiplier: Adjusts fees based on historical impermanent loss.
Example: 1 + 3 Γ HistoricalIL - SizeMultiplier: Higher fees for larger trades relative to liquidity.
Example: 1 + (TradeSize / TotalLiquidity)
Formula:
FlashLoanFee = LoanAmount Γ UtilizationMultiplier Γ LiquidityMultiplier Γ HistoricalMultiplier
- UtilizationMultiplier: Scales fee up as pool utilization increases.
- LiquidityMultiplier: Lowers fee when liquidity is high.
- HistoricalMultiplier: Adjusts fees based on past default or performance data.
-
Install Foundry (Forge)
Follow instructions at:
https://book.getfoundry.sh/getting-started/installation -
Run a Local Dev Node (e.g., Nitro Dev Node from Offchain Labs)
Clone and start the Arbitrum nitro dev node:git clone https://github.com/OffchainLabs/nitro-devnode.git cd nitro-devnode ./run-dev-node.sh
This runs the node on http://localhost:8547.
-
Check Dependencies
- Ensure you have
docker
anddocker-compose
for the Nitro Dev Node. - Ensure node.js and npm (or pnpm) are installed for the operator setup.
- Ensure you have
-
Build Contracts
forge build
-
Run Tests Against the Nitro Dev Node
With the Nitro dev node running on http://localhost:8547, run:forge test --match-path test/InsurancePoolHook.t.sol --rpc-url http://localhost:8547 -vvv --via-ir
--match-path
: Specifies the test file.--rpc-url
: Points to your local dev node.-vvv
: Increases verbosity.--via-ir
: Uses Yul IR pipeline for optimization.
-
View Logs and Results
The test output will show which tests passed or failed. Adjust parameters as needed for debugging or verbosity.