-
Notifications
You must be signed in to change notification settings - Fork 388
/
v3-heuristic-gas-model.ts
448 lines (395 loc) · 12.9 KB
/
v3-heuristic-gas-model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import { BigNumber } from '@ethersproject/bignumber';
import { Token } from '@uniswap/sdk-core';
import { FeeAmount, Pool } from '@uniswap/v3-sdk';
import _ from 'lodash';
import {
OptimismGasData,
SwapOptions,
WRAPPED_NATIVE_CURRENCY,
} from '../../../..';
import { IV3PoolProvider } from '../../../../providers/v3/pool-provider';
import { ChainId } from '../../../../util';
import { CurrencyAmount } from '../../../../util/amounts';
import { log } from '../../../../util/log';
import {
buildSwapMethodParameters,
buildTrade,
} from '../../../../util/methodParameters';
import { V3RouteWithValidQuote } from '../../entities/route-with-valid-quote';
import {
IGasModel,
IV3GasModelFactory,
usdGasTokensByChain,
} from '../gas-model';
import { BASE_SWAP_COST, COST_PER_HOP, COST_PER_INIT_TICK } from './gas-costs';
// Cost for crossing an uninitialized tick.
const COST_PER_UNINIT_TICK = BigNumber.from(0);
/**
* Computes a gas estimate for a V3 swap using heuristics.
* Considers number of hops in the route, number of ticks crossed
* and the typical base cost for a swap.
*
* We get the number of ticks crossed in a swap from the QuoterV2
* contract.
*
* We compute gas estimates off-chain because
* 1/ Calling eth_estimateGas for a swaps requires the caller to have
* the full balance token being swapped, and approvals.
* 2/ Tracking gas used using a wrapper contract is not accurate with Multicall
* due to EIP-2929. We would have to make a request for every swap we wanted to estimate.
* 3/ For V2 we simulate all our swaps off-chain so have no way to track gas used.
*
* @export
* @class V3HeuristicGasModelFactory
*/
export class V3HeuristicGasModelFactory extends IV3GasModelFactory {
constructor() {
super();
}
public async buildGasModel(
chainId: ChainId,
gasPriceWei: BigNumber,
poolProvider: IV3PoolProvider,
token: Token,
swapConfig?: SwapOptions,
gasData?: OptimismGasData
// this is the quoteToken
): Promise<IGasModel<V3RouteWithValidQuote>> {
// If our quote token is WETH, we don't need to convert our gas use to be in terms
// of the quote token in order to produce a gas adjusted amount.
// We do return a gas use in USD however, so we still convert to usd.
const nativeCurrency = WRAPPED_NATIVE_CURRENCY[chainId]!;
if (token.equals(nativeCurrency)) {
const usdPool: Pool = await this.getHighestLiquidityUSDPool(
chainId,
poolProvider
);
const estimateGasCost = (
routeWithValidQuote: V3RouteWithValidQuote
): {
gasEstimate: BigNumber;
gasCostInToken: CurrencyAmount;
gasCostInUSD: CurrencyAmount;
initTicksCrossed: BigNumber;
gasUseL1: BigNumber;
gasCostL1: BigNumber;
} => {
const {
totalGasCostNativeCurrency,
totalInitializedTicksCrossed,
l1GasUsed,
totalGasUse,
l1Fee,
} = this.estimateGas(
routeWithValidQuote,
gasPriceWei,
chainId,
swapConfig,
gasData
);
const token0 = usdPool.token0.address == nativeCurrency.address;
const nativeTokenPrice = token0
? usdPool.token0Price
: usdPool.token1Price;
const gasCostInTermsOfUSD: CurrencyAmount = nativeTokenPrice.quote(
totalGasCostNativeCurrency
) as CurrencyAmount;
return {
gasEstimate: totalGasUse,
gasCostInToken: totalGasCostNativeCurrency,
gasCostInUSD: gasCostInTermsOfUSD,
initTicksCrossed: totalInitializedTicksCrossed,
gasUseL1: l1GasUsed,
gasCostL1: l1Fee,
};
};
return {
estimateGasCost,
};
}
// If the quote token is not in the native currency, we convert the gas cost to be in terms of the quote token.
// We do this by getting the highest liquidity <quoteToken>/<nativeCurrency> pool. eg. <quoteToken>/ETH pool.
const nativePool: Pool | null = await this.getHighestLiquidityNativePool(
chainId,
token,
poolProvider
);
const usdPool: Pool = await this.getHighestLiquidityUSDPool(
chainId,
poolProvider
);
const usdToken =
usdPool.token0.address == nativeCurrency.address
? usdPool.token1
: usdPool.token0;
const estimateGasCost = (
routeWithValidQuote: V3RouteWithValidQuote
): {
gasEstimate: BigNumber;
gasCostInToken: CurrencyAmount;
gasCostInUSD: CurrencyAmount;
initTicksCrossed: BigNumber;
gasUseL1: BigNumber;
gasCostL1: BigNumber;
} => {
const {
totalGasCostNativeCurrency,
totalInitializedTicksCrossed,
l1GasUsed,
totalGasUse,
l1Fee,
} = this.estimateGas(
routeWithValidQuote,
gasPriceWei,
chainId,
swapConfig,
gasData
);
if (!nativePool) {
log.info(
`Unable to find ${nativeCurrency.symbol} pool with the quote token, ${token.symbol} to produce gas adjusted costs. Route will not account for gas.`
);
return {
gasEstimate: totalGasUse,
gasCostInToken: CurrencyAmount.fromRawAmount(token, 0),
gasCostInUSD: CurrencyAmount.fromRawAmount(usdToken, 0),
initTicksCrossed: totalInitializedTicksCrossed,
gasUseL1: l1GasUsed,
gasCostL1: l1Fee,
};
}
const token0 = nativePool.token0.address == nativeCurrency.address;
// returns mid price in terms of the native currency (the ratio of quoteToken/nativeToken)
const nativeTokenPrice = token0
? nativePool.token0Price
: nativePool.token1Price;
let gasCostInTermsOfQuoteToken: CurrencyAmount;
try {
// native token is base currency
gasCostInTermsOfQuoteToken = nativeTokenPrice.quote(
totalGasCostNativeCurrency
) as CurrencyAmount;
} catch (err) {
log.info(
{
nativeTokenPriceBase: nativeTokenPrice.baseCurrency,
nativeTokenPriceQuote: nativeTokenPrice.quoteCurrency,
gasCostInEth: totalGasCostNativeCurrency.currency,
},
'Debug eth price token issue'
);
throw err;
}
// true if token0 is the native currency
const token0USDPool = usdPool.token0.address == nativeCurrency.address;
// gets the mid price of the pool in terms of the native token
const nativeTokenPriceUSDPool = token0USDPool
? usdPool.token0Price
: usdPool.token1Price;
let gasCostInTermsOfUSD: CurrencyAmount;
try {
gasCostInTermsOfUSD = nativeTokenPriceUSDPool.quote(
totalGasCostNativeCurrency
) as CurrencyAmount;
} catch (err) {
log.info(
{
usdT1: usdPool.token0.symbol,
usdT2: usdPool.token1.symbol,
gasCostInNativeToken: totalGasCostNativeCurrency.currency.symbol,
},
'Failed to compute USD gas price'
);
throw err;
}
return {
gasEstimate: totalGasUse,
gasCostInToken: gasCostInTermsOfQuoteToken,
gasCostInUSD: gasCostInTermsOfUSD!,
initTicksCrossed: totalInitializedTicksCrossed,
gasUseL1: l1GasUsed,
gasCostL1: l1Fee,
};
};
return {
estimateGasCost: estimateGasCost.bind(this),
};
}
private estimateGas(
routeWithValidQuote: V3RouteWithValidQuote,
gasPriceWei: BigNumber,
chainId: ChainId,
swapConfig?: SwapOptions,
gasData?: OptimismGasData
) {
const totalInitializedTicksCrossed = BigNumber.from(
Math.max(1, _.sum(routeWithValidQuote.initializedTicksCrossedList))
);
const totalHops = BigNumber.from(routeWithValidQuote.route.pools.length);
const hopsGasUse = COST_PER_HOP(chainId).mul(totalHops);
const tickGasUse = COST_PER_INIT_TICK(chainId).mul(
totalInitializedTicksCrossed
);
const uninitializedTickGasUse = COST_PER_UNINIT_TICK.mul(0);
let l1GasUsed = BigNumber.from(0);
let l1Fee = BigNumber.from(0);
if (chainId == ChainId.OPTIMISM || chainId == ChainId.OPTIMISTIC_KOVAN) {
// account for the L1 security fee
if (!swapConfig) {
log.info('Skipping l1 security fee calculation');
} else {
[l1GasUsed, l1Fee] = this.calculateL1SecurityFee(
routeWithValidQuote,
swapConfig,
gasData!
);
}
}
const baseGasUse = BASE_SWAP_COST(chainId)
.add(hopsGasUse)
.add(tickGasUse)
.add(uninitializedTickGasUse);
const baseGasCostWei = gasPriceWei.mul(baseGasUse);
const totalGasUse = l1GasUsed.add(baseGasUse);
// total gas cost including l1 security fee if on optimism
const totalGasCostWei = l1Fee.add(baseGasCostWei);
const wrappedCurrency = WRAPPED_NATIVE_CURRENCY[chainId]!;
const totalGasCostNativeCurrency = CurrencyAmount.fromRawAmount(
wrappedCurrency,
totalGasCostWei.toString()
);
return {
totalGasCostNativeCurrency,
totalInitializedTicksCrossed,
l1GasUsed,
l1Fee,
totalGasUse,
};
}
private async getHighestLiquidityNativePool(
chainId: ChainId,
token: Token,
poolProvider: IV3PoolProvider
): Promise<Pool | null> {
const nativeCurrency = WRAPPED_NATIVE_CURRENCY[chainId]!;
const nativePools = _([FeeAmount.HIGH, FeeAmount.MEDIUM, FeeAmount.LOW])
.map<[Token, Token, FeeAmount]>((feeAmount) => {
return [nativeCurrency, token, feeAmount];
})
.value();
const poolAccessor = await poolProvider.getPools(nativePools);
const pools = _([FeeAmount.HIGH, FeeAmount.MEDIUM, FeeAmount.LOW])
.map((feeAmount) => {
return poolAccessor.getPool(nativeCurrency, token, feeAmount);
})
.compact()
.value();
if (pools.length == 0) {
log.error(
{ pools },
`Could not find a ${nativeCurrency.symbol} pool with ${token.symbol} for computing gas costs.`
);
return null;
}
const maxPool = _.maxBy(pools, (pool) => pool.liquidity) as Pool;
return maxPool;
}
private async getHighestLiquidityUSDPool(
chainId: ChainId,
poolProvider: IV3PoolProvider
): Promise<Pool> {
const usdTokens = usdGasTokensByChain[chainId];
const wrappedCurrency = WRAPPED_NATIVE_CURRENCY[chainId]!;
if (!usdTokens) {
throw new Error(
`Could not find a USD token for computing gas costs on ${chainId}`
);
}
const usdPools = _([
FeeAmount.HIGH,
FeeAmount.MEDIUM,
FeeAmount.LOW,
FeeAmount.LOWEST,
])
.flatMap((feeAmount) => {
return _.map<Token, [Token, Token, FeeAmount]>(
usdTokens,
(usdToken) => [wrappedCurrency, usdToken, feeAmount]
);
})
.value();
const poolAccessor = await poolProvider.getPools(usdPools);
const pools = _([
FeeAmount.HIGH,
FeeAmount.MEDIUM,
FeeAmount.LOW,
FeeAmount.LOWEST,
])
.flatMap((feeAmount) => {
const pools = [];
for (const usdToken of usdTokens) {
const pool = poolAccessor.getPool(
wrappedCurrency,
usdToken,
feeAmount
);
if (pool) {
pools.push(pool);
}
}
return pools;
})
.compact()
.value();
if (pools.length == 0) {
log.error(
{ pools },
`Could not find a USD/${wrappedCurrency.symbol} pool for computing gas costs.`
);
throw new Error(
`Can't find USD/${wrappedCurrency.symbol} pool for computing gas costs.`
);
}
const maxPool = _.maxBy(pools, (pool) => pool.liquidity) as Pool;
return maxPool;
}
private calculateL1SecurityFee(
route: V3RouteWithValidQuote,
swapConfig: SwapOptions,
gasData: OptimismGasData
): [BigNumber, BigNumber] {
const { l1BaseFee, scalar, decimals, overhead } = gasData;
// build trade for swap calldata
const trade = buildTrade(
route.amount.currency,
route.quote.currency,
route.tradeType,
[route]
);
const data = buildSwapMethodParameters(trade, swapConfig).calldata;
const l1GasUsed = this.getL1GasUsed(data, overhead);
const l1Fee = l1GasUsed.mul(l1BaseFee);
const unscaled = l1Fee.mul(scalar);
// scaled = unscaled / (10 ** decimals)
const scaledConversion = BigNumber.from(10).pow(decimals);
const scaled = unscaled.div(scaledConversion);
return [l1GasUsed, scaled];
}
private getL1GasUsed(data: string, overhead: BigNumber): BigNumber {
// data is hex encoded
const dataArr: string[] = data.slice(2).match(/.{1,2}/g)!;
const numBytes = dataArr.length;
let count = 0;
for (let i = 0; i < numBytes; i += 1) {
const byte = parseInt(dataArr[i]!, 16);
if (byte == 0) {
count += 4;
} else {
count += 16;
}
}
const unsigned = overhead.add(count);
const signedConversion = 68 * 16;
return unsigned.add(signedConversion);
}
}