1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-04 14:18:12 +00:00
trezor-wallet/src/js/reducers/SendFormReducer.js

210 lines
5.4 KiB
JavaScript
Raw Normal View History

2017-12-13 11:01:37 +00:00
/* @flow */
'use strict';
2018-02-20 09:30:36 +00:00
import { LOCATION_CHANGE } from 'react-router-redux';
import * as SEND from '../actions/constants/SendForm';
import * as WEB3 from '../actions/constants/Web3';
import * as ADDRESS from '../actions/constants/Address';
import EthereumjsUnits from 'ethereumjs-units';
import BigNumber from 'bignumber.js';
import { getFeeLevels } from '../actions/SendFormActions';
2017-12-13 11:01:37 +00:00
2018-02-20 09:30:36 +00:00
export type State = {
+deviceState: ?string;
+deviceId: ?string;
+deviceInstance: ?string;
2018-02-20 09:30:36 +00:00
+accountIndex: number;
+network: string;
2018-03-08 16:10:53 +00:00
+coinSymbol: string;
2018-02-20 09:30:36 +00:00
token: string;
location: string;
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>;
selectedFeeLevel: ?FeeLevel;
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 = {
deviceState: null,
deviceId: null,
deviceInstance: null,
2018-02-20 09:30:36 +00:00
accountIndex: 0,
network: '',
2018-03-08 16:10:53 +00:00
coinSymbol: '',
2018-02-20 09:30:36 +00:00
token: '',
location: '',
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: [],
selectedFeeLevel: null,
recommendedGasPrice: '0',
gasPriceNeedsUpdate: false,
gasLimit: '0',
gasPrice: '0',
data: '',
nonce: '0',
total: '0',
sending: false,
errors: {},
warnings: {},
infos: {},
}
const onGasPriceUpdated = (state: State, action: any): State => {
// function getRandomInt(min, max) {
// return Math.floor(Math.random() * (max - min + 1)) + min;
// }
// const newPrice = getRandomInt(10, 50).toString();
const newPrice = EthereumjsUnits.convert(action.gasPrice, 'wei', 'gwei');
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 {
const newFeeLevels = getFeeLevels(state.network, newPrice, state.gasLimit);
2018-02-20 09:30:36 +00:00
const selectedFeeLevel = newFeeLevels.find(f => f.value === 'Normal');
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
}
export default (state: State = initialState, action: any): State => {
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);
case ADDRESS.SET_BALANCE :
// case ADDRESS.SET_TOKEN_BALANCE :
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;
}
}