From d6bc0a3368f781dcd031c48c68e931010bd592e9 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Fri, 29 Jan 2021 07:04:32 +1000 Subject: [PATCH] fix: [asset-swapper] prevent error when multihop data is not present (#80) * fix: [asset-swapper] prevent error when multihop is not present for a route * Update changelog Co-authored-by: Michael Zhu --- packages/asset-swapper/CHANGELOG.json | 9 +++++++++ .../src/utils/market_operation_utils/index.ts | 11 +++++++++-- .../asset-swapper/src/utils/quote_report_generator.ts | 1 - 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/asset-swapper/CHANGELOG.json b/packages/asset-swapper/CHANGELOG.json index 3def916b4c..16aa584d17 100644 --- a/packages/asset-swapper/CHANGELOG.json +++ b/packages/asset-swapper/CHANGELOG.json @@ -1,4 +1,13 @@ [ + { + "version": "5.8.2", + "changes": [ + { + "note": "Fix error when Multihop data is not present", + "pr": 80 + } + ] + }, { "timestamp": 1611648096, "version": "5.8.1", 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 dd09dda91a..1329ad9b7a 100644 --- a/packages/asset-swapper/src/utils/market_operation_utils/index.ts +++ b/packages/asset-swapper/src/utils/market_operation_utils/index.ts @@ -204,12 +204,15 @@ export class MarketOperationUtils { : Promise.resolve([]); const [ - [tokenDecimals, orderFillableAmounts, ethToMakerAssetRate, ethToTakerAssetRate, dexQuotes, twoHopQuotes], + [tokenDecimals, orderFillableAmounts, ethToMakerAssetRate, ethToTakerAssetRate, dexQuotes, rawTwoHopQuotes], rfqtIndicativeQuotes, offChainBalancerQuotes, offChainCreamQuotes, ] = await Promise.all([samplerPromise, rfqtPromise, offChainBalancerPromise, offChainCreamPromise]); + // Filter out any invalid two hop quotes where we couldn't find a route + const twoHopQuotes = rawTwoHopQuotes.filter(q => q && q.fillData && q.fillData.firstHopSource); + const [makerTokenDecimals, takerTokenDecimals] = tokenDecimals; return { side: MarketOperation.Sell, @@ -321,11 +324,15 @@ export class MarketOperationUtils { : Promise.resolve([]); const [ - [tokenDecimals, orderFillableAmounts, ethToMakerAssetRate, ethToTakerAssetRate, dexQuotes, twoHopQuotes], + [tokenDecimals, orderFillableAmounts, ethToMakerAssetRate, ethToTakerAssetRate, dexQuotes, rawTwoHopQuotes], rfqtIndicativeQuotes, offChainBalancerQuotes, offChainCreamQuotes, ] = await Promise.all([samplerPromise, rfqtPromise, offChainBalancerPromise, offChainCreamPromise]); + + // Filter out any invalid two hop quotes where we couldn't find a route + const twoHopQuotes = rawTwoHopQuotes.filter(q => q && q.fillData && q.fillData.firstHopSource); + const [makerTokenDecimals, takerTokenDecimals] = tokenDecimals; return { side: MarketOperation.Buy, diff --git a/packages/asset-swapper/src/utils/quote_report_generator.ts b/packages/asset-swapper/src/utils/quote_report_generator.ts index 8a845bec39..088b941286 100644 --- a/packages/asset-swapper/src/utils/quote_report_generator.ts +++ b/packages/asset-swapper/src/utils/quote_report_generator.ts @@ -1,6 +1,5 @@ import { SignedOrder } from '@0x/types'; import { BigNumber } from '@0x/utils'; -import * as _ from 'lodash'; import { MarketOperation } from '../types';