@pioneer-platform/uniswap-client 0.1.0 → 0.2.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 (51) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/package.json +15 -24
  3. package/tsconfig.json +2 -1
  4. package/lib/constants/chainInfo.d.ts +0 -47
  5. package/lib/constants/chainInfo.js +0 -248
  6. package/lib/constants/chains.d.ts +0 -46
  7. package/lib/constants/chains.js +0 -136
  8. package/lib/constants/governance.d.ts +0 -8
  9. package/lib/constants/governance.js +0 -23
  10. package/lib/constants/lists.d.ts +0 -12
  11. package/lib/constants/lists.js +0 -56
  12. package/lib/constants/locales.d.ts +0 -6
  13. package/lib/constants/locales.js +0 -71
  14. package/lib/constants/misc.d.ts +0 -16
  15. package/lib/constants/misc.js +0 -31
  16. package/lib/constants/networks.d.ts +0 -21
  17. package/lib/constants/networks.js +0 -169
  18. package/lib/constants/proposals/index.d.ts +0 -5
  19. package/lib/constants/proposals/index.js +0 -8
  20. package/lib/constants/proposals/polygon_proposal_title.d.ts +0 -1
  21. package/lib/constants/proposals/polygon_proposal_title.js +0 -4
  22. package/lib/constants/proposals/uniswap_grants_proposal_description.d.ts +0 -1
  23. package/lib/constants/proposals/uniswap_grants_proposal_description.js +0 -109
  24. package/lib/constants/providers.d.ts +0 -20
  25. package/lib/constants/providers.js +0 -32
  26. package/lib/constants/routing.d.ts +0 -16
  27. package/lib/constants/routing.js +0 -106
  28. package/lib/constants/supportArticles.d.ts +0 -13
  29. package/lib/constants/supportArticles.js +0 -17
  30. package/lib/constants/tokens.d.ts +0 -70
  31. package/lib/constants/tokens.js +0 -252
  32. package/lib/index.d.ts +0 -1
  33. package/lib/index.js +0 -724
  34. package/lib/routing/clientSideSmartOrderRouter.d.ts +0 -5
  35. package/lib/routing/clientSideSmartOrderRouter.js +0 -102
  36. package/lib/routing/gas.d.ts +0 -5
  37. package/lib/routing/gas.js +0 -71
  38. package/lib/routing/types.d.ts +0 -393
  39. package/lib/routing/types.js +0 -320
  40. package/lib/routing/utils.d.ts +0 -28
  41. package/lib/routing/utils.js +0 -264
  42. package/lib/rpc/AppJsonRpcProvider.d.ts +0 -44
  43. package/lib/rpc/AppJsonRpcProvider.js +0 -92
  44. package/lib/rpc/ConfiguredJsonRpcProvider.d.ts +0 -8
  45. package/lib/rpc/ConfiguredJsonRpcProvider.js +0 -16
  46. package/lib/utils/contracts/getContract.d.ts +0 -3
  47. package/lib/utils/contracts/getContract.js +0 -28
  48. package/lib/utils/transformSwapRouteToGetQuoteResult.d.ts +0 -4
  49. package/lib/utils/transformSwapRouteToGetQuoteResult.js +0 -113
  50. package/lib/utils/uniswapData.d.ts +0 -21
  51. package/lib/utils/uniswapData.js +0 -139
