@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.
Files changed (88) hide show
  1. package/LICENSE +339 -0
  2. package/README.md +52 -0
  3. package/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json +1230 -0
  4. package/artifacts/contracts/NonfungibleTokenPositionDescriptor.sol/NonfungibleTokenPositionDescriptor.json +161 -0
  5. package/artifacts/contracts/SwapRouter.sol/SwapRouter.json +574 -0
  6. package/artifacts/contracts/V3Migrator.sol/V3Migrator.json +360 -0
  7. package/artifacts/contracts/examples/PairFlash.sol/PairFlash.json +196 -0
  8. package/artifacts/contracts/interfaces/IERC20Metadata.sol/IERC20Metadata.json +233 -0
  9. package/artifacts/contracts/interfaces/IERC721Permit.sol/IERC721Permit.json +360 -0
  10. package/artifacts/contracts/interfaces/IMulticall.sol/IMulticall.json +30 -0
  11. package/artifacts/contracts/interfaces/INonfungiblePositionManager.sol/INonfungiblePositionManager.json +998 -0
  12. package/artifacts/contracts/interfaces/INonfungibleTokenPositionDescriptor.sol/INonfungibleTokenPositionDescriptor.json +35 -0
  13. package/artifacts/contracts/interfaces/IPeripheryImmutableState.sol/IPeripheryImmutableState.json +37 -0
  14. package/artifacts/contracts/interfaces/IPeripheryPayments.sol/IPeripheryPayments.json +59 -0
  15. package/artifacts/contracts/interfaces/IPeripheryPaymentsWithFee.sol/IPeripheryPaymentsWithFee.json +120 -0
  16. package/artifacts/contracts/interfaces/IPoolInitializer.sol/IPoolInitializer.json +45 -0
  17. package/artifacts/contracts/interfaces/IQuoter.sol/IQuoter.json +137 -0
  18. package/artifacts/contracts/interfaces/IQuoterV2.sol/IQuoterV2.json +211 -0
  19. package/artifacts/contracts/interfaces/ISelfPermit.sol/ISelfPermit.json +163 -0
  20. package/artifacts/contracts/interfaces/ISwapRouter.sol/ISwapRouter.json +248 -0
  21. package/artifacts/contracts/interfaces/ITickLens.sol/ITickLens.json +52 -0
  22. package/artifacts/contracts/interfaces/IV3Migrator.sol/IV3Migrator.json +296 -0
  23. package/artifacts/contracts/interfaces/external/IERC1271.sol/IERC1271.json +35 -0
  24. package/artifacts/contracts/interfaces/external/IERC20PermitAllowed.sol/IERC20PermitAllowed.json +59 -0
  25. package/artifacts/contracts/interfaces/external/IWETH9.sol/IWETH9.json +214 -0
  26. package/artifacts/contracts/lens/Quoter.sol/Quoter.json +202 -0
  27. package/artifacts/contracts/lens/QuoterV2.sol/QuoterV2.json +276 -0
  28. package/artifacts/contracts/lens/TickLens.sol/TickLens.json +52 -0
  29. package/artifacts/contracts/lens/UniswapInterfaceMulticall.sol/UniswapInterfaceMulticall.json +101 -0
  30. package/artifacts/contracts/libraries/BytesLib.sol/BytesLib.json +10 -0
  31. package/artifacts/contracts/libraries/CallbackValidation.sol/CallbackValidation.json +10 -0
  32. package/artifacts/contracts/libraries/ChainId.sol/ChainId.json +10 -0
  33. package/artifacts/contracts/libraries/HexStrings.sol/HexStrings.json +10 -0
  34. package/artifacts/contracts/libraries/LiquidityAmounts.sol/LiquidityAmounts.json +10 -0
  35. package/artifacts/contracts/libraries/NFTDescriptor.sol/NFTDescriptor.json +102 -0
  36. package/artifacts/contracts/libraries/NFTSVG.sol/NFTSVG.json +10 -0
  37. package/artifacts/contracts/libraries/OracleLibrary.sol/OracleLibrary.json +10 -0
  38. package/artifacts/contracts/libraries/Path.sol/Path.json +10 -0
  39. package/artifacts/contracts/libraries/PoolAddress.sol/PoolAddress.json +10 -0
  40. package/artifacts/contracts/libraries/PoolTicksCounter.sol/PoolTicksCounter.json +10 -0
  41. package/artifacts/contracts/libraries/PositionKey.sol/PositionKey.json +10 -0
  42. package/artifacts/contracts/libraries/PositionValue.sol/PositionValue.json +10 -0
  43. package/artifacts/contracts/libraries/TokenRatioSortOrder.sol/TokenRatioSortOrder.json +10 -0
  44. package/artifacts/contracts/libraries/TransferHelper.sol/TransferHelper.json +10 -0
  45. package/contracts/base/BlockTimestamp.sol +12 -0
  46. package/contracts/base/ERC721Permit.sol +86 -0
  47. package/contracts/base/LiquidityManagement.sol +90 -0
  48. package/contracts/base/Multicall.sol +28 -0
  49. package/contracts/base/PeripheryImmutableState.sol +18 -0
  50. package/contracts/base/PeripheryPayments.sol +70 -0
  51. package/contracts/base/PeripheryPaymentsWithFee.sol +55 -0
  52. package/contracts/base/PeripheryValidation.sol +11 -0
  53. package/contracts/base/PoolInitializer.sol +32 -0
  54. package/contracts/base/SelfPermit.sol +63 -0
  55. package/contracts/interfaces/IERC20Metadata.sol +18 -0
  56. package/contracts/interfaces/IERC721Permit.sol +32 -0
  57. package/contracts/interfaces/IMulticall.sol +13 -0
  58. package/contracts/interfaces/INonfungiblePositionManager.sol +180 -0
  59. package/contracts/interfaces/INonfungibleTokenPositionDescriptor.sol +17 -0
  60. package/contracts/interfaces/IPeripheryImmutableState.sol +12 -0
  61. package/contracts/interfaces/IPeripheryPayments.sol +28 -0
  62. package/contracts/interfaces/IPeripheryPaymentsWithFee.sol +29 -0
  63. package/contracts/interfaces/IPoolInitializer.sol +22 -0
  64. package/contracts/interfaces/IQuoter.sol +51 -0
  65. package/contracts/interfaces/IQuoterV2.sol +98 -0
  66. package/contracts/interfaces/ISelfPermit.sol +76 -0
  67. package/contracts/interfaces/ISwapRouter.sol +67 -0
  68. package/contracts/interfaces/ITickLens.sol +25 -0
  69. package/contracts/interfaces/IV3Migrator.sol +34 -0
  70. package/contracts/interfaces/external/IERC1271.sol +16 -0
  71. package/contracts/interfaces/external/IERC20PermitAllowed.sol +27 -0
  72. package/contracts/interfaces/external/IWETH9.sol +13 -0
  73. package/contracts/libraries/BytesLib.sol +101 -0
  74. package/contracts/libraries/CallbackValidation.sol +36 -0
  75. package/contracts/libraries/ChainId.sol +13 -0
  76. package/contracts/libraries/HexStrings.sol +29 -0
  77. package/contracts/libraries/LiquidityAmounts.sol +137 -0
  78. package/contracts/libraries/NFTDescriptor.sol +477 -0
  79. package/contracts/libraries/NFTSVG.sol +406 -0
  80. package/contracts/libraries/OracleLibrary.sol +161 -0
  81. package/contracts/libraries/Path.sol +69 -0
  82. package/contracts/libraries/PoolAddress.sol +48 -0
  83. package/contracts/libraries/PoolTicksCounter.sol +97 -0
  84. package/contracts/libraries/PositionKey.sol +13 -0
  85. package/contracts/libraries/PositionValue.sol +167 -0
  86. package/contracts/libraries/TokenRatioSortOrder.sol +12 -0
  87. package/contracts/libraries/TransferHelper.sol +60 -0
  88. package/package.json +68 -0
