Eddie Wang 88532cdc3c Shapeshift Integration (#564)
* progress

* Normalize bity api response

* Filter api response

* Track swap information in component state

* Update dropdown onchange

* remove dead code

* Update Min Max Validation

* Update minmax err msg && fix onChangeOriginKind

* Add origin & destination to redux state

* Update types & Update tests

* Update types

* Update swap.spec.ts test

* Remove commented out code

* Remove hardcoded coin array

* Create types.ts for swap reducer

* Update swapinput type

* Update bityRates in localStorage & Replace all instances of ...Kind / ...Amount props

* Add shapeshift banner

* initial work for sagas

* Update Types

* Update swap reducer initial state

* Update Types & Store empty obj for bityRates / options

* Update more types

* added shapeshift file and rates comments

* action reducers and prop mapping to components

* add typings and swap icon

* more actions reducers and sagas

* debugging shapeshift service

* add Headers

* Fix content type

* add order reset saga and ui fixes

* remove console log and swap b/w Bity and Shapeshift

* working state for Shapeshift and Bity - tested with mainnet

* add icon component

* UI improvements and fix select bug

* fix timer bug

* add bity fallback options and toFixed floats

* tslint errors

* add arrow to dropdown and add support footer

* Add service provider

* fix minor $ bug and stop timer on order complete

* better load UX and dropdown UX

* fixed single test

* currRate prop bugs and reduce LS bloat

* takeEvery on timer saga and don't clear state.options to restartSwap reducer

* export tx sagas and fix minor type

* Add ShapeShift Rates functionality when selecting a ShapeShift pair.

* type fixes

* BugFix: Don't change displayed ShapeShift Rate Inputs on every dropdown change
Also contains some caching / performance improvements

* BugFix: Don't remote rate inputs when falsy amount

* fix type error

* Progress commit

* Implement saga logic

* Make address field factory component

* Shorten debounce time

* Make new actions / sagas  for handling single token lookup

* Implement working version of litesend

* Change saga into selector

* Add failing spec

* fix broken test

* add debounce to error message

* fix tests

* update snapshots

* test coverage

* move setState disabled property from debounce so we instantly can go to next step on valid amounts

* much deeper test coverage, fix debounce ux, and fix bity flashing at swap page load

* fix minor failing test

* seperate shapeshift erc20 token whitelist

* fix saveState store bug

* break orderTimeRemaining saga up and rewrite tests

* add new swap icon

* remove unused allowReadOnly prop

* change offlineaware to walletdecrypt for litesend

* fix LiteSend changewallet bug

* fix error message UX

* fix button styling to match develop

* fix liteSend test

* Fix LiteSend UX on unavl tokens, dropdown null value, and don't show decrypt in litesend after successful wallet decrypt.

* add litesend network check
2018-01-02 12:04:50 -06:00

139 lines
4.1 KiB
TypeScript

import { TokenValue, Wei } from 'libs/units';
import { Token } from 'config/data';
import { AppState } from 'reducers';
import { getNetworkConfig } from 'selectors/config';
import { IWallet, Web3Wallet, LedgerWallet, TrezorWallet, WalletConfig } from 'libs/wallet';
import { isEtherTransaction, getUnit } from './transaction';
export function getWalletInst(state: AppState): IWallet | null | undefined {
return state.wallet.inst;
}
export function getWalletConfig(state: AppState): WalletConfig | null | undefined {
return state.wallet.config;
}
export function isWalletFullyUnlocked(state: AppState): boolean | null | undefined {
return state.wallet.inst && !state.wallet.inst.isReadOnly;
}
export interface TokenBalance {
symbol: string;
balance: TokenValue;
custom: boolean;
decimal: number;
error: string | null;
}
export type MergedToken = Token & {
custom: boolean;
};
export function getTokens(state: AppState): MergedToken[] {
const network = getNetworkConfig(state);
const tokens: Token[] = network ? network.tokens : [];
return tokens.concat(
state.customTokens.map((token: Token) => {
const mergedToken = { ...token, custom: true };
return mergedToken;
})
) as MergedToken[];
}
export function getWalletConfigTokens(state: AppState): MergedToken[] {
const tokens = getTokens(state);
const config = getWalletConfig(state);
if (!config || !config.tokens) {
return [];
}
return config.tokens
.map(symbol => tokens.find(t => t.symbol === symbol))
.filter(token => token) as MergedToken[];
}
export const getToken = (state: AppState, unit: string): MergedToken | undefined => {
const tokens = getTokens(state);
const token = tokens.find(t => t.symbol === unit);
return token;
};
export function getTokenBalances(state: AppState, nonZeroOnly: boolean = false): TokenBalance[] {
const tokens = getTokens(state);
if (!tokens) {
return [];
}
const ret = tokens.map(t => ({
symbol: t.symbol,
balance: state.wallet.tokens[t.symbol]
? state.wallet.tokens[t.symbol].balance
: TokenValue('0'),
error: state.wallet.tokens[t.symbol] ? state.wallet.tokens[t.symbol].error : null,
custom: t.custom,
decimal: t.decimal
}));
return nonZeroOnly ? ret.filter(t => !t.balance.isZero()) : ret;
}
export const getTokenBalance = (state: AppState, unit: string): TokenValue | null => {
const token = getTokenWithBalance(state, unit);
if (!token) {
return token;
}
return token.balance;
};
export const getTokenWithBalance = (state: AppState, unit: string): TokenBalance => {
const tokens = getTokenBalances(state, false);
const currentToken = tokens.filter(t => t.symbol === unit);
//TODO: getting the first index is kinda hacky
return currentToken[0];
};
export interface IWalletType {
isWeb3Wallet: boolean;
isHardwareWallet: boolean;
}
export const getWallet = (state: AppState) => state.wallet;
export const getWalletType = (state: AppState): IWalletType => {
const wallet = getWalletInst(state);
const isWeb3Wallet = wallet instanceof Web3Wallet;
const isLedgerWallet = wallet instanceof LedgerWallet;
const isTrezorWallet = wallet instanceof TrezorWallet;
const isHardwareWallet = isLedgerWallet || isTrezorWallet;
return { isWeb3Wallet, isHardwareWallet };
};
export const isUnlocked = (state: AppState) => !!getWalletInst(state);
export const getEtherBalance = (state: AppState): Wei | null => getWallet(state).balance.wei;
export const getCurrentBalance = (state: AppState): Wei | TokenValue | null => {
const etherTransaction = isEtherTransaction(state);
if (etherTransaction) {
return getEtherBalance(state);
} else {
const unit = getUnit(state);
return getTokenBalance(state, unit);
}
};
export function getShownTokenBalances(
state: AppState,
nonZeroOnly: boolean = false
): TokenBalance[] {
const tokenBalances = getTokenBalances(state, nonZeroOnly);
const walletConfig = getWalletConfig(state);
let walletTokens: string[] = [];
if (walletConfig) {
if (walletConfig.tokens) {
walletTokens = walletConfig.tokens;
}
}
return tokenBalances.filter(t => walletTokens.includes(t.symbol));
}