$npx -y skills add Uniswap/uniswap-ai --skill v4-sdk-integrationApp-layer SDK guide for building swap and liquidity experiences directly with the Uniswap v4 SDK. Use when user asks about "v4 sdk", "uniswap v4", "v4 swap", "v4 liquidity", "PoolManager", "V4Planner", "StateView", "PositionManager", "pool state", "v4 position", "uniswap sdk", or
| 1 | # Uniswap v4 SDK Integration |
| 2 | |
| 3 | > App-layer SDK for swaps, quotes, and liquidity. For Solidity hook contracts, use the |
| 4 | > `uniswap-hooks` skill. For Trading API or v3-centric swaps, use the `swap-integration` skill. |
| 5 | |
| 6 | ## When to Use |
| 7 | |
| 8 | - Token swap UI (single-hop or multi-hop) |
| 9 | - Quote/price display before executing a trade |
| 10 | - Liquidity position management (add/remove/collect) |
| 11 | - Pool state reads (price, tick, liquidity) |
| 12 | |
| 13 | ## Packages |
| 14 | |
| 15 | ```bash |
| 16 | npm i @uniswap/v4-sdk @uniswap/sdk-core @uniswap/universal-router-sdk |
| 17 | ``` |
| 18 | |
| 19 | --- |
| 20 | |
| 21 | ## v4 vs v3 Decision Table |
| 22 | |
| 23 | | Aspect | v3 | v4 | |
| 24 | | ------------------ | -------------------------- | --------------------------------------------- | |
| 25 | | Swap execution | SwapRouter directly | Universal Router required (V4Planner) | |
| 26 | | Pool architecture | One contract per pool | Singleton PoolManager | |
| 27 | | Pool state reads | Direct pool contract | StateView contract | |
| 28 | | Native ETH | Wrap to WETH | Native support (`Ether.onChain(chainId)`) | |
| 29 | | Position NFTs | NonfungiblePositionManager | PositionManager + multicall | |
| 30 | | Fee collection | Explicit `collect()` | Automatic on position modification | |
| 31 | | Position discovery | Onchain enumeration | Offchain event indexing | |
| 32 | | Token approvals | Direct approve | Permit2 required | |
| 33 | | Contract addresses | Same across chains | Different per chain — verify from deployments | |
| 34 | |
| 35 | --- |
| 36 | |
| 37 | ## Core Contracts (Per Chain) |
| 38 | |
| 39 | Look up addresses at <https://docs.uniswap.org/contracts/v4/deployments> — they differ per chain. |
| 40 | |
| 41 | | Contract | Purpose | |
| 42 | | ---------------- | --------------------------------------------------------------------------------------- | |
| 43 | | PoolManager | Singleton pool state | |
| 44 | | Universal Router | Swap execution entry point | |
| 45 | | Quoter | Offchain quote simulation (callStatic) | |
| 46 | | StateView | Pool state reads (getSlot0, getLiquidity) | |
| 47 | | PositionManager | LP position lifecycle | |
| 48 | | Permit2 | Token approval layer (same across chains: `0x000000000022D473030F116dDEE9F6B43aC78BA3`) | |
| 49 | |
| 50 | --- |
| 51 | |
| 52 | ## Swap Pattern (Universal Router) |
| 53 | |
| 54 | All swaps use: V4Planner -> RoutePlanner -> Universal Router `execute()`. |
| 55 | |
| 56 | **Single-hop (exact input):** |
| 57 | |
| 58 | ```typescript |
| 59 | import { Actions, V4Planner } from '@uniswap/v4-sdk'; |
| 60 | import { CommandType, RoutePlanner } from '@uniswap/universal-router-sdk'; |
| 61 | |
| 62 | const v4Planner = new V4Planner(); |
| 63 | v4Planner.addAction(Actions.SWAP_EXACT_IN_SINGLE, [swapConfig]); |
| 64 | v4Planner.addAction(Actions.SETTLE_ALL, [inputCurrency, amountIn]); |
| 65 | v4Planner.addAction(Actions.TAKE_ALL, [outputCurrency, amountOutMinimum]); |
| 66 | |
| 67 | const routePlanner = new RoutePlanner(); |
| 68 | routePlanner.addCommand(CommandType.V4_SWAP, [v4Planner.actions, v4Planner.params]); |
| 69 | |
| 70 | const deadline = Math.floor(Date.now() / 1000) + 3600; |
| 71 | // Note: universalRouter.execute() is pseudocode for the viem call pattern. |
| 72 | // With viem, use: walletClient.writeContract({ address: UNIVERSAL_ROUTER_ADDRESS, abi: universalRouterAbi, functionName: 'execute', args: [routePlanner.commands, [v4Planner.finalize()], deadline], ...txOptions }) |
| 73 | await universalRouter.execute(routePlanner.commands, [v4Planner.finalize()], deadline, txOptions); |
| 74 | ``` |
| 75 | |
| 76 | **Multi-hop (exact input):** |
| 77 | |
| 78 | ```typescript |
| 79 | import { Actions, V4Planner, encodeMultihopExactInPath } from '@uniswap/v4-sdk'; |
| 80 | import { CommandType, RoutePlanner } from '@uniswap/universal-router-sdk'; |
| 81 | |
| 82 | const v4Planner = new V4Planner(); |
| 83 | // Build multi-hop path: tokenA -> tokenB -> tokenC |
| 84 | const path = encodeMultihopExactInPath([poolKeyAB, poolKeyBC], tokenA); |
| 85 | v4Planner.addAction(Actions.SWAP_EXACT_IN, [{ path, amountIn, amountOutMinimum }]); |
| 86 | // SETTLE_ALL uses first pool's input currency; TAKE_ALL uses last pool's output currency |
| 87 | v4Planner.addAction(Actions.SETTLE_ALL, [tokenA, amountIn]); |
| 88 | v4Planner.addAction( |