@huuduynvc/v3-periphery 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +339 -0
- package/README.md +52 -0
- package/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json +1230 -0
- package/artifacts/contracts/NonfungibleTokenPositionDescriptor.sol/NonfungibleTokenPositionDescriptor.json +161 -0
- package/artifacts/contracts/SwapRouter.sol/SwapRouter.json +574 -0
- package/artifacts/contracts/V3Migrator.sol/V3Migrator.json +360 -0
- package/artifacts/contracts/examples/PairFlash.sol/PairFlash.json +196 -0
- package/artifacts/contracts/interfaces/IERC20Metadata.sol/IERC20Metadata.json +233 -0
- package/artifacts/contracts/interfaces/IERC721Permit.sol/IERC721Permit.json +360 -0
- package/artifacts/contracts/interfaces/IMulticall.sol/IMulticall.json +30 -0
- package/artifacts/contracts/interfaces/INonfungiblePositionManager.sol/INonfungiblePositionManager.json +998 -0
- package/artifacts/contracts/interfaces/INonfungibleTokenPositionDescriptor.sol/INonfungibleTokenPositionDescriptor.json +35 -0
- package/artifacts/contracts/interfaces/IPeripheryImmutableState.sol/IPeripheryImmutableState.json +37 -0
- package/artifacts/contracts/interfaces/IPeripheryPayments.sol/IPeripheryPayments.json +59 -0
- package/artifacts/contracts/interfaces/IPeripheryPaymentsWithFee.sol/IPeripheryPaymentsWithFee.json +120 -0
- package/artifacts/contracts/interfaces/IPoolInitializer.sol/IPoolInitializer.json +45 -0
- package/artifacts/contracts/interfaces/IQuoter.sol/IQuoter.json +137 -0
- package/artifacts/contracts/interfaces/IQuoterV2.sol/IQuoterV2.json +211 -0
- package/artifacts/contracts/interfaces/ISelfPermit.sol/ISelfPermit.json +163 -0
- package/artifacts/contracts/interfaces/ISwapRouter.sol/ISwapRouter.json +248 -0
- package/artifacts/contracts/interfaces/ITickLens.sol/ITickLens.json +52 -0
- package/artifacts/contracts/interfaces/IV3Migrator.sol/IV3Migrator.json +296 -0
- package/artifacts/contracts/interfaces/external/IERC1271.sol/IERC1271.json +35 -0
- package/artifacts/contracts/interfaces/external/IERC20PermitAllowed.sol/IERC20PermitAllowed.json +59 -0
- package/artifacts/contracts/interfaces/external/IWETH9.sol/IWETH9.json +214 -0
- package/artifacts/contracts/lens/Quoter.sol/Quoter.json +202 -0
- package/artifacts/contracts/lens/QuoterV2.sol/QuoterV2.json +276 -0
- package/artifacts/contracts/lens/TickLens.sol/TickLens.json +52 -0
- package/artifacts/contracts/lens/UniswapInterfaceMulticall.sol/UniswapInterfaceMulticall.json +101 -0
- package/artifacts/contracts/libraries/BytesLib.sol/BytesLib.json +10 -0
- package/artifacts/contracts/libraries/CallbackValidation.sol/CallbackValidation.json +10 -0
- package/artifacts/contracts/libraries/ChainId.sol/ChainId.json +10 -0
- package/artifacts/contracts/libraries/HexStrings.sol/HexStrings.json +10 -0
- package/artifacts/contracts/libraries/LiquidityAmounts.sol/LiquidityAmounts.json +10 -0
- package/artifacts/contracts/libraries/NFTDescriptor.sol/NFTDescriptor.json +102 -0
- package/artifacts/contracts/libraries/NFTSVG.sol/NFTSVG.json +10 -0
- package/artifacts/contracts/libraries/OracleLibrary.sol/OracleLibrary.json +10 -0
- package/artifacts/contracts/libraries/Path.sol/Path.json +10 -0
- package/artifacts/contracts/libraries/PoolAddress.sol/PoolAddress.json +10 -0
- package/artifacts/contracts/libraries/PoolTicksCounter.sol/PoolTicksCounter.json +10 -0
- package/artifacts/contracts/libraries/PositionKey.sol/PositionKey.json +10 -0
- package/artifacts/contracts/libraries/PositionValue.sol/PositionValue.json +10 -0
- package/artifacts/contracts/libraries/TokenRatioSortOrder.sol/TokenRatioSortOrder.json +10 -0
- package/artifacts/contracts/libraries/TransferHelper.sol/TransferHelper.json +10 -0
- package/contracts/base/BlockTimestamp.sol +12 -0
- package/contracts/base/ERC721Permit.sol +86 -0
- package/contracts/base/LiquidityManagement.sol +90 -0
- package/contracts/base/Multicall.sol +28 -0
- package/contracts/base/PeripheryImmutableState.sol +18 -0
- package/contracts/base/PeripheryPayments.sol +70 -0
- package/contracts/base/PeripheryPaymentsWithFee.sol +55 -0
- package/contracts/base/PeripheryValidation.sol +11 -0
- package/contracts/base/PoolInitializer.sol +32 -0
- package/contracts/base/SelfPermit.sol +63 -0
- package/contracts/interfaces/IERC20Metadata.sol +18 -0
- package/contracts/interfaces/IERC721Permit.sol +32 -0
- package/contracts/interfaces/IMulticall.sol +13 -0
- package/contracts/interfaces/INonfungiblePositionManager.sol +180 -0
- package/contracts/interfaces/INonfungibleTokenPositionDescriptor.sol +17 -0
- package/contracts/interfaces/IPeripheryImmutableState.sol +12 -0
- package/contracts/interfaces/IPeripheryPayments.sol +28 -0
- package/contracts/interfaces/IPeripheryPaymentsWithFee.sol +29 -0
- package/contracts/interfaces/IPoolInitializer.sol +22 -0
- package/contracts/interfaces/IQuoter.sol +51 -0
- package/contracts/interfaces/IQuoterV2.sol +98 -0
- package/contracts/interfaces/ISelfPermit.sol +76 -0
- package/contracts/interfaces/ISwapRouter.sol +67 -0
- package/contracts/interfaces/ITickLens.sol +25 -0
- package/contracts/interfaces/IV3Migrator.sol +34 -0
- package/contracts/interfaces/external/IERC1271.sol +16 -0
- package/contracts/interfaces/external/IERC20PermitAllowed.sol +27 -0
- package/contracts/interfaces/external/IWETH9.sol +13 -0
- package/contracts/libraries/BytesLib.sol +101 -0
- package/contracts/libraries/CallbackValidation.sol +36 -0
- package/contracts/libraries/ChainId.sol +13 -0
- package/contracts/libraries/HexStrings.sol +29 -0
- package/contracts/libraries/LiquidityAmounts.sol +137 -0
- package/contracts/libraries/NFTDescriptor.sol +477 -0
- package/contracts/libraries/NFTSVG.sol +406 -0
- package/contracts/libraries/OracleLibrary.sol +161 -0
- package/contracts/libraries/Path.sol +69 -0
- package/contracts/libraries/PoolAddress.sol +48 -0
- package/contracts/libraries/PoolTicksCounter.sol +97 -0
- package/contracts/libraries/PositionKey.sol +13 -0
- package/contracts/libraries/PositionValue.sol +167 -0
- package/contracts/libraries/TokenRatioSortOrder.sol +12 -0
- package/contracts/libraries/TransferHelper.sol +60 -0
- package/package.json +68 -0
@@ -0,0 +1,406 @@
|
|
1
|
+
// SPDX-License-Identifier: GPL-2.0-or-later
|
2
|
+
pragma solidity >=0.7.6;
|
3
|
+
|
4
|
+
import '@openzeppelin/contracts/utils/Strings.sol';
|
5
|
+
import '@huuduynvc/v3-core/contracts/libraries/BitMath.sol';
|
6
|
+
import 'base64-sol/base64.sol';
|
7
|
+
|
8
|
+
/// @title NFTSVG
|
9
|
+
/// @notice Provides a function for generating an SVG associated with a Uniswap NFT
|
10
|
+
library NFTSVG {
|
11
|
+
using Strings for uint256;
|
12
|
+
|
13
|
+
string constant curve1 = 'M1 1C41 41 105 105 145 145';
|
14
|
+
string constant curve2 = 'M1 1C33 49 97 113 145 145';
|
15
|
+
string constant curve3 = 'M1 1C33 57 89 113 145 145';
|
16
|
+
string constant curve4 = 'M1 1C25 65 81 121 145 145';
|
17
|
+
string constant curve5 = 'M1 1C17 73 73 129 145 145';
|
18
|
+
string constant curve6 = 'M1 1C9 81 65 137 145 145';
|
19
|
+
string constant curve7 = 'M1 1C1 89 57.5 145 145 145';
|
20
|
+
string constant curve8 = 'M1 1C1 97 49 145 145 145';
|
21
|
+
|
22
|
+
struct SVGParams {
|
23
|
+
string quoteToken;
|
24
|
+
string baseToken;
|
25
|
+
address poolAddress;
|
26
|
+
string quoteTokenSymbol;
|
27
|
+
string baseTokenSymbol;
|
28
|
+
string feeTier;
|
29
|
+
int24 tickLower;
|
30
|
+
int24 tickUpper;
|
31
|
+
int24 tickSpacing;
|
32
|
+
int8 overRange;
|
33
|
+
uint256 tokenId;
|
34
|
+
string color0;
|
35
|
+
string color1;
|
36
|
+
string color2;
|
37
|
+
string color3;
|
38
|
+
string x1;
|
39
|
+
string y1;
|
40
|
+
string x2;
|
41
|
+
string y2;
|
42
|
+
string x3;
|
43
|
+
string y3;
|
44
|
+
}
|
45
|
+
|
46
|
+
function generateSVG(SVGParams memory params) internal pure returns (string memory svg) {
|
47
|
+
/*
|
48
|
+
address: "0xe8ab59d3bcde16a29912de83a90eb39628cfc163",
|
49
|
+
msg: "Forged in SVG for Uniswap in 2021 by 0xe8ab59d3bcde16a29912de83a90eb39628cfc163",
|
50
|
+
sig: "0x2df0e99d9cbfec33a705d83f75666d98b22dea7c1af412c584f7d626d83f02875993df740dc87563b9c73378f8462426da572d7989de88079a382ad96c57b68d1b",
|
51
|
+
version: "2"
|
52
|
+
*/
|
53
|
+
return
|
54
|
+
string(
|
55
|
+
abi.encodePacked(
|
56
|
+
generateSVGDefs(params),
|
57
|
+
generateSVGBorderText(
|
58
|
+
params.quoteToken,
|
59
|
+
params.baseToken,
|
60
|
+
params.quoteTokenSymbol,
|
61
|
+
params.baseTokenSymbol
|
62
|
+
),
|
63
|
+
generateSVGCardMantle(params.quoteTokenSymbol, params.baseTokenSymbol, params.feeTier),
|
64
|
+
generageSvgCurve(params.tickLower, params.tickUpper, params.tickSpacing, params.overRange),
|
65
|
+
generateSVGPositionDataAndLocationCurve(
|
66
|
+
params.tokenId.toString(),
|
67
|
+
params.tickLower,
|
68
|
+
params.tickUpper
|
69
|
+
),
|
70
|
+
generateSVGRareSparkle(params.tokenId, params.poolAddress),
|
71
|
+
'</svg>'
|
72
|
+
)
|
73
|
+
);
|
74
|
+
}
|
75
|
+
|
76
|
+
function generateSVGDefs(SVGParams memory params) private pure returns (string memory svg) {
|
77
|
+
svg = string(
|
78
|
+
abi.encodePacked(
|
79
|
+
'<svg width="290" height="500" viewBox="0 0 290 500" xmlns="http://www.w3.org/2000/svg"',
|
80
|
+
" xmlns:xlink='http://www.w3.org/1999/xlink'>",
|
81
|
+
'<defs>',
|
82
|
+
'<filter id="f1"><feImage result="p0" xlink:href="data:image/svg+xml;base64,',
|
83
|
+
Base64.encode(
|
84
|
+
bytes(
|
85
|
+
abi.encodePacked(
|
86
|
+
"<svg width='290' height='500' viewBox='0 0 290 500' xmlns='http://www.w3.org/2000/svg'><rect width='290px' height='500px' fill='#",
|
87
|
+
params.color0,
|
88
|
+
"'/></svg>"
|
89
|
+
)
|
90
|
+
)
|
91
|
+
),
|
92
|
+
'"/><feImage result="p1" xlink:href="data:image/svg+xml;base64,',
|
93
|
+
Base64.encode(
|
94
|
+
bytes(
|
95
|
+
abi.encodePacked(
|
96
|
+
"<svg width='290' height='500' viewBox='0 0 290 500' xmlns='http://www.w3.org/2000/svg'><circle cx='",
|
97
|
+
params.x1,
|
98
|
+
"' cy='",
|
99
|
+
params.y1,
|
100
|
+
"' r='120px' fill='#",
|
101
|
+
params.color1,
|
102
|
+
"'/></svg>"
|
103
|
+
)
|
104
|
+
)
|
105
|
+
),
|
106
|
+
'"/><feImage result="p2" xlink:href="data:image/svg+xml;base64,',
|
107
|
+
Base64.encode(
|
108
|
+
bytes(
|
109
|
+
abi.encodePacked(
|
110
|
+
"<svg width='290' height='500' viewBox='0 0 290 500' xmlns='http://www.w3.org/2000/svg'><circle cx='",
|
111
|
+
params.x2,
|
112
|
+
"' cy='",
|
113
|
+
params.y2,
|
114
|
+
"' r='120px' fill='#",
|
115
|
+
params.color2,
|
116
|
+
"'/></svg>"
|
117
|
+
)
|
118
|
+
)
|
119
|
+
),
|
120
|
+
'" />',
|
121
|
+
'<feImage result="p3" xlink:href="data:image/svg+xml;base64,',
|
122
|
+
Base64.encode(
|
123
|
+
bytes(
|
124
|
+
abi.encodePacked(
|
125
|
+
"<svg width='290' height='500' viewBox='0 0 290 500' xmlns='http://www.w3.org/2000/svg'><circle cx='",
|
126
|
+
params.x3,
|
127
|
+
"' cy='",
|
128
|
+
params.y3,
|
129
|
+
"' r='100px' fill='#",
|
130
|
+
params.color3,
|
131
|
+
"'/></svg>"
|
132
|
+
)
|
133
|
+
)
|
134
|
+
),
|
135
|
+
'" /><feBlend mode="overlay" in="p0" in2="p1" /><feBlend mode="exclusion" in2="p2" /><feBlend mode="overlay" in2="p3" result="blendOut" /><feGaussianBlur ',
|
136
|
+
'in="blendOut" stdDeviation="42" /></filter> <clipPath id="corners"><rect width="290" height="500" rx="42" ry="42" /></clipPath>',
|
137
|
+
'<path id="text-path-a" d="M40 12 H250 A28 28 0 0 1 278 40 V460 A28 28 0 0 1 250 488 H40 A28 28 0 0 1 12 460 V40 A28 28 0 0 1 40 12 z" />',
|
138
|
+
'<path id="minimap" d="M234 444C234 457.949 242.21 463 253 463" />',
|
139
|
+
'<filter id="top-region-blur"><feGaussianBlur in="SourceGraphic" stdDeviation="24" /></filter>',
|
140
|
+
'<linearGradient id="grad-up" x1="1" x2="0" y1="1" y2="0"><stop offset="0.0" stop-color="white" stop-opacity="1" />',
|
141
|
+
'<stop offset=".9" stop-color="white" stop-opacity="0" /></linearGradient>',
|
142
|
+
'<linearGradient id="grad-down" x1="0" x2="1" y1="0" y2="1"><stop offset="0.0" stop-color="white" stop-opacity="1" /><stop offset="0.9" stop-color="white" stop-opacity="0" /></linearGradient>',
|
143
|
+
'<mask id="fade-up" maskContentUnits="objectBoundingBox"><rect width="1" height="1" fill="url(#grad-up)" /></mask>',
|
144
|
+
'<mask id="fade-down" maskContentUnits="objectBoundingBox"><rect width="1" height="1" fill="url(#grad-down)" /></mask>',
|
145
|
+
'<mask id="none" maskContentUnits="objectBoundingBox"><rect width="1" height="1" fill="white" /></mask>',
|
146
|
+
'<linearGradient id="grad-symbol"><stop offset="0.7" stop-color="white" stop-opacity="1" /><stop offset=".95" stop-color="white" stop-opacity="0" /></linearGradient>',
|
147
|
+
'<mask id="fade-symbol" maskContentUnits="userSpaceOnUse"><rect width="290px" height="200px" fill="url(#grad-symbol)" /></mask></defs>',
|
148
|
+
'<g clip-path="url(#corners)">',
|
149
|
+
'<rect fill="',
|
150
|
+
params.color0,
|
151
|
+
'" x="0px" y="0px" width="290px" height="500px" />',
|
152
|
+
'<rect style="filter: url(#f1)" x="0px" y="0px" width="290px" height="500px" />',
|
153
|
+
' <g style="filter:url(#top-region-blur); transform:scale(1.5); transform-origin:center top;">',
|
154
|
+
'<rect fill="none" x="0px" y="0px" width="290px" height="500px" />',
|
155
|
+
'<ellipse cx="50%" cy="0px" rx="180px" ry="120px" fill="#000" opacity="0.85" /></g>',
|
156
|
+
'<rect x="0" y="0" width="290" height="500" rx="42" ry="42" fill="rgba(0,0,0,0)" stroke="rgba(255,255,255,0.2)" /></g>'
|
157
|
+
)
|
158
|
+
);
|
159
|
+
}
|
160
|
+
|
161
|
+
function generateSVGBorderText(
|
162
|
+
string memory quoteToken,
|
163
|
+
string memory baseToken,
|
164
|
+
string memory quoteTokenSymbol,
|
165
|
+
string memory baseTokenSymbol
|
166
|
+
) private pure returns (string memory svg) {
|
167
|
+
svg = string(
|
168
|
+
abi.encodePacked(
|
169
|
+
'<text text-rendering="optimizeSpeed">',
|
170
|
+
'<textPath startOffset="-100%" fill="white" font-family="\'Courier New\', monospace" font-size="10px" xlink:href="#text-path-a">',
|
171
|
+
baseToken,
|
172
|
+
unicode' • ',
|
173
|
+
baseTokenSymbol,
|
174
|
+
' <animate additive="sum" attributeName="startOffset" from="0%" to="100%" begin="0s" dur="30s" repeatCount="indefinite" />',
|
175
|
+
'</textPath> <textPath startOffset="0%" fill="white" font-family="\'Courier New\', monospace" font-size="10px" xlink:href="#text-path-a">',
|
176
|
+
baseToken,
|
177
|
+
unicode' • ',
|
178
|
+
baseTokenSymbol,
|
179
|
+
' <animate additive="sum" attributeName="startOffset" from="0%" to="100%" begin="0s" dur="30s" repeatCount="indefinite" /> </textPath>',
|
180
|
+
'<textPath startOffset="50%" fill="white" font-family="\'Courier New\', monospace" font-size="10px" xlink:href="#text-path-a">',
|
181
|
+
quoteToken,
|
182
|
+
unicode' • ',
|
183
|
+
quoteTokenSymbol,
|
184
|
+
' <animate additive="sum" attributeName="startOffset" from="0%" to="100%" begin="0s" dur="30s"',
|
185
|
+
' repeatCount="indefinite" /></textPath><textPath startOffset="-50%" fill="white" font-family="\'Courier New\', monospace" font-size="10px" xlink:href="#text-path-a">',
|
186
|
+
quoteToken,
|
187
|
+
unicode' • ',
|
188
|
+
quoteTokenSymbol,
|
189
|
+
' <animate additive="sum" attributeName="startOffset" from="0%" to="100%" begin="0s" dur="30s" repeatCount="indefinite" /></textPath></text>'
|
190
|
+
)
|
191
|
+
);
|
192
|
+
}
|
193
|
+
|
194
|
+
function generateSVGCardMantle(
|
195
|
+
string memory quoteTokenSymbol,
|
196
|
+
string memory baseTokenSymbol,
|
197
|
+
string memory feeTier
|
198
|
+
) private pure returns (string memory svg) {
|
199
|
+
svg = string(
|
200
|
+
abi.encodePacked(
|
201
|
+
'<g mask="url(#fade-symbol)"><rect fill="none" x="0px" y="0px" width="290px" height="200px" /> <text y="70px" x="32px" fill="white" font-family="\'Courier New\', monospace" font-weight="200" font-size="36px">',
|
202
|
+
quoteTokenSymbol,
|
203
|
+
'/',
|
204
|
+
baseTokenSymbol,
|
205
|
+
'</text><text y="115px" x="32px" fill="white" font-family="\'Courier New\', monospace" font-weight="200" font-size="36px">',
|
206
|
+
feeTier,
|
207
|
+
'</text></g>',
|
208
|
+
'<rect x="16" y="16" width="258" height="468" rx="26" ry="26" fill="rgba(0,0,0,0)" stroke="rgba(255,255,255,0.2)" />'
|
209
|
+
)
|
210
|
+
);
|
211
|
+
}
|
212
|
+
|
213
|
+
function generageSvgCurve(
|
214
|
+
int24 tickLower,
|
215
|
+
int24 tickUpper,
|
216
|
+
int24 tickSpacing,
|
217
|
+
int8 overRange
|
218
|
+
) private pure returns (string memory svg) {
|
219
|
+
string memory fade = overRange == 1 ? '#fade-up' : overRange == -1 ? '#fade-down' : '#none';
|
220
|
+
string memory curve = getCurve(tickLower, tickUpper, tickSpacing);
|
221
|
+
svg = string(
|
222
|
+
abi.encodePacked(
|
223
|
+
'<g mask="url(',
|
224
|
+
fade,
|
225
|
+
')"',
|
226
|
+
' style="transform:translate(72px,189px)">'
|
227
|
+
'<rect x="-16px" y="-16px" width="180px" height="180px" fill="none" />'
|
228
|
+
'<path d="',
|
229
|
+
curve,
|
230
|
+
'" stroke="rgba(0,0,0,0.3)" stroke-width="32px" fill="none" stroke-linecap="round" />',
|
231
|
+
'</g><g mask="url(',
|
232
|
+
fade,
|
233
|
+
')"',
|
234
|
+
' style="transform:translate(72px,189px)">',
|
235
|
+
'<rect x="-16px" y="-16px" width="180px" height="180px" fill="none" />',
|
236
|
+
'<path d="',
|
237
|
+
curve,
|
238
|
+
'" stroke="rgba(255,255,255,1)" fill="none" stroke-linecap="round" /></g>',
|
239
|
+
generateSVGCurveCircle(overRange)
|
240
|
+
)
|
241
|
+
);
|
242
|
+
}
|
243
|
+
|
244
|
+
function getCurve(
|
245
|
+
int24 tickLower,
|
246
|
+
int24 tickUpper,
|
247
|
+
int24 tickSpacing
|
248
|
+
) internal pure returns (string memory curve) {
|
249
|
+
int24 tickRange = (tickUpper - tickLower) / tickSpacing;
|
250
|
+
if (tickRange <= 4) {
|
251
|
+
curve = curve1;
|
252
|
+
} else if (tickRange <= 8) {
|
253
|
+
curve = curve2;
|
254
|
+
} else if (tickRange <= 16) {
|
255
|
+
curve = curve3;
|
256
|
+
} else if (tickRange <= 32) {
|
257
|
+
curve = curve4;
|
258
|
+
} else if (tickRange <= 64) {
|
259
|
+
curve = curve5;
|
260
|
+
} else if (tickRange <= 128) {
|
261
|
+
curve = curve6;
|
262
|
+
} else if (tickRange <= 256) {
|
263
|
+
curve = curve7;
|
264
|
+
} else {
|
265
|
+
curve = curve8;
|
266
|
+
}
|
267
|
+
}
|
268
|
+
|
269
|
+
function generateSVGCurveCircle(int8 overRange) internal pure returns (string memory svg) {
|
270
|
+
string memory curvex1 = '73';
|
271
|
+
string memory curvey1 = '190';
|
272
|
+
string memory curvex2 = '217';
|
273
|
+
string memory curvey2 = '334';
|
274
|
+
if (overRange == 1 || overRange == -1) {
|
275
|
+
svg = string(
|
276
|
+
abi.encodePacked(
|
277
|
+
'<circle cx="',
|
278
|
+
overRange == -1 ? curvex1 : curvex2,
|
279
|
+
'px" cy="',
|
280
|
+
overRange == -1 ? curvey1 : curvey2,
|
281
|
+
'px" r="4px" fill="white" /><circle cx="',
|
282
|
+
overRange == -1 ? curvex1 : curvex2,
|
283
|
+
'px" cy="',
|
284
|
+
overRange == -1 ? curvey1 : curvey2,
|
285
|
+
'px" r="24px" fill="none" stroke="white" />'
|
286
|
+
)
|
287
|
+
);
|
288
|
+
} else {
|
289
|
+
svg = string(
|
290
|
+
abi.encodePacked(
|
291
|
+
'<circle cx="',
|
292
|
+
curvex1,
|
293
|
+
'px" cy="',
|
294
|
+
curvey1,
|
295
|
+
'px" r="4px" fill="white" />',
|
296
|
+
'<circle cx="',
|
297
|
+
curvex2,
|
298
|
+
'px" cy="',
|
299
|
+
curvey2,
|
300
|
+
'px" r="4px" fill="white" />'
|
301
|
+
)
|
302
|
+
);
|
303
|
+
}
|
304
|
+
}
|
305
|
+
|
306
|
+
function generateSVGPositionDataAndLocationCurve(
|
307
|
+
string memory tokenId,
|
308
|
+
int24 tickLower,
|
309
|
+
int24 tickUpper
|
310
|
+
) private pure returns (string memory svg) {
|
311
|
+
string memory tickLowerStr = tickToString(tickLower);
|
312
|
+
string memory tickUpperStr = tickToString(tickUpper);
|
313
|
+
uint256 str1length = bytes(tokenId).length + 4;
|
314
|
+
uint256 str2length = bytes(tickLowerStr).length + 10;
|
315
|
+
uint256 str3length = bytes(tickUpperStr).length + 10;
|
316
|
+
(string memory xCoord, string memory yCoord) = rangeLocation(tickLower, tickUpper);
|
317
|
+
svg = string(
|
318
|
+
abi.encodePacked(
|
319
|
+
' <g style="transform:translate(29px, 384px)">',
|
320
|
+
'<rect width="',
|
321
|
+
uint256(7 * (str1length + 4)).toString(),
|
322
|
+
'px" height="26px" rx="8px" ry="8px" fill="rgba(0,0,0,0.6)" />',
|
323
|
+
'<text x="12px" y="17px" font-family="\'Courier New\', monospace" font-size="12px" fill="white"><tspan fill="rgba(255,255,255,0.6)">ID: </tspan>',
|
324
|
+
tokenId,
|
325
|
+
'</text></g>',
|
326
|
+
' <g style="transform:translate(29px, 414px)">',
|
327
|
+
'<rect width="',
|
328
|
+
uint256(7 * (str2length + 4)).toString(),
|
329
|
+
'px" height="26px" rx="8px" ry="8px" fill="rgba(0,0,0,0.6)" />',
|
330
|
+
'<text x="12px" y="17px" font-family="\'Courier New\', monospace" font-size="12px" fill="white"><tspan fill="rgba(255,255,255,0.6)">Min Tick: </tspan>',
|
331
|
+
tickLowerStr,
|
332
|
+
'</text></g>',
|
333
|
+
' <g style="transform:translate(29px, 444px)">',
|
334
|
+
'<rect width="',
|
335
|
+
uint256(7 * (str3length + 4)).toString(),
|
336
|
+
'px" height="26px" rx="8px" ry="8px" fill="rgba(0,0,0,0.6)" />',
|
337
|
+
'<text x="12px" y="17px" font-family="\'Courier New\', monospace" font-size="12px" fill="white"><tspan fill="rgba(255,255,255,0.6)">Max Tick: </tspan>',
|
338
|
+
tickUpperStr,
|
339
|
+
'</text></g>'
|
340
|
+
'<g style="transform:translate(226px, 433px)">',
|
341
|
+
'<rect width="36px" height="36px" rx="8px" ry="8px" fill="none" stroke="rgba(255,255,255,0.2)" />',
|
342
|
+
'<path stroke-linecap="round" d="M8 9C8.00004 22.9494 16.2099 28 27 28" fill="none" stroke="white" />',
|
343
|
+
'<circle style="transform:translate3d(',
|
344
|
+
xCoord,
|
345
|
+
'px, ',
|
346
|
+
yCoord,
|
347
|
+
'px, 0px)" cx="0px" cy="0px" r="4px" fill="white"/></g>'
|
348
|
+
)
|
349
|
+
);
|
350
|
+
}
|
351
|
+
|
352
|
+
function tickToString(int24 tick) private pure returns (string memory) {
|
353
|
+
string memory sign = '';
|
354
|
+
if (tick < 0) {
|
355
|
+
tick = tick * -1;
|
356
|
+
sign = '-';
|
357
|
+
}
|
358
|
+
return string(abi.encodePacked(sign, uint256(tick).toString()));
|
359
|
+
}
|
360
|
+
|
361
|
+
function rangeLocation(int24 tickLower, int24 tickUpper) internal pure returns (string memory, string memory) {
|
362
|
+
int24 midPoint = (tickLower + tickUpper) / 2;
|
363
|
+
if (midPoint < -125_000) {
|
364
|
+
return ('8', '7');
|
365
|
+
} else if (midPoint < -75_000) {
|
366
|
+
return ('8', '10.5');
|
367
|
+
} else if (midPoint < -25_000) {
|
368
|
+
return ('8', '14.25');
|
369
|
+
} else if (midPoint < -5_000) {
|
370
|
+
return ('10', '18');
|
371
|
+
} else if (midPoint < 0) {
|
372
|
+
return ('11', '21');
|
373
|
+
} else if (midPoint < 5_000) {
|
374
|
+
return ('13', '23');
|
375
|
+
} else if (midPoint < 25_000) {
|
376
|
+
return ('15', '25');
|
377
|
+
} else if (midPoint < 75_000) {
|
378
|
+
return ('18', '26');
|
379
|
+
} else if (midPoint < 125_000) {
|
380
|
+
return ('21', '27');
|
381
|
+
} else {
|
382
|
+
return ('24', '27');
|
383
|
+
}
|
384
|
+
}
|
385
|
+
|
386
|
+
function generateSVGRareSparkle(uint256 tokenId, address poolAddress) private pure returns (string memory svg) {
|
387
|
+
if (isRare(tokenId, poolAddress)) {
|
388
|
+
svg = string(
|
389
|
+
abi.encodePacked(
|
390
|
+
'<g style="transform:translate(226px, 392px)"><rect width="36px" height="36px" rx="8px" ry="8px" fill="none" stroke="rgba(255,255,255,0.2)" />',
|
391
|
+
'<g><path style="transform:translate(6px,6px)" d="M12 0L12.6522 9.56587L18 1.6077L13.7819 10.2181L22.3923 6L14.4341 ',
|
392
|
+
'11.3478L24 12L14.4341 12.6522L22.3923 18L13.7819 13.7819L18 22.3923L12.6522 14.4341L12 24L11.3478 14.4341L6 22.39',
|
393
|
+
'23L10.2181 13.7819L1.6077 18L9.56587 12.6522L0 12L9.56587 11.3478L1.6077 6L10.2181 10.2181L6 1.6077L11.3478 9.56587L12 0Z" fill="white" />',
|
394
|
+
'<animateTransform attributeName="transform" type="rotate" from="0 18 18" to="360 18 18" dur="10s" repeatCount="indefinite"/></g></g>'
|
395
|
+
)
|
396
|
+
);
|
397
|
+
} else {
|
398
|
+
svg = '';
|
399
|
+
}
|
400
|
+
}
|
401
|
+
|
402
|
+
function isRare(uint256 tokenId, address poolAddress) internal pure returns (bool) {
|
403
|
+
bytes32 h = keccak256(abi.encodePacked(tokenId, poolAddress));
|
404
|
+
return uint256(h) < type(uint256).max / (1 + BitMath.mostSignificantBit(tokenId) * 2);
|
405
|
+
}
|
406
|
+
}
|
@@ -0,0 +1,161 @@
|
|
1
|
+
// SPDX-License-Identifier: GPL-2.0-or-later
|
2
|
+
pragma solidity >=0.5.0 <0.8.0;
|
3
|
+
|
4
|
+
import '@huuduynvc/v3-core/contracts/libraries/FullMath.sol';
|
5
|
+
import '@huuduynvc/v3-core/contracts/libraries/TickMath.sol';
|
6
|
+
import '@huuduynvc/v3-core/contracts/interfaces/IUniswapV3Pool.sol';
|
7
|
+
|
8
|
+
/// @title Oracle library
|
9
|
+
/// @notice Provides functions to integrate with V3 pool oracle
|
10
|
+
library OracleLibrary {
|
11
|
+
/// @notice Calculates time-weighted means of tick and liquidity for a given Uniswap V3 pool
|
12
|
+
/// @param pool Address of the pool that we want to observe
|
13
|
+
/// @param secondsAgo Number of seconds in the past from which to calculate the time-weighted means
|
14
|
+
/// @return arithmeticMeanTick The arithmetic mean tick from (block.timestamp - secondsAgo) to block.timestamp
|
15
|
+
/// @return harmonicMeanLiquidity The harmonic mean liquidity from (block.timestamp - secondsAgo) to block.timestamp
|
16
|
+
function consult(address pool, uint32 secondsAgo)
|
17
|
+
internal
|
18
|
+
view
|
19
|
+
returns (int24 arithmeticMeanTick, uint128 harmonicMeanLiquidity)
|
20
|
+
{
|
21
|
+
require(secondsAgo != 0, 'BP');
|
22
|
+
|
23
|
+
uint32[] memory secondsAgos = new uint32[](2);
|
24
|
+
secondsAgos[0] = secondsAgo;
|
25
|
+
secondsAgos[1] = 0;
|
26
|
+
|
27
|
+
(int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s) =
|
28
|
+
IUniswapV3Pool(pool).observe(secondsAgos);
|
29
|
+
|
30
|
+
int56 tickCumulativesDelta = tickCumulatives[1] - tickCumulatives[0];
|
31
|
+
uint160 secondsPerLiquidityCumulativesDelta =
|
32
|
+
secondsPerLiquidityCumulativeX128s[1] - secondsPerLiquidityCumulativeX128s[0];
|
33
|
+
|
34
|
+
arithmeticMeanTick = int24(tickCumulativesDelta / secondsAgo);
|
35
|
+
// Always round to negative infinity
|
36
|
+
if (tickCumulativesDelta < 0 && (tickCumulativesDelta % secondsAgo != 0)) arithmeticMeanTick--;
|
37
|
+
|
38
|
+
// We are multiplying here instead of shifting to ensure that harmonicMeanLiquidity doesn't overflow uint128
|
39
|
+
uint192 secondsAgoX160 = uint192(secondsAgo) * type(uint160).max;
|
40
|
+
harmonicMeanLiquidity = uint128(secondsAgoX160 / (uint192(secondsPerLiquidityCumulativesDelta) << 32));
|
41
|
+
}
|
42
|
+
|
43
|
+
/// @notice Given a tick and a token amount, calculates the amount of token received in exchange
|
44
|
+
/// @param tick Tick value used to calculate the quote
|
45
|
+
/// @param baseAmount Amount of token to be converted
|
46
|
+
/// @param baseToken Address of an ERC20 token contract used as the baseAmount denomination
|
47
|
+
/// @param quoteToken Address of an ERC20 token contract used as the quoteAmount denomination
|
48
|
+
/// @return quoteAmount Amount of quoteToken received for baseAmount of baseToken
|
49
|
+
function getQuoteAtTick(
|
50
|
+
int24 tick,
|
51
|
+
uint128 baseAmount,
|
52
|
+
address baseToken,
|
53
|
+
address quoteToken
|
54
|
+
) internal pure returns (uint256 quoteAmount) {
|
55
|
+
uint160 sqrtRatioX96 = TickMath.getSqrtRatioAtTick(tick);
|
56
|
+
|
57
|
+
// Calculate quoteAmount with better precision if it doesn't overflow when multiplied by itself
|
58
|
+
if (sqrtRatioX96 <= type(uint128).max) {
|
59
|
+
uint256 ratioX192 = uint256(sqrtRatioX96) * sqrtRatioX96;
|
60
|
+
quoteAmount = baseToken < quoteToken
|
61
|
+
? FullMath.mulDiv(ratioX192, baseAmount, 1 << 192)
|
62
|
+
: FullMath.mulDiv(1 << 192, baseAmount, ratioX192);
|
63
|
+
} else {
|
64
|
+
uint256 ratioX128 = FullMath.mulDiv(sqrtRatioX96, sqrtRatioX96, 1 << 64);
|
65
|
+
quoteAmount = baseToken < quoteToken
|
66
|
+
? FullMath.mulDiv(ratioX128, baseAmount, 1 << 128)
|
67
|
+
: FullMath.mulDiv(1 << 128, baseAmount, ratioX128);
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
/// @notice Given a pool, it returns the number of seconds ago of the oldest stored observation
|
72
|
+
/// @param pool Address of Uniswap V3 pool that we want to observe
|
73
|
+
/// @return secondsAgo The number of seconds ago of the oldest observation stored for the pool
|
74
|
+
function getOldestObservationSecondsAgo(address pool) internal view returns (uint32 secondsAgo) {
|
75
|
+
(, , uint16 observationIndex, uint16 observationCardinality, , , ) = IUniswapV3Pool(pool).slot0();
|
76
|
+
require(observationCardinality > 0, 'NI');
|
77
|
+
|
78
|
+
(uint32 observationTimestamp, , , bool initialized) =
|
79
|
+
IUniswapV3Pool(pool).observations((observationIndex + 1) % observationCardinality);
|
80
|
+
|
81
|
+
// The next index might not be initialized if the cardinality is in the process of increasing
|
82
|
+
// In this case the oldest observation is always in index 0
|
83
|
+
if (!initialized) {
|
84
|
+
(observationTimestamp, , , ) = IUniswapV3Pool(pool).observations(0);
|
85
|
+
}
|
86
|
+
|
87
|
+
secondsAgo = uint32(block.timestamp) - observationTimestamp;
|
88
|
+
}
|
89
|
+
|
90
|
+
/// @notice Given a pool, it returns the tick value as of the start of the current block
|
91
|
+
/// @param pool Address of Uniswap V3 pool
|
92
|
+
/// @return The tick that the pool was in at the start of the current block
|
93
|
+
function getBlockStartingTickAndLiquidity(address pool) internal view returns (int24, uint128) {
|
94
|
+
(, int24 tick, uint16 observationIndex, uint16 observationCardinality, , , ) = IUniswapV3Pool(pool).slot0();
|
95
|
+
|
96
|
+
// 2 observations are needed to reliably calculate the block starting tick
|
97
|
+
require(observationCardinality > 1, 'NEO');
|
98
|
+
|
99
|
+
// If the latest observation occurred in the past, then no tick-changing trades have happened in this block
|
100
|
+
// therefore the tick in `slot0` is the same as at the beginning of the current block.
|
101
|
+
// We don't need to check if this observation is initialized - it is guaranteed to be.
|
102
|
+
(uint32 observationTimestamp, int56 tickCumulative, uint160 secondsPerLiquidityCumulativeX128, ) =
|
103
|
+
IUniswapV3Pool(pool).observations(observationIndex);
|
104
|
+
if (observationTimestamp != uint32(block.timestamp)) {
|
105
|
+
return (tick, IUniswapV3Pool(pool).liquidity());
|
106
|
+
}
|
107
|
+
|
108
|
+
uint256 prevIndex = (uint256(observationIndex) + observationCardinality - 1) % observationCardinality;
|
109
|
+
(
|
110
|
+
uint32 prevObservationTimestamp,
|
111
|
+
int56 prevTickCumulative,
|
112
|
+
uint160 prevSecondsPerLiquidityCumulativeX128,
|
113
|
+
bool prevInitialized
|
114
|
+
) = IUniswapV3Pool(pool).observations(prevIndex);
|
115
|
+
|
116
|
+
require(prevInitialized, 'ONI');
|
117
|
+
|
118
|
+
uint32 delta = observationTimestamp - prevObservationTimestamp;
|
119
|
+
tick = int24((tickCumulative - prevTickCumulative) / delta);
|
120
|
+
uint128 liquidity =
|
121
|
+
uint128(
|
122
|
+
(uint192(delta) * type(uint160).max) /
|
123
|
+
(uint192(secondsPerLiquidityCumulativeX128 - prevSecondsPerLiquidityCumulativeX128) << 32)
|
124
|
+
);
|
125
|
+
return (tick, liquidity);
|
126
|
+
}
|
127
|
+
|
128
|
+
/// @notice Information for calculating a weighted arithmetic mean tick
|
129
|
+
struct WeightedTickData {
|
130
|
+
int24 tick;
|
131
|
+
uint128 weight;
|
132
|
+
}
|
133
|
+
|
134
|
+
/// @notice Given an array of ticks and weights, calculates the weighted arithmetic mean tick
|
135
|
+
/// @param weightedTickData An array of ticks and weights
|
136
|
+
/// @return weightedArithmeticMeanTick The weighted arithmetic mean tick
|
137
|
+
/// @dev Each entry of `weightedTickData` should represents ticks from pools with the same underlying pool tokens. If they do not,
|
138
|
+
/// extreme care must be taken to ensure that ticks are comparable (including decimal differences).
|
139
|
+
/// @dev Note that the weighted arithmetic mean tick corresponds to the weighted geometric mean price.
|
140
|
+
function getWeightedArithmeticMeanTick(WeightedTickData[] memory weightedTickData)
|
141
|
+
internal
|
142
|
+
pure
|
143
|
+
returns (int24 weightedArithmeticMeanTick)
|
144
|
+
{
|
145
|
+
// Accumulates the sum of products between each tick and its weight
|
146
|
+
int256 numerator;
|
147
|
+
|
148
|
+
// Accumulates the sum of the weights
|
149
|
+
uint256 denominator;
|
150
|
+
|
151
|
+
// Products fit in 152 bits, so it would take an array of length ~2**104 to overflow this logic
|
152
|
+
for (uint256 i; i < weightedTickData.length; i++) {
|
153
|
+
numerator += weightedTickData[i].tick * int256(weightedTickData[i].weight);
|
154
|
+
denominator += weightedTickData[i].weight;
|
155
|
+
}
|
156
|
+
|
157
|
+
weightedArithmeticMeanTick = int24(numerator / int256(denominator));
|
158
|
+
// Always round to negative infinity
|
159
|
+
if (numerator < 0 && (numerator % int256(denominator) != 0)) weightedArithmeticMeanTick--;
|
160
|
+
}
|
161
|
+
}
|
@@ -0,0 +1,69 @@
|
|
1
|
+
// SPDX-License-Identifier: GPL-2.0-or-later
|
2
|
+
pragma solidity >=0.6.0;
|
3
|
+
|
4
|
+
import './BytesLib.sol';
|
5
|
+
|
6
|
+
/// @title Functions for manipulating path data for multihop swaps
|
7
|
+
library Path {
|
8
|
+
using BytesLib for bytes;
|
9
|
+
|
10
|
+
/// @dev The length of the bytes encoded address
|
11
|
+
uint256 private constant ADDR_SIZE = 20;
|
12
|
+
/// @dev The length of the bytes encoded fee
|
13
|
+
uint256 private constant FEE_SIZE = 3;
|
14
|
+
|
15
|
+
/// @dev The offset of a single token address and pool fee
|
16
|
+
uint256 private constant NEXT_OFFSET = ADDR_SIZE + FEE_SIZE;
|
17
|
+
/// @dev The offset of an encoded pool key
|
18
|
+
uint256 private constant POP_OFFSET = NEXT_OFFSET + ADDR_SIZE;
|
19
|
+
/// @dev The minimum length of an encoding that contains 2 or more pools
|
20
|
+
uint256 private constant MULTIPLE_POOLS_MIN_LENGTH = POP_OFFSET + NEXT_OFFSET;
|
21
|
+
|
22
|
+
/// @notice Returns true iff the path contains two or more pools
|
23
|
+
/// @param path The encoded swap path
|
24
|
+
/// @return True if path contains two or more pools, otherwise false
|
25
|
+
function hasMultiplePools(bytes memory path) internal pure returns (bool) {
|
26
|
+
return path.length >= MULTIPLE_POOLS_MIN_LENGTH;
|
27
|
+
}
|
28
|
+
|
29
|
+
/// @notice Returns the number of pools in the path
|
30
|
+
/// @param path The encoded swap path
|
31
|
+
/// @return The number of pools in the path
|
32
|
+
function numPools(bytes memory path) internal pure returns (uint256) {
|
33
|
+
// Ignore the first token address. From then on every fee and token offset indicates a pool.
|
34
|
+
return ((path.length - ADDR_SIZE) / NEXT_OFFSET);
|
35
|
+
}
|
36
|
+
|
37
|
+
/// @notice Decodes the first pool in path
|
38
|
+
/// @param path The bytes encoded swap path
|
39
|
+
/// @return tokenA The first token of the given pool
|
40
|
+
/// @return tokenB The second token of the given pool
|
41
|
+
/// @return fee The fee level of the pool
|
42
|
+
function decodeFirstPool(bytes memory path)
|
43
|
+
internal
|
44
|
+
pure
|
45
|
+
returns (
|
46
|
+
address tokenA,
|
47
|
+
address tokenB,
|
48
|
+
uint24 fee
|
49
|
+
)
|
50
|
+
{
|
51
|
+
tokenA = path.toAddress(0);
|
52
|
+
fee = path.toUint24(ADDR_SIZE);
|
53
|
+
tokenB = path.toAddress(NEXT_OFFSET);
|
54
|
+
}
|
55
|
+
|
56
|
+
/// @notice Gets the segment corresponding to the first pool in the path
|
57
|
+
/// @param path The bytes encoded swap path
|
58
|
+
/// @return The segment containing all data necessary to target the first pool in the path
|
59
|
+
function getFirstPool(bytes memory path) internal pure returns (bytes memory) {
|
60
|
+
return path.slice(0, POP_OFFSET);
|
61
|
+
}
|
62
|
+
|
63
|
+
/// @notice Skips a token + fee element from the buffer and returns the remainder
|
64
|
+
/// @param path The swap path
|
65
|
+
/// @return The remaining token + fee elements in the path
|
66
|
+
function skipToken(bytes memory path) internal pure returns (bytes memory) {
|
67
|
+
return path.slice(NEXT_OFFSET, path.length - NEXT_OFFSET);
|
68
|
+
}
|
69
|
+
}
|