@paraswap/dex-lib 3.8.21 → 3.8.22-infusion
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/build/dex/index.js +2 -0
- package/build/dex/index.js.map +1 -1
- package/build/dex/lite-psm/lite-psm.js +1 -1
- package/build/dex/lite-psm/lite-psm.js.map +1 -1
- package/build/dex/maker-psm/maker-psm.js +1 -1
- package/build/dex/trader-joe-v2.1/base.js +5 -16
- package/build/dex/trader-joe-v2.1/base.js.map +1 -1
- package/build/dex/trader-joe-v2.1/config.js +2 -2
- package/build/dex/trader-joe-v2.1/types.d.ts +3 -3
- package/package.json +1 -1
- package/src/abi/infusion/InfusionFactory.json +147 -0
- package/src/abi/infusion/InfusionPair.json +658 -0
- package/src/abi/infusion/InfusionRouter.json +442 -0
- package/src/dex/index.ts +2 -0
- package/src/dex/infusion/config.ts +27 -0
- package/src/dex/infusion/infusion-e2e.test.ts +110 -0
- package/src/dex/infusion/infusion-integration.test.ts +232 -0
- package/src/dex/infusion/infusion-stable-pool.ts +91 -0
- package/src/dex/infusion/infusion.ts +749 -0
- package/src/dex/infusion/types.ts +60 -0
- package/src/dex/infusion/utils/isStablePair.ts +18 -0
- package/src/dex/lite-psm/lite-psm.ts +1 -1
- package/src/dex/maker-psm/maker-psm.ts +1 -1
- package/src/dex/trader-joe-v2.1/base.ts +5 -16
- package/src/dex/trader-joe-v2.1/config.ts +2 -2
- package/src/dex/trader-joe-v2.1/types.ts +3 -3
- package/tests/constants-e2e.ts +1 -1
|
@@ -0,0 +1,749 @@
|
|
|
1
|
+
import { UniswapV2 } from '../uniswap-v2/uniswap-v2';
|
|
2
|
+
import {
|
|
3
|
+
Network,
|
|
4
|
+
NULL_ADDRESS,
|
|
5
|
+
SUBGRAPH_TIMEOUT,
|
|
6
|
+
DEST_TOKEN_PARASWAP_TRANSFERS,
|
|
7
|
+
SRC_TOKEN_PARASWAP_TRANSFERS,
|
|
8
|
+
} from '../../constants';
|
|
9
|
+
import {
|
|
10
|
+
AdapterExchangeParam,
|
|
11
|
+
Address,
|
|
12
|
+
DexExchangeParam,
|
|
13
|
+
ExchangePrices,
|
|
14
|
+
PoolLiquidity,
|
|
15
|
+
SimpleExchangeParam,
|
|
16
|
+
Token,
|
|
17
|
+
TransferFeeParams,
|
|
18
|
+
} from '../../types';
|
|
19
|
+
import { IDexHelper } from '../../dex-helper';
|
|
20
|
+
import erc20ABI from '../../abi/erc20.json';
|
|
21
|
+
import { UniswapData, UniswapV2Data } from '../uniswap-v2/types';
|
|
22
|
+
import { getBigIntPow, getDexKeysWithNetwork, isETHAddress } from '../../utils';
|
|
23
|
+
import infusionFactoryABI from '../../abi/infusion/InfusionFactory.json';
|
|
24
|
+
import infusionPairABI from '../../abi/infusion/InfusionPair.json';
|
|
25
|
+
import infusionRouterABI from '../../abi/infusion/InfusionRouter.json';
|
|
26
|
+
import _ from 'lodash';
|
|
27
|
+
import { NumberAsString, SwapSide } from '@paraswap/core';
|
|
28
|
+
import { Interface, AbiCoder } from '@ethersproject/abi';
|
|
29
|
+
import { InfusionStablePool } from './infusion-stable-pool';
|
|
30
|
+
import { Uniswapv2ConstantProductPool } from '../uniswap-v2/uniswap-v2-constant-product-pool';
|
|
31
|
+
import {
|
|
32
|
+
PoolState,
|
|
33
|
+
InfusionData,
|
|
34
|
+
InfusionPair,
|
|
35
|
+
InfusionPool,
|
|
36
|
+
InfusionPoolOrderedParams,
|
|
37
|
+
InfusionParam,
|
|
38
|
+
} from './types';
|
|
39
|
+
import { InfusionConfig, Adapters } from './config';
|
|
40
|
+
import { applyTransferFee } from '../../lib/token-transfer-fee';
|
|
41
|
+
import { isStablePair } from './utils/isStablePair';
|
|
42
|
+
|
|
43
|
+
export enum InfusionRouterFunctions {
|
|
44
|
+
sellExactEth = 'swapExactETHForTokens',
|
|
45
|
+
sellExactToken = 'swapExactTokensForETH',
|
|
46
|
+
swapExactIn = 'swapExactTokensForTokens',
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface SmardexData extends Omit<UniswapV2Data, 'feeFactor'> {
|
|
50
|
+
deadline: number;
|
|
51
|
+
receiver: Address;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const VelodromeFactoryABI = [
|
|
55
|
+
{
|
|
56
|
+
inputs: [{ internalType: 'bool', name: '_stable', type: 'bool' }],
|
|
57
|
+
name: 'getFee',
|
|
58
|
+
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
|
|
59
|
+
stateMutability: 'view',
|
|
60
|
+
type: 'function',
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
const velodromeFactoryIface = new Interface(VelodromeFactoryABI);
|
|
65
|
+
const erc20Iface = new Interface(erc20ABI);
|
|
66
|
+
const infusionPairIface = new Interface(infusionPairABI);
|
|
67
|
+
const defaultAbiCoder = new AbiCoder();
|
|
68
|
+
|
|
69
|
+
export class Infusion extends UniswapV2 {
|
|
70
|
+
pairs: { [key: string]: InfusionPair } = {};
|
|
71
|
+
stableFee?: number;
|
|
72
|
+
volatileFee?: number;
|
|
73
|
+
|
|
74
|
+
readonly isFeeOnTransferSupported: boolean = true;
|
|
75
|
+
readonly SRC_TOKEN_DEX_TRANSFERS = 1;
|
|
76
|
+
readonly DEST_TOKEN_DEX_TRANSFERS = 1;
|
|
77
|
+
|
|
78
|
+
public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
|
|
79
|
+
getDexKeysWithNetwork(_.pick(InfusionConfig, ['Infusion']));
|
|
80
|
+
|
|
81
|
+
constructor(
|
|
82
|
+
protected network: Network,
|
|
83
|
+
dexKey: string,
|
|
84
|
+
protected dexHelper: IDexHelper,
|
|
85
|
+
isDynamicFees = true,
|
|
86
|
+
factoryAddress?: Address,
|
|
87
|
+
subgraphURL?: string,
|
|
88
|
+
initCode?: string,
|
|
89
|
+
feeCode?: number,
|
|
90
|
+
poolGasCost?: number,
|
|
91
|
+
routerAddress?: Address,
|
|
92
|
+
) {
|
|
93
|
+
super(
|
|
94
|
+
network,
|
|
95
|
+
dexKey,
|
|
96
|
+
dexHelper,
|
|
97
|
+
isDynamicFees,
|
|
98
|
+
factoryAddress !== undefined
|
|
99
|
+
? factoryAddress
|
|
100
|
+
: InfusionConfig[dexKey][network].factoryAddress,
|
|
101
|
+
subgraphURL === ''
|
|
102
|
+
? undefined
|
|
103
|
+
: subgraphURL !== undefined
|
|
104
|
+
? subgraphURL
|
|
105
|
+
: InfusionConfig[dexKey][network].subgraphURL,
|
|
106
|
+
initCode !== undefined
|
|
107
|
+
? initCode
|
|
108
|
+
: InfusionConfig[dexKey][network].initCode,
|
|
109
|
+
feeCode !== undefined ? feeCode : InfusionConfig[dexKey][network].feeCode,
|
|
110
|
+
poolGasCost !== undefined
|
|
111
|
+
? poolGasCost
|
|
112
|
+
: InfusionConfig[dexKey][network].poolGasCost,
|
|
113
|
+
infusionPairIface,
|
|
114
|
+
Adapters[network] || undefined,
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
this.stableFee = InfusionConfig[dexKey][network].stableFee;
|
|
118
|
+
this.volatileFee = InfusionConfig[dexKey][network].volatileFee;
|
|
119
|
+
|
|
120
|
+
this.factory = new dexHelper.web3Provider.eth.Contract(
|
|
121
|
+
infusionFactoryABI as any,
|
|
122
|
+
factoryAddress !== undefined
|
|
123
|
+
? factoryAddress
|
|
124
|
+
: InfusionConfig[dexKey][network].factoryAddress,
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
this.router =
|
|
128
|
+
routerAddress !== undefined
|
|
129
|
+
? routerAddress
|
|
130
|
+
: InfusionConfig[dexKey][network].router || '';
|
|
131
|
+
|
|
132
|
+
this.feeFactor =
|
|
133
|
+
InfusionConfig[dexKey][network].feeFactor || this.feeFactor;
|
|
134
|
+
|
|
135
|
+
// this.exchangeRouterInterface = new Interface(infusionRouterABI);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async findInfusionPair(from: Token, to: Token, stable: boolean) {
|
|
139
|
+
if (from.address.toLowerCase() === to.address.toLowerCase()) return null;
|
|
140
|
+
const [token0, token1] =
|
|
141
|
+
from.address.toLowerCase() < to.address.toLowerCase()
|
|
142
|
+
? [from, to]
|
|
143
|
+
: [to, from];
|
|
144
|
+
|
|
145
|
+
const typePostfix = this.poolPostfix(stable);
|
|
146
|
+
const key = `${token0.address.toLowerCase()}-${token1.address.toLowerCase()}-${typePostfix}`;
|
|
147
|
+
let pair = this.pairs[key];
|
|
148
|
+
if (pair) return pair;
|
|
149
|
+
|
|
150
|
+
let exchange = await this.factory.methods
|
|
151
|
+
// Infusion has additional boolean parameter "StablePool"
|
|
152
|
+
// At first we look for uniswap-like volatile pool
|
|
153
|
+
.getPair(token0.address, token1.address, stable)
|
|
154
|
+
.call();
|
|
155
|
+
|
|
156
|
+
if (exchange === NULL_ADDRESS) {
|
|
157
|
+
pair = { token0, token1, stable };
|
|
158
|
+
} else {
|
|
159
|
+
pair = { token0, token1, exchange, stable };
|
|
160
|
+
}
|
|
161
|
+
this.pairs[key] = pair;
|
|
162
|
+
return pair;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async batchCatchUpPairs(pairs: [Token, Token][], blockNumber: number) {
|
|
166
|
+
if (!blockNumber) return;
|
|
167
|
+
const pairsToFetch: InfusionPair[] = [];
|
|
168
|
+
for (const _pair of pairs) {
|
|
169
|
+
for (const stable of [false, true]) {
|
|
170
|
+
const pair = await this.findInfusionPair(_pair[0], _pair[1], stable);
|
|
171
|
+
if (!(pair && pair.exchange)) continue;
|
|
172
|
+
if (!pair.pool) {
|
|
173
|
+
pairsToFetch.push(pair);
|
|
174
|
+
} else if (!pair.pool.getState(blockNumber)) {
|
|
175
|
+
pairsToFetch.push(pair);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (!pairsToFetch.length) return;
|
|
181
|
+
|
|
182
|
+
const reserves = await this.getManyPoolReserves(pairsToFetch, blockNumber);
|
|
183
|
+
|
|
184
|
+
if (reserves.length !== pairsToFetch.length) {
|
|
185
|
+
this.logger.error(
|
|
186
|
+
`Error_getManyPoolReserves didn't get any pool reserves`,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
for (let i = 0; i < pairsToFetch.length; i++) {
|
|
191
|
+
const pairState = reserves[i];
|
|
192
|
+
const pair = pairsToFetch[i];
|
|
193
|
+
if (!pair.pool) {
|
|
194
|
+
await this.addPool(
|
|
195
|
+
pair,
|
|
196
|
+
pairState.reserves0,
|
|
197
|
+
pairState.reserves1,
|
|
198
|
+
pairState.feeCode,
|
|
199
|
+
blockNumber,
|
|
200
|
+
);
|
|
201
|
+
} else pair.pool.setState(pairState, blockNumber);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async getManyPoolReserves(
|
|
206
|
+
pairs: InfusionPair[],
|
|
207
|
+
blockNumber: number,
|
|
208
|
+
): Promise<PoolState[]> {
|
|
209
|
+
try {
|
|
210
|
+
const multiCallFeeData = pairs.map(pair =>
|
|
211
|
+
this.getFeesMultiCallData(pair),
|
|
212
|
+
);
|
|
213
|
+
const calldata = pairs
|
|
214
|
+
.map((pair, i) => {
|
|
215
|
+
let calldata = [
|
|
216
|
+
{
|
|
217
|
+
target: pair.token0.address,
|
|
218
|
+
callData: erc20Iface.encodeFunctionData('balanceOf', [
|
|
219
|
+
pair.exchange!,
|
|
220
|
+
]),
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
target: pair.token1.address,
|
|
224
|
+
callData: erc20Iface.encodeFunctionData('balanceOf', [
|
|
225
|
+
pair.exchange!,
|
|
226
|
+
]),
|
|
227
|
+
},
|
|
228
|
+
];
|
|
229
|
+
if (this.isDynamicFees) calldata.push(multiCallFeeData[i]!.callEntry);
|
|
230
|
+
return calldata;
|
|
231
|
+
})
|
|
232
|
+
.flat();
|
|
233
|
+
|
|
234
|
+
const data: { returnData: any[] } =
|
|
235
|
+
await this.dexHelper.multiContract.methods
|
|
236
|
+
.aggregate(calldata)
|
|
237
|
+
.call({}, blockNumber);
|
|
238
|
+
|
|
239
|
+
const returnData = _.chunk(data.returnData, this.isDynamicFees ? 3 : 2);
|
|
240
|
+
|
|
241
|
+
return pairs.map((pair, i) => ({
|
|
242
|
+
reserves0: defaultAbiCoder
|
|
243
|
+
.decode(['uint256'], returnData[i][0])[0]
|
|
244
|
+
.toString(),
|
|
245
|
+
reserves1: defaultAbiCoder
|
|
246
|
+
.decode(['uint256'], returnData[i][1])[0]
|
|
247
|
+
.toString(),
|
|
248
|
+
feeCode: this.isDynamicFees
|
|
249
|
+
? multiCallFeeData[i]!.callDecoder(returnData[i][2])
|
|
250
|
+
: (pair.stable ? this.stableFee : this.volatileFee) || this.feeCode,
|
|
251
|
+
}));
|
|
252
|
+
} catch (e) {
|
|
253
|
+
this.logger.error(
|
|
254
|
+
`Error_getManyPoolReserves could not get reserves with error:`,
|
|
255
|
+
e,
|
|
256
|
+
);
|
|
257
|
+
return [];
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async getSellPrice(
|
|
262
|
+
priceParams: InfusionPoolOrderedParams,
|
|
263
|
+
srcAmount: bigint,
|
|
264
|
+
): Promise<bigint> {
|
|
265
|
+
return priceParams.stable
|
|
266
|
+
? InfusionStablePool.getSellPrice(priceParams, srcAmount, this.feeFactor)
|
|
267
|
+
: Uniswapv2ConstantProductPool.getSellPrice(
|
|
268
|
+
priceParams,
|
|
269
|
+
srcAmount,
|
|
270
|
+
this.feeFactor,
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async getBuyPrice(
|
|
275
|
+
priceParams: InfusionPoolOrderedParams,
|
|
276
|
+
srcAmount: bigint,
|
|
277
|
+
): Promise<bigint> {
|
|
278
|
+
if (priceParams.stable) throw new Error(`Buy not supported`);
|
|
279
|
+
return Uniswapv2ConstantProductPool.getBuyPrice(
|
|
280
|
+
priceParams,
|
|
281
|
+
srcAmount,
|
|
282
|
+
this.feeFactor,
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
async getPricesVolume(
|
|
287
|
+
_from: Token,
|
|
288
|
+
_to: Token,
|
|
289
|
+
amounts: bigint[],
|
|
290
|
+
side: SwapSide,
|
|
291
|
+
blockNumber: number,
|
|
292
|
+
// list of pool identifiers to use for pricing, if undefined use all pools
|
|
293
|
+
limitPools?: string[],
|
|
294
|
+
transferFees: TransferFeeParams = {
|
|
295
|
+
srcFee: 0,
|
|
296
|
+
destFee: 0,
|
|
297
|
+
srcDexFee: 0,
|
|
298
|
+
destDexFee: 0,
|
|
299
|
+
},
|
|
300
|
+
): Promise<ExchangePrices<UniswapV2Data> | null> {
|
|
301
|
+
try {
|
|
302
|
+
if (side === SwapSide.BUY) return null; // Buy side not implemented yet
|
|
303
|
+
const from = this.dexHelper.config.wrapETH(_from);
|
|
304
|
+
const to = this.dexHelper.config.wrapETH(_to);
|
|
305
|
+
|
|
306
|
+
if (from.address.toLowerCase() === to.address.toLowerCase()) {
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const tokenAddress = [
|
|
311
|
+
from.address.toLowerCase(),
|
|
312
|
+
to.address.toLowerCase(),
|
|
313
|
+
]
|
|
314
|
+
.sort((a, b) => (a > b ? 1 : -1))
|
|
315
|
+
.join('_');
|
|
316
|
+
|
|
317
|
+
await this.batchCatchUpPairs([[from, to]], blockNumber);
|
|
318
|
+
|
|
319
|
+
const resultPromises = [false, true].map(async stable => {
|
|
320
|
+
// We don't support fee on transfer for stable pools yet
|
|
321
|
+
if (
|
|
322
|
+
stable &&
|
|
323
|
+
(transferFees.srcFee !== 0 || transferFees.srcDexFee !== 0)
|
|
324
|
+
) {
|
|
325
|
+
return null;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const poolIdentifier =
|
|
329
|
+
`${this.dexKey}_${tokenAddress}` + this.poolPostfix(stable);
|
|
330
|
+
|
|
331
|
+
if (limitPools && limitPools.every(p => p !== poolIdentifier))
|
|
332
|
+
return null;
|
|
333
|
+
|
|
334
|
+
const isSell = side === SwapSide.SELL;
|
|
335
|
+
const pairParam = await this.getInfusionPairOrderedParams(
|
|
336
|
+
from,
|
|
337
|
+
to,
|
|
338
|
+
blockNumber,
|
|
339
|
+
stable,
|
|
340
|
+
transferFees.srcDexFee,
|
|
341
|
+
);
|
|
342
|
+
|
|
343
|
+
if (!pairParam) return null;
|
|
344
|
+
|
|
345
|
+
const unitAmount = getBigIntPow(
|
|
346
|
+
// @ts-expect-error Buy side is not implemented yet
|
|
347
|
+
side === SwapSide.BUY ? to.decimals : from.decimals,
|
|
348
|
+
);
|
|
349
|
+
|
|
350
|
+
const [unitVolumeWithFee, ...amountsWithFee] = applyTransferFee(
|
|
351
|
+
[unitAmount, ...amounts],
|
|
352
|
+
side,
|
|
353
|
+
isSell ? transferFees.srcFee : transferFees.destFee,
|
|
354
|
+
isSell ? SRC_TOKEN_PARASWAP_TRANSFERS : DEST_TOKEN_PARASWAP_TRANSFERS,
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
const unit =
|
|
358
|
+
// @ts-expect-error Buy side is not implemented yet
|
|
359
|
+
side === SwapSide.BUY
|
|
360
|
+
? await this.getBuyPricePath(unitVolumeWithFee, [pairParam])
|
|
361
|
+
: await this.getSellPricePath(unitVolumeWithFee, [pairParam]);
|
|
362
|
+
|
|
363
|
+
const prices =
|
|
364
|
+
// @ts-expect-error Buy side is not implemented yet
|
|
365
|
+
side === SwapSide.BUY
|
|
366
|
+
? await Promise.all(
|
|
367
|
+
amountsWithFee.map(amount =>
|
|
368
|
+
amount === 0n
|
|
369
|
+
? 0n
|
|
370
|
+
: this.getBuyPricePath(amount, [pairParam]),
|
|
371
|
+
),
|
|
372
|
+
)
|
|
373
|
+
: await Promise.all(
|
|
374
|
+
amountsWithFee.map(amount =>
|
|
375
|
+
amount === 0n
|
|
376
|
+
? 0n
|
|
377
|
+
: this.getSellPricePath(amount, [pairParam]),
|
|
378
|
+
),
|
|
379
|
+
);
|
|
380
|
+
|
|
381
|
+
const [unitOutWithFee, ...outputsWithFee] = applyTransferFee(
|
|
382
|
+
[unit, ...prices],
|
|
383
|
+
side,
|
|
384
|
+
// This part is confusing, because we treat differently SELL and BUY fees
|
|
385
|
+
// If Buy, we should apply transfer fee on srcToken on top of dexFee applied earlier
|
|
386
|
+
// But for Sell we should apply only one dexFee
|
|
387
|
+
isSell ? transferFees.destDexFee : transferFees.srcFee,
|
|
388
|
+
isSell ? this.DEST_TOKEN_DEX_TRANSFERS : SRC_TOKEN_PARASWAP_TRANSFERS,
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
return {
|
|
392
|
+
prices: outputsWithFee,
|
|
393
|
+
unit: unitOutWithFee,
|
|
394
|
+
data: {
|
|
395
|
+
router: this.router,
|
|
396
|
+
path: [from.address.toLowerCase(), to.address.toLowerCase()],
|
|
397
|
+
factory: this.factoryAddress,
|
|
398
|
+
initCode: this.initCode,
|
|
399
|
+
feeFactor: this.feeFactor,
|
|
400
|
+
isFeeTokenInRoute: Object.values(transferFees).some(f => f !== 0),
|
|
401
|
+
pools: [
|
|
402
|
+
{
|
|
403
|
+
address: pairParam.exchange,
|
|
404
|
+
fee: parseInt(pairParam.fee),
|
|
405
|
+
direction: pairParam.direction,
|
|
406
|
+
},
|
|
407
|
+
],
|
|
408
|
+
},
|
|
409
|
+
exchange: this.dexKey,
|
|
410
|
+
poolIdentifier,
|
|
411
|
+
gasCost: this.poolGasCost,
|
|
412
|
+
poolAddresses: [pairParam.exchange],
|
|
413
|
+
};
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
const resultPools = (await Promise.all(
|
|
417
|
+
resultPromises,
|
|
418
|
+
)) as ExchangePrices<UniswapV2Data>;
|
|
419
|
+
const resultPoolsFiltered = resultPools.filter(item => !!item); // filter null elements
|
|
420
|
+
return resultPoolsFiltered.length > 0 ? resultPoolsFiltered : null;
|
|
421
|
+
} catch (e) {
|
|
422
|
+
if (blockNumber === 0)
|
|
423
|
+
this.logger.error(
|
|
424
|
+
`Error_getPricesVolume: Aurelius block manager not yet instantiated`,
|
|
425
|
+
);
|
|
426
|
+
this.logger.error(`Error_getPrices:`, e);
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
async getTopPoolsForToken(
|
|
432
|
+
tokenAddress: Address,
|
|
433
|
+
count: number,
|
|
434
|
+
): Promise<PoolLiquidity[]> {
|
|
435
|
+
if (!this.subgraphURL) return [];
|
|
436
|
+
|
|
437
|
+
let stableFieldKey = 'isStable';
|
|
438
|
+
|
|
439
|
+
// if (this.dexKey.toLowerCase() === 'infusion') {
|
|
440
|
+
// stableFieldKey = 'stable';
|
|
441
|
+
// } else if (this.dexKey.toLowerCase() !== 'infusionv2') {
|
|
442
|
+
// stableFieldKey = 'isStable';
|
|
443
|
+
// }
|
|
444
|
+
|
|
445
|
+
const query = `query ($token: Bytes!, $count: Int) {
|
|
446
|
+
pools0: pairs(first: $count, orderBy: reserveUSD, orderDirection: desc, where: {token0: $token, reserve0_gt: 1, reserve1_gt: 1}) {
|
|
447
|
+
id
|
|
448
|
+
${stableFieldKey}
|
|
449
|
+
token0 {
|
|
450
|
+
id
|
|
451
|
+
decimals
|
|
452
|
+
}
|
|
453
|
+
token1 {
|
|
454
|
+
id
|
|
455
|
+
decimals
|
|
456
|
+
}
|
|
457
|
+
reserveUSD
|
|
458
|
+
}
|
|
459
|
+
pools1: pairs(first: $count, orderBy: reserveUSD, orderDirection: desc, where: {token1: $token, reserve0_gt: 1, reserve1_gt: 1}) {
|
|
460
|
+
id
|
|
461
|
+
${stableFieldKey}
|
|
462
|
+
token0 {
|
|
463
|
+
id
|
|
464
|
+
decimals
|
|
465
|
+
}
|
|
466
|
+
token1 {
|
|
467
|
+
id
|
|
468
|
+
decimals
|
|
469
|
+
}
|
|
470
|
+
reserveUSD
|
|
471
|
+
}
|
|
472
|
+
}`;
|
|
473
|
+
|
|
474
|
+
const { data } = await this.dexHelper.httpRequest.post(
|
|
475
|
+
this.subgraphURL,
|
|
476
|
+
{
|
|
477
|
+
query,
|
|
478
|
+
variables: { token: tokenAddress.toLowerCase(), count },
|
|
479
|
+
},
|
|
480
|
+
SUBGRAPH_TIMEOUT,
|
|
481
|
+
);
|
|
482
|
+
|
|
483
|
+
if (!(data && data.pools0 && data.pools1))
|
|
484
|
+
throw new Error("Couldn't fetch the pools from the subgraph");
|
|
485
|
+
const pools0 = _.map(data.pools0, pool => ({
|
|
486
|
+
exchange: this.dexKey,
|
|
487
|
+
stable: pool[stableFieldKey],
|
|
488
|
+
address: pool.id.toLowerCase(),
|
|
489
|
+
connectorTokens: [
|
|
490
|
+
{
|
|
491
|
+
address: pool.token1.id.toLowerCase(),
|
|
492
|
+
decimals: parseInt(pool.token1.decimals),
|
|
493
|
+
},
|
|
494
|
+
],
|
|
495
|
+
liquidityUSD: parseFloat(pool.reserveUSD),
|
|
496
|
+
}));
|
|
497
|
+
|
|
498
|
+
const pools1 = _.map(data.pools1, pool => ({
|
|
499
|
+
exchange: this.dexKey,
|
|
500
|
+
stable: pool[stableFieldKey],
|
|
501
|
+
address: pool.id.toLowerCase(),
|
|
502
|
+
connectorTokens: [
|
|
503
|
+
{
|
|
504
|
+
address: pool.token0.id.toLowerCase(),
|
|
505
|
+
decimals: parseInt(pool.token0.decimals),
|
|
506
|
+
},
|
|
507
|
+
],
|
|
508
|
+
liquidityUSD: parseFloat(pool.reserveUSD),
|
|
509
|
+
}));
|
|
510
|
+
|
|
511
|
+
return _.slice(
|
|
512
|
+
_.sortBy(_.concat(pools0, pools1), [pool => -1 * pool.liquidityUSD]),
|
|
513
|
+
0,
|
|
514
|
+
count,
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// Same as at uniswap-v2-pool.json, but extended with decimals and stable
|
|
519
|
+
async getInfusionPairOrderedParams(
|
|
520
|
+
from: Token,
|
|
521
|
+
to: Token,
|
|
522
|
+
blockNumber: number,
|
|
523
|
+
stable: boolean,
|
|
524
|
+
tokenDexTransferFee: number,
|
|
525
|
+
): Promise<InfusionPoolOrderedParams | null> {
|
|
526
|
+
const pair = await this.findInfusionPair(from, to, stable);
|
|
527
|
+
if (!(pair && pair.pool && pair.exchange)) return null;
|
|
528
|
+
const pairState = pair?.pool.getState(blockNumber);
|
|
529
|
+
|
|
530
|
+
if (!pairState) {
|
|
531
|
+
this.logger.error(
|
|
532
|
+
`Error_orderPairParams expected reserves, got none (maybe the pool doesn't exist) ${
|
|
533
|
+
from.symbol || from.address
|
|
534
|
+
} ${to.symbol || to.address}`,
|
|
535
|
+
);
|
|
536
|
+
return null;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
const fee = (pairState.feeCode + tokenDexTransferFee).toString();
|
|
540
|
+
const pairReversed =
|
|
541
|
+
pair.token1.address.toLowerCase() === from.address.toLowerCase();
|
|
542
|
+
if (pairReversed) {
|
|
543
|
+
return {
|
|
544
|
+
tokenIn: from.address,
|
|
545
|
+
tokenOut: to.address,
|
|
546
|
+
reservesIn: pairState.reserves1,
|
|
547
|
+
reservesOut: pairState.reserves0,
|
|
548
|
+
fee,
|
|
549
|
+
direction: false,
|
|
550
|
+
exchange: pair.exchange,
|
|
551
|
+
decimalsIn: from.decimals,
|
|
552
|
+
decimalsOut: to.decimals,
|
|
553
|
+
stable,
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
return {
|
|
557
|
+
tokenIn: from.address,
|
|
558
|
+
tokenOut: to.address,
|
|
559
|
+
reservesIn: pairState.reserves0,
|
|
560
|
+
reservesOut: pairState.reserves1,
|
|
561
|
+
fee,
|
|
562
|
+
direction: true,
|
|
563
|
+
exchange: pair.exchange,
|
|
564
|
+
decimalsIn: from.decimals,
|
|
565
|
+
decimalsOut: to.decimals,
|
|
566
|
+
stable,
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
async getPoolIdentifiers(
|
|
571
|
+
_from: Token,
|
|
572
|
+
_to: Token,
|
|
573
|
+
side: SwapSide,
|
|
574
|
+
blockNumber: number,
|
|
575
|
+
): Promise<string[]> {
|
|
576
|
+
if (side === SwapSide.BUY) return [];
|
|
577
|
+
|
|
578
|
+
const from = this.dexHelper.config.wrapETH(_from);
|
|
579
|
+
const to = this.dexHelper.config.wrapETH(_to);
|
|
580
|
+
|
|
581
|
+
if (from.address.toLowerCase() === to.address.toLowerCase()) {
|
|
582
|
+
return [];
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const tokenAddress = [from.address.toLowerCase(), to.address.toLowerCase()]
|
|
586
|
+
.sort((a, b) => (a > b ? 1 : -1))
|
|
587
|
+
.join('_');
|
|
588
|
+
|
|
589
|
+
const poolIdentifier = `${this.dexKey}_${tokenAddress}`;
|
|
590
|
+
const poolIdentifierUniswap = poolIdentifier + this.poolPostfix(false);
|
|
591
|
+
const poolIdentifierStable = poolIdentifier + this.poolPostfix(true);
|
|
592
|
+
return [poolIdentifierUniswap, poolIdentifierStable];
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
poolPostfix(stable: boolean) {
|
|
596
|
+
return stable ? 'S' : 'V';
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
async getSimpleParam(
|
|
600
|
+
src: Address,
|
|
601
|
+
dest: Address,
|
|
602
|
+
srcAmount: NumberAsString,
|
|
603
|
+
destAmount: NumberAsString,
|
|
604
|
+
data: UniswapData,
|
|
605
|
+
side: SwapSide,
|
|
606
|
+
): Promise<SimpleExchangeParam> {
|
|
607
|
+
if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
|
|
608
|
+
|
|
609
|
+
let routerMethod: any;
|
|
610
|
+
let routerArgs: InfusionParam;
|
|
611
|
+
const stable = isStablePair(this.network, src, dest);
|
|
612
|
+
|
|
613
|
+
const from = isETHAddress(src)
|
|
614
|
+
? this.dexHelper.config.data.wrappedNativeTokenAddress
|
|
615
|
+
: src;
|
|
616
|
+
const to = isETHAddress(dest)
|
|
617
|
+
? this.dexHelper.config.data.wrappedNativeTokenAddress
|
|
618
|
+
: dest;
|
|
619
|
+
|
|
620
|
+
routerMethod = isETHAddress(src)
|
|
621
|
+
? InfusionRouterFunctions.sellExactEth
|
|
622
|
+
: InfusionRouterFunctions.swapExactIn;
|
|
623
|
+
routerMethod = isETHAddress(dest)
|
|
624
|
+
? InfusionRouterFunctions.sellExactToken
|
|
625
|
+
: routerMethod;
|
|
626
|
+
|
|
627
|
+
routerArgs = isETHAddress(src)
|
|
628
|
+
? [
|
|
629
|
+
destAmount,
|
|
630
|
+
[{ from, to, stable }],
|
|
631
|
+
this.augustusAddress,
|
|
632
|
+
Math.floor(new Date().getTime()) + 120,
|
|
633
|
+
]
|
|
634
|
+
: [
|
|
635
|
+
srcAmount,
|
|
636
|
+
destAmount,
|
|
637
|
+
[{ from, to, stable }],
|
|
638
|
+
this.augustusAddress,
|
|
639
|
+
Math.floor(new Date().getTime()) + 120,
|
|
640
|
+
];
|
|
641
|
+
|
|
642
|
+
// console.log('routerMethod', routerMethod, 'routerArgs', routerArgs);
|
|
643
|
+
const swapData = new Interface(infusionRouterABI).encodeFunctionData(
|
|
644
|
+
routerMethod,
|
|
645
|
+
routerArgs as InfusionParam,
|
|
646
|
+
);
|
|
647
|
+
return this.buildSimpleParamWithoutWETHConversion(
|
|
648
|
+
src,
|
|
649
|
+
srcAmount,
|
|
650
|
+
dest,
|
|
651
|
+
destAmount,
|
|
652
|
+
swapData,
|
|
653
|
+
data.router,
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
getAdapterParam(
|
|
658
|
+
srcToken: Address,
|
|
659
|
+
destToken: Address,
|
|
660
|
+
srcAmount: NumberAsString,
|
|
661
|
+
toAmount: NumberAsString, // required for buy case
|
|
662
|
+
data: InfusionData,
|
|
663
|
+
side: SwapSide,
|
|
664
|
+
): AdapterExchangeParam {
|
|
665
|
+
return {
|
|
666
|
+
targetExchange: data.router,
|
|
667
|
+
payload: '',
|
|
668
|
+
networkFee: '0',
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
getDexParam(
|
|
673
|
+
src: Address,
|
|
674
|
+
dest: Address,
|
|
675
|
+
srcAmount: NumberAsString,
|
|
676
|
+
destAmount: NumberAsString,
|
|
677
|
+
recipient: Address,
|
|
678
|
+
data: InfusionData,
|
|
679
|
+
side: SwapSide,
|
|
680
|
+
): DexExchangeParam {
|
|
681
|
+
if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
|
|
682
|
+
|
|
683
|
+
let routerMethod: any;
|
|
684
|
+
let routerArgs: InfusionParam;
|
|
685
|
+
const stable = isStablePair(this.network, src, dest);
|
|
686
|
+
|
|
687
|
+
const from = isETHAddress(src)
|
|
688
|
+
? this.dexHelper.config.data.wrappedNativeTokenAddress
|
|
689
|
+
: src;
|
|
690
|
+
const to = isETHAddress(dest)
|
|
691
|
+
? this.dexHelper.config.data.wrappedNativeTokenAddress
|
|
692
|
+
: dest;
|
|
693
|
+
|
|
694
|
+
routerMethod = isETHAddress(src)
|
|
695
|
+
? InfusionRouterFunctions.sellExactEth
|
|
696
|
+
: InfusionRouterFunctions.swapExactIn;
|
|
697
|
+
routerMethod = isETHAddress(dest)
|
|
698
|
+
? InfusionRouterFunctions.sellExactToken
|
|
699
|
+
: routerMethod;
|
|
700
|
+
|
|
701
|
+
routerArgs = isETHAddress(src)
|
|
702
|
+
? [
|
|
703
|
+
destAmount,
|
|
704
|
+
[{ from, to, stable }],
|
|
705
|
+
recipient,
|
|
706
|
+
Math.floor(new Date().getTime()) + 120,
|
|
707
|
+
]
|
|
708
|
+
: [
|
|
709
|
+
srcAmount,
|
|
710
|
+
destAmount,
|
|
711
|
+
[{ from, to, stable }],
|
|
712
|
+
recipient,
|
|
713
|
+
Math.floor(new Date().getTime()) + 120,
|
|
714
|
+
];
|
|
715
|
+
|
|
716
|
+
const exchangeData = new Interface(infusionRouterABI).encodeFunctionData(
|
|
717
|
+
routerMethod,
|
|
718
|
+
routerArgs as InfusionParam,
|
|
719
|
+
);
|
|
720
|
+
|
|
721
|
+
return {
|
|
722
|
+
needWrapNative: this.needWrapNative,
|
|
723
|
+
dexFuncHasRecipient: true,
|
|
724
|
+
exchangeData,
|
|
725
|
+
targetExchange: data.router,
|
|
726
|
+
returnAmountPos: undefined,
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
protected getFeesMultiCallData(pair: InfusionPair) {
|
|
731
|
+
const callEntry = {
|
|
732
|
+
target: this.factoryAddress,
|
|
733
|
+
callData: velodromeFactoryIface.encodeFunctionData('getFee', [
|
|
734
|
+
pair.stable,
|
|
735
|
+
]),
|
|
736
|
+
};
|
|
737
|
+
const callDecoder = (values: any[]) =>
|
|
738
|
+
parseInt(
|
|
739
|
+
velodromeFactoryIface
|
|
740
|
+
.decodeFunctionResult('getFee', values)[0]
|
|
741
|
+
.toString(),
|
|
742
|
+
);
|
|
743
|
+
|
|
744
|
+
return {
|
|
745
|
+
callEntry,
|
|
746
|
+
callDecoder,
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
}
|