2018-04-16 21:19:50 +00:00
|
|
|
/* @flow */
|
2018-07-30 10:52:13 +00:00
|
|
|
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-08-14 13:18:16 +00:00
|
|
|
import * as TOKEN from 'actions/constants/token';
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
import type {
|
|
|
|
GetState, AsyncAction, Action, Dispatch,
|
2018-08-14 12:56:47 +00:00
|
|
|
} from 'flowtype';
|
2018-08-14 13:18:16 +00:00
|
|
|
import type { State, Token } from 'reducers/TokensReducer';
|
|
|
|
import type { Account } from 'reducers/AccountsReducer';
|
|
|
|
import type { NetworkToken } from 'reducers/LocalStorageReducer';
|
2018-12-03 18:04:57 +00:00
|
|
|
import * as BlockchainActions from 'actions/ethereum/BlockchainActions';
|
2018-04-16 21:19:50 +00:00
|
|
|
|
|
|
|
export type TokenAction = {
|
|
|
|
type: typeof TOKEN.FROM_STORAGE,
|
|
|
|
payload: State
|
|
|
|
} | {
|
|
|
|
type: typeof TOKEN.ADD,
|
|
|
|
payload: Token
|
|
|
|
} | {
|
|
|
|
type: typeof TOKEN.REMOVE,
|
|
|
|
token: Token
|
|
|
|
} | {
|
|
|
|
type: typeof TOKEN.SET_BALANCE,
|
2018-04-23 10:20:15 +00:00
|
|
|
payload: State
|
2018-04-16 21:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// action from component <reactSelect>
|
2018-09-23 06:49:43 +00:00
|
|
|
export const load = ($input: string, network: string): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<any> => {
|
|
|
|
let input = $input;
|
2018-08-17 07:24:26 +00:00
|
|
|
if (input.length < 1) input = '0x';
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
const tokens = getState().localStorage.tokens[network];
|
|
|
|
const value = input.toLowerCase();
|
|
|
|
const result = tokens.filter(t => t.symbol.toLowerCase().indexOf(value) >= 0
|
2018-08-29 09:36:07 +00:00
|
|
|
|| t.address.toLowerCase().indexOf(value) >= 0
|
|
|
|
|| t.name.toLowerCase().indexOf(value) >= 0);
|
2018-04-23 10:20:15 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
if (result.length > 0) {
|
2018-08-29 09:36:07 +00:00
|
|
|
// TODO: Temporary fix for async select
|
|
|
|
// async react-select starts getting very laggy
|
|
|
|
// when options is a large list (>200 items)
|
|
|
|
return result.slice(0, 100);
|
2018-04-16 21:19:50 +00:00
|
|
|
}
|
2018-07-30 10:52:13 +00:00
|
|
|
|
2018-09-23 06:49:43 +00:00
|
|
|
const info = await dispatch(BlockchainActions.getTokenInfo(input, network));
|
2018-07-30 10:52:13 +00:00
|
|
|
if (info) {
|
2018-08-29 09:36:07 +00:00
|
|
|
return [info];
|
2018-07-30 10:52:13 +00:00
|
|
|
}
|
2018-09-23 06:49:43 +00:00
|
|
|
|
|
|
|
return null;
|
2018-07-30 10:52:13 +00:00
|
|
|
};
|
|
|
|
|
2018-09-06 15:04:28 +00:00
|
|
|
export const setBalance = (tokenAddress: string, ethAddress: string, balance: string): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
|
|
|
const newState: Array<Token> = [...getState().tokens];
|
|
|
|
const token: ?Token = newState.find(t => t.address === tokenAddress && t.ethAddress === ethAddress);
|
|
|
|
if (token) {
|
2018-10-18 12:17:52 +00:00
|
|
|
const others = newState.filter(t => t !== token);
|
|
|
|
dispatch({
|
|
|
|
type: TOKEN.SET_BALANCE,
|
|
|
|
payload: others.concat([{
|
|
|
|
...token,
|
|
|
|
loaded: true,
|
|
|
|
balance,
|
|
|
|
}]),
|
|
|
|
});
|
2018-09-06 15:04:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-23 06:49:43 +00:00
|
|
|
export const add = (token: NetworkToken, account: Account): AsyncAction => async (dispatch: Dispatch): Promise<void> => {
|
2018-07-30 10:52:13 +00:00
|
|
|
const tkn: Token = {
|
|
|
|
loaded: false,
|
|
|
|
deviceState: account.deviceState,
|
|
|
|
network: account.network,
|
|
|
|
name: token.name,
|
|
|
|
symbol: token.symbol,
|
|
|
|
address: token.address,
|
2018-12-21 09:58:53 +00:00
|
|
|
ethAddress: account.descriptor,
|
2018-07-30 10:52:13 +00:00
|
|
|
decimals: token.decimals,
|
|
|
|
balance: '0',
|
|
|
|
};
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: TOKEN.ADD,
|
|
|
|
payload: tkn,
|
|
|
|
});
|
|
|
|
|
2018-09-23 06:49:43 +00:00
|
|
|
const tokenBalance = await dispatch(BlockchainActions.getTokenBalance(tkn));
|
2018-12-21 09:58:53 +00:00
|
|
|
dispatch(setBalance(token.address, account.descriptor, tokenBalance));
|
2018-07-30 10:52:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const remove = (token: Token): Action => ({
|
|
|
|
type: TOKEN.REMOVE,
|
|
|
|
token,
|
|
|
|
});
|