Simplify _findTopQuoteAggId call in fetchAndSetQuotes (#9542)

feature/default_network_editable
Erik Marks 4 years ago committed by GitHub
parent 5fd4657588
commit d35db87447
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      app/scripts/controllers/swaps.js
  2. 4
      test/unit/app/controllers/swaps-test.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()

@ -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)
})
})

Loading…
Cancel
Save