aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/ducks
diff options
context:
space:
mode:
authorDan Miller <danjm.com@gmail.com>2018-08-16 20:28:27 +0800
committerDan Miller <danjm.com@gmail.com>2018-12-04 11:36:04 +0800
commit0a7dfcd55d02a7204d8f0773ff9d91f325aabea8 (patch)
treea4c7d3e219ca926f17f26d020fddc78854a23b53 /ui/app/ducks
parent112d18e316df312a648b8c8ac17c201314fc9ed6 (diff)
downloadtangerine-wallet-browser-0a7dfcd55d02a7204d8f0773ff9d91f325aabea8.tar.gz
tangerine-wallet-browser-0a7dfcd55d02a7204d8f0773ff9d91f325aabea8.tar.zst
tangerine-wallet-browser-0a7dfcd55d02a7204d8f0773ff9d91f325aabea8.zip
Connect the gas-button-group component to redux and a live api.
Diffstat (limited to 'ui/app/ducks')
-rw-r--r--ui/app/ducks/custom-gas.js67
-rw-r--r--ui/app/ducks/gas.duck.js189
2 files changed, 189 insertions, 67 deletions
diff --git a/ui/app/ducks/custom-gas.js b/ui/app/ducks/custom-gas.js
deleted file mode 100644
index f1f483e93..000000000
--- a/ui/app/ducks/custom-gas.js
+++ /dev/null
@@ -1,67 +0,0 @@
-import extend from 'xtend'
-
-// Actions
-const SET_CUSTOM_GAS_PRICE = 'metamask/custom-gas/SET_CUSTOM_GAS_PRICE'
-const SET_CUSTOM_GAS_LIMIT = 'metamask/custom-gas/SET_CUSTOM_GAS_LIMIT'
-const SET_CUSTOM_GAS_ERRORS = 'metamask/custom-gas/SET_CUSTOM_GAS_ERRORS'
-const RESET_CUSTOM_GAS_STATE = 'metamask/custom-gas/RESET_CUSTOM_GAS_STATE'
-
-// TODO: determine if this approach to initState is consistent with conventional ducks pattern
-const initState = {
- price: 0,
- limit: 21000,
- errors: {},
-}
-
-// Reducer
-export default function reducer ({ customGas: customGasState = initState }, action = {}) {
- const newState = extend({}, customGasState)
-
- switch (action.type) {
- case SET_CUSTOM_GAS_PRICE:
- return extend(newState, {
- price: action.value,
- })
- case SET_CUSTOM_GAS_LIMIT:
- return extend(newState, {
- limit: action.value,
- })
- case SET_CUSTOM_GAS_ERRORS:
- return extend(newState, {
- errors: {
- ...newState.errors,
- ...action.value,
- },
- })
- case RESET_CUSTOM_GAS_STATE:
- return extend({}, initState)
- default:
- return newState
- }
-}
-
-// Action Creators
-export function setCustomGasPrice (newPrice) {
- return {
- type: SET_CUSTOM_GAS_PRICE,
- value: newPrice,
- }
-}
-
-export function setCustomGasLimit (newLimit) {
- return {
- type: SET_CUSTOM_GAS_LIMIT,
- value: newLimit,
- }
-}
-
-export function setCustomGasErrors (newErrors) {
- return {
- type: SET_CUSTOM_GAS_ERRORS,
- value: newErrors,
- }
-}
-
-export function resetCustomGasState () {
- return { type: RESET_CUSTOM_GAS_STATE }
-}
diff --git a/ui/app/ducks/gas.duck.js b/ui/app/ducks/gas.duck.js
new file mode 100644
index 000000000..8b2fbcfdb
--- /dev/null
+++ b/ui/app/ducks/gas.duck.js
@@ -0,0 +1,189 @@
+import { clone } from 'ramda'
+
+// 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 RESET_CUSTOM_GAS_STATE = 'metamask/gas/RESET_CUSTOM_GAS_STATE'
+const SET_BASIC_GAS_ESTIMATE_DATA = 'metamask/gas/SET_BASIC_GAS_ESTIMATE_DATA'
+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'
+
+// TODO: determine if this approach to initState is consistent with conventional ducks pattern
+const initState = {
+ customData: {
+ price: 0,
+ limit: 21000,
+ },
+ basicEstimates: {
+ average: null,
+ fastestWait: null,
+ fastWait: null,
+ fast: null,
+ safeLowWait: null,
+ blockNum: null,
+ avgWait: null,
+ blockTime: null,
+ speed: null,
+ fastest: null,
+ safeLow: null,
+ },
+ basicEstimateIsLoading: true,
+ errors: {},
+}
+
+// Reducer
+export default function reducer ({ gas: gasState = initState }, action = {}) {
+ const newState = clone(gasState)
+
+ switch (action.type) {
+ case BASIC_GAS_ESTIMATE_LOADING_STARTED:
+ return {
+ ...newState,
+ basicEstimateIsLoading: true,
+ }
+ case BASIC_GAS_ESTIMATE_LOADING_FINISHED:
+ return {
+ ...newState,
+ basicEstimateIsLoading: false,
+ }
+ case SET_BASIC_GAS_ESTIMATE_DATA:
+ return {
+ ...newState,
+ basicEstimates: action.value,
+ }
+ case SET_CUSTOM_GAS_PRICE:
+ return {
+ ...newState,
+ customData: {
+ ...newState.customData,
+ price: action.value,
+ },
+ }
+ case SET_CUSTOM_GAS_LIMIT:
+ return {
+ ...newState,
+ customData: {
+ ...newState.customData,
+ limit: action.value,
+ },
+ }
+ case SET_CUSTOM_GAS_TOTAL:
+ return {
+ ...newState,
+ customData: {
+ ...newState.customData,
+ total: action.value,
+ },
+ }
+ case SET_CUSTOM_GAS_ERRORS:
+ return {
+ ...newState,
+ errors: {
+ ...newState.errors,
+ ...action.value,
+ },
+ }
+ case RESET_CUSTOM_GAS_STATE:
+ return clone(initState)
+ default:
+ return newState
+ }
+}
+
+// Action Creators
+export function basicGasEstimatesLoadingStarted () {
+ return {
+ type: BASIC_GAS_ESTIMATE_LOADING_STARTED,
+ }
+}
+
+export function basicGasEstimatesLoadingFinished () {
+ return {
+ type: BASIC_GAS_ESTIMATE_LOADING_FINISHED,
+ }
+}
+
+export function fetchGasEstimates () {
+ return (dispatch) => {
+ dispatch(basicGasEstimatesLoadingStarted())
+
+ return 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,
+ fastWait,
+ safeLow,
+ safeLowWait,
+ speed,
+ }) => {
+ dispatch(setBasicGasEstimateData({
+ average,
+ avgWait,
+ blockTime,
+ blockNum,
+ fast,
+ fastest,
+ fastestWait,
+ fastWait,
+ safeLow,
+ safeLowWait,
+ speed,
+ }))
+ dispatch(basicGasEstimatesLoadingFinished())
+ })
+ }
+}
+
+export function setBasicGasEstimateData (basicGasEstimateData) {
+ return {
+ type: SET_BASIC_GAS_ESTIMATE_DATA,
+ value: basicGasEstimateData,
+ }
+}
+
+export function setCustomGasPrice (newPrice) {
+ return {
+ type: SET_CUSTOM_GAS_PRICE,
+ value: newPrice,
+ }
+}
+
+export function setCustomGasLimit (newLimit) {
+ return {
+ type: SET_CUSTOM_GAS_LIMIT,
+ value: newLimit,
+ }
+}
+
+export function setCustomGasTotal (newTotal) {
+ return {
+ type: SET_CUSTOM_GAS_TOTAL,
+ value: newTotal,
+ }
+}
+
+export function setCustomGasErrors (newErrors) {
+ return {
+ type: SET_CUSTOM_GAS_ERRORS,
+ value: newErrors,
+ }
+}
+
+export function resetCustomGasState () {
+ return { type: RESET_CUSTOM_GAS_STATE }
+}