1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-13 03:48:59 +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 */
'use strict';
import * as ADDRESS from './constants/address';
import * as ACCOUNT from './constants/account';
import type { Action, TrezorDevice } from '../flowtype';
import type { State } from '../reducers/AccountsReducer';
export type AddressAction =
AddressFromStorageAction
| AddressCreateAction
| AddressSetBalanceAction
| AddressSetNonceAction;
export type AccountAction =
AccountFromStorageAction
| AccountCreateAction
| AccountSetBalanceAction
| AccountSetNonceAction;
export type AddressFromStorageAction = {
type: typeof ADDRESS.FROM_STORAGE,
export type AccountFromStorageAction = {
type: typeof ACCOUNT.FROM_STORAGE,
payload: State
}
export type AddressCreateAction = {
type: typeof ADDRESS.CREATE,
export type AccountCreateAction = {
type: typeof ACCOUNT.CREATE,
device: TrezorDevice,
network: string,
index: number,
@ -25,16 +25,16 @@ export type AddressCreateAction = {
address: string
}
export type AddressSetBalanceAction = {
type: typeof ADDRESS.SET_BALANCE,
export type AccountSetBalanceAction = {
type: typeof ACCOUNT.SET_BALANCE,
address: string,
network: string,
deviceState: string,
balance: string
}
export type AddressSetNonceAction = {
type: typeof ADDRESS.SET_NONCE,
export type AccountSetNonceAction = {
type: typeof ACCOUNT.SET_NONCE,
address: string,
network: string,
deviceState: string,
@ -43,7 +43,7 @@ export type AddressSetNonceAction = {
export const setBalance = (address: string, network: string, deviceState: string, balance: string): Action => {
return {
type: ADDRESS.SET_BALANCE,
type: ACCOUNT.SET_BALANCE,
address,
network,
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 => {
return {
type: ADDRESS.SET_NONCE,
type: ACCOUNT.SET_NONCE,
address,
network,
deviceState,

View File

@ -4,10 +4,10 @@
import TrezorConnect from 'trezor-connect';
import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
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 NOTIFICATION from './constants/notification';
import * as AddressActions from '../actions/AddressActions';
import * as AccountsActions from '../actions/AccountsActions';
import HDKey from 'hdkey';
import EthereumjsUtil from 'ethereumjs-util';
@ -199,7 +199,7 @@ const discoverAddress = (device: TrezorDevice, discoveryProcess: Discovery): Asy
const network = discoveryProcess.network;
dispatch({
type: ADDRESS.CREATE,
type: ACCOUNT.CREATE,
device,
network,
index: discoveryProcess.accountIndex,
@ -285,7 +285,7 @@ const discoverAddress = (device: TrezorDevice, discoveryProcess: Discovery): Asy
const balance = await getBalanceAsync(web3instance.web3, ethAddress);
if (discoveryProcess.interrupted) return;
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 = [];
@ -302,7 +302,7 @@ const discoverAddress = (device: TrezorDevice, discoveryProcess: Discovery): Asy
const nonce: number = await getNonceAsync(web3instance.web3, ethAddress);
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);

View File

@ -2,7 +2,7 @@
'use strict';
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 DISCOVERY from './constants/discovery';
import * as STORAGE from './constants/localStorage';
@ -104,7 +104,7 @@ export function loadTokensFromJSON(): AsyncAction {
const accounts: ?string = get('accounts');
if (accounts) {
dispatch({
type: ADDRESS.FROM_STORAGE,
type: ACCOUNT.FROM_STORAGE,
payload: JSON.parse(accounts)
})
}

View File

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

View File

@ -2,7 +2,6 @@
'use strict';
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 CONNECT from './constants/TrezorConnect';
import * as NOTIFICATION from './constants/notification';

View File

@ -7,10 +7,9 @@ import EthereumjsUtil from 'ethereumjs-util';
import EthereumjsTx from 'ethereumjs-tx';
import TrezorConnect from 'trezor-connect';
import { strip } from '../utils/ethUtils';
import * as ADDRESS from './constants/address';
import * as WEB3 from './constants/web3';
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 type {
@ -172,7 +171,7 @@ export function init(instance: ?Web3, coinIndex: number = 0): AsyncAction {
for (const account of accounts) {
const nonce = await getNonceAsync(web3, account.address);
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) );
// TODO: check if nonce was updated,
@ -246,7 +245,7 @@ export function getBalance(account: Account): AsyncAction {
if (!error) {
const newBalance: string = web3.fromWei(balance.toString(), 'ether');
if (account.balance !== newBalance) {
dispatch(AddressActions.setBalance(
dispatch(AccountsActions.setBalance(
account.address,
account.network,
account.deviceState,
@ -292,7 +291,7 @@ export function getNonce(account: Account): AsyncAction {
web3.eth.getTransactionCount(account.address, (error: Error, result: number) => {
if (!error) {
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 DISPOSE: 'account__dispose' = 'account__dispose';
// 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';
export const CREATE: 'account__create' = 'account__create';
export const REMOVE: 'account__remove' = 'account__remove';
export const SET_BALANCE: 'account__set_balance' = 'account__set_balance';
export const SET_NONCE: 'account__set_nonce' = 'account__set_nonce';
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
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 { StorageAction } from '../actions/LocalStorageActions';
import type { LogAction } from '../actions/LogActions';
@ -111,7 +111,7 @@ export type Action =
| UiEventAction
| AbstractAccountAction
| AddressAction
| AccountAction
| DiscoveryAction
| StorageAction
| LogAction

View File

@ -2,14 +2,14 @@
'use strict';
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 {
AddressCreateAction,
AddressSetBalanceAction,
AddressSetNonceAction
} from '../actions/AddressActions';
AccountCreateAction,
AccountSetBalanceAction,
AccountSetNonceAction
} from '../actions/AccountsActions';
export type Account = {
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);
}
const createAccount = (state: State, action: AddressCreateAction): State => {
const createAccount = (state: State, action: AccountCreateAction): State => {
// TODO check with device_id
// 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);
// }
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 newState: State = [ ...state ];
newState[index].loaded = true;
@ -73,7 +73,7 @@ const setBalance = (state: State, action: AddressSetBalanceAction): State => {
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 newState: State = [ ...state ];
newState[index].loaded = true;
@ -85,7 +85,7 @@ export default (state: State = initialState, action: Action): State => {
switch (action.type) {
case ADDRESS.CREATE :
case ACCOUNT.CREATE :
return createAccount(state, action);
case CONNECT.FORGET :
@ -95,12 +95,12 @@ export default (state: State = initialState, action: Action): State => {
//case CONNECT.FORGET_SINGLE :
// return forgetAccounts(state, action);
case ADDRESS.SET_BALANCE :
case ACCOUNT.SET_BALANCE :
return setBalance(state, action);
case ADDRESS.SET_NONCE :
case ACCOUNT.SET_NONCE :
return setNonce(state, action);
case ADDRESS.FROM_STORAGE :
case ACCOUNT.FROM_STORAGE :
return action.payload;
default:

View File

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

View File

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

View File

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