2018-02-12 20:43:07 +00:00
|
|
|
import { AppState } from 'reducers';
|
|
|
|
import { getUnit } from 'selectors/transaction/meta';
|
|
|
|
import { SHAPESHIFT_TOKEN_WHITELIST } from 'api/shapeshift';
|
2018-03-02 01:24:14 +00:00
|
|
|
import { getStaticNetworkConfig, isNetworkUnit } from 'selectors/config';
|
2018-02-12 20:43:07 +00:00
|
|
|
import { Token } from 'types/network';
|
|
|
|
|
|
|
|
export function getNetworkTokens(state: AppState): Token[] {
|
|
|
|
const network = getStaticNetworkConfig(state);
|
|
|
|
return network ? network.tokens : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAllTokens(state: AppState): Token[] {
|
|
|
|
const networkTokens = getNetworkTokens(state);
|
|
|
|
return networkTokens.concat(state.customTokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSelectedTokenContractAddress(state: AppState): string {
|
|
|
|
const allTokens = getAllTokens(state);
|
|
|
|
const currentUnit = getUnit(state);
|
|
|
|
|
2018-03-02 01:24:14 +00:00
|
|
|
if (isNetworkUnit(state, currentUnit)) {
|
2018-02-12 20:43:07 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return allTokens.reduce((tokenAddr, tokenInfo) => {
|
|
|
|
if (tokenAddr && tokenAddr.length) {
|
|
|
|
return tokenAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tokenInfo.symbol === currentUnit) {
|
|
|
|
return tokenInfo.address;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokenAddr;
|
|
|
|
}, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function tokenExists(state: AppState, token: string): boolean {
|
|
|
|
const existInWhitelist = SHAPESHIFT_TOKEN_WHITELIST.includes(token);
|
|
|
|
const existsInNetwork = !!getAllTokens(state).find(t => t.symbol === token);
|
|
|
|
return existsInNetwork || existInWhitelist;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isSupportedUnit(state: AppState, unit: string) {
|
|
|
|
const isToken: boolean = tokenExists(state, unit);
|
2018-03-02 01:24:14 +00:00
|
|
|
const isEther: boolean = isNetworkUnit(state, unit);
|
2018-02-12 20:43:07 +00:00
|
|
|
if (!isToken && !isEther) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|