From 4492dbae63f8301a9575bbd5040a8d6ed0c741e9 Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Wed, 5 Dec 2018 14:12:23 +0100 Subject: [PATCH] feeLevels in ripple --- src/actions/ripple/SendFormActions.js | 4 +- .../ripple/SendFormValidationActions.js | 75 +++++++++++++++++-- src/reducers/SendFormRippleReducer.js | 4 + 3 files changed, 75 insertions(+), 8 deletions(-) diff --git a/src/actions/ripple/SendFormActions.js b/src/actions/ripple/SendFormActions.js index ed80e0a8..17e9928d 100644 --- a/src/actions/ripple/SendFormActions.js +++ b/src/actions/ripple/SendFormActions.js @@ -45,7 +45,7 @@ export const observe = (prevState: ReducersState, action: Action): ThunkAction = // handle gasPrice update from backend // recalculate fee levels if needed if (action.type === BLOCKCHAIN.UPDATE_FEE) { - // dispatch(ValidationActions.onGasPriceUpdated(action.network, action.gasPrice)); + dispatch(ValidationActions.onFeeUpdated(action.shortcut, action.fee)); return; } @@ -94,7 +94,7 @@ export const init = (): AsyncAction => async (dispatch: Dispatch, getState: GetS return; } - const feeLevels = ValidationActions.getFeeLevels(network.symbol); + const feeLevels = dispatch(ValidationActions.getFeeLevels(network.symbol)); const selectedFeeLevel = ValidationActions.getSelectedFeeLevel(feeLevels, initialState.selectedFeeLevel); dispatch({ diff --git a/src/actions/ripple/SendFormValidationActions.js b/src/actions/ripple/SendFormValidationActions.js index 2b4f194d..c573997a 100644 --- a/src/actions/ripple/SendFormValidationActions.js +++ b/src/actions/ripple/SendFormValidationActions.js @@ -1,6 +1,7 @@ /* @flow */ import BigNumber from 'bignumber.js'; +import * as SEND from 'actions/constants/send'; import { findDevice, getPendingAmount } from 'reducers/utils'; import { toDecimalAmount } from 'utils/formatUtils'; @@ -16,6 +17,47 @@ const XRP_ADDRESS_RE = new RegExp('^r[1-9A-HJ-NP-Za-km-z]{25,34}$'); const NUMBER_RE: RegExp = new RegExp('^(0|0\\.([0-9]+)?|[1-9][0-9]*\\.?([0-9]+)?|\\.[0-9]+)$'); const XRP_6_RE = new RegExp('^(0|0\\.([0-9]{0,6})?|[1-9][0-9]*\\.?([0-9]{0,6})?|\\.[0-9]{0,6})$'); +/* +* Called from SendFormActions.observe +* Reaction for BLOCKCHAIN.FEE_UPDATED action +*/ +export const onFeeUpdated = (network: string, fee: string): PayloadAction => (dispatch: Dispatch, getState: GetState): void => { + const state = getState().sendFormRipple; + if (network === state.networkSymbol) return; + + + if (!state.untouched) { + // if there is a transaction draft let the user know + // and let him update manually + dispatch({ + type: SEND.CHANGE, + networkType: 'ripple', + state: { + ...state, + feeNeedsUpdate: true, + recommendedFee: fee, + }, + }); + return; + } + + // automatically update feeLevels and gasPrice + const feeLevels = dispatch(getFeeLevels(state.networkSymbol)); + const selectedFeeLevel = getSelectedFeeLevel(feeLevels, state.selectedFeeLevel); + dispatch({ + type: SEND.CHANGE, + networkType: 'ripple', + state: { + ...state, + feeNeedsUpdate: false, + recommendedFee: fee, + gasPrice: selectedFeeLevel.gasPrice, + feeLevels, + selectedFeeLevel, + }, + }); +}; + /* * Recalculate amount, total and fees */ @@ -170,13 +212,34 @@ const calculateMaxAmount = (balance: BigNumber, fee: string): string => { } }; -export const getFeeLevels = (shortcut: string): Array => ([ - { +export const getFeeLevels = (symbol: string): PayloadAction> => (dispatch: Dispatch, getState: GetState): Array => { + const blockchain = getState().blockchain.find(b => b.shortcut === symbol.toLowerCase()); + if (!blockchain) { + // return default fee levels (TODO: get them from config) + return [{ + value: 'Normal', + gasPrice: '0.000012', + label: `0.000012 ${symbol}`, + }]; + } + + const xrpDrops = toDecimalAmount(blockchain.fee, 6); + + // TODO: calc fee levels + return [{ value: 'Normal', - gasPrice: '1', - label: `1 ${shortcut}`, - }, -]); + gasPrice: xrpDrops, + label: `${xrpDrops} ${symbol}`, + }]; +}; + +// export const getFeeLevels = (shortcut: string): Array => ([ +// { +// value: 'Normal', +// gasPrice: '1', +// label: `1 ${shortcut}`, +// }, +// ]); export const getSelectedFeeLevel = (feeLevels: Array, selected: FeeLevel): FeeLevel => { diff --git a/src/reducers/SendFormRippleReducer.js b/src/reducers/SendFormRippleReducer.js index 90af5cac..f340936c 100644 --- a/src/reducers/SendFormRippleReducer.js +++ b/src/reducers/SendFormRippleReducer.js @@ -24,6 +24,8 @@ export type State = { setMax: boolean; feeLevels: Array; selectedFeeLevel: FeeLevel; + recommendedFee: string; + feeNeedsUpdate: boolean; sequence: string; total: string; @@ -51,6 +53,8 @@ export const initialState: State = { gasPrice: '0', value: 'Normal', }, + recommendedFee: '0', + feeNeedsUpdate: false, sequence: '0', total: '0',