/
types.ts
357 lines (310 loc) · 9.13 KB
/
types.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
import { ChainId } from '@0x/contract-addresses';
import { MarketOperation } from '@0x/types';
import { BigNumber } from '@0x/utils';
import {
NativeOrderWithFillableAmounts,
ERC20BridgeSource,
OptimizedOrder,
FillData,
FeeSchedule,
Fill,
FillAdjustor,
ExchangeProxyOverhead,
ExtendedQuoteReportSources,
PriceComparisonsReport,
QuoteReport,
} from '../../types';
import { V4RFQIndicativeQuoteMM } from '../../utils/quote_requestor';
import { SourceFilters } from './source_filters';
/**
* Common exception messages thrown by aggregation logic.
*/
export enum AggregationError {
NoOptimalPath = 'NO_OPTIMAL_PATH',
EmptyOrders = 'EMPTY_ORDERS',
NotERC20AssetData = 'NOT_ERC20ASSET_DATA',
NoBridgeForSource = 'NO_BRIDGE_FOR_SOURCE',
}
/**
* Curve contract function selectors.
*/
export enum CurveFunctionSelectors {
None = '0x00000000',
exchange = '0x3df02124',
exchange_underlying = '0xa6417ed6', // exchange_underlying(int128 i, int128 j, uint256 dx, uint256 min_dy)
get_dy_underlying = '0x07211ef7',
get_dx_underlying = '0x0e71d1b9',
get_dy = '0x5e0d443f', // get_dy(int128,int128,uint256)
get_dx = '0x67df02ca',
get_dy_uint256 = '0x556d6e9f', // get_dy(uint256,uint256,uint256)
exchange_underlying_uint256 = '0x65b2489b', // exchange_underlying(uint256,uint256,uint256,uint256)
// Curve V2
exchange_v2 = '0x5b41b908',
exchange_underlying_v2 = '0x65b2489b',
get_dy_v2 = '0x556d6e9f',
get_dy_underlying_v2 = '0x85f11d1e',
// Smoothy(deprecated)
swap_uint256 = '0x5673b02d', // swap(uint256,uint256,uint256,uint256)
get_swap_amount = '0x45cf2ef6', // getSwapAmount(uint256,uint256,uint256)
// Nerve BSC, Saddle Mainnet, Synapse
swap = '0x91695586', // swap(uint8,uint8,uint256,uint256,uint256)
calculateSwap = '0xa95b089f', // calculateSwap(uint8,uint8,uint256)
calculateSwapUnderlying = '0x75d8e3e4', // calculateSwapUnderlying(uint8,uint8,uint256)
swapUnderlying = '0x78e0fae8', // swapUnderlying(uint8,uint8,uint256,uint256,uint256)
}
/**
* Configuration info on a Curve pool.
*/
export interface CurveInfo {
exchangeFunctionSelector: CurveFunctionSelectors;
sellQuoteFunctionSelector: CurveFunctionSelectors;
buyQuoteFunctionSelector: CurveFunctionSelectors;
poolAddress: string;
tokens: string[];
metaTokens: string[] | undefined;
gasSchedule: number;
}
/**
* Configuration for a specific PSM vault
*/
export interface PsmInfo {
psmAddress: string;
ilkIdentifier: string;
gemTokenAddress: string;
}
/**
* Configuration for a Lido deployment
*/
export interface LidoInfo {
stEthToken: string;
wethToken: string;
wstEthToken: string;
}
/**
* Configuration info for a Balancer V2 pool.
*/
export interface BalancerV2PoolInfo {
poolId: string;
vault: string;
}
export interface AaveInfo {
lendingPool: string;
aToken: string;
underlyingToken: string;
}
// Represents an individual DEX sample from the sampler contract
export interface DexSample<TFillData extends FillData = FillData> {
source: ERC20BridgeSource;
fillData: TFillData;
input: BigNumber;
output: BigNumber;
}
export interface CurveFillData extends FillData {
fromTokenIdx: number;
toTokenIdx: number;
pool: CurveInfo;
}
interface BalancerBatchSwapStep {
poolId: string;
assetInIndex: number;
assetOutIndex: number;
amount: BigNumber;
userData: string;
}
export interface BalancerSwaps {
swapInfoExactIn: BalancerSwapInfo[];
swapInfoExactOut: BalancerSwapInfo[];
}
export interface BalancerSwapInfo {
assets: string[];
swapSteps: BalancerBatchSwapStep[];
}
export interface BalancerFillData extends FillData {
poolAddress: string;
}
export interface BalancerV2FillData extends FillData {
vault: string;
poolId: string;
}
export interface BalancerV2BatchSwapFillData extends FillData {
vault: string;
swapSteps: BalancerBatchSwapStep[];
assets: string[];
}
export interface UniswapV2FillData extends FillData {
tokenAddressPath: string[];
router: string;
}
export interface ShellFillData extends FillData {
poolAddress: string;
}
export interface BancorFillData extends FillData {
path: string[];
networkAddress: string;
}
export interface MooniswapFillData extends FillData {
poolAddress: string;
}
export interface DODOFillData extends FillData {
poolAddress: string;
isSellBase: boolean;
helperAddress: string;
}
export interface GenericRouterFillData extends FillData {
router: string;
}
export interface MultiHopFillData extends FillData {
firstHopSource: SourceQuoteOperation;
secondHopSource: SourceQuoteOperation;
intermediateToken: string;
}
interface MakerPsmExtendedData {
isSellOperation: boolean;
takerToken: string;
}
export type MakerPsmFillData = FillData & MakerPsmExtendedData & PsmInfo;
export interface HopInfo {
sourceIndex: BigNumber;
returnData: string;
}
export interface UniswapV3PathAmount {
uniswapPath: string;
inputAmount: BigNumber;
gasUsed: number;
}
export interface UniswapV3FillData extends FillData {
tokenAddressPath: string[];
router: string;
pathAmounts: UniswapV3PathAmount[];
// Only needed for gas estimation.
}
export interface KyberDmmFillData extends UniswapV2FillData {
poolsPath: string[];
}
/**
* Determines whether FillData is UniswapV3FillData or FinalUniswapV3FillData
*/
export function isFinalUniswapV3FillData(
data: UniswapV3FillData | FinalUniswapV3FillData,
): data is FinalUniswapV3FillData {
return !!(data as FinalUniswapV3FillData).uniswapPath;
}
export interface FinalUniswapV3FillData extends Omit<UniswapV3FillData, 'pathAmounts'> {
// The uniswap-encoded path that can fll the maximum input amount.
uniswapPath: string;
gasUsed: number;
}
export interface LidoFillData extends FillData {
stEthTokenAddress: string;
wstEthTokenAddress: string;
takerToken: string;
makerToken: string;
}
export interface AaveV2FillData extends FillData {
lendingPool: string;
aToken: string;
underlyingToken: string;
takerToken: string;
}
interface AaveV3L2EncodedParameter {
inputAmount: BigNumber;
l2Parameter: string;
}
export interface AaveV3FillData extends AaveV2FillData {
l2EncodedParams: AaveV3L2EncodedParameter[];
}
export interface CompoundFillData extends FillData {
cToken: string;
takerToken: string;
makerToken: string;
}
export interface PlatypusInfo {
poolAddress: string;
tokens: string[];
gasSchedule: number;
}
export interface GMXFillData extends FillData {
router: string;
reader: string;
vault: string;
tokenAddressPath: string[];
}
export interface PlatypusFillData extends FillData {
router: string;
pool: string[];
tokenAddressPath: string[];
}
export interface WOOFiFillData extends FillData {
poolAddress: string;
takerToken: string;
makerToken: string;
// Only needed for gas estimation
chainId: ChainId;
}
export interface VelodromeFillData extends FillData {
router: string;
stable: boolean;
}
export interface SynthetixFillData extends FillData {
synthetix: string;
takerTokenSymbolBytes32: string;
makerTokenSymbolBytes32: string;
// Only needed for gas estimation.
chainId: ChainId;
}
/**
* A composable operation the be run in `DexOrderSampler.executeAsync()`.
*/
export interface BatchedOperation<TResult> {
encodeCall(): string;
handleCallResults(callResults: string): TResult;
handleRevert(callResults: string): TResult;
}
export interface SourceQuoteOperation<TFillData extends FillData = FillData> extends BatchedOperation<BigNumber[]> {
readonly source: ERC20BridgeSource;
fillData: TFillData;
}
export interface OptimizerResult {
optimizedOrders: OptimizedOrder[];
sourceFlags: bigint;
liquidityDelivered: Readonly<Fill[] | DexSample<MultiHopFillData>>;
marketSideLiquidity: MarketSideLiquidity;
adjustedRate: BigNumber;
takerAmountPerEth: BigNumber;
makerAmountPerEth: BigNumber;
}
export interface OptimizerResultWithReport extends OptimizerResult {
quoteReport?: QuoteReport;
extendedQuoteReportSources?: ExtendedQuoteReportSources;
priceComparisonsReport?: PriceComparisonsReport;
}
export interface MarketSideLiquidity {
side: MarketOperation;
inputAmount: BigNumber;
inputToken: string;
outputToken: string;
outputAmountPerEth: BigNumber;
inputAmountPerEth: BigNumber;
quoteSourceFilters: SourceFilters;
makerTokenDecimals: number;
takerTokenDecimals: number;
quotes: RawQuotes;
isRfqSupported: boolean;
blockNumber: number;
}
export interface RawQuotes {
nativeOrders: NativeOrderWithFillableAmounts[];
rfqtIndicativeQuotes: V4RFQIndicativeQuoteMM[];
twoHopQuotes: DexSample<MultiHopFillData>[];
dexQuotes: DexSample<FillData>[][];
}
export interface GenerateOptimizedOrdersOpts {
feeSchedule: FeeSchedule;
exchangeProxyOverhead: ExchangeProxyOverhead;
gasPrice: BigNumber;
neonRouterNumSamples: number;
fillAdjustor: FillAdjustor;
}
export interface ComparisonPrice {
wholeOrder: BigNumber | undefined;
}