MyCrypto/common/api/shapeshift.ts
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

176 lines
4.4 KiB
TypeScript

import { checkHttpStatus, parseJSON } from 'api/utils';
const SHAPESHIFT_BASE_URL = 'https://shapeshift.io';
export const SHAPESHIFT_TOKEN_WHITELIST = [
'OMG',
'REP',
'SNT',
'SNGLS',
'ZRX',
'SWT',
'ANT',
'BAT',
'BNT',
'CVC',
'DNT',
'1ST',
'GNO',
'GNT',
'EDG',
'FUN',
'RLC',
'TRST',
'GUP',
'ETH'
];
export const SHAPESHIFT_WHITELIST = [...SHAPESHIFT_TOKEN_WHITELIST, 'ETC', 'BTC'];
class ShapeshiftService {
public whitelist = SHAPESHIFT_WHITELIST;
private url = SHAPESHIFT_BASE_URL;
private apiKey = '0ca1ccd50b708a3f8c02327f0caeeece06d3ddc1b0ac749a987b453ee0f4a29bdb5da2e53bc35e57fb4bb7ae1f43c93bb098c3c4716375fc1001c55d8c94c160';
private postHeaders = {
'Content-Type': 'application/json'
};
public checkStatus(address) {
return fetch(`${this.url}/txStat/${address}`)
.then(checkHttpStatus)
.then(parseJSON);
}
public sendAmount(withdrawal, originKind, destinationKind, destinationAmount) {
const pair = `${originKind.toLowerCase()}_${destinationKind.toLowerCase()}`;
return fetch(`${this.url}/sendamount`, {
method: 'POST',
body: JSON.stringify({
amount: destinationAmount,
pair,
apiKey: this.apiKey,
withdrawal
}),
headers: new Headers(this.postHeaders)
})
.then(checkHttpStatus)
.then(parseJSON);
}
public getCoins() {
return fetch(`${this.url}/getcoins`)
.then(checkHttpStatus)
.then(parseJSON);
}
public getAllRates = async () => {
const marketInfo = await this.getMarketInfo();
const pairRates = await this.getPairRates(marketInfo);
const checkAvl = await this.checkAvl(pairRates);
const mappedRates = this.mapMarketInfo(checkAvl);
return mappedRates;
};
private getPairRates(marketInfo) {
const filteredMarketInfo = marketInfo.filter(obj => {
const { pair } = obj;
const pairArr = pair.split('_');
return this.whitelist.includes(pairArr[0]) && this.whitelist.includes(pairArr[1])
? true
: false;
});
const pairRates = filteredMarketInfo.map(p => {
const { pair } = p;
const singlePair = Promise.resolve(this.getSinglePairRate(pair));
return { ...p, ...singlePair };
});
return pairRates;
}
private async checkAvl(pairRates) {
const avlCoins = await this.getAvlCoins();
const mapAvl = pairRates.map(p => {
const { pair } = p;
const pairArr = pair.split('_');
if (pairArr[0] in avlCoins && pairArr[1] in avlCoins) {
return {
...p,
...{
[pairArr[0]]: {
name: avlCoins[pairArr[0]].name,
status: avlCoins[pairArr[0]].status,
image: avlCoins[pairArr[0]].image
},
[pairArr[1]]: {
name: avlCoins[pairArr[1]].name,
status: avlCoins[pairArr[1]].status,
image: avlCoins[pairArr[1]].image
}
}
};
}
});
return mapAvl;
}
private getAvlCoins() {
return fetch(`${this.url}/getcoins`)
.then(checkHttpStatus)
.then(parseJSON);
}
private getSinglePairRate(pair) {
return fetch(`${this.url}/rate/${pair}`)
.then(checkHttpStatus)
.then(parseJSON);
}
private getMarketInfo() {
return fetch(`${this.url}/marketinfo`)
.then(checkHttpStatus)
.then(parseJSON);
}
private isWhitelisted(coin) {
return this.whitelist.includes(coin);
}
private mapMarketInfo(marketInfo) {
const tokenMap = {};
marketInfo.forEach(m => {
const originKind = m.pair.substring(0, 3);
const destinationKind = m.pair.substring(4, 7);
if (this.isWhitelisted(originKind) && this.isWhitelisted(destinationKind)) {
const pairName = originKind + destinationKind;
const { rate, limit, min } = m;
tokenMap[pairName] = {
id: pairName,
options: [
{
id: originKind,
status: m[originKind].status,
image: m[originKind].image,
name: m[originKind].name
},
{
id: destinationKind,
status: m[destinationKind].status,
image: m[destinationKind].image,
name: m[destinationKind].name
}
],
rate,
limit,
min
};
}
});
return tokenMap;
}
}
const shapeshift = new ShapeshiftService();
export default shapeshift;