@@ -0,0 +1,477 @@
1
+ // SPDX-License-Identifier: UNLICENSED
2
+ pragma solidity >=0.7.0;
3
+ pragma abicoder v2;
4
+
5
+ import '@huuduynvc/v3-core/contracts/interfaces/IUniswapV3Pool.sol';
6
+ import '@huuduynvc/v3-core/contracts/libraries/TickMath.sol';
7
+ import '@huuduynvc/v3-core/contracts/libraries/BitMath.sol';
8
+ import '@huuduynvc/v3-core/contracts/libraries/FullMath.sol';
9
+ import '@openzeppelin/contracts/utils/Strings.sol';
10
+ import '@openzeppelin/contracts/math/SafeMath.sol';
11
+ import '@openzeppelin/contracts/math/SignedSafeMath.sol';
12
+ import 'base64-sol/base64.sol';
13
+ import './HexStrings.sol';
14
+ import './NFTSVG.sol';
15
+
16
+ library NFTDescriptor {
17
+ using TickMath for int24;
18
+ using Strings for uint256;
19
+ using SafeMath for uint256;
20
+ using SafeMath for uint160;
21
+ using SafeMath for uint8;
22
+ using SignedSafeMath for int256;
23
+ using HexStrings for uint256;
24
+
25
+ uint256 constant sqrt10X128 = 1076067327063303206878105757264492625226;
26
+
27
+ struct ConstructTokenURIParams {
28
+ uint256 tokenId;
29
+ address quoteTokenAddress;
30
+ address baseTokenAddress;
31
+ string quoteTokenSymbol;
32
+ string baseTokenSymbol;
33
+ uint8 quoteTokenDecimals;
34
+ uint8 baseTokenDecimals;
35
+ bool flipRatio;
36
+ int24 tickLower;
37
+ int24 tickUpper;
38
+ int24 tickCurrent;
39
+ int24 tickSpacing;
40
+ uint24 fee;
41
+ address poolAddress;
42
+ }
43
+
44
+ function constructTokenURI(ConstructTokenURIParams memory params) public pure returns (string memory) {
45
+ string memory name = generateName(params, feeToPercentString(params.fee));
46
+ string memory descriptionPartOne =
47
+ generateDescriptionPartOne(
48
+ escapeQuotes(params.quoteTokenSymbol),
49
+ escapeQuotes(params.baseTokenSymbol),
50
+ addressToString(params.poolAddress)
51
+ );
52
+ string memory descriptionPartTwo =
53
+ generateDescriptionPartTwo(
54
+ params.tokenId.toString(),
55
+ escapeQuotes(params.baseTokenSymbol),
56
+ addressToString(params.quoteTokenAddress),
57
+ addressToString(params.baseTokenAddress),
58
+ feeToPercentString(params.fee)
59
+ );
60
+ string memory image = Base64.encode(bytes(generateSVGImage(params)));
61
+
62
+ return
63
+ string(
64
+ abi.encodePacked(
65
+ 'data:application/json;base64,',
66
+ Base64.encode(
67
+ bytes(
68
+ abi.encodePacked(
69
+ '{"name":"',
70
+ name,
71
+ '", "description":"',
72
+ descriptionPartOne,
73
+ descriptionPartTwo,
74
+ '", "image": "',
75
+ 'data:image/svg+xml;base64,',
76
+ image,
77
+ '"}'
78
+ )
79
+ )
80
+ )
81
+ )
82
+ );
83
+ }
84
+
85
+ function escapeQuotes(string memory symbol) internal pure returns (string memory) {
86
+ bytes memory symbolBytes = bytes(symbol);
87
+ uint8 quotesCount = 0;
88
+ for (uint8 i = 0; i < symbolBytes.length; i++) {
89
+ if (symbolBytes[i] == '"') {
90
+ quotesCount++;
91
+ }
92
+ }
93
+ if (quotesCount > 0) {
94
+ bytes memory escapedBytes = new bytes(symbolBytes.length + (quotesCount));
95
+ uint256 index;
96
+ for (uint8 i = 0; i < symbolBytes.length; i++) {
97
+ if (symbolBytes[i] == '"') {
98
+ escapedBytes[index++] = '\\';
99
+ }
100
+ escapedBytes[index++] = symbolBytes[i];
101
+ }
102
+ return string(escapedBytes);
103
+ }
104
+ return symbol;
105
+ }
106
+
107
+ function generateDescriptionPartOne(
108
+ string memory quoteTokenSymbol,
109
+ string memory baseTokenSymbol,
110
+ string memory poolAddress
111
+ ) private pure returns (string memory) {
112
+ return
113
+ string(
114
+ abi.encodePacked(
115
+ 'This NFT represents a liquidity position in a Uniswap V3 ',
116
+ quoteTokenSymbol,
117
+ '-',
118
+ baseTokenSymbol,
119
+ ' pool. ',
120
+ 'The owner of this NFT can modify or redeem the position.\\n',
121
+ '\\nPool Address: ',
122
+ poolAddress,
123
+ '\\n',
124
+ quoteTokenSymbol
125
+ )
126
+ );
127
+ }
128
+
129
+ function generateDescriptionPartTwo(
130
+ string memory tokenId,
131
+ string memory baseTokenSymbol,
132
+ string memory quoteTokenAddress,
133
+ string memory baseTokenAddress,
134
+ string memory feeTier
135
+ ) private pure returns (string memory) {
136
+ return
137
+ string(
138
+ abi.encodePacked(
139
+ ' Address: ',
140
+ quoteTokenAddress,
141
+ '\\n',
142
+ baseTokenSymbol,
143
+ ' Address: ',
144
+ baseTokenAddress,
145
+ '\\nFee Tier: ',
146
+ feeTier,
147
+ '\\nToken ID: ',
148
+ tokenId,
149
+ '\\n\\n',
150
+ unicode'⚠️ DISCLAIMER: Due diligence is imperative when assessing this NFT. Make sure token addresses match the expected tokens, as token symbols may be imitated.'
151
+ )
152
+ );
153
+ }
154
+
155
+ function generateName(ConstructTokenURIParams memory params, string memory feeTier)
156
+ private
157
+ pure
158
+ returns (string memory)
159
+ {
160
+ return
161
+ string(
162
+ abi.encodePacked(
163
+ 'Uniswap - ',
164
+ feeTier,
165
+ ' - ',
166
+ escapeQuotes(params.quoteTokenSymbol),
167
+ '/',
168
+ escapeQuotes(params.baseTokenSymbol),
169
+ ' - ',
170
+ tickToDecimalString(
171
+ !params.flipRatio ? params.tickLower : params.tickUpper,
172
+ params.tickSpacing,
173
+ params.baseTokenDecimals,
174
+ params.quoteTokenDecimals,
175
+ params.flipRatio
176
+ ),
177
+ '<>',
178
+ tickToDecimalString(
179
+ !params.flipRatio ? params.tickUpper : params.tickLower,
180
+ params.tickSpacing,
181
+ params.baseTokenDecimals,
182
+ params.quoteTokenDecimals,
183
+ params.flipRatio
184
+ )
185
+ )
186
+ );
187
+ }
188
+
189
+ struct DecimalStringParams {
190
+ // significant figures of decimal
191
+ uint256 sigfigs;
192
+ // length of decimal string
193
+ uint8 bufferLength;
194
+ // ending index for significant figures (funtion works backwards when copying sigfigs)
195
+ uint8 sigfigIndex;
196
+ // index of decimal place (0 if no decimal)
197
+ uint8 decimalIndex;
198
+ // start index for trailing/leading 0's for very small/large numbers
199
+ uint8 zerosStartIndex;
200
+ // end index for trailing/leading 0's for very small/large numbers
201
+ uint8 zerosEndIndex;
202
+ // true if decimal number is less than one
203
+ bool isLessThanOne;
204
+ // true if string should include "%"
205
+ bool isPercent;
206
+ }
207
+
208
+ function generateDecimalString(DecimalStringParams memory params) private pure returns (string memory) {
209
+ bytes memory buffer = new bytes(params.bufferLength);
210
+ if (params.isPercent) {
211
+ buffer[buffer.length - 1] = '%';
212
+ }
213
+ if (params.isLessThanOne) {
214
+ buffer[0] = '0';
215
+ buffer[1] = '.';
216
+ }
217
+
218
+ // add leading/trailing 0's
219
+ for (uint256 zerosCursor = params.zerosStartIndex; zerosCursor < params.zerosEndIndex.add(1); zerosCursor++) {
220
+ buffer[zerosCursor] = bytes1(uint8(48));
221
+ }
222
+ // add sigfigs
223
+ while (params.sigfigs > 0) {
224
+ if (params.decimalIndex > 0 && params.sigfigIndex == params.decimalIndex) {
225
+ buffer[params.sigfigIndex--] = '.';
226
+ }
227
+ buffer[params.sigfigIndex--] = bytes1(uint8(uint256(48).add(params.sigfigs % 10)));
228
+ params.sigfigs /= 10;
229
+ }
230
+ return string(buffer);
231
+ }
232
+
233
+ function tickToDecimalString(
234
+ int24 tick,
235
+ int24 tickSpacing,
236
+ uint8 baseTokenDecimals,
237
+ uint8 quoteTokenDecimals,
238
+ bool flipRatio
239
+ ) internal pure returns (string memory) {
240
+ if (tick == (TickMath.MIN_TICK / tickSpacing) * tickSpacing) {
241
+ return !flipRatio ? 'MIN' : 'MAX';
242
+ } else if (tick == (TickMath.MAX_TICK / tickSpacing) * tickSpacing) {
243
+ return !flipRatio ? 'MAX' : 'MIN';
244
+ } else {
245
+ uint160 sqrtRatioX96 = TickMath.getSqrtRatioAtTick(tick);
246
+ if (flipRatio) {
247
+ sqrtRatioX96 = uint160(uint256(1 << 192).div(sqrtRatioX96));
248
+ }
249
+ return fixedPointToDecimalString(sqrtRatioX96, baseTokenDecimals, quoteTokenDecimals);
250
+ }
251
+ }
252
+
253
+ function sigfigsRounded(uint256 value, uint8 digits) private pure returns (uint256, bool) {
254
+ bool extraDigit;
255
+ if (digits > 5) {
256
+ value = value.div((10**(digits - 5)));
257
+ }
258
+ bool roundUp = value % 10 > 4;
259
+ value = value.div(10);
260
+ if (roundUp) {
261
+ value = value + 1;
262
+ }
263
+ // 99999 -> 100000 gives an extra sigfig
264
+ if (value == 100000) {
265
+ value /= 10;
266
+ extraDigit = true;
267
+ }
268
+ return (value, extraDigit);
269
+ }
270
+
271
+ function adjustForDecimalPrecision(
272
+ uint160 sqrtRatioX96,
273
+ uint8 baseTokenDecimals,
274
+ uint8 quoteTokenDecimals
275
+ ) private pure returns (uint256 adjustedSqrtRatioX96) {
276
+ uint256 difference = abs(int256(baseTokenDecimals).sub(int256(quoteTokenDecimals)));
277
+ if (difference > 0 && difference <= 18) {
278
+ if (baseTokenDecimals > quoteTokenDecimals) {
279
+ adjustedSqrtRatioX96 = sqrtRatioX96.mul(10**(difference.div(2)));
280
+ if (difference % 2 == 1) {
281
+ adjustedSqrtRatioX96 = FullMath.mulDiv(adjustedSqrtRatioX96, sqrt10X128, 1 << 128);
282
+ }
283
+ } else {
284
+ adjustedSqrtRatioX96 = sqrtRatioX96.div(10**(difference.div(2)));
285
+ if (difference % 2 == 1) {
286
+ adjustedSqrtRatioX96 = FullMath.mulDiv(adjustedSqrtRatioX96, 1 << 128, sqrt10X128);
287
+ }
288
+ }
289
+ } else {
290
+ adjustedSqrtRatioX96 = uint256(sqrtRatioX96);
291
+ }
292
+ }
293
+
294
+ function abs(int256 x) private pure returns (uint256) {
295
+ return uint256(x >= 0 ? x : -x);
296
+ }
297
+
298
+ // @notice Returns string that includes first 5 significant figures of a decimal number
299
+ // @param sqrtRatioX96 a sqrt price
300
+ function fixedPointToDecimalString(
301
+ uint160 sqrtRatioX96,
302
+ uint8 baseTokenDecimals,
303
+ uint8 quoteTokenDecimals
304
+ ) internal pure returns (string memory) {
305
+ uint256 adjustedSqrtRatioX96 = adjustForDecimalPrecision(sqrtRatioX96, baseTokenDecimals, quoteTokenDecimals);
306
+ uint256 value = FullMath.mulDiv(adjustedSqrtRatioX96, adjustedSqrtRatioX96, 1 << 64);
307
+
308
+ bool priceBelow1 = adjustedSqrtRatioX96 < 2**96;
309
+ if (priceBelow1) {
310
+ // 10 ** 43 is precision needed to retreive 5 sigfigs of smallest possible price + 1 for rounding
311
+ value = FullMath.mulDiv(value, 10**44, 1 << 128);
312
+ } else {
313
+ // leave precision for 4 decimal places + 1 place for rounding
314
+ value = FullMath.mulDiv(value, 10**5, 1 << 128);
315
+ }
316
+
317
+ // get digit count
318
+ uint256 temp = value;
319
+ uint8 digits;
320
+ while (temp != 0) {
321
+ digits++;
322
+ temp /= 10;
323
+ }
324
+ // don't count extra digit kept for rounding
325
+ digits = digits - 1;
326
+
327
+ // address rounding
328
+ (uint256 sigfigs, bool extraDigit) = sigfigsRounded(value, digits);
329
+ if (extraDigit) {
330
+ digits++;
331
+ }
332
+
333
+ DecimalStringParams memory params;
334
+ if (priceBelow1) {
335
+ // 7 bytes ( "0." and 5 sigfigs) + leading 0's bytes
336
+ params.bufferLength = uint8(uint8(7).add(uint8(43).sub(digits)));
337
+ params.zerosStartIndex = 2;
338
+ params.zerosEndIndex = uint8(uint256(43).sub(digits).add(1));
339
+ params.sigfigIndex = uint8(params.bufferLength.sub(1));
340
+ } else if (digits >= 9) {
341
+ // no decimal in price string
342
+ params.bufferLength = uint8(digits.sub(4));
343
+ params.zerosStartIndex = 5;
344
+ params.zerosEndIndex = uint8(params.bufferLength.sub(1));
345
+ params.sigfigIndex = 4;
346
+ } else {
347
+ // 5 sigfigs surround decimal
348
+ params.bufferLength = 6;
349
+ params.sigfigIndex = 5;
350
+ params.decimalIndex = uint8(digits.sub(5).add(1));
351
+ }
352
+ params.sigfigs = sigfigs;
353
+ params.isLessThanOne = priceBelow1;
354
+ params.isPercent = false;
355
+
356
+ return generateDecimalString(params);
357
+ }
358
+
359
+ // @notice Returns string as decimal percentage of fee amount.
360
+ // @param fee fee amount
361
+ function feeToPercentString(uint24 fee) internal pure returns (string memory) {
362
+ if (fee == 0) {
363
+ return '0%';
364
+ }
365
+ uint24 temp = fee;
366
+ uint256 digits;
367
+ uint8 numSigfigs;
368
+ while (temp != 0) {
369
+ if (numSigfigs > 0) {
370
+ // count all digits preceding least significant figure
371
+ numSigfigs++;
372
+ } else if (temp % 10 != 0) {
373
+ numSigfigs++;
374
+ }
375
+ digits++;
376
+ temp /= 10;
377
+ }
378
+
379
+ DecimalStringParams memory params;
380
+ uint256 nZeros;
381
+ if (digits >= 5) {
382
+ // if decimal > 1 (5th digit is the ones place)
383
+ uint256 decimalPlace = digits.sub(numSigfigs) >= 4 ? 0 : 1;
384
+ nZeros = digits.sub(5) < (numSigfigs.sub(1)) ? 0 : digits.sub(5).sub(numSigfigs.sub(1));
385
+ params.zerosStartIndex = numSigfigs;
386
+ params.zerosEndIndex = uint8(params.zerosStartIndex.add(nZeros).sub(1));
387
+ params.sigfigIndex = uint8(params.zerosStartIndex.sub(1).add(decimalPlace));
388
+ params.bufferLength = uint8(nZeros.add(numSigfigs.add(1)).add(decimalPlace));
389
+ } else {
390
+ // else if decimal < 1
391
+ nZeros = uint256(5).sub(digits);
392
+ params.zerosStartIndex = 2;
393
+ params.zerosEndIndex = uint8(nZeros.add(params.zerosStartIndex).sub(1));
394
+ params.bufferLength = uint8(nZeros.add(numSigfigs.add(2)));
395
+ params.sigfigIndex = uint8((params.bufferLength).sub(2));
396
+ params.isLessThanOne = true;
397
+ }
398
+ params.sigfigs = uint256(fee).div(10**(digits.sub(numSigfigs)));
399
+ params.isPercent = true;
400
+ params.decimalIndex = digits > 4 ? uint8(digits.sub(4)) : 0;
401
+
402
+ return generateDecimalString(params);
403
+ }
404
+
405
+ function addressToString(address addr) internal pure returns (string memory) {
406
+ return (uint256(addr)).toHexString(20);
407
+ }
408
+
409
+ function generateSVGImage(ConstructTokenURIParams memory params) internal pure returns (string memory svg) {
410
+ NFTSVG.SVGParams memory svgParams =
411
+ NFTSVG.SVGParams({
412
+ quoteToken: addressToString(params.quoteTokenAddress),
413
+ baseToken: addressToString(params.baseTokenAddress),
414
+ poolAddress: params.poolAddress,
415
+ quoteTokenSymbol: params.quoteTokenSymbol,
416
+ baseTokenSymbol: params.baseTokenSymbol,
417
+ feeTier: feeToPercentString(params.fee),
418
+ tickLower: params.tickLower,
419
+ tickUpper: params.tickUpper,
420
+ tickSpacing: params.tickSpacing,
421
+ overRange: overRange(params.tickLower, params.tickUpper, params.tickCurrent),
422
+ tokenId: params.tokenId,
423
+ color0: tokenToColorHex(uint256(params.quoteTokenAddress), 136),
424
+ color1: tokenToColorHex(uint256(params.baseTokenAddress), 136),
425
+ color2: tokenToColorHex(uint256(params.quoteTokenAddress), 0),
426
+ color3: tokenToColorHex(uint256(params.baseTokenAddress), 0),
427
+ x1: scale(getCircleCoord(uint256(params.quoteTokenAddress), 16, params.tokenId), 0, 255, 16, 274),
428
+ y1: scale(getCircleCoord(uint256(params.baseTokenAddress), 16, params.tokenId), 0, 255, 100, 484),
429
+ x2: scale(getCircleCoord(uint256(params.quoteTokenAddress), 32, params.tokenId), 0, 255, 16, 274),
430
+ y2: scale(getCircleCoord(uint256(params.baseTokenAddress), 32, params.tokenId), 0, 255, 100, 484),
431
+ x3: scale(getCircleCoord(uint256(params.quoteTokenAddress), 48, params.tokenId), 0, 255, 16, 274),
432
+ y3: scale(getCircleCoord(uint256(params.baseTokenAddress), 48, params.tokenId), 0, 255, 100, 484)
433
+ });
434
+
435
+ return NFTSVG.generateSVG(svgParams);
436
+ }
437
+
438
+ function overRange(
439
+ int24 tickLower,
440
+ int24 tickUpper,
441
+ int24 tickCurrent
442
+ ) private pure returns (int8) {
443
+ if (tickCurrent < tickLower) {
444
+ return -1;
445
+ } else if (tickCurrent > tickUpper) {
446
+ return 1;
447
+ } else {
448
+ return 0;
449
+ }
450
+ }
451
+
452
+ function scale(
453
+ uint256 n,
454
+ uint256 inMn,
455
+ uint256 inMx,
456
+ uint256 outMn,
457
+ uint256 outMx
458
+ ) private pure returns (string memory) {
459
+ return (n.sub(inMn).mul(outMx.sub(outMn)).div(inMx.sub(inMn)).add(outMn)).toString();
460
+ }
461
+
462
+ function tokenToColorHex(uint256 token, uint256 offset) internal pure returns (string memory str) {
463
+ return string((token >> offset).toHexStringNoPrefix(3));
464
+ }
465
+
466
+ function getCircleCoord(
467
+ uint256 tokenAddress,
468
+ uint256 offset,
469
+ uint256 tokenId
470
+ ) internal pure returns (uint256) {
471
+ return (sliceTokenHex(tokenAddress, offset) * tokenId) % 255;
472
+ }
473
+
474
+ function sliceTokenHex(uint256 token, uint256 offset) internal pure returns (uint256) {
475
+ return uint256(uint8(token >> offset));
476
+ }
477
+ }