refactor coding and avoid running indicative quotes when sampling market liquidity

This commit is contained in:
Daniel Pyrathon 2020-09-29 00:59:14 -07:00
parent 90ad5eb6c4
commit 507690f9db

View File

@ -195,16 +195,6 @@ export class MarketOperationUtils {
), ),
); );
const rfqtPromise = quoteSourceFilters.isAllowed(ERC20BridgeSource.Native)
? getRfqtIndicativeQuotesAsync(
nativeOrders[0].makerAssetData,
nativeOrders[0].takerAssetData,
MarketOperation.Sell,
takerAmount,
_opts,
)
: Promise.resolve([]);
const offChainBalancerPromise = sampleBalancerOffChain const offChainBalancerPromise = sampleBalancerOffChain
? this._sampler.getBalancerSellQuotesOffChainAsync(makerToken, takerToken, sampleAmounts) ? this._sampler.getBalancerSellQuotesOffChainAsync(makerToken, takerToken, sampleAmounts)
: Promise.resolve([]); : Promise.resolve([]);
@ -215,10 +205,9 @@ export class MarketOperationUtils {
const [ const [
[orderFillableAmounts, ethToMakerAssetRate, ethToTakerAssetRate, dexQuotes, twoHopQuotes], [orderFillableAmounts, ethToMakerAssetRate, ethToTakerAssetRate, dexQuotes, twoHopQuotes],
rfqtIndicativeQuotes,
offChainBalancerQuotes, offChainBalancerQuotes,
offChainBancorQuotes, offChainBancorQuotes,
] = await Promise.all([samplerPromise, rfqtPromise, offChainBalancerPromise, offChainBancorPromise]); ] = await Promise.all([samplerPromise, offChainBalancerPromise, offChainBancorPromise]);
return { return {
side: MarketOperation.Sell, side: MarketOperation.Sell,
@ -230,7 +219,7 @@ export class MarketOperationUtils {
orderFillableAmounts, orderFillableAmounts,
ethToOutputRate: ethToMakerAssetRate, ethToOutputRate: ethToMakerAssetRate,
ethToInputRate: ethToTakerAssetRate, ethToInputRate: ethToTakerAssetRate,
rfqtIndicativeQuotes, rfqtIndicativeQuotes: [],
twoHopQuotes, twoHopQuotes,
quoteSourceFilters, quoteSourceFilters,
}; };
@ -311,25 +300,14 @@ export class MarketOperationUtils {
), ),
); );
const rfqtPromise = quoteSourceFilters.isAllowed(ERC20BridgeSource.Native)
? getRfqtIndicativeQuotesAsync(
nativeOrders[0].makerAssetData,
nativeOrders[0].takerAssetData,
MarketOperation.Buy,
makerAmount,
_opts,
)
: Promise.resolve([]);
const offChainBalancerPromise = sampleBalancerOffChain const offChainBalancerPromise = sampleBalancerOffChain
? this._sampler.getBalancerBuyQuotesOffChainAsync(makerToken, takerToken, sampleAmounts) ? this._sampler.getBalancerBuyQuotesOffChainAsync(makerToken, takerToken, sampleAmounts)
: Promise.resolve([]); : Promise.resolve([]);
const [ const [
[orderFillableAmounts, ethToMakerAssetRate, ethToTakerAssetRate, dexQuotes, twoHopQuotes], [orderFillableAmounts, ethToMakerAssetRate, ethToTakerAssetRate, dexQuotes, twoHopQuotes],
rfqtIndicativeQuotes,
offChainBalancerQuotes, offChainBalancerQuotes,
] = await Promise.all([samplerPromise, rfqtPromise, offChainBalancerPromise]); ] = await Promise.all([samplerPromise, offChainBalancerPromise]);
// Attach the MultiBridge address to the sample fillData // Attach the MultiBridge address to the sample fillData
(dexQuotes.find(quotes => quotes[0] && quotes[0].source === ERC20BridgeSource.MultiBridge) || []).forEach( (dexQuotes.find(quotes => quotes[0] && quotes[0].source === ERC20BridgeSource.MultiBridge) || []).forEach(
q => (q.fillData = { poolAddress: this._multiBridge }), q => (q.fillData = { poolAddress: this._multiBridge }),
@ -344,7 +322,7 @@ export class MarketOperationUtils {
orderFillableAmounts, orderFillableAmounts,
ethToOutputRate: ethToTakerAssetRate, ethToOutputRate: ethToTakerAssetRate,
ethToInputRate: ethToMakerAssetRate, ethToInputRate: ethToMakerAssetRate,
rfqtIndicativeQuotes, rfqtIndicativeQuotes: [],
twoHopQuotes, twoHopQuotes,
quoteSourceFilters, quoteSourceFilters,
}; };
@ -501,7 +479,15 @@ export class MarketOperationUtils {
// Compute an optimized path for on-chain DEX and open-orderbook. This should not include RFQ liquidity. // Compute an optimized path for on-chain DEX and open-orderbook. This should not include RFQ liquidity.
const marketLiquidityFnAsync = side === MarketOperation.Sell ? this.getMarketSellLiquidityAsync.bind(this) : this.getMarketBuyLiquidityAsync.bind(this); const marketLiquidityFnAsync = side === MarketOperation.Sell ? this.getMarketSellLiquidityAsync.bind(this) : this.getMarketBuyLiquidityAsync.bind(this);
const marketSideLiquidity = await marketLiquidityFnAsync(nativeOrders, amount, defaultOpts); const marketSideLiquidity = await marketLiquidityFnAsync(nativeOrders, amount, defaultOpts);
let optimizerResult = await this._generateOptimizedOrdersAsync(marketSideLiquidity, optimizerOpts); let optimizerResult: OptimizerResult | undefined;
try {
optimizerResult = await this._generateOptimizedOrdersAsync(marketSideLiquidity, optimizerOpts);
} catch (e) {
// If no on-chain or off-chain Open Orderbook orders are present, a `NoOptimalPath` will be thrown.
if (e.message !== AggregationError.NoOptimalPath) {
throw e;
}
}
// If RFQ liquidity is enabled, make a request to check RFQ liquidity // If RFQ liquidity is enabled, make a request to check RFQ liquidity
const { rfqt } = defaultOpts; const { rfqt } = defaultOpts;
@ -516,6 +502,7 @@ export class MarketOperationUtils {
amount, amount,
defaultOpts, defaultOpts,
); );
// Re-run optimizer with the new indicative quote
if (indicativeQuotes.length > 0) { if (indicativeQuotes.length > 0) {
optimizerResult = await this._generateOptimizedOrdersAsync({ optimizerResult = await this._generateOptimizedOrdersAsync({
...marketSideLiquidity, ...marketSideLiquidity,
@ -539,15 +526,25 @@ export class MarketOperationUtils {
rfqt, rfqt,
); );
if (firmQuotes.length > 0) { if (firmQuotes.length > 0) {
// Re-run optimizer with the new firm quote. This is the second and last time
// we run the optimized in a block of code. In this case, we don't catch a potential `NoOptimalPath` exception
// and we let it bubble up if it happens.
optimizerResult = await this._generateOptimizedOrdersAsync({ optimizerResult = await this._generateOptimizedOrdersAsync({
...marketSideLiquidity, ...marketSideLiquidity,
nativeOrders: marketSideLiquidity.nativeOrders.concat(firmQuotes.map(quote => quote.signedOrder)), nativeOrders: marketSideLiquidity.nativeOrders.concat(firmQuotes.map(quote => quote.signedOrder)),
orderFillableAmounts: marketSideLiquidity.orderFillableAmounts.concat(firmQuotes.map(quote => quote.signedOrder.takerAssetAmount)),
}, optimizerOpts); }, optimizerOpts);
} }
} }
} }
} }
// At this point we should have at least one valid optimizer result, therefore we manually raise
// `NoOptimalPath` if no optimizer result was ever set.
if (optimizerResult === undefined) {
throw new Error(AggregationError.NoOptimalPath);
}
// Compute Quote Report and return the results. // Compute Quote Report and return the results.
let quoteReport: QuoteReport | undefined; let quoteReport: QuoteReport | undefined;
if (defaultOpts.shouldGenerateQuoteReport) { if (defaultOpts.shouldGenerateQuoteReport) {