package/lib/index.js DELETED
@@ -1,724 +0,0 @@
1
- "use strict";
2
- /*
3
- Uniswap Integration
4
- - Highlander
5
-
6
- BASE
7
- https://docs.base.org/contracts/
8
-
9
-
10
-
11
- */
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- const TAG = " | Uniswap | ";
14
- const axios = require('axios');
15
- const BaseDecimalMap = {
16
- "ETH": 18,
17
- "BASE": 18,
18
- "BTC": 8
19
- };
20
- // @ts-ignore
21
- const pioneer_discovery_1 = require("@pioneer-platform/pioneer-discovery");
22
- let { caipToNetworkId, shortListSymbolToCaip, ChainToNetworkId } = require("@pioneer-platform/pioneer-caip");
23
- const { uuid } = require('uuidv4');
24
- const log = require('@pioneer-platform/loggerdog')();
25
- const { ethers, BigNumber } = require('ethers');
26
- const { utils } = require('ethers');
27
- let networkSupport = [
28
- ChainToNetworkId["ETH"],
29
- ChainToNetworkId["BASE"],
30
- ];
31
- const EIP155_MAINNET_CHAINS = {
32
- 'eip155:1': {
33
- chainId: 1,
34
- WETH: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
35
- name: 'Ethereum',
36
- logo: '/chain-logos/eip155-1.png',
37
- rgb: '99, 125, 234',
38
- universalRouter: '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D',
39
- rpc: 'https://eth.llamarpc.com',
40
- defaultGasLimit: 250000,
41
- namespace: 'eip155'
42
- },
43
- 'eip155:8453': {
44
- chainId: 8453,
45
- WETH: '0x4200000000000000000000000000000000000006',
46
- name: 'Base',
47
- logo: '/chain-logos/base.png',
48
- rgb: '242, 242, 242',
49
- universalRouter: '0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD',
50
- rpc: 'https://mainnet.base.org',
51
- defaultGasLimit: 135120,
52
- namespace: 'eip155'
53
- }
54
- };
55
- const ERC20_ABI = [
56
- "function allowance(address owner, address spender) view returns (uint256)",
57
- "function balanceOf(address owner) view returns (uint256)",
58
- "function approve(address spender, uint256 amount) returns (bool)",
59
- "function transfer(address to, uint256 amount) returns (bool)",
60
- "function symbol() view returns (string)",
61
- "function decimals() view returns (uint8)"
62
- ];
63
- function toDeadline(expiration) {
64
- return Math.floor((Date.now() + expiration) / 1000);
65
- }
66
- const PERMIT_EXPIRATION = 2592000000; // 30 days in milliseconds
67
- const PERMIT_SIG_EXPIRATION = 1800000; // 30 minutes in milliseconds
68
- module.exports = {
69
- init: function (settings) {
70
- return true;
71
- },
72
- networkSupport: function () {
73
- return networkSupport;
74
- },
75
- assetSupport: function () {
76
- return getAssetSupport();
77
- },
78
- buildPermitTx: function (permit) {
79
- return get_permit(permit);
80
- },
81
- buildLpTx: function (quote) {
82
- return build_lp_tx(quote);
83
- },
84
- getQuote: function (quote) {
85
- return get_quote_local(quote);
86
- }
87
- //ProvideLP
88
- // getQuote: function (quote:any) {
89
- // return get_quote_api(quote);
90
- // }
91
- };
92
- const build_lp_tx = async function (input) {
93
- let tag = TAG + " | build_lp_tx | ";
94
- try {
95
- log.info("input: ", input);
96
- let output = {};
97
- //
98
- let inputChain = input.chain;
99
- let fromAddress = input.fromAddress;
100
- log.info("inputChain: ", inputChain);
101
- let providerUrl = EIP155_MAINNET_CHAINS[inputChain].rpc;
102
- if (!providerUrl)
103
- throw new Error("missing providerUrl");
104
- log.info("providerUrl: ", providerUrl);
105
- //
106
- const provider = new ethers.providers.JsonRpcProvider(providerUrl); // Set your Ethereum RPC URL
107
- const positionManagerAddress = '0x03a520b32c04bf3beef7beb72e919cf822ed34f1';
108
- const positionManagerABI = [
109
- "function mint((address token0, address token1, uint24 fee, int24 tickLower, int24 tickUpper, uint128 amount0Desired, uint128 amount1Desired, uint128 amount0Min, uint128 amount1Min, address recipient, uint256 deadline)) external returns (uint256 tokenId, uint128 liquidity, uint256 amount0, uint256 amount1)"
110
- ];
111
- const positionManager = new ethers.Contract(positionManagerAddress, positionManagerABI);
112
- // Define the parameters for the liquidity position
113
- //ETH
114
- const token0 = '0x4200000000000000000000000000000000000006'; // Address of token0
115
- //PRO
116
- const token1 = '0xef743df8eda497bcf1977393c401a636518dd630'; // Address of token1
117
- const fee = 3000; // Fee tier, for example 0.3%
118
- const tickLower = -60000; // Lower tick
119
- const tickUpper = 60000; // Upper tick
120
- const amount0Desired = ethers.utils.parseUnits("0.01", 18); // 10 token0
121
- const amount1Desired = ethers.utils.parseUnits("41.2", 18); // 10 token1
122
- const amount0Min = ethers.utils.parseUnits("0.001", 18); // Min token0
123
- const amount1Min = ethers.utils.parseUnits(".9", 18); // Min token1
124
- const recipient = fromAddress; // Recipient address
125
- const deadline = Math.floor(Date.now() / 1000) + 60 * 20; // Transaction deadline
126
- // Construct the mint parameters
127
- const params = {
128
- token0,
129
- token1,
130
- fee,
131
- tickLower,
132
- tickUpper,
133
- amount0Desired,
134
- amount1Desired,
135
- amount0Min,
136
- amount1Min,
137
- recipient,
138
- deadline
139
- };
140
- log.info(tag, "params: ", params);
141
- const txData = await positionManager.mint.call(params);
142
- log.info(tag, "Transaction data prepared:", txData);
143
- const value = token0 === ethers.constants.AddressZero ? amount0Desired : '0x0'; // Assuming token0 is ETH
144
- log.info("calldata: ", txData.data);
145
- log.info("value: ", value);
146
- const nonce = await provider.getTransactionCount(fromAddress, "latest");
147
- log.info("nonce: ", nonce);
148
- let gas = `0x${BigInt("935120").toString(16)}`; // 935120
149
- const gasPrice = await provider.getGasPrice();
150
- log.info("gasPrice: ", gasPrice.toString());
151
- const adjustedGasPrice = gasPrice.mul(ethers.BigNumber.from(110)).div(ethers.BigNumber.from(100)); // Example: Increase by 10%
152
- let isZero = function isZero(hexNumberString) {
153
- return hexNumberString === '0' || /^0x0*$/.test(hexNumberString);
154
- };
155
- output.txs = [];
156
- const tx = {
157
- from: fromAddress,
158
- to: positionManagerAddress,
159
- chainId: inputChain.split(':')[1],
160
- data: txData.data,
161
- ...(value && !isZero(value) ? { value: utils.toHex(value) } : {}),
162
- gas,
163
- gasPrice: utils.toHex(adjustedGasPrice),
164
- nonce: utils.toHex(nonce),
165
- };
166
- output.txs.push({
167
- type: "evm",
168
- description: 'mint PRO position',
169
- chain: inputChain,
170
- txParams: tx
171
- });
172
- output.meta = {
173
- quoteMode: "LP"
174
- };
175
- output.steps = 1;
176
- output.complete = true;
177
- output.type = 'EVM';
178
- output.id = uuid();
179
- return output;
180
- }
181
- catch (e) {
182
- console.error(e);
183
- }
184
- };
185
- let get_permit = function (permit) {
186
- let tag = TAG + " | get_permit | ";
187
- try {
188
- let from = permit.from;
189
- let token = permit.token;
190
- let amount = permit.amount;
191
- let chainId = permit.chainId;
192
- //iterate over chains
193
- let permit712 = {
194
- "types": {
195
- "PermitSingle": [
196
- {
197
- "name": "details",
198
- "type": "PermitDetails"
199
- },
200
- {
201
- "name": "spender",
202
- "type": "address"
203
- },
204
- {
205
- "name": "sigDeadline",
206
- "type": "uint256"
207
- }
208
- ],
209
- "PermitDetails": [
210
- {
211
- "name": "token",
212
- "type": "address"
213
- },
214
- {
215
- "name": "amount",
216
- "type": "uint160"
217
- },
218
- {
219
- "name": "expiration",
220
- "type": "uint48"
221
- },
222
- {
223
- "name": "nonce",
224
- "type": "uint48"
225
- }
226
- ],
227
- "EIP712Domain": [
228
- {
229
- "name": "name",
230
- "type": "string"
231
- },
232
- {
233
- "name": "chainId",
234
- "type": "uint256"
235
- },
236
- {
237
- "name": "verifyingContract",
238
- "type": "address"
239
- }
240
- ]
241
- },
242
- "domain": {
243
- "name": "Permit2",
244
- "chainId": permit.chainId,
245
- "verifyingContract": "0x000000000022d473030f116ddee9f6b43ac78ba3"
246
- },
247
- "primaryType": "PermitSingle",
248
- "message": {
249
- "details": {
250
- "token": permit.token,
251
- "amount": permit.amount,
252
- "expiration": permit.expiry,
253
- "nonce": "1"
254
- },
255
- "spender": EIP155_MAINNET_CHAINS['eip155:' + chainId].universalRouter,
256
- "sigDeadline": permit.sigDeadline
257
- }
258
- };
259
- return permit712;
260
- }
261
- catch (e) {
262
- console.error(e);
263
- }
264
- };
265
- let getAssetSupport = function () {
266
- let tag = TAG + " | getAssetSupport | ";
267
- try {
268
- //iterate over chains
269
- let allAssets = Object.keys(pioneer_discovery_1.assetData);
270
- // log.info(tag,"allAssets: ",allAssets)
271
- let supportedAssets = [];
272
- for (let i = 0; i < allAssets.length; i++) {
273
- let asset = allAssets[i];
274
- let networkId = caipToNetworkId(asset);
275
- // console.log("networkId: ",networkId)
276
- if (networkSupport.indexOf(networkId) > -1) {
277
- supportedAssets.push(asset);
278
- }
279
- }
280
- return supportedAssets;
281
- }
282
- catch (e) {
283
- console.error(e);
284
- }
285
- };
286
- const get_quote_local = async function (quote) {
287
- let tag = TAG + " | get_quote_local | ";
288
- try {
289
- let output = {};
290
- if (!quote.sellAsset)
291
- throw new Error("missing sellAsset");
292
- if (!quote.buyAsset)
293
- throw new Error("missing buyAsset");
294
- if (!quote.sellAmount)
295
- throw new Error("missing sellAmount");
296
- if (!quote.senderAddress)
297
- throw new Error("missing senderAddress");
298
- if (!quote.recipientAddress)
299
- throw new Error("missing recipientAddress");
300
- if (!quote.slippage)
301
- throw new Error("missing slippage");
302
- if (!networkSupport.includes(caipToNetworkId(quote.buyAsset))) {
303
- throw new Error("unsupported buyAsset");
304
- }
305
- if (!networkSupport.includes(caipToNetworkId(quote.sellAsset))) {
306
- throw new Error("unsupported sellAsset");
307
- }
308
- // if(!quote.permit2) throw new Error("missing permit2, required for uniswap")
309
- output.txs = [];
310
- let from = quote.senderAddress;
311
- // let slippageTolerance = quote.slippage
312
- let recipient = quote.recipientAddress;
313
- output.sellAsset = {};
314
- output.source = 'uniswap';
315
- output.sellAsset.caip = quote.sellAsset;
316
- output.sellAmount = quote.sellAmount;
317
- output.buyAsset = {};
318
- output.buyAsset.caip = quote.buyAsset;
319
- //NO CROSS CHAIN
320
- let inputChain = caipToNetworkId(quote.sellAsset);
321
- let outputChain = caipToNetworkId(quote.buyAsset);
322
- if (inputChain != outputChain)
323
- throw new Error("Cross Chain not supported");
324
- log.info("inputChain: ", inputChain);
325
- let providerUrl = EIP155_MAINNET_CHAINS[inputChain].rpc;
326
- if (!providerUrl)
327
- throw new Error("missing providerUrl");
328
- log.info("providerUrl: ", providerUrl);
329
- let chainIdInt = parseInt(inputChain.replace('eip155:', ''));
330
- log.info(tag, "chainIdInt: ", chainIdInt);
331
- let chainId = chainIdInt;
332
- //TODO TEST provider for liveness
333
- //get procider for chain
334
- const provider = new ethers.providers.JsonRpcProvider(providerUrl); // Set your Ethereum RPC URL
335
- // Initialize contracts based on whether they are tokens
336
- let sellTokenContract, buyTokenContract;
337
- //get pool for contract
338
- let BUY_TOKEN, SELL_TOKEN;
339
- let BUY_TOKEN_ADDRESS, SELL_TOKEN_ADDRESS;
340
- if (quote.buyAsset.indexOf('erc20') > -1) {
341
- //token input get pool for input
342
- let buyTokenAddress = quote.buyAsset.split(":")[2].toLowerCase();
343
- BUY_TOKEN_ADDRESS = buyTokenAddress;
344
- log.info("buyTokenAddress: ", buyTokenAddress);
345
- buyTokenContract = new ethers.Contract(buyTokenAddress.toLowerCase(), ERC20_ABI, provider);
346
- const symbolBuy = await buyTokenContract.symbol();
347
- const decimalsBuy = await buyTokenContract.decimals();
348
- log.info("symbolBuy: ", symbolBuy);
349
- log.info("decimalsBuy: ", decimalsBuy);
350
- //get balance
351
- let balance = await buyTokenContract.balanceOf(quote.senderAddress);
352
- log.info("balance: ", balance.toString());
353
- if (!symbolBuy)
354
- throw new Error("missing symbolBuy");
355
- if (!decimalsBuy)
356
- throw new Error("missing decimalsBuy");
357
- BUY_TOKEN = new ethers.Contract(buyTokenAddress, ERC20_ABI, provider);
358
- log.info("BUY_TOKEN: ", BUY_TOKEN);
359
- }
360
- else {
361
- BUY_TOKEN = new ethers.Contract(EIP155_MAINNET_CHAINS[inputChain].WETH, ERC20_ABI, provider);
362
- BUY_TOKEN_ADDRESS = EIP155_MAINNET_CHAINS[inputChain].WETH;
363
- log.info("BUY_TOKEN: ", BUY_TOKEN);
364
- }
365
- if (quote.sellAsset.indexOf('erc20') > -1) {
366
- //token output get pools for output
367
- let sellTokenAddress = quote.sellAsset.split(":")[2].toLowerCase();
368
- SELL_TOKEN_ADDRESS = sellTokenAddress;
369
- log.info("sellTokenAddress: ", sellTokenAddress);
370
- sellTokenContract = new ethers.Contract(sellTokenAddress, ERC20_ABI, provider);
371
- const symbolSell = await sellTokenContract.symbol();
372
- const decimalsSell = await sellTokenContract.decimals();
373
- if (!symbolSell)
374
- throw new Error("missing symbolSell");
375
- if (!decimalsSell)
376
- throw new Error("missing decimalsSell");
377
- SELL_TOKEN = new ethers.Contract(sellTokenAddress, ERC20_ABI, provider);
378
- log.info("SELL_TOKEN: ", SELL_TOKEN);
379
- }
380
- else {
381
- //WETH
382
- SELL_TOKEN = new ethers.Contract(EIP155_MAINNET_CHAINS[inputChain].WETH, ERC20_ABI, provider);
383
- SELL_TOKEN_ADDRESS = EIP155_MAINNET_CHAINS[inputChain].WETH;
384
- }
385
- const inputPRO = utils.parseUnits(quote.sellAmount, 18).toString();
386
- // @ts-ignore
387
- // let POOL_PRO = await get_pool(BUY_TOKEN, SELL_TOKEN, FeeAmount.MEDIUM, 0, provider)
388
- // if(!POOL_PRO) throw new Error("missing POOL_PRO")
389
- let spender = EIP155_MAINNET_CHAINS['eip155:' + chainId].universalRouter;
390
- let allowance = await sellTokenContract.allowance(from, spender);
391
- log.info(tag, "inputPRO: ", inputPRO.toString());
392
- log.info(tag, "allowance: ", allowance.toString());
393
- if (allowance.lt(inputPRO)) {
394
- log.info(tag, "allowance is less than sellAmount, approving...");
395
- // build approval tx
396
- throw Error('TODO APPROVE TOKENS');
397
- }
398
- const CLIENT_PARAMS = {
399
- protocols: "V3"
400
- };
401
- // Define FEE_AMOUNT as a constant (no longer referencing FeeAmount.MEDIUM)
402
- const FEE_AMOUNT = 3000; // MEDIUM fee amount (0.3%)
403
- let args = {
404
- tokenInAddress: SELL_TOKEN_ADDRESS,
405
- tokenInDecimals: SELL_TOKEN.decimals,
406
- tokenInChainId: chainIdInt,
407
- tokenOutAddress: BUY_TOKEN_ADDRESS,
408
- tokenOutChainId: chainIdInt,
409
- tokenOutDecimals: BUY_TOKEN.decimals,
410
- amount: inputPRO,
411
- tradeType: 'EXACT_INPUT',
412
- sendPortionEnabled: false,
413
- };
414
- let Protocol;
415
- (function (Protocol) {
416
- Protocol["V2"] = "V2";
417
- Protocol["V3"] = "V3";
418
- Protocol["MIXED"] = "MIXED";
419
- })(Protocol || (Protocol = {}));
420
- let QuoteIntent;
421
- (function (QuoteIntent) {
422
- QuoteIntent["Pricing"] = "pricing";
423
- QuoteIntent["Quote"] = "quote";
424
- })(QuoteIntent || (QuoteIntent = {}));
425
- // Get gasPrice before using it in the mock quoteResult
426
- const gasPrice = await provider.getGasPrice();
427
- // Mock the router and quoteResult
428
- const router = { provider: provider };
429
- const quoteResult = {
430
- quote: ethers.utils.parseUnits("1.0", 18),
431
- quoteGasAdjusted: ethers.utils.parseUnits("0.98", 18),
432
- gasUseEstimateQuote: ethers.utils.parseUnits("0.02", 18),
433
- gasUseEstimate: ethers.BigNumber.from("150000"),
434
- gasPriceWei: gasPrice,
435
- route: [],
436
- estimatedGasUsed: ethers.BigNumber.from("150000"),
437
- methodParameters: {
438
- calldata: "0x1234567890abcdef",
439
- value: "0x0"
440
- }
441
- };
442
- // @ts-ignore
443
- const trade = await transformQuoteToTrade(args, quoteResult.data, QuoteMethod.CLIENT_SIDE_FALLBACK);
444
- if (!trade)
445
- throw Error("trade is undefined");
446
- log.info(tag, 'trade: ', trade);
447
- const permit = {
448
- details: {
449
- token: quote.sellAsset.split(":")[2].toLowerCase(),
450
- amount: '1000000000000000000000000',
451
- expiration: toDeadline(PERMIT_EXPIRATION),
452
- nonce: 0,
453
- },
454
- spender: EIP155_MAINNET_CHAINS['eip155:' + chainId].universalRouter,
455
- sigDeadline: toDeadline(PERMIT_SIG_EXPIRATION),
456
- signature: quote.permit2
457
- };
458
- // @ts-ignore
459
- const { calldata, value } = SwapRouter.swapERC20CallParameters(trade, {
460
- // recipient: from,
461
- // @ts-ignore
462
- slippageTolerance,
463
- // deadlineOrPreviousBlockhash: deadline,
464
- // inputTokenPermit: undefined,
465
- // @ts-ignore
466
- inputTokenPermit: permit,
467
- // fee: options.feeOptions,
468
- });
469
- log.info("calldata: ", calldata);
470
- log.info("value: ", value);
471
- const nonce = await provider.getTransactionCount(from, "latest");
472
- log.info("nonce: ", nonce);
473
- let gas = `0x${BigInt("935120").toString(16)}`; // 935120
474
- const adjustedGasPrice = gasPrice.mul(ethers.BigNumber.from(110)).div(ethers.BigNumber.from(100)); // Example: Increase by 10%
475
- let isZero = function isZero(hexNumberString) {
476
- return hexNumberString === '0' || /^0x0*$/.test(hexNumberString);
477
- };
478
- const tx = {
479
- from,
480
- to: EIP155_MAINNET_CHAINS['eip155:' + chainId].universalRouter,
481
- chainId,
482
- data: calldata,
483
- // TODO: universal-router-sdk returns a non-hexlified value.
484
- ...(value && !isZero(value) ? { value: utils.toHex(value) } : {}),
485
- gas,
486
- gasPrice: utils.toHex(adjustedGasPrice),
487
- nonce: utils.toHex(nonce),
488
- };
489
- output.txs.push({
490
- type: "evm",
491
- description: 'swap tokens',
492
- chain: inputChain,
493
- txParams: tx
494
- });
495
- output.meta = {
496
- quoteMode: "ERC20-ERC20"
497
- };
498
- output.steps = 1;
499
- output.complete = true;
500
- output.type = 'EVM';
501
- output.id = uuid();
502
- return output;
503
- }
504
- catch (e) {
505
- console.error(e);
506
- }
507
- };
508
- const get_quote_api = async function (quote) {
509
- let tag = TAG + " | get_quote | ";
510
- try {
511
- let output = {};
512
- if (!quote.sellAsset)
513
- throw new Error("missing sellAsset");
514
- if (!quote.buyAsset)
515
- throw new Error("missing buyAsset");
516
- if (!quote.sellAmount)
517
- throw new Error("missing sellAmount");
518
- if (!quote.senderAddress)
519
- throw new Error("missing senderAddress");
520
- if (!quote.recipientAddress)
521
- throw new Error("missing recipientAddress");
522
- if (!quote.slippage)
523
- throw new Error("missing slippage");
524
- if (!networkSupport.includes(caipToNetworkId(quote.buyAsset))) {
525
- throw new Error("unsupported buyAsset");
526
- }
527
- if (!networkSupport.includes(caipToNetworkId(quote.sellAsset))) {
528
- throw new Error("unsupported sellAsset");
529
- }
530
- // if(!quote.permit2) throw new Error("missing permit2, required for uniswap")
531
- output.txs = [];
532
- let from = quote.senderAddress;
533
- // let slippageTolerance = quote.slippage
534
- let recipient = quote.recipientAddress;
535
- output.sellAsset = {};
536
- output.source = 'uniswap';
537
- output.sellAsset.caip = quote.sellAsset;
538
- output.sellAmount = quote.sellAmount;
539
- output.buyAsset = {};
540
- output.buyAsset.caip = quote.buyAsset;
541
- //NO CROSS CHAIN
542
- let inputChain = caipToNetworkId(quote.sellAsset);
543
- let outputChain = caipToNetworkId(quote.buyAsset);
544
- if (inputChain != outputChain)
545
- throw new Error("Cross Chain not supported");
546
- log.info("inputChain: ", inputChain);
547
- let providerUrl = EIP155_MAINNET_CHAINS[inputChain].rpc;
548
- if (!providerUrl)
549
- throw new Error("missing providerUrl");
550
- log.info("providerUrl: ", providerUrl);
551
- let chainIdInt = parseInt(inputChain.replace('eip155:', ''));
552
- log.info(tag, "chainIdInt: ", chainIdInt);
553
- let chainId = chainIdInt;
554
- //TODO TEST provider for liveness
555
- //get procider for chain
556
- const provider = new ethers.providers.JsonRpcProvider(providerUrl); // Set your Ethereum RPC URL
557
- // Initialize contracts based on whether they are tokens
558
- let sellTokenContract, buyTokenContract;
559
- //get pool for contract
560
- let BUY_TOKEN, SELL_TOKEN;
561
- let BUY_TOKEN_ADDRESS, SELL_TOKEN_ADDRESS;
562
- if (quote.buyAsset.indexOf('erc20') > -1) {
563
- //token input get pool for input
564
- let buyTokenAddress = quote.buyAsset.split(":")[2].toLowerCase();
565
- BUY_TOKEN_ADDRESS = buyTokenAddress;
566
- log.info("buyTokenAddress: ", buyTokenAddress);
567
- buyTokenContract = new ethers.Contract(buyTokenAddress.toLowerCase(), ERC20_ABI, provider);
568
- const symbolBuy = await buyTokenContract.symbol();
569
- const decimalsBuy = await buyTokenContract.decimals();
570
- //get balance
571
- let balance = await buyTokenContract.balanceOf(quote.senderAddress);
572
- log.info("balance: ", balance.toString());
573
- BUY_TOKEN = new ethers.Contract(buyTokenAddress, ERC20_ABI, provider);
574
- }
575
- else {
576
- BUY_TOKEN = new ethers.Contract(EIP155_MAINNET_CHAINS[inputChain].WETH, ERC20_ABI, provider);
577
- BUY_TOKEN_ADDRESS = EIP155_MAINNET_CHAINS[inputChain].WETH;
578
- }
579
- if (quote.sellAsset.indexOf('erc20') > -1) {
580
- //token output get pools for output
581
- let sellTokenAddress = quote.sellAsset.split(":")[2].toLowerCase();
582
- SELL_TOKEN_ADDRESS = sellTokenAddress;
583
- log.info("sellTokenAddress: ", sellTokenAddress);
584
- sellTokenContract = new ethers.Contract(sellTokenAddress, ERC20_ABI, provider);
585
- const symbolSell = await sellTokenContract.symbol();
586
- const decimalsSell = await sellTokenContract.decimals();
587
- SELL_TOKEN = new ethers.Contract(sellTokenAddress, ERC20_ABI, provider);
588
- }
589
- else {
590
- //WETH
591
- SELL_TOKEN = new ethers.Contract(EIP155_MAINNET_CHAINS[inputChain].WETH, ERC20_ABI, provider);
592
- SELL_TOKEN_ADDRESS = EIP155_MAINNET_CHAINS[inputChain].WETH;
593
- }
594
- //check approval
595
- // let spender = EIP155_MAINNET_CHAINS['eip155:' + chainId].universalRouter;
596
- // let allowance = await sellTokenContract.allowance(from, spender)
597
- // log.info(tag,"allowance: ",allowance.toString())
598
- // if(allowance.lt(quote.sellAmount)) {
599
- // log.info(tag,"allowance is less than sellAmount, approving...")
600
- // // build approval tx
601
- //
602
- // }
603
- // @ts-ignore
604
- // let POOL_PRO = await get_pool(BUY_TOKEN, SELL_TOKEN, FeeAmount.MEDIUM, 0, provider)
605
- // if(!POOL_PRO) throw new Error("missing POOL_PRO")
606
- const FEE_AMOUNT = FeeAmount.MEDIUM;
607
- const inputPRO = utils.parseUnits('1000', 18).toString();
608
- //let mockedTrade = {"swaps":[{"route":{"_midPrice":{"numerator":[0,0,0,0,0,0,4096],"denominator":[61675844,569729604,374875920,86073376,789779649,857607051,27312043],"baseCurrency":{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},"quoteCurrency":{"chainId":8453,"decimals":18,"symbol":"ETH","name":"Ethereum","isNative":true,"isToken":false},"scalar":{"numerator":[660865024,931322574],"denominator":[660865024,931322574]}},"pools":[{"token0":{"chainId":8453,"decimals":18,"symbol":"WETH","isNative":false,"isToken":true,"address":"0x4200000000000000000000000000000000000006"},"token1":{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},"fee":3000,"sqrtRatioX96":[976906734,801624171,99421808,5226],"liquidity":[669390988,785588334,1],"tickCurrent":88055,"tickDataProvider":{},"_token1Price":{"numerator":[0,0,0,0,0,0,4096],"denominator":[61675844,569729604,374875920,86073376,789779649,857607051,27312043],"baseCurrency":{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},"quoteCurrency":{"chainId":8453,"decimals":18,"symbol":"WETH","isNative":false,"isToken":true,"address":"0x4200000000000000000000000000000000000006"},"scalar":{"numerator":[660865024,931322574],"denominator":[660865024,931322574]}}}],"tokenPath":[{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},{"chainId":8453,"decimals":18,"symbol":"WETH","isNative":false,"isToken":true,"address":"0x4200000000000000000000000000000000000006"}],"input":{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},"output":{"chainId":8453,"decimals":18,"symbol":"ETH","name":"Ethereum","isNative":true,"isToken":false},"protocol":"V3","path":[{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},{"chainId":8453,"decimals":18,"symbol":"WETH","isNative":false,"isToken":true,"address":"0x4200000000000000000000000000000000000006"}]},"inputAmount":{"numerator":[166199296,723291154,8],"denominator":[1],"currency":{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},"decimalScale":[660865024,931322574]},"outputAmount":{"numerator":[711636223,1308984],"denominator":[1],"currency":{"chainId":8453,"decimals":18,"symbol":"ETH","name":"Ethereum","isNative":true,"isToken":false},"decimalScale":[660865024,931322574]}}],"routes":[{"_midPrice":{"numerator":[0,0,0,0,0,0,4096],"denominator":[61675844,569729604,374875920,86073376,789779649,857607051,27312043],"baseCurrency":{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},"quoteCurrency":{"chainId":8453,"decimals":18,"symbol":"ETH","name":"Ethereum","isNative":true,"isToken":false},"scalar":{"numerator":[660865024,931322574],"denominator":[660865024,931322574]}},"pools":[{"token0":{"chainId":8453,"decimals":18,"symbol":"WETH","isNative":false,"isToken":true,"address":"0x4200000000000000000000000000000000000006"},"token1":{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},"fee":3000,"sqrtRatioX96":[976906734,801624171,99421808,5226],"liquidity":[669390988,785588334,1],"tickCurrent":88055,"tickDataProvider":{},"_token1Price":{"numerator":[0,0,0,0,0,0,4096],"denominator":[61675844,569729604,374875920,86073376,789779649,857607051,27312043],"baseCurrency":{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},"quoteCurrency":{"chainId":8453,"decimals":18,"symbol":"WETH","isNative":false,"isToken":true,"address":"0x4200000000000000000000000000000000000006"},"scalar":{"numerator":[660865024,931322574],"denominator":[660865024,931322574]}}}],"tokenPath":[{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},{"chainId":8453,"decimals":18,"symbol":"WETH","isNative":false,"isToken":true,"address":"0x4200000000000000000000000000000000000006"}],"input":{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},"output":{"chainId":8453,"decimals":18,"symbol":"ETH","name":"Ethereum","isNative":true,"isToken":false},"protocol":"V3","path":[{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},{"chainId":8453,"decimals":18,"symbol":"WETH","isNative":false,"isToken":true,"address":"0x4200000000000000000000000000000000000006"}]}],"tradeType":0,"fillType":"classic","approveInfo":{"needsApprove":false},"gasUseEstimate":164000,"gasUseEstimateUSD":0.005144,"blockNumber":"13097036","requestId":"747525fe-2ccf-494b-b0d3-677e2ebda037","quoteMethod":"ROUTING_API","swapFee":{"recipient":"0x067170777BA8027cED27E034102D54074d062d71","percent":{"numerator":[25],"denominator":[10000],"isPercent":true},"amount":"3522585411987"},"_outputAmount":{"numerator":[711636223,1308984],"denominator":[1],"currency":{"chainId":8453,"decimals":18,"symbol":"ETH","name":"Ethereum","isNative":true,"isToken":false},"decimalScale":[660865024,931322574]},"_inputAmount":{"numerator":[166199296,723291154,8],"denominator":[1],"currency":{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},"decimalScale":[660865024,931322574]},"_priceImpact":{"numerator":[941165072,174164585,107079465,969784123,796584146,369125678,162674660,985493716,122670731,392389178,573847026,872116559,752945511,679526963,814010822,56],"denominator":[0,0,0,0,0,0,0,706548328,653255527,1067448068,1064290579,550590138,219848419,764239412,729848924,903],"isPercent":true},"_executionPrice":{"numerator":[711636223,1308984],"denominator":[166199296,723291154,8],"baseCurrency":{"chainId":8453,"decimals":18,"symbol":"PRO","isNative":false,"isToken":true,"address":"0xEF743df8eDa497bCf1977393c401A636518DD630"},"quoteCurrency":{"chainId":8453,"decimals":18,"symbol":"ETH","name":"Ethereum","isNative":true,"isToken":false},"scalar":{"numerator":[660865024,931322574],"denominator":[660865024,931322574]}}}
609
- let args = {
610
- tokenInAddress: SELL_TOKEN_ADDRESS,
611
- tokenInChainId: chainIdInt,
612
- tokenOutAddress: BUY_TOKEN_ADDRESS,
613
- tokenOutChainId: chainIdInt,
614
- amount: "10000000000000000000",
615
- tradeType: 'EXACT_INPUT',
616
- sendPortionEnabled: true,
617
- };
618
- let Protocol;
619
- (function (Protocol) {
620
- Protocol["V2"] = "V2";
621
- Protocol["V3"] = "V3";
622
- Protocol["MIXED"] = "MIXED";
623
- })(Protocol || (Protocol = {}));
624
- let QuoteIntent;
625
- (function (QuoteIntent) {
626
- QuoteIntent["Pricing"] = "pricing";
627
- QuoteIntent["Quote"] = "quote";
628
- })(QuoteIntent || (QuoteIntent = {}));
629
- const CLIENT_PARAMS = {
630
- protocols: [Protocol.V2, Protocol.V3, Protocol.MIXED],
631
- };
632
- const { tokenInAddress: tokenIn, tokenInChainId, tokenOutAddress: tokenOut, tokenOutChainId, amount, tradeType, sendPortionEnabled, } = args;
633
- const requestBody = {
634
- tokenInChainId,
635
- tokenIn,
636
- tokenOutChainId,
637
- tokenOut,
638
- amount,
639
- sendPortionEnabled,
640
- type: 'EXACT_INPUT',
641
- intent: QuoteIntent.Quote,
642
- configs: [
643
- {
644
- "protocols": [
645
- "V2",
646
- "V3",
647
- "MIXED"
648
- ],
649
- "enableUniversalRouter": true,
650
- "routingType": "CLASSIC",
651
- "recipient": from,
652
- "enableFeeOnTransferFeeFetching": true
653
- }
654
- ],
655
- };
656
- // const UNISWAP_GATEWAY_DNS_URL = 'https://interface.gateway.uniswap.org/v2'
657
- log.info("requestBody: ", requestBody);
658
- log.info("requestBody: ", JSON.stringify(requestBody));
659
- let response = await axios({
660
- method: 'POST',
661
- url: 'https://interface.gateway.uniswap.org/v2/quote',
662
- data: requestBody,
663
- headers: {
664
- 'x-request-source': 'uniswap-web',
665
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
666
- 'Origin': 'http://localhost:3000',
667
- 'Referer': 'http://localhost:3000',
668
- 'Accept': 'application/json, text/plain, */*',
669
- 'Content-Type': 'application/json'
670
- }
671
- });
672
- log.info(tag, 'response', response.data);
673
- const uraQuoteResponse = response.data;
674
- log.info(tag, 'uraQuoteResponse', uraQuoteResponse);
675
- output.amountOut = uraQuoteResponse.quote.quoteDecimals;
676
- output.gas = uraQuoteResponse.quote.gasUseEstimate;
677
- output.gasUsd = uraQuoteResponse.quote.gasUseEstimateUSD;
678
- output.id = uraQuoteResponse.quote.requestId;
679
- output.slippage = uraQuoteResponse.quote.slippage;
680
- // @ts-ignore
681
- let calldata = uraQuoteResponse.quote.methodParameters.calldata;
682
- // @ts-ignore
683
- let value = uraQuoteResponse.quote.methodParameters.value;
684
- log.info("calldata: ", calldata);
685
- log.info("value: ", value);
686
- const nonce = await provider.getTransactionCount(from, "latest");
687
- log.info("nonce: ", nonce);
688
- let gas = `0x${BigInt("935120").toString(16)}`; // 935120
689
- const gasPrice = await provider.getGasPrice();
690
- log.info("gasPrice: ", gasPrice.toString());
691
- const adjustedGasPrice = gasPrice.mul(ethers.BigNumber.from(110)).div(ethers.BigNumber.from(100)); // Example: Increase by 10%
692
- let isZero = function isZero(hexNumberString) {
693
- return hexNumberString === '0' || /^0x0*$/.test(hexNumberString);
694
- };
695
- const tx = {
696
- from,
697
- to: EIP155_MAINNET_CHAINS['eip155:' + chainId].universalRouter,
698
- chainId,
699
- data: calldata,
700
- // TODO: universal-router-sdk returns a non-hexlified value.
701
- ...(value && !isZero(value) ? { value: utils.toHex(value) } : {}),
702
- gas,
703
- gasPrice: utils.toHex(adjustedGasPrice),
704
- nonce: utils.toHex(nonce),
705
- };
706
- output.txs.push({
707
- type: "evm",
708
- description: 'swap tokens',
709
- chain: inputChain,
710
- txParams: tx
711
- });
712
- output.meta = {
713
- quoteMode: "ERC20-ERC20"
714
- };
715
- output.steps = 1;
716
- output.complete = true;
717
- output.type = 'EVM';
718
- return output;
719
- }
720
- catch (e) {
721
- console.error(tag, "e: ", e);
722
- throw e;
723
- }
724
- };