1
0
mirror of https://github.com/trezor/trezor-wallet synced 2025-01-09 23:51:05 +00:00
trezor-wallet/src/js/reducers/TokensReducer.js

82 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-02-20 09:30:36 +00:00
/* @flow */
2018-07-30 10:52:13 +00:00
2018-02-20 09:30:36 +00:00
import * as CONNECT from '../actions/constants/TrezorConnect';
import * as WALLET from '../actions/constants/wallet';
2018-04-11 10:13:38 +00:00
import * as TOKEN from '../actions/constants/token';
2018-02-20 09:30:36 +00:00
2018-08-14 12:56:47 +00:00
import type { Action, TrezorDevice } from 'flowtype';
import type { Account } from './AccountsReducer';
2018-04-16 21:19:50 +00:00
2018-02-20 09:30:36 +00:00
export type Token = {
loaded: boolean;
+deviceState: string;
2018-05-07 13:33:34 +00:00
+network: string;
2018-02-20 09:30:36 +00:00
+name: string;
+symbol: string;
+address: string;
+ethAddress: string; // foreign key
2018-05-02 09:01:08 +00:00
+decimals: number;
2018-02-20 09:30:36 +00:00
balance: string;
}
2018-04-16 21:19:50 +00:00
export type State = Array<Token>;
const initialState: State = [];
// Helper for actions
2018-07-30 10:52:13 +00:00
export const findToken = (state: Array<Token>, address: string, symbol: string, deviceState: string): ?Token => state.find(t => t.ethAddress === address && t.symbol === symbol && t.deviceState === deviceState);
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
export const findAccountTokens = (state: Array<Token>, account: Account): Array<Token> => state.filter(t => t.ethAddress === account.address && t.network === account.network && t.deviceState === account.deviceState);
2018-04-23 10:20:15 +00:00
// const setBalance = (state: State, payload: any): State => {
// const newState: Array<Token> = [ ...state ];
// let index: number = state.findIndex(t => t.address === payload.address && t.ethAddress === payload.ethAddress);
// if (index >= 0) {
// newState[index].loaded = true;
// newState[index].balance = payload.balance;
// }
// return newState;
// }
const create = (state: State, token: Token): State => {
2018-07-30 10:52:13 +00:00
const newState: State = [...state];
2018-02-20 09:30:36 +00:00
newState.push(token);
return newState;
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
const forget = (state: State, device: TrezorDevice): State => state.filter(t => t.deviceState !== device.state);
2018-02-20 09:30:36 +00:00
const clear = (state: State, devices: Array<TrezorDevice>): State => {
2018-07-30 10:52:13 +00:00
let newState: State = [...state];
devices.forEach((d) => {
newState = forget(newState, d);
});
return newState;
2018-07-30 10:52:13 +00:00
};
2018-07-30 10:52:13 +00:00
const remove = (state: State, token: Token): State => state.filter(t => !(t.ethAddress === token.ethAddress && t.address === token.address && t.deviceState === token.deviceState));
2018-03-08 16:10:53 +00:00
2018-04-16 21:19:50 +00:00
export default (state: State = initialState, action: Action): State => {
2018-02-20 09:30:36 +00:00
switch (action.type) {
2018-07-30 10:52:13 +00:00
case TOKEN.FROM_STORAGE:
2018-04-16 21:19:50 +00:00
return action.payload;
2018-07-30 10:52:13 +00:00
case TOKEN.ADD:
2018-02-20 09:30:36 +00:00
return create(state, action.payload);
2018-07-30 10:52:13 +00:00
case TOKEN.REMOVE:
2018-04-16 21:19:50 +00:00
return remove(state, action.token);
2018-07-30 10:52:13 +00:00
case TOKEN.SET_BALANCE:
2018-04-23 10:20:15 +00:00
return action.payload;
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE:
2018-04-16 21:19:50 +00:00
return forget(state, action.device);
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
case WALLET.CLEAR_UNAVAILABLE_DEVICE_DATA:
return clear(state, action.devices);
2018-02-20 09:30:36 +00:00
default:
return state;
}
2018-07-30 10:52:13 +00:00
};