1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-24 09:18:09 +00:00

refactor AddressActions to AccountActions

This commit is contained in:
Szymon Lesisz 2018-05-10 14:40:25 +02:00
parent 9bf1f61495
commit dfcca2f959
13 changed files with 57 additions and 70 deletions

View File

@ -1,23 +1,23 @@
/* @flow */ /* @flow */
'use strict'; 'use strict';
import * as ADDRESS from './constants/address'; import * as ACCOUNT from './constants/account';
import type { Action, TrezorDevice } from '../flowtype'; import type { Action, TrezorDevice } from '../flowtype';
import type { State } from '../reducers/AccountsReducer'; import type { State } from '../reducers/AccountsReducer';
export type AddressAction = export type AccountAction =
AddressFromStorageAction AccountFromStorageAction
| AddressCreateAction | AccountCreateAction
| AddressSetBalanceAction | AccountSetBalanceAction
| AddressSetNonceAction; | AccountSetNonceAction;
export type AddressFromStorageAction = { export type AccountFromStorageAction = {
type: typeof ADDRESS.FROM_STORAGE, type: typeof ACCOUNT.FROM_STORAGE,
payload: State payload: State
} }
export type AddressCreateAction = { export type AccountCreateAction = {
type: typeof ADDRESS.CREATE, type: typeof ACCOUNT.CREATE,
device: TrezorDevice, device: TrezorDevice,
network: string, network: string,
index: number, index: number,
@ -25,16 +25,16 @@ export type AddressCreateAction = {
address: string address: string
} }
export type AddressSetBalanceAction = { export type AccountSetBalanceAction = {
type: typeof ADDRESS.SET_BALANCE, type: typeof ACCOUNT.SET_BALANCE,
address: string, address: string,
network: string, network: string,
deviceState: string, deviceState: string,
balance: string balance: string
} }
export type AddressSetNonceAction = { export type AccountSetNonceAction = {
type: typeof ADDRESS.SET_NONCE, type: typeof ACCOUNT.SET_NONCE,
address: string, address: string,
network: string, network: string,
deviceState: string, deviceState: string,
@ -43,7 +43,7 @@ export type AddressSetNonceAction = {
export const setBalance = (address: string, network: string, deviceState: string, balance: string): Action => { export const setBalance = (address: string, network: string, deviceState: string, balance: string): Action => {
return { return {
type: ADDRESS.SET_BALANCE, type: ACCOUNT.SET_BALANCE,
address, address,
network, network,
deviceState, deviceState,
@ -53,7 +53,7 @@ export const setBalance = (address: string, network: string, deviceState: string
export const setNonce = (address: string, network: string, deviceState: string, nonce: number): Action => { export const setNonce = (address: string, network: string, deviceState: string, nonce: number): Action => {
return { return {
type: ADDRESS.SET_NONCE, type: ACCOUNT.SET_NONCE,
address, address,
network, network,
deviceState, deviceState,

View File

@ -4,10 +4,10 @@
import TrezorConnect from 'trezor-connect'; import TrezorConnect from 'trezor-connect';
import { findSelectedDevice } from '../reducers/TrezorConnectReducer'; import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
import * as DISCOVERY from './constants/discovery'; import * as DISCOVERY from './constants/discovery';
import * as ADDRESS from './constants/address'; import * as ACCOUNT from './constants/account';
import * as TOKEN from './constants/token'; import * as TOKEN from './constants/token';
import * as NOTIFICATION from './constants/notification'; import * as NOTIFICATION from './constants/notification';
import * as AddressActions from '../actions/AddressActions'; import * as AccountsActions from '../actions/AccountsActions';
import HDKey from 'hdkey'; import HDKey from 'hdkey';
import EthereumjsUtil from 'ethereumjs-util'; import EthereumjsUtil from 'ethereumjs-util';
@ -199,7 +199,7 @@ const discoverAddress = (device: TrezorDevice, discoveryProcess: Discovery): Asy
const network = discoveryProcess.network; const network = discoveryProcess.network;
dispatch({ dispatch({
type: ADDRESS.CREATE, type: ACCOUNT.CREATE,
device, device,
network, network,
index: discoveryProcess.accountIndex, index: discoveryProcess.accountIndex,
@ -285,7 +285,7 @@ const discoverAddress = (device: TrezorDevice, discoveryProcess: Discovery): Asy
const balance = await getBalanceAsync(web3instance.web3, ethAddress); const balance = await getBalanceAsync(web3instance.web3, ethAddress);
if (discoveryProcess.interrupted) return; if (discoveryProcess.interrupted) return;
dispatch( dispatch(
AddressActions.setBalance(ethAddress, network, device.state || 'undefined', web3instance.web3.fromWei(balance.toString(), 'ether')) AccountsActions.setBalance(ethAddress, network, device.state || 'undefined', web3instance.web3.fromWei(balance.toString(), 'ether'))
); );
const userTokens = []; const userTokens = [];
@ -302,7 +302,7 @@ const discoverAddress = (device: TrezorDevice, discoveryProcess: Discovery): Asy
const nonce: number = await getNonceAsync(web3instance.web3, ethAddress); const nonce: number = await getNonceAsync(web3instance.web3, ethAddress);
if (discoveryProcess.interrupted) return; if (discoveryProcess.interrupted) return;
dispatch(AddressActions.setNonce(ethAddress, network, device.state || 'undefined', nonce)); dispatch(AccountsActions.setNonce(ethAddress, network, device.state || 'undefined', nonce));
const addressIsEmpty = nonce < 1 && !balance.greaterThan(0); const addressIsEmpty = nonce < 1 && !balance.greaterThan(0);

View File

@ -2,7 +2,7 @@
'use strict'; 'use strict';
import * as CONNECT from './constants/TrezorConnect'; import * as CONNECT from './constants/TrezorConnect';
import * as ADDRESS from './constants/address'; import * as ACCOUNT from './constants/account';
import * as TOKEN from './constants/token'; import * as TOKEN from './constants/token';
import * as DISCOVERY from './constants/discovery'; import * as DISCOVERY from './constants/discovery';
import * as STORAGE from './constants/localStorage'; import * as STORAGE from './constants/localStorage';
@ -104,7 +104,7 @@ export function loadTokensFromJSON(): AsyncAction {
const accounts: ?string = get('accounts'); const accounts: ?string = get('accounts');
if (accounts) { if (accounts) {
dispatch({ dispatch({
type: ADDRESS.FROM_STORAGE, type: ACCOUNT.FROM_STORAGE,
payload: JSON.parse(accounts) payload: JSON.parse(accounts)
}) })
} }

View File

@ -4,7 +4,6 @@
import EthereumjsUtil from 'ethereumjs-util'; import EthereumjsUtil from 'ethereumjs-util';
import * as SUMMARY from './constants/summary'; import * as SUMMARY from './constants/summary';
import * as TOKEN from './constants/token'; import * as TOKEN from './constants/token';
import * as ADDRESS from './constants/address';
import { resolveAfter } from '../utils/promiseUtils'; import { resolveAfter } from '../utils/promiseUtils';
import { initialState } from '../reducers/SummaryReducer'; import { initialState } from '../reducers/SummaryReducer';
import { findSelectedDevice } from '../reducers/TrezorConnectReducer'; import { findSelectedDevice } from '../reducers/TrezorConnectReducer';

View File

@ -2,7 +2,6 @@
'use strict'; 'use strict';
import TrezorConnect, { UI, DEVICE, DEVICE_EVENT, UI_EVENT, TRANSPORT_EVENT } from 'trezor-connect'; import TrezorConnect, { UI, DEVICE, DEVICE_EVENT, UI_EVENT, TRANSPORT_EVENT } from 'trezor-connect';
import * as ADDRESS from './constants/address';
import * as TOKEN from './constants/token'; import * as TOKEN from './constants/token';
import * as CONNECT from './constants/TrezorConnect'; import * as CONNECT from './constants/TrezorConnect';
import * as NOTIFICATION from './constants/notification'; import * as NOTIFICATION from './constants/notification';

View File

@ -7,10 +7,9 @@ import EthereumjsUtil from 'ethereumjs-util';
import EthereumjsTx from 'ethereumjs-tx'; import EthereumjsTx from 'ethereumjs-tx';
import TrezorConnect from 'trezor-connect'; import TrezorConnect from 'trezor-connect';
import { strip } from '../utils/ethUtils'; import { strip } from '../utils/ethUtils';
import * as ADDRESS from './constants/address';
import * as WEB3 from './constants/web3'; import * as WEB3 from './constants/web3';
import * as PENDING from './constants/pendingTx'; import * as PENDING from './constants/pendingTx';
import * as AddressActions from '../actions/AddressActions'; import * as AccountsActions from '../actions/AccountsActions';
import * as TokenActions from '../actions/TokenActions'; import * as TokenActions from '../actions/TokenActions';
import type { import type {
@ -172,7 +171,7 @@ export function init(instance: ?Web3, coinIndex: number = 0): AsyncAction {
for (const account of accounts) { for (const account of accounts) {
const nonce = await getNonceAsync(web3, account.address); const nonce = await getNonceAsync(web3, account.address);
if (nonce !== account.nonce) { if (nonce !== account.nonce) {
dispatch(AddressActions.setNonce(account.address, account.network, account.deviceState, nonce)); dispatch(AccountsActions.setNonce(account.address, account.network, account.deviceState, nonce));
// dispatch( getBalance(account) ); // dispatch( getBalance(account) );
// TODO: check if nonce was updated, // TODO: check if nonce was updated,
@ -246,7 +245,7 @@ export function getBalance(account: Account): AsyncAction {
if (!error) { if (!error) {
const newBalance: string = web3.fromWei(balance.toString(), 'ether'); const newBalance: string = web3.fromWei(balance.toString(), 'ether');
if (account.balance !== newBalance) { if (account.balance !== newBalance) {
dispatch(AddressActions.setBalance( dispatch(AccountsActions.setBalance(
account.address, account.address,
account.network, account.network,
account.deviceState, account.deviceState,
@ -292,7 +291,7 @@ export function getNonce(account: Account): AsyncAction {
web3.eth.getTransactionCount(account.address, (error: Error, result: number) => { web3.eth.getTransactionCount(account.address, (error: Error, result: number) => {
if (!error) { if (!error) {
if (account.nonce !== result) { if (account.nonce !== result) {
dispatch(AddressActions.setNonce(account.address, account.network, account.deviceState, result)); dispatch(AccountsActions.setNonce(account.address, account.network, account.deviceState, result));
} }
} }
}); });

View File

@ -4,8 +4,8 @@
export const INIT: 'account__init' = 'account__init'; export const INIT: 'account__init' = 'account__init';
export const DISPOSE: 'account__dispose' = 'account__dispose'; export const DISPOSE: 'account__dispose' = 'account__dispose';
// export const CREATE: 'address__create' = 'address__create'; export const CREATE: 'account__create' = 'account__create';
// export const REMOVE: 'address__remove' = 'address__remove'; export const REMOVE: 'account__remove' = 'account__remove';
// export const SET_BALANCE: 'address__set_balance' = 'address__set_balance'; export const SET_BALANCE: 'account__set_balance' = 'account__set_balance';
// export const SET_NONCE: 'address__set_nonce' = 'address__set_nonce'; export const SET_NONCE: 'account__set_nonce' = 'account__set_nonce';
// export const FROM_STORAGE: 'address__from_storage' = 'address__from_storage'; export const FROM_STORAGE: 'account__from_storage' = 'account__from_storage';

View File

@ -1,9 +0,0 @@
/* @flow */
'use strict';
export const CREATE: 'address__create' = 'address__create';
export const REMOVE: 'address__remove' = 'address__remove';
export const SET_BALANCE: 'address__set_balance' = 'address__set_balance';
export const SET_NONCE: 'address__set_nonce' = 'address__set_nonce';
export const FROM_STORAGE: 'address__from_storage' = 'address__from_storage';

View File

@ -16,7 +16,7 @@ import type { Reducers, ReducersState } from '../reducers';
// Actions // Actions
import type { AbstractAccountAction } from '../actions/AbstractAccountActions'; import type { AbstractAccountAction } from '../actions/AbstractAccountActions';
import type { AddressAction } from '../actions/AddressActions'; import type { AccountAction } from '../actions/AccountsActions';
import type { DiscoveryAction } from '../actions/DiscoveryActions'; import type { DiscoveryAction } from '../actions/DiscoveryActions';
import type { StorageAction } from '../actions/LocalStorageActions'; import type { StorageAction } from '../actions/LocalStorageActions';
import type { LogAction } from '../actions/LogActions'; import type { LogAction } from '../actions/LogActions';
@ -111,7 +111,7 @@ export type Action =
| UiEventAction | UiEventAction
| AbstractAccountAction | AbstractAccountAction
| AddressAction | AccountAction
| DiscoveryAction | DiscoveryAction
| StorageAction | StorageAction
| LogAction | LogAction

View File

@ -2,14 +2,14 @@
'use strict'; 'use strict';
import * as CONNECT from '../actions/constants/TrezorConnect'; import * as CONNECT from '../actions/constants/TrezorConnect';
import * as ADDRESS from '../actions/constants/address'; import * as ACCOUNT from '../actions/constants/account';
import type { Action, TrezorDevice } from '../flowtype'; import type { Action, TrezorDevice } from '../flowtype';
import type { import type {
AddressCreateAction, AccountCreateAction,
AddressSetBalanceAction, AccountSetBalanceAction,
AddressSetNonceAction AccountSetNonceAction
} from '../actions/AddressActions'; } from '../actions/AccountsActions';
export type Account = { export type Account = {
loaded: boolean; loaded: boolean;
@ -31,7 +31,7 @@ export const findAccount = (state: State, index: number, deviceState: string, ne
return state.find(a => a.deviceState === deviceState && a.index === index && a.network === network); return state.find(a => a.deviceState === deviceState && a.index === index && a.network === network);
} }
const createAccount = (state: State, action: AddressCreateAction): State => { const createAccount = (state: State, action: AccountCreateAction): State => {
// TODO check with device_id // TODO check with device_id
// check if account was created before // check if account was created before
@ -65,7 +65,7 @@ const removeAccounts = (state: State, device: TrezorDevice): State => {
// return state.filter(account => action.accounts.indexOf(account) === -1); // return state.filter(account => action.accounts.indexOf(account) === -1);
// } // }
const setBalance = (state: State, action: AddressSetBalanceAction): State => { const setBalance = (state: State, action: AccountSetBalanceAction): State => {
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network && account.deviceState === action.deviceState); const index: number = state.findIndex(account => account.address === action.address && account.network === action.network && account.deviceState === action.deviceState);
const newState: State = [ ...state ]; const newState: State = [ ...state ];
newState[index].loaded = true; newState[index].loaded = true;
@ -73,7 +73,7 @@ const setBalance = (state: State, action: AddressSetBalanceAction): State => {
return newState; return newState;
} }
const setNonce = (state: State, action: AddressSetNonceAction): State => { const setNonce = (state: State, action: AccountSetNonceAction): State => {
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network && account.deviceState === action.deviceState); const index: number = state.findIndex(account => account.address === action.address && account.network === action.network && account.deviceState === action.deviceState);
const newState: State = [ ...state ]; const newState: State = [ ...state ];
newState[index].loaded = true; newState[index].loaded = true;
@ -85,7 +85,7 @@ export default (state: State = initialState, action: Action): State => {
switch (action.type) { switch (action.type) {
case ADDRESS.CREATE : case ACCOUNT.CREATE :
return createAccount(state, action); return createAccount(state, action);
case CONNECT.FORGET : case CONNECT.FORGET :
@ -95,12 +95,12 @@ export default (state: State = initialState, action: Action): State => {
//case CONNECT.FORGET_SINGLE : //case CONNECT.FORGET_SINGLE :
// return forgetAccounts(state, action); // return forgetAccounts(state, action);
case ADDRESS.SET_BALANCE : case ACCOUNT.SET_BALANCE :
return setBalance(state, action); return setBalance(state, action);
case ADDRESS.SET_NONCE : case ACCOUNT.SET_NONCE :
return setNonce(state, action); return setNonce(state, action);
case ADDRESS.FROM_STORAGE : case ACCOUNT.FROM_STORAGE :
return action.payload; return action.payload;
default: default:

View File

@ -4,7 +4,7 @@
import HDKey from 'hdkey'; import HDKey from 'hdkey';
import * as DISCOVERY from '../actions/constants/discovery'; import * as DISCOVERY from '../actions/constants/discovery';
import * as ADDRESS from '../actions/constants/address'; import * as ACCOUNT from '../actions/constants/account';
import * as CONNECT from '../actions/constants/TrezorConnect'; import * as CONNECT from '../actions/constants/TrezorConnect';
import type { Action, TrezorDevice } from '../flowtype'; import type { Action, TrezorDevice } from '../flowtype';
@ -16,8 +16,8 @@ import type {
} from '../actions/DiscoveryActions'; } from '../actions/DiscoveryActions';
import type { import type {
AddressCreateAction AccountCreateAction
} from '../actions/AddressActions' } from '../actions/AccountsActions'
export type Discovery = { export type Discovery = {
network: string; network: string;
@ -76,7 +76,7 @@ const complete = (state: State, action: DiscoveryCompleteAction): State => {
return newState; return newState;
} }
const addressCreate = (state: State, action: AddressCreateAction): State => { const addressCreate = (state: State, action: AccountCreateAction): State => {
const index: number = findIndex(state, action.network, action.device.state || '0'); const index: number = findIndex(state, action.network, action.device.state || '0');
const newState: State = [ ...state ]; const newState: State = [ ...state ];
newState[index].accountIndex++; newState[index].accountIndex++;
@ -158,7 +158,7 @@ export default function discovery(state: State = initialState, action: Action):
switch (action.type) { switch (action.type) {
case DISCOVERY.START : case DISCOVERY.START :
return start(state, action); return start(state, action);
case ADDRESS.CREATE : case ACCOUNT.CREATE :
return addressCreate(state, action); return addressCreate(state, action);
case DISCOVERY.STOP : case DISCOVERY.STOP :
return stop(state, action); return stop(state, action);

View File

@ -3,7 +3,7 @@
import * as SEND from '../actions/constants/send'; import * as SEND from '../actions/constants/send';
import * as WEB3 from '../actions/constants/web3'; import * as WEB3 from '../actions/constants/web3';
import * as ADDRESS from '../actions/constants/address'; import * as ACCOUNT from '../actions/constants/account';
import EthereumjsUnits from 'ethereumjs-units'; import EthereumjsUnits from 'ethereumjs-units';
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
import { getFeeLevels } from '../actions/SendFormActions'; import { getFeeLevels } from '../actions/SendFormActions';
@ -138,8 +138,7 @@ export default (state: State = initialState, action: Action): State => {
case WEB3.GAS_PRICE_UPDATED : case WEB3.GAS_PRICE_UPDATED :
return onGasPriceUpdated(state, action); return onGasPriceUpdated(state, action);
case ADDRESS.SET_BALANCE : case ACCOUNT.SET_BALANCE :
// case ADDRESS.SET_TOKEN_BALANCE :
return onBalanceUpdated(state, action); return onBalanceUpdated(state, action);
case SEND.TOGGLE_ADVANCED : case SEND.TOGGLE_ADVANCED :

View File

@ -7,7 +7,7 @@ import { DEVICE } from 'trezor-connect';
import * as CONNECT from '../actions/constants/TrezorConnect'; import * as CONNECT from '../actions/constants/TrezorConnect';
import * as MODAL from '../actions/constants/modal'; import * as MODAL from '../actions/constants/modal';
import * as TOKEN from '../actions/constants/token'; import * as TOKEN from '../actions/constants/token';
import * as ADDRESS from '../actions/constants/address'; import * as ACCOUNT from '../actions/constants/account';
import * as DISCOVERY from '../actions/constants/discovery'; import * as DISCOVERY from '../actions/constants/discovery';
import * as SEND from '../actions/constants/send'; import * as SEND from '../actions/constants/send';
import * as WEB3 from '../actions/constants/web3'; import * as WEB3 from '../actions/constants/web3';
@ -110,9 +110,9 @@ const LocalStorageService: Middleware = (api: MiddlewareAPI) => (next: Middlewar
save(api.dispatch, api.getState); save(api.dispatch, api.getState);
break; break;
case ADDRESS.CREATE : case ACCOUNT.CREATE :
case ADDRESS.SET_BALANCE : case ACCOUNT.SET_BALANCE :
case ADDRESS.SET_NONCE : case ACCOUNT.SET_NONCE :
save(api.dispatch, api.getState); save(api.dispatch, api.getState);
break; break;