-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtradeQuoter.ts
532 lines (461 loc) · 17.4 KB
/
tradeQuoter.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
Copyright 2021 Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict';
import BigDecimal from 'js-big-decimal';
import { BigNumber, FixedNumber, utils as ethersUtils } from 'ethers';
import type TradeModuleWrapper from '@src/wrappers/set-protocol-v2/TradeModuleWrapper';
import {
CoinGeckoCoinPrices,
TradeQuoteOptions,
SwapQuoteOptions,
TradeQuote,
SwapQuote,
ZeroExApiUrls
} from '../../types/index';
import {
CoinGeckoDataService,
USD_CURRENCY_CODE
} from './coingecko';
import { Address } from '@setprotocol/set-protocol-v2/utils/types';
import { GasOracleService } from './gasOracle';
import { ZeroExTradeQuoter } from './zeroex';
export const ZERO_EX_ADAPTER_NAME = 'ZeroExApiAdapterV5';
const SCALE = BigNumber.from(10).pow(18);
/**
* @title TradeQuote
* @author Set Protocol
*
* A utility library to generate trade quotes for token pairs associated with a
* set for Ethereum and Polygon chains. Uses 0xAPI to get quote and requires a valid
* 0x api key.
*/
export class TradeQuoter {
private tradeQuoteGasBuffer: number = 5;
private feeRecipient: Address = '0xD3D555Bb655AcBA9452bfC6D7cEa8cC7b3628C55';
private feePercentage: number = 0;
private isFirmQuote: boolean = true;
private slippagePercentage: number = 2;
private excludedSources: string[] = [];
private zeroExApiKey: string;
private zeroExApiUrls: ZeroExApiUrls;
constructor(zeroExApiKey?: string, zeroExApiUrls?: ZeroExApiUrls) {
this.zeroExApiKey = zeroExApiKey;
this.zeroExApiUrls = zeroExApiUrls;
}
/**
* Generates a trade quote for a token pair in a SetToken. This method is useful for
* operations like rebalancing where the ratio of existing SetToken components is modified.
*
* @param options TradeQuoteOptions: options / config to generate the trade quote
* @return TradeQuote: trade quote object
*/
async generateQuoteForTrade(options: TradeQuoteOptions): Promise<TradeQuote> {
const chainId = options.chainId;
const feePercentage = options.feePercentage || this.feePercentage;
const isFirmQuote = (options.isFirmQuote === false) ? false : this.isFirmQuote;
const slippagePercentage = options.slippagePercentage || this.slippagePercentage;
const feeRecipient = options.feeRecipient || this.feeRecipient;
const excludedSources = options.excludedSources || this.excludedSources;
const exchangeAdapterName = ZERO_EX_ADAPTER_NAME;
const {
fromTokenAddress,
toTokenAddress,
fromAddress,
} = this.sanitizeAddress(options.fromToken, options.toToken, options.fromAddress);
const amount = this.sanitizeAmount(options.rawAmount, options.fromTokenDecimals);
const setOnChainDetails = await options.setToken.fetchSetDetailsAsync(
fromAddress, [fromTokenAddress, toTokenAddress]
);
const fromTokenRequestAmount = this.calculateFromTokenAmount(
setOnChainDetails,
fromTokenAddress,
amount
);
const {
fromTokenAmount,
fromUnits,
toTokenAmount,
toUnits,
calldata,
} = await this.fetchZeroExQuoteForTradeModule( // fetchQuote (and switch...)
fromTokenAddress,
toTokenAddress,
fromTokenRequestAmount,
setOnChainDetails.manager,
(setOnChainDetails as any).totalSupply, // Typings incorrect,
chainId,
isFirmQuote,
slippagePercentage,
feeRecipient,
excludedSources,
feePercentage,
);
// Sanity check response from quote APIs
this.validateQuoteValues(
setOnChainDetails,
fromTokenAddress,
toTokenAddress,
fromUnits,
toUnits
);
// We should use the zeroex estimates plus a constant...
const gas = await this.estimateGasCost(
options.tradeModule,
fromTokenAddress,
fromUnits,
toTokenAddress,
toUnits,
exchangeAdapterName,
fromAddress,
calldata,
setOnChainDetails.manager
);
const coinGecko = new CoinGeckoDataService(chainId);
const coinPrices = await coinGecko.fetchCoinPrices({
contractAddresses: [this.chainCurrencyAddress(chainId), fromTokenAddress, toTokenAddress],
vsCurrencies: [ USD_CURRENCY_CODE, USD_CURRENCY_CODE, USD_CURRENCY_CODE ],
});
if (!options.gasPrice) {
const gasOracle = new GasOracleService(chainId);
options.gasPrice = await gasOracle.fetchGasPrice();
}
return {
from: fromAddress,
fromTokenAddress,
toTokenAddress,
exchangeAdapterName,
calldata,
gas: gas.toString(),
gasPrice: options.gasPrice.toString(),
slippagePercentage: this.formatAsPercentage(slippagePercentage),
fromTokenAmount: fromUnits.toString(),
toTokenAmount: toUnits.toString(),
display: {
inputAmountRaw: options.rawAmount.toString(),
inputAmount: amount.toString(),
quoteAmount: fromTokenRequestAmount.toString(),
fromTokenDisplayAmount: this.tokenDisplayAmount(fromTokenAmount, options.fromTokenDecimals),
toTokenDisplayAmount: this.tokenDisplayAmount(toTokenAmount, options.toTokenDecimals),
fromTokenPriceUsd: this.tokenPriceUsd(
fromTokenAmount,
fromTokenAddress,
options.fromTokenDecimals,
coinPrices
),
toTokenPriceUsd: this.tokenPriceUsd(
toTokenAmount,
toTokenAddress,
options.toTokenDecimals,
coinPrices
),
gasCostsUsd: this.gasCostsUsd(options.gasPrice, gas, coinPrices, chainId),
gasCostsChainCurrency: this.gasCostsChainCurrency(options.gasPrice, gas, chainId),
feePercentage: this.formatAsPercentage(feePercentage),
slippage: this.calculateSlippage(
fromTokenAmount,
toTokenAmount,
fromTokenAddress,
toTokenAddress,
options.fromTokenDecimals,
options.toTokenDecimals,
coinPrices
),
},
};
}
/**
* Generates a ZeroEx swap quote for any token pair. This method is useful for operations
* like ExchangeIssuance where a liquid token or native chain currency is used to acquire a
* SetToken component that will be supplied to a SetToken issuance flow.
*
* @param options SwapQuoteOptions: options / config to generate the swap quote
* @return SwapQuote: swap quote object
*/
async generateQuoteForSwap(options: SwapQuoteOptions): Promise<SwapQuote> {
const chainId = options.chainId;
const useBuyAmount = options.useBuyAmount;
const feeRecipient = options.feeRecipient || this.feeRecipient;
const excludedSources = options.excludedSources || this.excludedSources;
const isFirmQuote = (options.isFirmQuote === false)
? false
: this.isFirmQuote;
const slippagePercentage = (options.slippagePercentage !== undefined)
? options.slippagePercentage
: this.slippagePercentage;
const feePercentage = (options.feePercentage !== undefined)
? options.feePercentage
: this.feePercentage;
const {
fromTokenAddress,
toTokenAddress,
fromAddress,
} = this.sanitizeAddress(options.fromToken, options.toToken, options.fromAddress);
const amount = BigNumber.from(options.rawAmount);
const setManager = await options.setToken.getManagerAddressAsync(fromAddress);
const zeroEx = new ZeroExTradeQuoter({
chainId: chainId,
zeroExApiKey: this.zeroExApiKey,
zeroExApiUrls: this.zeroExApiUrls,
});
const quote = await zeroEx.fetchTradeQuote(
fromTokenAddress,
toTokenAddress,
amount,
useBuyAmount,
setManager,
isFirmQuote,
(slippagePercentage / 100),
feeRecipient,
excludedSources,
(feePercentage / 100)
);
return {
from: fromAddress,
fromTokenAddress,
toTokenAddress,
calldata: quote.calldata,
gas: quote.gas.toString(),
gasPrice: options.gasPrice.toString(),
slippagePercentage: this.formatAsPercentage(slippagePercentage),
fromTokenAmount: quote.sellAmount.toString(),
toTokenAmount: quote.buyAmount.toString(),
_quote: quote._quote,
};
}
private sanitizeAddress(fromToken: Address, toToken: Address, fromAddress: Address) {
return {
fromTokenAddress: fromToken.toLowerCase(),
toTokenAddress: toToken.toLowerCase(),
fromAddress: fromAddress.toLowerCase(),
};
}
private sanitizeAmount(rawAmount: string, decimals: number): BigNumber {
return ethersUtils.parseUnits(rawAmount, decimals);
}
private async fetchZeroExQuoteForTradeModule(
fromTokenAddress: Address,
toTokenAddress: Address,
fromTokenRequestAmount: BigNumber,
manager: Address,
setTotalSupply: BigNumber,
chainId: number,
isFirmQuote: boolean,
slippagePercentage: number,
feeRecipient: Address,
excludedSources: string[],
feePercentage: number,
) {
const zeroEx = new ZeroExTradeQuoter({
chainId: chainId,
zeroExApiKey: this.zeroExApiKey,
zeroExApiUrls: this.zeroExApiUrls,
});
const quote = await zeroEx.fetchTradeQuote(
fromTokenAddress,
toTokenAddress,
fromTokenRequestAmount,
false, // Input amount is `sellAmount` of fromToken
manager,
isFirmQuote,
(slippagePercentage / 100),
feeRecipient,
excludedSources,
(feePercentage / 100)
);
const fromTokenAmount = quote.sellAmount;
// Convert to BigDecimal to get ceiling in fromUnits calculation
// This is necessary to derive the trade amount ZeroEx expects when scaling is
// done in the TradeModule contract. (ethers.FixedNumber does not work for this case)
const fromTokenAmountBD = new BigDecimal(fromTokenAmount.toString());
const scaleBD = new BigDecimal(SCALE.toString());
const setTotalSupplyBD = new BigDecimal(setTotalSupply.toString());
const fromUnitsBD = fromTokenAmountBD.multiply(scaleBD).divide(setTotalSupplyBD, 10).ceil();
const fromUnits = BigNumber.from(fromUnitsBD.getValue());
const toTokenAmount = quote.buyAmount;
// BigNumber does not do fixed point math & FixedNumber underflows w/ numbers less than 1
// Multiply the slippage by a factor and divide the end result by same...
const percentMultiplier = 1000;
const slippageAndFee = slippagePercentage + feePercentage;
const slippageToleranceBN = Math.floor(percentMultiplier * this.outputSlippageTolerance(slippageAndFee));
const toTokenAmountMinusSlippage = toTokenAmount.mul(slippageToleranceBN).div(percentMultiplier);
const toUnits = toTokenAmountMinusSlippage.mul(SCALE).div(setTotalSupply);
return {
fromTokenAmount,
fromUnits,
toTokenAmount,
toUnits,
calldata: quote.calldata,
};
}
private validateQuoteValues(
setOnChainDetails: any,
fromTokenAddress: Address,
toTokenAddress: Address,
quoteFromRemainingUnits: BigNumber,
quoteToUnits: BigNumber
) {
// fromToken
const positionForFromToken = setOnChainDetails
.positions
.find((p: any) => p.component.toLowerCase() === fromTokenAddress.toLowerCase());
const currentPositionUnits = BigNumber.from(positionForFromToken.unit);
const remainingPositionUnits = currentPositionUnits.sub(quoteFromRemainingUnits);
const remainingPositionUnitsTooSmall = remainingPositionUnits.gt(0) && remainingPositionUnits.lt(50);
if (remainingPositionUnitsTooSmall) {
throw new Error('Remaining units too small, incorrectly attempting max');
}
// toToken
const positionForToToken = setOnChainDetails
.positions
.find((p: any) => p.component.toLowerCase() === toTokenAddress.toLowerCase());
const newToPositionUnits = (positionForToToken !== undefined)
? positionForToToken.unit.add(quoteToUnits)
: quoteToUnits;
const newToUnitsTooSmall = newToPositionUnits.gt(0) && newToPositionUnits.lt(50);
if (newToUnitsTooSmall) {
throw new Error('Receive units too small');
}
}
private calculateFromTokenAmount(
setOnChainDetails: any,
fromTokenAddress: Address,
amount: BigNumber
): BigNumber {
const positionForFromToken = setOnChainDetails
.positions
.find((p: any) => p.component.toLowerCase() === fromTokenAddress.toLowerCase());
if (positionForFromToken === undefined) {
throw new Error('Invalid fromToken input');
}
const totalSupply = setOnChainDetails.totalSupply;
const impliedMaxNotional = positionForFromToken.unit.mul(totalSupply).div(SCALE);
const isGreaterThanMax = amount.gt(impliedMaxNotional);
const isMax = amount.eq(impliedMaxNotional);
if (isGreaterThanMax) {
throw new Error('Amount is greater than quantity of component in Set');
} else if (isMax) {
return impliedMaxNotional.toString();
} else {
const amountMulScaleOverTotalSupply = amount.mul(SCALE).div(totalSupply);
return amountMulScaleOverTotalSupply.mul(totalSupply).div(SCALE);
}
}
private tokenDisplayAmount(amount: BigNumber, decimals: number): string {
return this.normalizeTokenAmount(amount, decimals).toString();
}
private chainCurrencyAddress(chainId: number): Address {
switch (chainId) {
case 1: return '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'; // WETH
case 10: return '0x4200000000000000000000000000000000000006'; // Optimism WETH
case 137: return '0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270'; // WMATIC
default: throw new Error(`chainId: ${chainId} is not supported`);
}
}
private normalizeTokenAmount(amount: BigNumber, decimals: number): number {
const tokenScale = BigNumber.from(10).pow(decimals);
return FixedNumber.from(amount).divUnsafe(FixedNumber.from(tokenScale)).toUnsafeFloat();
}
private tokenPriceUsd(
amount: BigNumber,
address: Address,
decimals: number,
coinPrices: CoinGeckoCoinPrices
): string {
const coinPrice = coinPrices[address][USD_CURRENCY_CODE];
const normalizedAmount = this.normalizeTokenAmount(amount, decimals) * coinPrice;
return new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD'}).format(normalizedAmount);
}
private formatAsPercentage(percentage: number): string {
return percentage.toFixed(2) + '%';
}
private totalGasCost(gasPrice: number, gas: number): number {
return (gasPrice / 1e9) * gas;
}
private gasCostsUsd(
gasPrice: number,
gas: number,
coinPrices: CoinGeckoCoinPrices,
chainId: number
): string {
const totalGasCost = this.totalGasCost(gasPrice, gas);
const chainCurrencyAddress = this.chainCurrencyAddress(chainId);
const coinPrice = coinPrices[chainCurrencyAddress][USD_CURRENCY_CODE];
const cost = totalGasCost * coinPrice;
// Polygon prices are low - using 4 significant digits here so something besides zero appears
const options = {
style: 'currency',
currency: 'USD',
maximumSignificantDigits: (chainId === 137) ? 4 : undefined,
};
return new Intl.NumberFormat('en-US', options).format(cost);
}
private gasCostsChainCurrency(gasPrice: number, gas: number, chainId: number): string {
const chainCurrency = this.chainCurrency(chainId);
const totalGasCostText = this.totalGasCost(gasPrice, gas).toFixed(7).toString();
return `${totalGasCostText} ${chainCurrency}`;
}
private chainCurrency(chainId: number): string {
switch (chainId) {
case 1: return 'ETH';
case 137: return 'MATIC';
default: return '';
}
}
private async estimateGasCost(
tradeModule: TradeModuleWrapper,
fromTokenAddress: Address,
fromTokenUnits: BigNumber,
toTokenAddress: Address,
toTokenUnits: BigNumber,
adapterName: string,
fromAddress: Address,
calldata: string,
managerAddress: Address
): Promise<number> {
try {
const gas = await tradeModule.estimateGasForTradeAsync(
fromAddress,
adapterName,
fromTokenAddress,
fromTokenUnits,
toTokenAddress,
toTokenUnits,
calldata,
managerAddress
);
const gasCostBuffer = (100 + this.tradeQuoteGasBuffer) / 100;
return Math.floor(gas.toNumber() * gasCostBuffer);
} catch (error) {
throw new Error('Unable to fetch gas cost estimate for trade' + error);
}
}
private calculateSlippage(
fromTokenAmount: BigNumber,
toTokenAmount: BigNumber,
fromTokenAddress: Address,
toTokenAddress: Address,
fromTokenDecimals: number,
toTokenDecimals: number,
coinPrices: CoinGeckoCoinPrices
): string {
const fromTokenPriceUsd = coinPrices[fromTokenAddress][USD_CURRENCY_CODE];
const toTokenPriceUsd = coinPrices[toTokenAddress][USD_CURRENCY_CODE];
const fromTokenTotalUsd = this.normalizeTokenAmount(fromTokenAmount, fromTokenDecimals) * fromTokenPriceUsd;
const toTokenTotalUsd = this.normalizeTokenAmount(toTokenAmount, toTokenDecimals) * toTokenPriceUsd;
const slippageRaw = (fromTokenTotalUsd - toTokenTotalUsd) / fromTokenTotalUsd;
return this.formatAsPercentage(slippageRaw * 100);
}
private outputSlippageTolerance(slippagePercentage: number): number {
return (100 - slippagePercentage) / 100;
}
}