mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-01-10 02:55:41 +00:00
aabcd3f7a3
* Set default unit to 'ETH' instead of 'ether' * Use 'isEtherUnit()' everywhere * Set default unit to empty string * Update isEthUnit to isNetworkUnit * Fix unit conversion for non-ethereum networks * Set default network unit properly * Fix tests * fix typos * Update isNetworkUnit selector * Update isNetworkUnit * Fix validationhelpers tests * Add mock state to tests & Move isNetworkUnit to selectors * Fix validation helper spec * fix unit swap spec
52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import { AppState } from 'reducers';
|
|
import { getUnit } from 'selectors/transaction/meta';
|
|
import { SHAPESHIFT_TOKEN_WHITELIST } from 'api/shapeshift';
|
|
import { getStaticNetworkConfig, isNetworkUnit } from 'selectors/config';
|
|
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);
|
|
|
|
if (isNetworkUnit(state, currentUnit)) {
|
|
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);
|
|
const isEther: boolean = isNetworkUnit(state, unit);
|
|
if (!isToken && !isEther) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|