Skip to content

Commit

Permalink
Validate refresh response, don't provide defaultValue
Browse files Browse the repository at this point in the history
  • Loading branch information
darkwing committed Dec 15, 2020
1 parent 8c7d2a5 commit 24dd8fe
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
6 changes: 2 additions & 4 deletions app/scripts/controllers/swaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const initialState = {
topAggId: null,
routeState: '',
swapsFeatureIsLive: false,
swapsQuoteRefreshTime: 0,
swapsQuoteRefreshTime: FALLBACK_QUOTE_REFRESH_TIME,
},
}

Expand Down Expand Up @@ -115,9 +115,7 @@ export default class SwapsController {
// Default to fallback time unless API returns valid response
let swapsQuoteRefreshTime = FALLBACK_QUOTE_REFRESH_TIME
try {
swapsQuoteRefreshTime = await this._fetchSwapsQuoteRefreshTime(
FALLBACK_QUOTE_REFRESH_TIME,
)
swapsQuoteRefreshTime = await this._fetchSwapsQuoteRefreshTime()
} catch (e) {
console.error('Request for swaps quote refresh time failed: ', e)
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/app/controllers/swaps-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const EMPTY_INIT_STATE = {
topAggId: null,
routeState: '',
swapsFeatureIsLive: false,
swapsQuoteRefreshTime: 0,
swapsQuoteRefreshTime: 60000,
},
}

Expand Down
1 change: 1 addition & 0 deletions ui/app/ducks/swaps/swaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ export const navigateBackToBuildQuote = (history) => {
return async (dispatch) => {
// TODO: Ensure any fetch in progress is cancelled
dispatch(navigatedBackToBuildQuote())

history.push(BUILD_QUOTE_ROUTE)
}
}
Expand Down
1 change: 0 additions & 1 deletion ui/app/helpers/utils/fetch-with-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const fetchWithCache = async (
cachedResponse: responseJson,
cachedTime: currentTime,
}

cachedFetch[url] = cacheEntry
await setStorageItem('cachedFetch', cachedFetch)
return responseJson
Expand Down
8 changes: 5 additions & 3 deletions ui/app/pages/swaps/swaps.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,19 +332,21 @@ export async function fetchSwapsFeatureLiveness() {
return status?.active
}

export async function fetchSwapsQuoteRefreshTime(defaultValue = 0) {
export async function fetchSwapsQuoteRefreshTime() {
const response = await fetchWithCache(
getBaseApi('refreshTime'),
{ method: 'GET' },
{ cacheRefreshTime: 600000 },
)

// We presently use milliseconds in the UI
if (response?.seconds) {
if (typeof response?.seconds === 'number' && response.seconds > 0) {
return response.seconds * 1000
}

return defaultValue
throw new Error(
`MetaMask - refreshTime provided invalid response: ${response}`,
)
}

export async function fetchTokenPrice(address) {
Expand Down

0 comments on commit 24dd8fe

Please sign in to comment.