From d35db874475dc7013bb25e02ff4964929fbe7ede Mon Sep 17 00:00:00 2001 From: Erik Marks <25517051+rekmarks@users.noreply.github.com> Date: Fri, 9 Oct 2020 11:00:20 -0700 Subject: [PATCH] Simplify _findTopQuoteAggId call in fetchAndSetQuotes (#9542) --- app/scripts/controllers/swaps.js | 28 ++++++++----------------- test/unit/app/controllers/swaps-test.js | 4 ++-- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/app/scripts/controllers/swaps.js b/app/scripts/controllers/swaps.js index 91a025cff..7a9568863 100644 --- a/app/scripts/controllers/swaps.js +++ b/app/scripts/controllers/swaps.js @@ -164,33 +164,23 @@ export default class SwapsController { } } - let topAggIdData - let topAggId - let isBest + let topAggId = null // We can reduce time on the loading screen by only doing this after the // loading screen and best quote have rendered. if (!approvalRequired && !fetchParams?.balanceError) { newQuotes = await this.getAllQuotesWithGasEstimates(newQuotes) + } - if (Object.values(newQuotes).length === 0) { - this.setSwapsErrorKey(QUOTES_NOT_AVAILABLE_ERROR) - } else { - const { - topAggId: topAggIdAfterModifications, - isBest: isBestAfterModifications, - } = await this._findTopQuoteAggId(newQuotes) - topAggId = topAggIdAfterModifications - isBest = isBestAfterModifications - } + if (Object.values(newQuotes).length === 0) { + this.setSwapsErrorKey(QUOTES_NOT_AVAILABLE_ERROR) } else { - topAggIdData = await this._findTopQuoteAggId(newQuotes) - topAggId = topAggIdData.topAggId - isBest = topAggIdData.isBest - } + const topAggData = await this._findTopQuoteAggId(newQuotes) - if (isBest) { - newQuotes[topAggId].isBestQuote = true + if (topAggData.topAggId) { + topAggId = topAggData.topAggId + newQuotes[topAggId].isBestQuote = topAggData.isBest + } } const { swapsState } = this.store.getState() diff --git a/test/unit/app/controllers/swaps-test.js b/test/unit/app/controllers/swaps-test.js index 703283f82..b6159dafd 100644 --- a/test/unit/app/controllers/swaps-test.js +++ b/test/unit/app/controllers/swaps-test.js @@ -404,7 +404,7 @@ describe('SwapsController', function () { assert.strictEqual(newQuotes[topAggId].isBestQuote, true) }) - it('does not set isBestQuote if no conversion rate exists for destination token', async function () { + it('does not mark as best quote if no conversion rate exists for destination token', async function () { fetchTradesInfoStub.resolves(MOCK_QUOTES) // Make it so approval is not required @@ -420,7 +420,7 @@ describe('SwapsController', function () { MOCK_FETCH_METADATA, ) - assert.strictEqual(newQuotes[topAggId].isBestQuote, undefined) + assert.strictEqual(newQuotes[topAggId].isBestQuote, false) }) })