|
1 | | -<img width="1284" height="689" alt="image" src="https://github.com/user-attachments/assets/1df7b169-7832-41e0-aabe-09738df1329c" /># Uniswap V3 |
2 | | - |
3 | | -[](https://github.com/Uniswap/uniswap-v3-core/actions/workflows/lint.yml) |
4 | | -[](https://github.com/Uniswap/uniswap-v3-core/actions/workflows/tests.yml) |
5 | | -[](https://github.com/Uniswap/uniswap-v3-core/actions/workflows/fuzz-testing.yml) |
6 | | -[](https://github.com/Uniswap/uniswap-v3-core/actions/workflows/mythx.yml) |
7 | | -[](https://www.npmjs.com/package/@uniswap/v3-core/v/latest) |
8 | | - |
9 | | -This repository contains the core smart contracts for the Uniswap V3 Protocol. |
10 | | -For higher level contracts, see the [uniswap-v3-periphery](https://github.com/Uniswap/uniswap-v3-periphery) |
11 | | -repository. |
12 | | - |
13 | | -## Bug bounty |
14 | | - |
15 | | -This repository is subject to the Uniswap V3 bug bounty program, per the terms defined [here](./bug-bounty.md). |
16 | | - |
17 | | -## Local deployment |
18 | | - |
19 | | -In order to deploy this code to a local testnet, you should install the npm package |
20 | | -`@uniswap/v3-core` |
21 | | -and import the factory bytecode located at |
22 | | -`@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json`. |
23 | | -For example: |
24 | | - |
25 | | -```typescript |
26 | | -import { |
27 | | - abi as FACTORY_ABI, |
28 | | - bytecode as FACTORY_BYTECODE, |
29 | | -} from '@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json' |
30 | | - |
31 | | -// deploy the bytecode |
32 | | -``` |
33 | | - |
34 | | -This will ensure that you are testing against the same bytecode that is deployed to |
35 | | -mainnet and public testnets, and all Uniswap code will correctly interoperate with |
36 | | -your local deployment. |
37 | | - |
38 | | -## Using solidity interfaces |
39 | | - |
40 | | -The Uniswap v3 interfaces are available for import into solidity smart contracts |
41 | | -via the npm artifact `@uniswap/v3-core`, e.g.: |
42 | | - |
43 | | -```solidity |
44 | | -import '@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol'; |
45 | | -
|
46 | | -contract MyContract { |
47 | | - IUniswapV3Pool pool; |
48 | | -
|
49 | | - function doSomethingWithPool() { |
50 | | - // pool.swap(...); |
51 | | - } |
52 | | -} |
53 | | -
|
54 | | -``` |
55 | | -## Example: Reading Pool Data |
56 | | - |
57 | | -Here is a simple example using Ethers.js to read basic pool data from a Uniswap V3 pool: |
58 | | - |
59 | | -```javascript |
60 | | -import { ethers } from "ethers"; |
61 | | - |
62 | | -const provider = new ethers.providers.JsonRpcProvider("YOUR_RPC_URL"); |
63 | | - |
64 | | -const poolAddress = "UNISWAP_POOL_ADDRESS"; |
65 | | - |
66 | | -const abi = [ |
67 | | - "function slot0() external view returns (uint160 sqrtPriceX96, int24 tick)" |
68 | | -]; |
69 | | - |
70 | | -const contract = new ethers.Contract(poolAddress, abi, provider); |
71 | | - |
72 | | -async function main() { |
73 | | - const data = await contract.slot0(); |
74 | | - console.log("sqrtPriceX96:", data.sqrtPriceX96.toString()); |
75 | | - console.log("tick:", data.tick); |
76 | | -} |
77 | | - |
78 | | -main(); |
79 | | -``` |
80 | | -β οΈ Make sure to replace `YOUR_RPC_URL` and `UNISWAP_POOL_ADDRESS` with valid values. |
81 | | -## π Uniswap V3 Pool Data Flow (Simplified) |
82 | | - |
83 | | -```text |
84 | | -+-----------------------------------------------------------+ |
85 | | -| UNISWAP V3 POOL FLOW | |
86 | | -+-----------------------------------------------------------+ |
87 | | -
|
88 | | - π€ User / Trader |
89 | | - (swap / mint / burn / collect) |
90 | | - β |
91 | | - β on-chain call |
92 | | - βΌ |
93 | | - +-----------------------------------+ |
94 | | - | Uniswap V3 Pool Contract | |
95 | | - | (UniswapV3Pool.sol) | |
96 | | - +-----------------------------------+ |
97 | | - β |
98 | | - βΌ |
99 | | - +-----------------------------------+ |
100 | | - | slot0 (Core State) | |
101 | | - | (single SLOAD, packed storage) | |
102 | | - +-----------------------------------+ |
103 | | - β β β |
104 | | - βΌ βΌ βΌ |
105 | | - +---------------+ +---------------+ +-------------------+ |
106 | | - | sqrtPriceX96 | | tick | | observationIndex | |
107 | | - | (Q64.96) | | log(1.0001 P) | | oracle pointer | |
108 | | - +---------------+ +---------------+ +-------------------+ |
109 | | - β β β |
110 | | - βΌ βΌ βΌ |
111 | | - +---------------+ +---------------+ +-------------------+ |
112 | | - | Spot Price | | Active Range | | TWAP / Oracle | |
113 | | - | price=(βP)^2 | | tickLower <= | | cumulative ticks | |
114 | | - | | | tick <= upper | | / time β avgPrice | |
115 | | - +---------------+ +---------------+ +-------------------+ |
116 | | - β |
117 | | - βΌ |
118 | | - +-----------------------------------+ |
119 | | - | Derived Outputs | |
120 | | - | β’ Token Price | |
121 | | - | β’ Liquidity | |
122 | | - | β’ Pool State | |
123 | | - +-----------------------------------+ |
124 | | -``` |
125 | | -## Licensing |
126 | | - |
127 | | -The primary license for Uniswap V3 Core is the Business Source License 1.1 (`BUSL-1.1`), see [`LICENSE`](./LICENSE). However, some files are dual licensed under `GPL-2.0-or-later`: |
128 | | - |
129 | | -- All files in `contracts/interfaces/` may also be licensed under `GPL-2.0-or-later` (as indicated in their SPDX headers), see [`contracts/interfaces/LICENSE`](./contracts/interfaces/LICENSE) |
130 | | -- Several files in `contracts/libraries/` may also be licensed under `GPL-2.0-or-later` (as indicated in their SPDX headers), see [`contracts/libraries/LICENSE`](contracts/libraries/LICENSE) |
131 | | - |
132 | | -### Other Exceptions |
133 | | - |
134 | | -- `contracts/libraries/FullMath.sol` is licensed under `MIT` (as indicated in its SPDX header), see [`contracts/libraries/LICENSE_MIT`](contracts/libraries/LICENSE_MIT) |
135 | | -- All files in `contracts/test` remain unlicensed (as indicated in their SPDX headers). |
| 1 | +<img width="1284" height="689" alt="image" src="https://github.com/user-attachments/assets/1df7b169-7832-41e0-aabe-09738df1329c" /># Uniswap V3 |
| 2 | + |
| 3 | +[](https://github.com/Uniswap/uniswap-v3-core/actions/workflows/lint.yml) |
| 4 | +[](https://github.com/Uniswap/uniswap-v3-core/actions/workflows/tests.yml) |
| 5 | +[](https://github.com/Uniswap/uniswap-v3-core/actions/workflows/fuzz-testing.yml) |
| 6 | +[](https://github.com/Uniswap/uniswap-v3-core/actions/workflows/mythx.yml) |
| 7 | +[](https://www.npmjs.com/package/@uniswap/v3-core/v/latest) |
| 8 | + |
| 9 | +This repository contains the core smart contracts for the Uniswap V3 Protocol. |
| 10 | +For higher level contracts, see the [uniswap-v3-periphery](https://github.com/Uniswap/uniswap-v3-periphery) |
| 11 | +repository. |
| 12 | + |
| 13 | +## Bug bounty |
| 14 | + |
| 15 | +This repository is subject to the Uniswap V3 bug bounty program, per the terms defined [here](./bug-bounty.md). |
| 16 | + |
| 17 | +## Local deployment |
| 18 | + |
| 19 | +In order to deploy this code to a local testnet, you should install the npm package |
| 20 | +`@uniswap/v3-core` |
| 21 | +and import the factory bytecode located at |
| 22 | +`@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json`. |
| 23 | +For example: |
| 24 | + |
| 25 | +```typescript |
| 26 | +import { |
| 27 | + abi as FACTORY_ABI, |
| 28 | + bytecode as FACTORY_BYTECODE, |
| 29 | +} from '@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json' |
| 30 | + |
| 31 | +// deploy the bytecode |
| 32 | +``` |
| 33 | + |
| 34 | +This will ensure that you are testing against the same bytecode that is deployed to |
| 35 | +mainnet and public testnets, and all Uniswap code will correctly interoperate with |
| 36 | +your local deployment. |
| 37 | + |
| 38 | +## Using solidity interfaces |
| 39 | + |
| 40 | +The Uniswap v3 interfaces are available for import into solidity smart contracts |
| 41 | +via the npm artifact `@uniswap/v3-core`, e.g.: |
| 42 | + |
| 43 | +```solidity |
| 44 | +import '@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol'; |
| 45 | +
|
| 46 | +contract MyContract { |
| 47 | + IUniswapV3Pool pool; |
| 48 | +
|
| 49 | + function doSomethingWithPool() { |
| 50 | + // pool.swap(...); |
| 51 | + } |
| 52 | +} |
| 53 | +
|
| 54 | +``` |
| 55 | +## Example: Reading Pool Data |
| 56 | + |
| 57 | +Here is a simple example using Ethers.js to read basic pool data from a Uniswap V3 pool: |
| 58 | + |
| 59 | +```javascript |
| 60 | +import { ethers } from "ethers"; |
| 61 | + |
| 62 | +const provider = new ethers.providers.JsonRpcProvider("YOUR_RPC_URL"); |
| 63 | + |
| 64 | +const poolAddress = "UNISWAP_POOL_ADDRESS"; |
| 65 | + |
| 66 | +const abi = [ |
| 67 | + "function slot0() external view returns (uint160 sqrtPriceX96, int24 tick)" |
| 68 | +]; |
| 69 | + |
| 70 | +const contract = new ethers.Contract(poolAddress, abi, provider); |
| 71 | + |
| 72 | +async function main() { |
| 73 | + const data = await contract.slot0(); |
| 74 | + console.log("sqrtPriceX96:", data.sqrtPriceX96.toString()); |
| 75 | + console.log("tick:", data.tick); |
| 76 | +} |
| 77 | + |
| 78 | +main(); |
| 79 | +``` |
| 80 | +β οΈ Make sure to replace `YOUR_RPC_URL` and `UNISWAP_POOL_ADDRESS` with valid values. |
| 81 | +## π Uniswap V3 Pool Data Flow (Simplified) |
| 82 | + |
| 83 | +```text |
| 84 | ++-----------------------------------------------------------+ |
| 85 | +| UNISWAP V3 POOL FLOW | |
| 86 | ++-----------------------------------------------------------+ |
| 87 | +
|
| 88 | + π€ User / Trader |
| 89 | + (swap / mint / burn / collect) |
| 90 | + β |
| 91 | + β on-chain call |
| 92 | + βΌ |
| 93 | + +-----------------------------------+ |
| 94 | + | Uniswap V3 Pool Contract | |
| 95 | + | (UniswapV3Pool.sol) | |
| 96 | + +-----------------------------------+ |
| 97 | + β |
| 98 | + βΌ |
| 99 | + +-----------------------------------+ |
| 100 | + | slot0 (Core State) | |
| 101 | + | (single SLOAD, packed storage) | |
| 102 | + +-----------------------------------+ |
| 103 | + β β β |
| 104 | + βΌ βΌ βΌ |
| 105 | + +---------------+ +---------------+ +-------------------+ |
| 106 | + | sqrtPriceX96 | | tick | | observationIndex | |
| 107 | + | (Q64.96) | | log(1.0001 P) | | oracle pointer | |
| 108 | + +---------------+ +---------------+ +-------------------+ |
| 109 | + β β β |
| 110 | + βΌ βΌ βΌ |
| 111 | + +---------------+ +---------------+ +-------------------+ |
| 112 | + | Spot Price | | Active Range | | TWAP / Oracle | |
| 113 | + | price=(βP)^2 | | tickLower <= | | cumulative ticks | |
| 114 | + | | | tick <= upper | | / time β avgPrice | |
| 115 | + +---------------+ +---------------+ +-------------------+ |
| 116 | + β |
| 117 | + βΌ |
| 118 | + +-----------------------------------+ |
| 119 | + | Derived Outputs | |
| 120 | + | β’ Token Price | |
| 121 | + | β’ Liquidity | |
| 122 | + | β’ Pool State | |
| 123 | + +-----------------------------------+ |
| 124 | +``` |
| 125 | +## π§ Liquidity in Uniswap V3 (Simple Explanation) |
| 126 | + |
| 127 | +Liquidity refers to the tokens deposited into a pool that enable trading. |
| 128 | + |
| 129 | +### π§ V2 vs V3 Difference |
| 130 | + |
| 131 | +- V2: Liquidity is distributed across the entire price range (0 β β) |
| 132 | +- V3: Liquidity is provided within a specific price range (more efficient) |
| 133 | + |
| 134 | +### π Example |
| 135 | + |
| 136 | +Assume: |
| 137 | +ETH price = $2000 |
| 138 | + |
| 139 | +You provide liquidity in the range: |
| 140 | +$1800 β $2200 |
| 141 | + |
| 142 | +π Your liquidity will only be active within this price range. |
| 143 | + |
| 144 | +### π₯ Key Concept |
| 145 | + |
| 146 | +- When price is within your range β you earn fees π° |
| 147 | +- When price moves outside β your liquidity becomes inactive β |
| 148 | + |
| 149 | +### π― Why this matters? |
| 150 | + |
| 151 | +- Higher capital efficiency |
| 152 | +- Better control over positions |
| 153 | +- Enables advanced liquidity strategies |
| 154 | +## Licensing |
| 155 | + |
| 156 | +The primary license for Uniswap V3 Core is the Business Source License 1.1 (`BUSL-1.1`), see [`LICENSE`](./LICENSE). However, some files are dual licensed under `GPL-2.0-or-later`: |
| 157 | + |
| 158 | +- All files in `contracts/interfaces/` may also be licensed under `GPL-2.0-or-later` (as indicated in their SPDX headers), see [`contracts/interfaces/LICENSE`](./contracts/interfaces/LICENSE) |
| 159 | +- Several files in `contracts/libraries/` may also be licensed under `GPL-2.0-or-later` (as indicated in their SPDX headers), see [`contracts/libraries/LICENSE`](contracts/libraries/LICENSE) |
| 160 | + |
| 161 | +### Other Exceptions |
| 162 | + |
| 163 | +- `contracts/libraries/FullMath.sol` is licensed under `MIT` (as indicated in its SPDX header), see [`contracts/libraries/LICENSE_MIT`](contracts/libraries/LICENSE_MIT) |
| 164 | +- All files in `contracts/test` remain unlicensed (as indicated in their SPDX headers). |
0 commit comments