diff options
author | Dan Miller <danjm.com@gmail.com> | 2018-10-10 00:35:54 +0800 |
---|---|---|
committer | Dan Miller <danjm.com@gmail.com> | 2018-12-04 11:36:05 +0800 |
commit | a2bbf504b891a63f32070961118ec1ae6fa5fdd8 (patch) | |
tree | 60add1370c5d0ab38958ba96806d692bd87c5231 /ui/app/ducks/gas.duck.js | |
parent | 2dbae581ac3f9190ddd1c3457bd51b41eef8051b (diff) | |
download | tangerine-wallet-browser-a2bbf504b891a63f32070961118ec1ae6fa5fdd8.tar tangerine-wallet-browser-a2bbf504b891a63f32070961118ec1ae6fa5fdd8.tar.gz tangerine-wallet-browser-a2bbf504b891a63f32070961118ec1ae6fa5fdd8.tar.bz2 tangerine-wallet-browser-a2bbf504b891a63f32070961118ec1ae6fa5fdd8.tar.lz tangerine-wallet-browser-a2bbf504b891a63f32070961118ec1ae6fa5fdd8.tar.xz tangerine-wallet-browser-a2bbf504b891a63f32070961118ec1ae6fa5fdd8.tar.zst tangerine-wallet-browser-a2bbf504b891a63f32070961118ec1ae6fa5fdd8.zip |
Read only connection of gas price chart to redux
Diffstat (limited to 'ui/app/ducks/gas.duck.js')
-rw-r--r-- | ui/app/ducks/gas.duck.js | 85 |
1 files changed, 81 insertions, 4 deletions
diff --git a/ui/app/ducks/gas.duck.js b/ui/app/ducks/gas.duck.js index 558047cf7..35e26544a 100644 --- a/ui/app/ducks/gas.duck.js +++ b/ui/app/ducks/gas.duck.js @@ -1,8 +1,12 @@ -import { clone } from 'ramda' +import { mockGasEstimateData } from './mock-gas-estimate-data' +import { clone, uniqBy } from 'ramda' +import BigNumber from 'bignumber.js' // Actions const BASIC_GAS_ESTIMATE_LOADING_FINISHED = 'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_FINISHED' const BASIC_GAS_ESTIMATE_LOADING_STARTED = 'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_STARTED' +const GAS_ESTIMATE_LOADING_FINISHED = 'metamask/gas/GAS_ESTIMATE_LOADING_FINISHED' +const GAS_ESTIMATE_LOADING_STARTED = 'metamask/gas/GAS_ESTIMATE_LOADING_STARTED' const RESET_CUSTOM_GAS_STATE = 'metamask/gas/RESET_CUSTOM_GAS_STATE' const RESET_CUSTOM_DATA = 'metamask/gas/RESET_CUSTOM_DATA' const SET_BASIC_GAS_ESTIMATE_DATA = 'metamask/gas/SET_BASIC_GAS_ESTIMATE_DATA' @@ -10,6 +14,7 @@ const SET_CUSTOM_GAS_ERRORS = 'metamask/gas/SET_CUSTOM_GAS_ERRORS' const SET_CUSTOM_GAS_LIMIT = 'metamask/gas/SET_CUSTOM_GAS_LIMIT' 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' // TODO: determine if this approach to initState is consistent with conventional ducks pattern const initState = { @@ -31,6 +36,8 @@ const initState = { safeLow: null, }, basicEstimateIsLoading: true, + gasEstimatesLoading: true, + priceAndTimeEstimates: [], errors: {}, } @@ -49,6 +56,16 @@ export default function reducer ({ gas: gasState = initState }, action = {}) { ...newState, basicEstimateIsLoading: false, } + case GAS_ESTIMATE_LOADING_STARTED: + return { + ...newState, + gasEstimatesLoading: true, + } + case GAS_ESTIMATE_LOADING_FINISHED: + return { + ...newState, + gasEstimatesLoading: false, + } case SET_BASIC_GAS_ESTIMATE_DATA: return { ...newState, @@ -78,6 +95,11 @@ export default function reducer ({ gas: gasState = initState }, action = {}) { total: action.value, }, } + case SET_PRICE_AND_TIME_ESTIMATES: + return { + ...newState, + priceAndTimeEstimates: action.value, + } case SET_CUSTOM_GAS_ERRORS: return { ...newState, @@ -111,7 +133,19 @@ export function basicGasEstimatesLoadingFinished () { } } -export function fetchGasEstimates () { +export function gasEstimatesLoadingStarted () { + return { + type: GAS_ESTIMATE_LOADING_STARTED, + } +} + +export function gasEstimatesLoadingFinished () { + return { + type: GAS_ESTIMATE_LOADING_FINISHED, + } +} + +export function fetchBasicGasEstimates () { return (dispatch) => { dispatch(basicGasEstimatesLoadingStarted()) @@ -137,7 +171,7 @@ export function fetchGasEstimates () { safeLowWait, speed, }) => { - dispatch(setBasicGasEstimateData({ + const basicEstimates = { average, avgWait, blockTime, @@ -149,8 +183,44 @@ export function fetchGasEstimates () { safeLow, safeLowWait, speed, - })) + } + dispatch(setBasicGasEstimateData(basicEstimates)) dispatch(basicGasEstimatesLoadingFinished()) + return basicEstimates + }) + } +} + +export function fetchGasEstimates (blockTime) { + return (dispatch) => { + dispatch(gasEstimatesLoadingStarted()) + + // TODO: uncomment code when live api is ready + // return fetch('https://ethgasstation.info/json/predictTable.json', { + // 'headers': {}, + // 'referrer': 'http://ethgasstation.info/json/', + // 'referrerPolicy': 'no-referrer-when-downgrade', + // 'body': null, + // 'method': 'GET', + // 'mode': 'cors'} + // ) + return new Promise(resolve => { + resolve(mockGasEstimateData) + }) + // .then(r => r.json()) + .then(r => { + const estimatedPricesAndTimes = r.map(({ expectedTime, expectedWait, gasprice }) => ({ expectedTime, expectedWait, gasprice })) + const estimatedTimeWithUniquePrices = uniqBy(({ expectedTime }) => expectedTime, estimatedPricesAndTimes) + const timeMappedToSeconds = estimatedTimeWithUniquePrices.map(({ expectedWait, gasprice }) => { + const expectedTime = (new BigNumber(expectedWait)).times(Number(blockTime), 10).div(60, 10).toString(10) + return { + expectedTime, + expectedWait, + gasprice, + } + }) + dispatch(setPricesAndTimeEstimates(timeMappedToSeconds.slice(1))) + dispatch(gasEstimatesLoadingFinished()) }) } } @@ -162,6 +232,13 @@ export function setBasicGasEstimateData (basicGasEstimateData) { } } +export function setPricesAndTimeEstimates (estimatedPricesAndTimes) { + return { + type: SET_PRICE_AND_TIME_ESTIMATES, + value: estimatedPricesAndTimes, + } +} + export function setCustomGasPrice (newPrice) { return { type: SET_CUSTOM_GAS_PRICE, |