You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trezor-wallet/src/js/actions/TokenActions.js

113 lines
3.1 KiB

/* @flow */
'use strict';
import * as TOKEN from './constants/token';
import { getTokenInfoAsync, getTokenBalanceAsync } from './Web3Actions';
import type { GetState, AsyncAction, Action } from '../flowtype';
import type { State, Token } from '../reducers/TokensReducer';
import type { Account } from '../reducers/AccountsReducer';
import type { NetworkToken } from '../reducers/LocalStorageReducer';
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,
payload: {
ethAddress: string,
address: string,
}
}
export const setBalance = (web3: any, coinIndex: number = 0): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
}
}
type SelectOptions = {
options?: Array<NetworkToken>
}
// action from component <reactSelect>
export const load = (input: string, network: string): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<any> => {
if (input.length < 1) return;
const tokens = getState().localStorage.tokens[ network ];
const value = input.toLowerCase();
const result = tokens.filter(t =>
t.symbol.toLowerCase().indexOf(value) >= 0 ||
t.address.toLowerCase().indexOf(value) >= 0 ||
t.name.toLowerCase().indexOf(value) >= 0
);
if (result.length > 0) {
return { options: result };
} else {
const web3instance = getState().web3.find(w3 => w3.network === network);
if (!web3instance) return;
const info = await getTokenInfoAsync(web3instance.erc20, input);
info.address = input;
if (info) {
return {
options: [ info ]
}
}
//await resolveAfter(300000);
//await resolveAfter(3000);
}
return;
}
}
export const add = (token: NetworkToken, account: Account): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const web3instance = getState().web3.find(w3 => w3.network === account.network);
if (!web3instance) return;
dispatch({
type: TOKEN.ADD,
payload: {
loaded: false,
deviceState: account.deviceState,
name: token.name,
symbol: token.symbol,
address: token.address,
ethAddress: account.address,
decimals: token.decimals,
balance: '0'
}
});
const tokenBalance = await getTokenBalanceAsync(web3instance.erc20, token.address, account.address);
dispatch({
type: TOKEN.SET_BALANCE,
payload: {
ethAddress: account.address,
address: token.address,
balance: tokenBalance.toString()
}
})
}
}
export const remove = (token: Token): Action => {
return {
type: TOKEN.REMOVE,
token
}
}