From a03d0800b014410f2bb61f5f90f45d548fad725c Mon Sep 17 00:00:00 2001 From: Daniel Pyrathon Date: Mon, 19 Oct 2020 16:58:45 -0700 Subject: [PATCH] Add changes for the feature flag --- packages/asset-swapper/src/constants.ts | 2 ++ packages/asset-swapper/src/swap_quoter.ts | 27 ++++++++++++++- .../src/utils/market_operation_utils/index.ts | 34 ++++++++++++++++--- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/packages/asset-swapper/src/constants.ts b/packages/asset-swapper/src/constants.ts index e68c605930..6b5bd1a792 100644 --- a/packages/asset-swapper/src/constants.ts +++ b/packages/asset-swapper/src/constants.ts @@ -122,3 +122,5 @@ export const constants = { DEFAULT_INFO_LOGGER, DEFAULT_WARNING_LOGGER, }; + +export const ENABLE_PRICE_AWARE_RFQ: boolean = false; diff --git a/packages/asset-swapper/src/swap_quoter.ts b/packages/asset-swapper/src/swap_quoter.ts index 3de0e452bd..0f55949545 100644 --- a/packages/asset-swapper/src/swap_quoter.ts +++ b/packages/asset-swapper/src/swap_quoter.ts @@ -8,7 +8,7 @@ import { BlockParamLiteral, SupportedProvider, ZeroExProvider } from 'ethereum-t import * as _ from 'lodash'; import { artifacts } from './artifacts'; -import { constants } from './constants'; +import { constants, ENABLE_PRICE_AWARE_RFQ } from './constants'; import { CalculateSwapQuoteOpts, LiquidityForTakerMakerAssetDataPair, @@ -696,6 +696,31 @@ export class SwapQuoter { opts.rfqt = undefined; } + if ( + !ENABLE_PRICE_AWARE_RFQ && // Price-aware RFQ is disabled. + opts.rfqt && // This is an RFQT-enabled API request + opts.rfqt.intentOnFilling && // The requestor is asking for a firm quote + opts.rfqt.apiKey && + this._isApiKeyWhitelisted(opts.rfqt.apiKey) && // A valid API key was provided + sourceFilters.isAllowed(ERC20BridgeSource.Native) // Native liquidity is not excluded + ) { + if (!opts.rfqt.takerAddress || opts.rfqt.takerAddress === constants.NULL_ADDRESS) { + throw new Error('RFQ-T requests must specify a taker address'); + } + orderBatchPromises.push( + quoteRequestor + .requestRfqtFirmQuotesAsync( + makerAssetData, + takerAssetData, + assetFillAmount, + marketOperation, + undefined, + opts.rfqt, + ) + .then(firmQuotes => firmQuotes.map(quote => quote.signedOrder)), + ); + } + const orderBatches: SignedOrder[][] = await Promise.all(orderBatchPromises); const unsortedOrders: SignedOrder[] = orderBatches.reduce((_orders, batch) => _orders.concat(...batch), []); const orders = sortingUtils.sortOrders(unsortedOrders); diff --git a/packages/asset-swapper/src/utils/market_operation_utils/index.ts b/packages/asset-swapper/src/utils/market_operation_utils/index.ts index 29576fc678..c2a2af7c4b 100644 --- a/packages/asset-swapper/src/utils/market_operation_utils/index.ts +++ b/packages/asset-swapper/src/utils/market_operation_utils/index.ts @@ -4,6 +4,7 @@ import { RFQTIndicativeQuote } from '@0x/quote-server'; import { SignedOrder } from '@0x/types'; import { BigNumber, NULL_ADDRESS } from '@0x/utils'; import * as _ from 'lodash'; +import { ENABLE_PRICE_AWARE_RFQ } from '../../constants'; import { MarketOperation, Omit } from '../../types'; import { QuoteRequestor } from '../quote_requestor'; @@ -216,6 +217,17 @@ export class MarketOperationUtils { ), ); + const rfqtPromise = !ENABLE_PRICE_AWARE_RFQ && quoteSourceFilters.isAllowed(ERC20BridgeSource.Native) + ? getRfqtIndicativeQuotesAsync( + nativeOrders[0].makerAssetData, + nativeOrders[0].takerAssetData, + MarketOperation.Sell, + takerAmount, + undefined, + _opts, + ) + : Promise.resolve([]); + const offChainBalancerPromise = sampleBalancerOffChain ? this._sampler.getBalancerSellQuotesOffChainAsync(makerToken, takerToken, sampleAmounts) : Promise.resolve([]); @@ -230,11 +242,13 @@ export class MarketOperationUtils { const [ [tokenDecimals, orderFillableAmounts, ethToMakerAssetRate, ethToTakerAssetRate, dexQuotes, twoHopQuotes], + rfqtIndicativeQuotes, offChainBalancerQuotes, offChainCreamQuotes, offChainBancorQuotes, ] = await Promise.all([ samplerPromise, + rfqtPromise, offChainBalancerPromise, offChainCreamPromise, offChainBancorPromise, @@ -251,7 +265,7 @@ export class MarketOperationUtils { orderFillableAmounts, ethToOutputRate: ethToMakerAssetRate, ethToInputRate: ethToTakerAssetRate, - rfqtIndicativeQuotes: [], + rfqtIndicativeQuotes, twoHopQuotes, quoteSourceFilters, makerTokenDecimals: makerTokenDecimals.toNumber(), @@ -350,7 +364,16 @@ export class MarketOperationUtils { this._liquidityProviderRegistry, ), ); - + const rfqtPromise = !ENABLE_PRICE_AWARE_RFQ && quoteSourceFilters.isAllowed(ERC20BridgeSource.Native) + ? getRfqtIndicativeQuotesAsync( + nativeOrders[0].makerAssetData, + nativeOrders[0].takerAssetData, + MarketOperation.Buy, + makerAmount, + undefined, + _opts, + ) + : Promise.resolve([]); const offChainBalancerPromise = sampleBalancerOffChain ? this._sampler.getBalancerBuyQuotesOffChainAsync(makerToken, takerToken, sampleAmounts) : Promise.resolve([]); @@ -361,9 +384,10 @@ export class MarketOperationUtils { const [ [tokenDecimals, orderFillableAmounts, ethToMakerAssetRate, ethToTakerAssetRate, dexQuotes, twoHopQuotes], + rfqtIndicativeQuotes, offChainBalancerQuotes, offChainCreamQuotes, - ] = await Promise.all([samplerPromise, offChainBalancerPromise, offChainCreamPromise]); + ] = await Promise.all([samplerPromise, rfqtPromise, offChainBalancerPromise, offChainCreamPromise]); // Attach the MultiBridge address to the sample fillData (dexQuotes.find(quotes => quotes[0] && quotes[0].source === ERC20BridgeSource.MultiBridge) || []).forEach( q => (q.fillData = { poolAddress: this._multiBridge }), @@ -379,7 +403,7 @@ export class MarketOperationUtils { orderFillableAmounts, ethToOutputRate: ethToTakerAssetRate, ethToInputRate: ethToMakerAssetRate, - rfqtIndicativeQuotes: [], + rfqtIndicativeQuotes, twoHopQuotes, quoteSourceFilters, makerTokenDecimals: makerTokenDecimals.toNumber(), @@ -651,7 +675,7 @@ export class MarketOperationUtils { // If RFQ liquidity is enabled, make a request to check RFQ liquidity const { rfqt } = _opts; - if (rfqt && rfqt.quoteRequestor && marketSideLiquidity.quoteSourceFilters.isAllowed(ERC20BridgeSource.Native)) { + if (ENABLE_PRICE_AWARE_RFQ && rfqt && rfqt.quoteRequestor && marketSideLiquidity.quoteSourceFilters.isAllowed(ERC20BridgeSource.Native)) { // Calculate a suggested price. For now, this is simply the overall price of the aggregation. let comparisonPrice: BigNumber | undefined; if (optimizerResult) {