2017-12-13 11:01:37 +00:00
|
|
|
/* @flow */
|
|
|
|
'use strict';
|
|
|
|
|
2018-04-11 10:13:38 +00:00
|
|
|
import * as SEND from '../actions/constants/send';
|
|
|
|
import * as WEB3 from '../actions/constants/web3';
|
2018-05-10 12:40:25 +00:00
|
|
|
import * as ACCOUNT from '../actions/constants/account';
|
2018-02-20 09:30:36 +00:00
|
|
|
import EthereumjsUnits from 'ethereumjs-units';
|
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
import { getFeeLevels } from '../actions/SendFormActions';
|
2017-12-13 11:01:37 +00:00
|
|
|
|
2018-04-23 10:20:15 +00:00
|
|
|
import type { Action } from '../flowtype';
|
2018-04-16 21:19:50 +00:00
|
|
|
import type {
|
|
|
|
Web3CreateAction,
|
|
|
|
Web3UpdateBlockAction,
|
|
|
|
Web3UpdateGasPriceAction
|
|
|
|
} from '../actions/Web3Actions';
|
|
|
|
|
2018-02-20 09:30:36 +00:00
|
|
|
export type State = {
|
2018-04-16 21:19:50 +00:00
|
|
|
+network: string;
|
2018-03-08 16:10:53 +00:00
|
|
|
+coinSymbol: string;
|
2018-05-10 10:39:49 +00:00
|
|
|
selectedCurrency: string;
|
2018-02-20 09:30:36 +00:00
|
|
|
balanceNeedUpdate: boolean;
|
|
|
|
|
|
|
|
// form fields
|
|
|
|
advanced: boolean;
|
|
|
|
untouched: boolean; // set to true when user made any changes in form
|
|
|
|
touched: {[k: string]: boolean};
|
2017-12-13 11:01:37 +00:00
|
|
|
address: string;
|
2018-02-20 09:30:36 +00:00
|
|
|
amount: string;
|
|
|
|
setMax: boolean;
|
|
|
|
feeLevels: Array<FeeLevel>;
|
2018-04-16 21:19:50 +00:00
|
|
|
selectedFeeLevel: FeeLevel;
|
2018-02-20 09:30:36 +00:00
|
|
|
recommendedGasPrice: string;
|
|
|
|
gasPriceNeedsUpdate: boolean;
|
|
|
|
gasLimit: string;
|
|
|
|
gasPrice: string;
|
2017-12-13 11:01:37 +00:00
|
|
|
data: string;
|
2018-02-20 09:30:36 +00:00
|
|
|
nonce: string;
|
|
|
|
total: string;
|
|
|
|
sending: boolean;
|
|
|
|
errors: {[k: string]: string};
|
|
|
|
warnings: {[k: string]: string};
|
|
|
|
infos: {[k: string]: string};
|
|
|
|
}
|
|
|
|
|
|
|
|
export type FeeLevel = {
|
|
|
|
label: string;
|
|
|
|
gasPrice: string;
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const initialState: State = {
|
2018-04-16 21:19:50 +00:00
|
|
|
network: '',
|
2018-03-08 16:10:53 +00:00
|
|
|
coinSymbol: '',
|
2018-05-10 10:39:49 +00:00
|
|
|
selectedCurrency: '',
|
2018-02-20 09:30:36 +00:00
|
|
|
|
|
|
|
advanced: false,
|
|
|
|
untouched: true,
|
|
|
|
touched: {},
|
|
|
|
balanceNeedUpdate: false,
|
2018-03-08 16:10:53 +00:00
|
|
|
address: '',
|
|
|
|
//address: '0x574BbB36871bA6b78E27f4B4dCFb76eA0091880B',
|
2018-02-20 09:30:36 +00:00
|
|
|
amount: '',
|
|
|
|
setMax: false,
|
|
|
|
feeLevels: [],
|
2018-04-16 21:19:50 +00:00
|
|
|
selectedFeeLevel: {
|
|
|
|
label: 'Normal',
|
|
|
|
gasPrice: '0',
|
|
|
|
value: 'Normal'
|
|
|
|
},
|
2018-02-20 09:30:36 +00:00
|
|
|
recommendedGasPrice: '0',
|
|
|
|
gasPriceNeedsUpdate: false,
|
|
|
|
gasLimit: '0',
|
|
|
|
gasPrice: '0',
|
|
|
|
data: '',
|
|
|
|
nonce: '0',
|
|
|
|
total: '0',
|
|
|
|
sending: false,
|
|
|
|
errors: {},
|
|
|
|
warnings: {},
|
|
|
|
infos: {},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-16 21:19:50 +00:00
|
|
|
const onGasPriceUpdated = (state: State, action: Web3UpdateGasPriceAction): State => {
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-04-11 10:06:46 +00:00
|
|
|
// function getRandomInt(min, max) {
|
|
|
|
// return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
|
|
// }
|
|
|
|
// const newPrice = getRandomInt(10, 50).toString();
|
2018-04-16 21:19:50 +00:00
|
|
|
const newPrice: string = EthereumjsUnits.convert(action.gasPrice, 'wei', 'gwei');
|
2018-03-29 11:38:09 +00:00
|
|
|
if (action.network === state.network && newPrice !== state.recommendedGasPrice) {
|
2018-02-20 09:30:36 +00:00
|
|
|
const newState: State = { ...state };
|
|
|
|
if (!state.untouched) {
|
|
|
|
newState.gasPriceNeedsUpdate = true;
|
|
|
|
newState.recommendedGasPrice = newPrice;
|
|
|
|
} else {
|
2018-05-11 12:40:52 +00:00
|
|
|
const newFeeLevels = getFeeLevels(state.coinSymbol, newPrice, state.gasLimit);
|
2018-04-16 21:19:50 +00:00
|
|
|
const selectedFeeLevel: ?FeeLevel = newFeeLevels.find(f => f.value === 'Normal');
|
|
|
|
if (!selectedFeeLevel) return state;
|
2018-02-20 09:30:36 +00:00
|
|
|
newState.recommendedGasPrice = newPrice;
|
|
|
|
newState.feeLevels = newFeeLevels;
|
|
|
|
newState.selectedFeeLevel = selectedFeeLevel;
|
|
|
|
newState.gasPrice = selectedFeeLevel.gasPrice;
|
|
|
|
}
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
const onBalanceUpdated = (state: State, action: any): State => {
|
|
|
|
// balanceNeedUpdate
|
|
|
|
// if (state.senderAddress === action.address) {
|
|
|
|
// return {
|
|
|
|
// ...state,
|
|
|
|
// balance: '1'
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// TODO: handle balance update during send form lifecycle
|
|
|
|
return state;
|
2017-12-13 11:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-23 10:20:15 +00:00
|
|
|
export default (state: State = initialState, action: Action): State => {
|
2017-12-13 11:01:37 +00:00
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
2018-02-20 09:30:36 +00:00
|
|
|
case SEND.INIT :
|
|
|
|
return action.state;
|
|
|
|
|
|
|
|
case SEND.DISPOSE :
|
|
|
|
return initialState;
|
|
|
|
|
|
|
|
// this will be called right after Web3 instance initialization before any view is shown
|
|
|
|
// and async during app live time
|
|
|
|
case WEB3.GAS_PRICE_UPDATED :
|
|
|
|
return onGasPriceUpdated(state, action);
|
|
|
|
|
2018-05-10 12:40:25 +00:00
|
|
|
case ACCOUNT.SET_BALANCE :
|
2018-02-20 09:30:36 +00:00
|
|
|
return onBalanceUpdated(state, action);
|
|
|
|
|
|
|
|
case SEND.TOGGLE_ADVANCED :
|
2017-12-13 11:01:37 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2018-02-20 09:30:36 +00:00
|
|
|
advanced: !state.advanced
|
2017-12-13 11:01:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-20 09:30:36 +00:00
|
|
|
|
|
|
|
// user actions
|
|
|
|
case SEND.ADDRESS_CHANGE :
|
|
|
|
case SEND.AMOUNT_CHANGE :
|
|
|
|
case SEND.SET_MAX :
|
|
|
|
case SEND.CURRENCY_CHANGE :
|
|
|
|
case SEND.FEE_LEVEL_CHANGE :
|
|
|
|
case SEND.UPDATE_FEE_LEVELS :
|
|
|
|
case SEND.GAS_PRICE_CHANGE :
|
|
|
|
case SEND.GAS_LIMIT_CHANGE :
|
|
|
|
case SEND.DATA_CHANGE :
|
|
|
|
return action.state;
|
|
|
|
|
|
|
|
case SEND.SEND :
|
2017-12-13 11:01:37 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2018-02-20 09:30:36 +00:00
|
|
|
sending: true,
|
2017-12-13 11:01:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-20 09:30:36 +00:00
|
|
|
case SEND.TX_COMPLETE :
|
2017-12-13 11:01:37 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2018-03-08 16:10:53 +00:00
|
|
|
|
2018-02-20 09:30:36 +00:00
|
|
|
sending: false,
|
2018-03-08 16:10:53 +00:00
|
|
|
touched: {},
|
|
|
|
address: '',
|
|
|
|
amount: '',
|
|
|
|
setMax: false,
|
|
|
|
gasPriceNeedsUpdate: false,
|
|
|
|
gasLimit: state.gasLimit,
|
|
|
|
gasPrice: state.recommendedGasPrice,
|
|
|
|
data: '',
|
|
|
|
nonce: '0',
|
|
|
|
total: '0',
|
|
|
|
errors: {},
|
|
|
|
warnings: {},
|
|
|
|
infos: {},
|
|
|
|
|
2017-12-13 11:01:37 +00:00
|
|
|
}
|
2018-02-20 09:30:36 +00:00
|
|
|
case SEND.TX_ERROR :
|
2017-12-13 11:01:37 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2018-02-20 09:30:36 +00:00
|
|
|
sending: false,
|
2017-12-13 11:01:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-20 09:30:36 +00:00
|
|
|
|
|
|
|
case SEND.VALIDATION :
|
2017-12-13 11:01:37 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2018-02-20 09:30:36 +00:00
|
|
|
errors: action.errors,
|
|
|
|
warnings: action.warnings,
|
|
|
|
infos: action.infos,
|
2017-12-13 11:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|