diff options
author | Dan Miller <danjm.com@gmail.com> | 2018-11-14 00:36:52 +0800 |
---|---|---|
committer | Dan Miller <danjm.com@gmail.com> | 2018-12-04 11:36:22 +0800 |
commit | 7f2c5c09de67a67972fcbaae254d39aac6c96f56 (patch) | |
tree | 827332f85c8086dbdb427d6addbf10eef03a9bb6 /ui/app/ducks/gas.duck.js | |
parent | fe535159bb3ec5849d670d9bc53067f5d6f330b7 (diff) | |
download | tangerine-wallet-browser-7f2c5c09de67a67972fcbaae254d39aac6c96f56.tar tangerine-wallet-browser-7f2c5c09de67a67972fcbaae254d39aac6c96f56.tar.gz tangerine-wallet-browser-7f2c5c09de67a67972fcbaae254d39aac6c96f56.tar.bz2 tangerine-wallet-browser-7f2c5c09de67a67972fcbaae254d39aac6c96f56.tar.lz tangerine-wallet-browser-7f2c5c09de67a67972fcbaae254d39aac6c96f56.tar.xz tangerine-wallet-browser-7f2c5c09de67a67972fcbaae254d39aac6c96f56.tar.zst tangerine-wallet-browser-7f2c5c09de67a67972fcbaae254d39aac6c96f56.zip |
Uses more reliable api on main send screen; caches basic api results in modal
Diffstat (limited to 'ui/app/ducks/gas.duck.js')
-rw-r--r-- | ui/app/ducks/gas.duck.js | 98 |
1 files changed, 85 insertions, 13 deletions
diff --git a/ui/app/ducks/gas.duck.js b/ui/app/ducks/gas.duck.js index 6ae927b12..ffd1e773f 100644 --- a/ui/app/ducks/gas.duck.js +++ b/ui/app/ducks/gas.duck.js @@ -19,6 +19,7 @@ const SET_CUSTOM_GAS_PRICE = 'metamask/gas/SET_CUSTOM_GAS_PRICE' const SET_CUSTOM_GAS_TOTAL = 'metamask/gas/SET_CUSTOM_GAS_TOTAL' const SET_PRICE_AND_TIME_ESTIMATES = 'metamask/gas/SET_PRICE_AND_TIME_ESTIMATES' const SET_API_ESTIMATES_LAST_RETRIEVED = 'metamask/gas/SET_API_ESTIMATES_LAST_RETRIEVED' +const SET_BASIC_API_ESTIMATES_LAST_RETRIEVED = 'metamask/gas/SET_BASIC_API_ESTIMATES_LAST_RETRIEVED' // TODO: determine if this approach to initState is consistent with conventional ducks pattern const initState = { @@ -42,7 +43,9 @@ const initState = { basicEstimateIsLoading: true, gasEstimatesLoading: true, priceAndTimeEstimates: [], + basicPriceAndTimeEstimates: [], priceAndTimeEstimatesLastRetrieved: 0, + basicPriceAndTimeEstimatesLastRetrieved: 0, errors: {}, } @@ -118,6 +121,11 @@ export default function reducer ({ gas: gasState = initState }, action = {}) { ...newState, priceAndTimeEstimatesLastRetrieved: action.value, } + case SET_BASIC_API_ESTIMATES_LAST_RETRIEVED: + return { + ...newState, + basicPriceAndTimeEstimatesLastRetrieved: action.value, + } case RESET_CUSTOM_DATA: return { ...newState, @@ -159,9 +167,9 @@ export function fetchBasicGasEstimates () { return (dispatch) => { dispatch(basicGasEstimatesLoadingStarted()) - return fetch('https://ethgasstation.info/json/ethgasAPI.json', { + return fetch('https://dev.blockscale.net/api/gasexpress.json', { 'headers': {}, - 'referrer': 'http://ethgasstation.info/json/', + 'referrer': 'https://dev.blockscale.net/api/', 'referrerPolicy': 'no-referrer-when-downgrade', 'body': null, 'method': 'GET', @@ -169,23 +177,53 @@ export function fetchBasicGasEstimates () { ) .then(r => r.json()) .then(({ - average, - avgWait, - block_time: blockTime, - blockNum, + safeLow, + standard: average, fast, fastest, - fastestWait, - fastWait, - safeLow, - safeLowWait, - speed, + block_time: blockTime, + blockNum, }) => { const basicEstimates = { + safeLow, average, - avgWait, + fast, + fastest, blockTime, blockNum, + } + dispatch(setBasicGasEstimateData(basicEstimates)) + dispatch(basicGasEstimatesLoadingFinished()) + return basicEstimates + }) + } +} + +export function fetchBasicGasAndTimeEstimates () { + return (dispatch, getState) => { + const { + basicPriceAndTimeEstimatesLastRetrieved, + basicPriceAndTimeEstimates, + } = getState().gas + const timeLastRetrieved = basicPriceAndTimeEstimatesLastRetrieved || loadLocalStorageData('BASIC_GAS_AND_TIME_API_ESTIMATES_LAST_RETRIEVED') || 0 + + dispatch(basicGasEstimatesLoadingStarted()) + + const promiseToFetch = Date.now() - timeLastRetrieved > 75000 + ? fetch('https://ethgasstation.info/json/ethgasAPI.json', { + 'headers': {}, + 'referrer': 'http://ethgasstation.info/json/', + 'referrerPolicy': 'no-referrer-when-downgrade', + 'body': null, + 'method': 'GET', + 'mode': 'cors'} + ) + .then(r => r.json()) + .then(({ + average, + avgWait, + block_time: blockTime, + blockNum, fast, fastest, fastestWait, @@ -193,7 +231,34 @@ export function fetchBasicGasEstimates () { safeLow, safeLowWait, speed, - } + }) => { + const basicEstimates = { + average, + avgWait, + blockTime, + blockNum, + fast, + fastest, + fastestWait, + fastWait, + safeLow, + safeLowWait, + speed, + } + + const timeRetrieved = Date.now() + dispatch(setBasicApiEstimatesLastRetrieved(timeRetrieved)) + saveLocalStorageData(timeRetrieved, 'BASIC_GAS_AND_TIME_API_ESTIMATES_LAST_RETRIEVED') + saveLocalStorageData(basicEstimates, 'BASIC_GAS_AND_TIME_API_ESTIMATES') + + return basicEstimates + }) + : Promise.resolve(basicPriceAndTimeEstimates.length + ? basicPriceAndTimeEstimates + : loadLocalStorageData('BASIC_GAS_AND_TIME_API_ESTIMATES') + ) + + return promiseToFetch.then(basicEstimates => { dispatch(setBasicGasEstimateData(basicEstimates)) dispatch(basicGasEstimatesLoadingFinished()) return basicEstimates @@ -301,6 +366,13 @@ export function setApiEstimatesLastRetrieved (retrievalTime) { } } +export function setBasicApiEstimatesLastRetrieved (retrievalTime) { + return { + type: SET_BASIC_API_ESTIMATES_LAST_RETRIEVED, + value: retrievalTime, + } +} + export function resetCustomGasState () { return { type: RESET_CUSTOM_GAS_STATE } } |