This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
/
swap_quoter.ts
510 lines (488 loc) · 25.9 KB
/
swap_quoter.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
import { ContractAddresses, getContractAddressesForChainOrThrow } from '@0x/contract-addresses';
import { DevUtilsContract, IERC20BridgeSamplerContract } from '@0x/contract-wrappers';
import { schemas } from '@0x/json-schemas';
import { assetDataUtils, SignedOrder } from '@0x/order-utils';
import { MeshOrderProviderOpts, Orderbook, SRAPollingOrderProviderOpts } from '@0x/orderbook';
import { BigNumber, providerUtils } from '@0x/utils';
import { SupportedProvider, ZeroExProvider } from 'ethereum-types';
import * as _ from 'lodash';
import { constants } from './constants';
import {
LiquidityForTakerMakerAssetDataPair,
MarketBuySwapQuote,
MarketOperation,
MarketSellSwapQuote,
OrderPrunerPermittedFeeTypes,
SignedOrderWithFillableAmounts,
SwapQuote,
SwapQuoteRequestOpts,
SwapQuoterOpts,
} from './types';
import { assert } from './utils/assert';
import { calculateLiquidity } from './utils/calculate_liquidity';
import { MarketOperationUtils } from './utils/market_operation_utils';
import { dummyOrderUtils } from './utils/market_operation_utils/dummy_order_utils';
import { orderPrunerUtils } from './utils/order_prune_utils';
import { OrderStateUtils } from './utils/order_state_utils';
import { ProtocolFeeUtils } from './utils/protocol_fee_utils';
import { sortingUtils } from './utils/sorting_utils';
import { SwapQuoteCalculator } from './utils/swap_quote_calculator';
export class SwapQuoter {
public readonly provider: ZeroExProvider;
public readonly orderbook: Orderbook;
public readonly expiryBufferMs: number;
public readonly chainId: number;
public readonly permittedOrderFeeTypes: Set<OrderPrunerPermittedFeeTypes>;
private readonly _contractAddresses: ContractAddresses;
private readonly _protocolFeeUtils: ProtocolFeeUtils;
private readonly _swapQuoteCalculator: SwapQuoteCalculator;
private readonly _devUtilsContract: DevUtilsContract;
private readonly _marketOperationUtils: MarketOperationUtils;
private readonly _orderStateUtils: OrderStateUtils;
/**
* Instantiates a new SwapQuoter instance given existing liquidity in the form of orders and feeOrders.
* @param supportedProvider The Provider instance you would like to use for interacting with the Ethereum network.
* @param orders A non-empty array of objects that conform to SignedOrder. All orders must have the same makerAssetData and takerAssetData.
* @param options Initialization options for the SwapQuoter. See type definition for details.
*
* @return An instance of SwapQuoter
*/
public static getSwapQuoterForProvidedOrders(
supportedProvider: SupportedProvider,
orders: SignedOrder[],
options: Partial<SwapQuoterOpts> = {},
): SwapQuoter {
assert.doesConformToSchema('orders', orders, schemas.signedOrdersSchema);
assert.assert(orders.length !== 0, `Expected orders to contain at least one order`);
const orderbook = Orderbook.getOrderbookForProvidedOrders(orders);
const swapQuoter = new SwapQuoter(supportedProvider, orderbook, options);
return swapQuoter;
}
/**
* Instantiates a new SwapQuoter instance given a [Standard Relayer API](https://github.com/0xProject/standard-relayer-api) endpoint
* @param supportedProvider The Provider instance you would like to use for interacting with the Ethereum network.
* @param sraApiUrl The standard relayer API base HTTP url you would like to source orders from.
* @param options Initialization options for the SwapQuoter. See type definition for details.
*
* @return An instance of SwapQuoter
*/
public static getSwapQuoterForStandardRelayerAPIUrl(
supportedProvider: SupportedProvider,
sraApiUrl: string,
options: Partial<SwapQuoterOpts & SRAPollingOrderProviderOpts> = {},
): SwapQuoter {
const provider = providerUtils.standardizeOrThrow(supportedProvider);
assert.isWebUri('sraApiUrl', sraApiUrl);
const orderbook = Orderbook.getOrderbookForPollingProvider({
httpEndpoint: sraApiUrl,
pollingIntervalMs:
options.orderRefreshIntervalMs || constants.DEFAULT_SWAP_QUOTER_OPTS.orderRefreshIntervalMs,
perPage: options.perPage || constants.DEFAULT_PER_PAGE,
});
const swapQuoter = new SwapQuoter(provider, orderbook, options);
return swapQuoter;
}
/**
* Instantiates a new SwapQuoter instance given a [Standard Relayer API](https://github.com/0xProject/standard-relayer-api) endpoint
* and a websocket endpoint. This is more effecient than `getSwapQuoterForStandardRelayerAPIUrl` when requesting multiple quotes.
* @param supportedProvider The Provider instance you would like to use for interacting with the Ethereum network.
* @param sraApiUrl The standard relayer API base HTTP url you would like to source orders from.
* @param sraWebsocketApiUrl The standard relayer API Websocket url you would like to subscribe to.
* @param options Initialization options for the SwapQuoter. See type definition for details.
*
* @return An instance of SwapQuoter
*/
public static getSwapQuoterForStandardRelayerAPIWebsocket(
supportedProvider: SupportedProvider,
sraApiUrl: string,
sraWebsocketAPIUrl: string,
options: Partial<SwapQuoterOpts> = {},
): SwapQuoter {
const provider = providerUtils.standardizeOrThrow(supportedProvider);
assert.isWebUri('sraApiUrl', sraApiUrl);
assert.isUri('sraWebsocketAPIUrl', sraWebsocketAPIUrl);
const orderbook = Orderbook.getOrderbookForWebsocketProvider({
httpEndpoint: sraApiUrl,
websocketEndpoint: sraWebsocketAPIUrl,
});
const swapQuoter = new SwapQuoter(provider, orderbook, options);
return swapQuoter;
}
/**
* Instantiates a new SwapQuoter instance given a 0x Mesh endpoint. This pulls all available liquidity stored in Mesh
* @param supportedProvider The Provider instance you would like to use for interacting with the Ethereum network.
* @param meshEndpoint The standard relayer API base HTTP url you would like to source orders from.
* @param options Initialization options for the SwapQuoter. See type definition for details.
*
* @return An instance of SwapQuoter
*/
public static getSwapQuoterForMeshEndpoint(
supportedProvider: SupportedProvider,
meshEndpoint: string,
options: Partial<SwapQuoterOpts & MeshOrderProviderOpts> = {},
): SwapQuoter {
const provider = providerUtils.standardizeOrThrow(supportedProvider);
assert.isUri('meshEndpoint', meshEndpoint);
const orderbook = Orderbook.getOrderbookForMeshProvider({
websocketEndpoint: meshEndpoint,
wsOpts: options.wsOpts,
});
const swapQuoter = new SwapQuoter(provider, orderbook, options);
return swapQuoter;
}
/**
* Instantiates a new SwapQuoter instance
* @param supportedProvider The Provider instance you would like to use for interacting with the Ethereum network.
* @param orderbook An object that conforms to Orderbook, see type for definition.
* @param options Initialization options for the SwapQuoter. See type definition for details.
*
* @return An instance of SwapQuoter
*/
constructor(supportedProvider: SupportedProvider, orderbook: Orderbook, options: Partial<SwapQuoterOpts> = {}) {
const { chainId, expiryBufferMs, permittedOrderFeeTypes } = _.merge(
{},
constants.DEFAULT_SWAP_QUOTER_OPTS,
options,
);
const provider = providerUtils.standardizeOrThrow(supportedProvider);
assert.isValidOrderbook('orderbook', orderbook);
assert.isNumber('chainId', chainId);
assert.isNumber('expiryBufferMs', expiryBufferMs);
this.chainId = chainId;
this.provider = provider;
this.orderbook = orderbook;
this.expiryBufferMs = expiryBufferMs;
this.permittedOrderFeeTypes = permittedOrderFeeTypes;
this._contractAddresses = getContractAddressesForChainOrThrow(chainId);
this._devUtilsContract = new DevUtilsContract(this._contractAddresses.devUtils, provider);
this._protocolFeeUtils = new ProtocolFeeUtils(constants.PROTOCOL_FEE_UTILS_POLLING_INTERVAL_IN_MS);
this._orderStateUtils = new OrderStateUtils(this._devUtilsContract);
const samplerContract = new IERC20BridgeSamplerContract(
this._contractAddresses.erc20BridgeSampler,
this.provider,
);
this._marketOperationUtils = new MarketOperationUtils(samplerContract, this._contractAddresses, {
chainId,
exchangeAddress: this._contractAddresses.exchange,
});
this._swapQuoteCalculator = new SwapQuoteCalculator(this._protocolFeeUtils, this._marketOperationUtils);
}
/**
* Get a `SwapQuote` containing all information relevant to fulfilling a swap between a desired ERC20 token address and ERC20 owned by a provided address.
* You can then pass the `SwapQuote` to a `SwapQuoteConsumer` to execute a buy, or process SwapQuote for on-chain consumption.
* @param makerAssetData The makerAssetData of the desired asset to swap for (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
* @param takerAssetData The takerAssetData of the asset to swap makerAssetData for (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
* @param takerAssetSellAmount The amount of taker asset to swap for.
* @param options Options for the request. See type definition for more information.
*
* @return An object that conforms to SwapQuote that satisfies the request. See type definition for more information.
*/
public async getMarketSellSwapQuoteForAssetDataAsync(
makerAssetData: string,
takerAssetData: string,
takerAssetSellAmount: BigNumber,
options: Partial<SwapQuoteRequestOpts> = {},
): Promise<MarketSellSwapQuote> {
assert.isBigNumber('takerAssetSellAmount', takerAssetSellAmount);
return (await this._getSwapQuoteAsync(
makerAssetData,
takerAssetData,
takerAssetSellAmount,
MarketOperation.Sell,
options,
)) as MarketSellSwapQuote;
}
/**
* Get a `SwapQuote` containing all information relevant to fulfilling a swap between a desired ERC20 token address and ERC20 owned by a provided address.
* You can then pass the `SwapQuote` to a `SwapQuoteConsumer` to execute a buy, or process SwapQuote for on-chain consumption.
* @param makerAssetData The makerAssetData of the desired asset to swap for (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
* @param takerAssetData The takerAssetData of the asset to swap makerAssetData for (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
* @param makerAssetBuyAmount The amount of maker asset to swap for.
* @param options Options for the request. See type definition for more information.
*
* @return An object that conforms to SwapQuote that satisfies the request. See type definition for more information.
*/
public async getMarketBuySwapQuoteForAssetDataAsync(
makerAssetData: string,
takerAssetData: string,
makerAssetBuyAmount: BigNumber,
options: Partial<SwapQuoteRequestOpts> = {},
): Promise<MarketBuySwapQuote> {
assert.isBigNumber('makerAssetBuyAmount', makerAssetBuyAmount);
return (await this._getSwapQuoteAsync(
makerAssetData,
takerAssetData,
makerAssetBuyAmount,
MarketOperation.Buy,
options,
)) as MarketBuySwapQuote;
}
/**
* Get a `SwapQuote` containing all information relevant to fulfilling a swap between a desired ERC20 token address and ERC20 owned by a provided address.
* You can then pass the `SwapQuote` to a `SwapQuoteConsumer` to execute a buy, or process SwapQuote for on-chain consumption.
* @param makerTokenAddress The address of the maker asset
* @param takerTokenAddress The address of the taker asset
* @param makerAssetBuyAmount The amount of maker asset to swap for.
* @param options Options for the request. See type definition for more information.
*
* @return An object that conforms to SwapQuote that satisfies the request. See type definition for more information.
*/
public async getMarketBuySwapQuoteAsync(
makerTokenAddress: string,
takerTokenAddress: string,
makerAssetBuyAmount: BigNumber,
options: Partial<SwapQuoteRequestOpts> = {},
): Promise<MarketBuySwapQuote> {
assert.isETHAddressHex('makerTokenAddress', makerTokenAddress);
assert.isETHAddressHex('takerTokenAddress', takerTokenAddress);
assert.isBigNumber('makerAssetBuyAmount', makerAssetBuyAmount);
const makerAssetData = assetDataUtils.encodeERC20AssetData(makerTokenAddress);
const takerAssetData = assetDataUtils.encodeERC20AssetData(takerTokenAddress);
const swapQuote = this.getMarketBuySwapQuoteForAssetDataAsync(
makerAssetData,
takerAssetData,
makerAssetBuyAmount,
options,
);
return swapQuote;
}
/**
* Get a `SwapQuote` containing all information relevant to fulfilling a swap between a desired ERC20 token address and ERC20 owned by a provided address.
* You can then pass the `SwapQuote` to a `SwapQuoteConsumer` to execute a buy, or process SwapQuote for on-chain consumption.
* @param makerTokenAddress The address of the maker asset
* @param takerTokenAddress The address of the taker asset
* @param takerAssetSellAmount The amount of taker asset to sell.
* @param options Options for the request. See type definition for more information.
*
* @return An object that conforms to SwapQuote that satisfies the request. See type definition for more information.
*/
public async getMarketSellSwapQuoteAsync(
makerTokenAddress: string,
takerTokenAddress: string,
takerAssetSellAmount: BigNumber,
options: Partial<SwapQuoteRequestOpts> = {},
): Promise<MarketSellSwapQuote> {
assert.isETHAddressHex('makerTokenAddress', makerTokenAddress);
assert.isETHAddressHex('takerTokenAddress', takerTokenAddress);
assert.isBigNumber('takerAssetSellAmount', takerAssetSellAmount);
const makerAssetData = assetDataUtils.encodeERC20AssetData(makerTokenAddress);
const takerAssetData = assetDataUtils.encodeERC20AssetData(takerTokenAddress);
const swapQuote = this.getMarketSellSwapQuoteForAssetDataAsync(
makerAssetData,
takerAssetData,
takerAssetSellAmount,
options,
);
return swapQuote;
}
/**
* Returns information about available liquidity for an asset
* Does not factor in slippage or fees
* @param makerAssetData The makerAssetData of the desired asset to swap for (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
* @param takerAssetData The takerAssetData of the asset to swap makerAssetData for (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
*
* @return An object that conforms to LiquidityForTakerMakerAssetDataPair that satisfies the request. See type definition for more information.
*/
public async getLiquidityForMakerTakerAssetDataPairAsync(
makerAssetData: string,
takerAssetData: string,
): Promise<LiquidityForTakerMakerAssetDataPair> {
assert.isString('makerAssetData', makerAssetData);
assert.isString('takerAssetData', takerAssetData);
assetDataUtils.decodeAssetDataOrThrow(takerAssetData);
assetDataUtils.decodeAssetDataOrThrow(makerAssetData);
const assetPairs = await this.getAvailableMakerAssetDatasAsync(takerAssetData);
if (!assetPairs.includes(makerAssetData)) {
return {
makerAssetAvailableInBaseUnits: new BigNumber(0),
takerAssetAvailableInBaseUnits: new BigNumber(0),
};
}
const ordersWithFillableAmounts = await this.getSignedOrdersWithFillableAmountsAsync(
makerAssetData,
takerAssetData,
);
return calculateLiquidity(ordersWithFillableAmounts);
}
/**
* Get the asset data of all assets that can be used to purchase makerAssetData in the order provider passed in at init.
*
* @return An array of asset data strings that can purchase makerAssetData.
*/
public async getAvailableTakerAssetDatasAsync(makerAssetData: string): Promise<string[]> {
assert.isString('makerAssetData', makerAssetData);
assetDataUtils.decodeAssetDataOrThrow(makerAssetData);
const allAssetPairs = await this.orderbook.getAvailableAssetDatasAsync();
const assetPairs = allAssetPairs
.filter(pair => pair.assetDataA.assetData === makerAssetData)
.map(pair => pair.assetDataB.assetData);
return assetPairs;
}
/**
* Get the asset data of all assets that are purchaseable with takerAssetData in the order provider passed in at init.
*
* @return An array of asset data strings that are purchaseable with takerAssetData.
*/
public async getAvailableMakerAssetDatasAsync(takerAssetData: string): Promise<string[]> {
assert.isString('takerAssetData', takerAssetData);
assetDataUtils.decodeAssetDataOrThrow(takerAssetData);
const allAssetPairs = await this.orderbook.getAvailableAssetDatasAsync();
const assetPairs = allAssetPairs
.filter(pair => pair.assetDataB.assetData === takerAssetData)
.map(pair => pair.assetDataA.assetData);
return assetPairs;
}
/**
* Validates the taker + maker asset pair is available from the order provider provided to `SwapQuote`.
* @param makerAssetData The makerAssetData of the desired asset to swap for (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
* @param takerAssetData The takerAssetData of the asset to swap makerAssetData for (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
*
* @return A boolean on if the taker, maker pair exists
*/
public async isTakerMakerAssetDataPairAvailableAsync(
makerAssetData: string,
takerAssetData: string,
): Promise<boolean> {
assert.isString('makerAssetData', makerAssetData);
assert.isString('takerAssetData', takerAssetData);
assetDataUtils.decodeAssetDataOrThrow(takerAssetData);
assetDataUtils.decodeAssetDataOrThrow(makerAssetData);
const availableMakerAssetDatas = await this.getAvailableMakerAssetDatasAsync(takerAssetData);
return _.includes(availableMakerAssetDatas, makerAssetData);
}
/**
* Grab orders from the order provider, prunes for valid orders with provided OrderPruner options
* @param makerAssetData The makerAssetData of the desired asset to swap for (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
* @param takerAssetData The takerAssetData of the asset to swap makerAssetData for (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
*/
public async getSignedOrdersWithFillableAmountsAsync(
makerAssetData: string,
takerAssetData: string,
): Promise<SignedOrderWithFillableAmounts[]> {
assert.isString('makerAssetData', makerAssetData);
assert.isString('takerAssetData', takerAssetData);
assetDataUtils.decodeAssetDataOrThrow(takerAssetData);
assetDataUtils.decodeAssetDataOrThrow(makerAssetData);
// get orders
const apiOrders = await this.orderbook.getOrdersAsync(makerAssetData, takerAssetData);
const orders = _.map(apiOrders, o => o.order);
const prunedOrders = orderPrunerUtils.pruneForUsableSignedOrders(
orders,
this.permittedOrderFeeTypes,
this.expiryBufferMs,
);
const sortedPrunedOrders = sortingUtils.sortOrders(prunedOrders);
const ordersWithFillableAmounts = await this._orderStateUtils.getSignedOrdersWithFillableAmountsAsync(
sortedPrunedOrders,
);
return ordersWithFillableAmounts;
}
/**
* Util function to check if takerAddress's allowance is enough for 0x exchange contracts to conduct the swap specified by the swapQuote.
* @param swapQuote The swapQuote in question to check enough allowance enabled for 0x exchange contracts to conduct the swap.
* @param takerAddress The address of the taker of the provided swapQuote
*/
public async isSwapQuoteFillableByTakerAddressAsync(
swapQuote: SwapQuote,
takerAddress: string,
): Promise<[boolean, boolean]> {
const balanceAndAllowance = await this._devUtilsContract
.getBalanceAndAssetProxyAllowance(takerAddress, swapQuote.takerAssetData)
.callAsync();
return [
balanceAndAllowance[1].isGreaterThanOrEqualTo(swapQuote.bestCaseQuoteInfo.totalTakerAssetAmount),
balanceAndAllowance[1].isGreaterThanOrEqualTo(swapQuote.worstCaseQuoteInfo.totalTakerAssetAmount),
];
}
/**
* Destroys any subscriptions or connections.
*/
public async destroyAsync(): Promise<void> {
return this.orderbook.destroyAsync();
}
/**
* Utility function to get assetData for Ether token.
*/
public async getEtherTokenAssetDataOrThrowAsync(): Promise<string> {
return assetDataUtils.encodeERC20AssetData(this._contractAddresses.etherToken);
}
/**
* Grab orders from the order provider, prunes for valid orders with provided OrderPruner options
* @param makerAssetData The makerAssetData of the desired asset to swap for (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
* @param takerAssetData The takerAssetData of the asset to swap makerAssetData for (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
*/
private async _getSignedOrdersAsync(makerAssetData: string, takerAssetData: string): Promise<SignedOrder[]> {
assert.isString('makerAssetData', makerAssetData);
assert.isString('takerAssetData', takerAssetData);
assetDataUtils.decodeAssetDataOrThrow(takerAssetData);
assetDataUtils.decodeAssetDataOrThrow(makerAssetData);
// get orders
const apiOrders = await this.orderbook.getOrdersAsync(makerAssetData, takerAssetData);
const orders = _.map(apiOrders, o => o.order);
const prunedOrders = orderPrunerUtils.pruneForUsableSignedOrders(
orders,
this.permittedOrderFeeTypes,
this.expiryBufferMs,
);
const sortedPrunedOrders = sortingUtils.sortOrders(prunedOrders);
return sortedPrunedOrders;
}
/**
* General function for getting swap quote, conditionally uses different logic per specified marketOperation
*/
private async _getSwapQuoteAsync(
makerAssetData: string,
takerAssetData: string,
assetFillAmount: BigNumber,
marketOperation: MarketOperation,
options: Partial<SwapQuoteRequestOpts>,
): Promise<SwapQuote> {
const { slippagePercentage, ...calculateSwapQuoteOpts } = _.merge(
{},
constants.DEFAULT_SWAP_QUOTE_REQUEST_OPTS,
options,
);
assert.isString('makerAssetData', makerAssetData);
assert.isString('takerAssetData', takerAssetData);
assert.isNumber('slippagePercentage', slippagePercentage);
let gasPrice: BigNumber;
if (!!options.gasPrice) {
gasPrice = options.gasPrice;
assert.isBigNumber('gasPrice', gasPrice);
} else {
gasPrice = await this._protocolFeeUtils.getGasPriceEstimationOrThrowAsync();
}
// get the relevant orders for the makerAsset
let prunedOrders = await this._getSignedOrdersAsync(makerAssetData, takerAssetData);
// if no native orders, pass in a dummy order for the sampler to have required metadata for sampling
if (prunedOrders.length === 0) {
prunedOrders = [
dummyOrderUtils.createDummyOrderForSampler(
makerAssetData,
takerAssetData,
this._contractAddresses.uniswapBridge,
),
];
}
let swapQuote: SwapQuote;
if (marketOperation === MarketOperation.Buy) {
swapQuote = await this._swapQuoteCalculator.calculateMarketBuySwapQuoteAsync(
prunedOrders,
assetFillAmount,
slippagePercentage,
gasPrice,
calculateSwapQuoteOpts,
);
} else {
swapQuote = await this._swapQuoteCalculator.calculateMarketSellSwapQuoteAsync(
prunedOrders,
assetFillAmount,
slippagePercentage,
gasPrice,
calculateSwapQuoteOpts,
);
}
return swapQuote;
}
}
// tslint:disable-next-line: max-file-line-count