2018-01-20 20:06:28 +00:00
|
|
|
import { WhitelistedCoins, bityConfig } from 'config';
|
2017-12-11 17:44:53 +00:00
|
|
|
import { checkHttpStatus, parseJSON, filter } from './utils';
|
2018-01-02 18:04:50 +00:00
|
|
|
import bitcoinIcon from 'assets/images/bitcoin.png';
|
|
|
|
import repIcon from 'assets/images/augur.png';
|
|
|
|
import etherIcon from 'assets/images/ether.png';
|
2017-12-11 17:44:53 +00:00
|
|
|
|
|
|
|
const isCryptoPair = (from: string, to: string, arr: WhitelistedCoins[]) => {
|
|
|
|
return filter(from, arr) && filter(to, arr);
|
|
|
|
};
|
2017-06-12 01:01:27 +00:00
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
const btcOptions = {
|
|
|
|
id: 'BTC',
|
|
|
|
status: 'available',
|
|
|
|
image: bitcoinIcon,
|
|
|
|
name: 'Bitcoin'
|
|
|
|
};
|
|
|
|
|
|
|
|
const ethOptions = {
|
|
|
|
id: 'ETH',
|
|
|
|
status: 'available',
|
|
|
|
image: etherIcon,
|
|
|
|
name: 'Ether'
|
|
|
|
};
|
|
|
|
|
|
|
|
const repOptions = {
|
|
|
|
id: 'REP',
|
|
|
|
status: 'available',
|
|
|
|
image: repIcon,
|
|
|
|
name: 'Augur'
|
|
|
|
};
|
|
|
|
|
2018-03-07 23:36:05 +00:00
|
|
|
export interface MappedRates {
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
2017-07-04 00:08:35 +00:00
|
|
|
export function getAllRates() {
|
2018-03-07 23:36:05 +00:00
|
|
|
const mappedRates: MappedRates = {};
|
2017-11-30 05:35:17 +00:00
|
|
|
return _getAllRates().then(bityRates => {
|
2018-03-07 23:36:05 +00:00
|
|
|
bityRates.objects.forEach((each: any) => {
|
2017-07-04 00:08:35 +00:00
|
|
|
const pairName = each.pair;
|
2017-12-11 17:44:53 +00:00
|
|
|
const from = { id: pairName.substring(0, 3) };
|
|
|
|
const to = { id: pairName.substring(3, 6) };
|
|
|
|
// Check if rate exists= && check if the pair only crypto to crypto, not crypto to fiat, or any other combination
|
|
|
|
if (parseFloat(each.rate_we_sell) && isCryptoPair(from.id, to.id, ['BTC', 'ETH', 'REP'])) {
|
2018-01-02 18:04:50 +00:00
|
|
|
let fromOptions;
|
|
|
|
let toOptions;
|
|
|
|
switch (from.id) {
|
|
|
|
case 'BTC':
|
|
|
|
fromOptions = btcOptions;
|
|
|
|
break;
|
|
|
|
case 'ETH':
|
|
|
|
fromOptions = ethOptions;
|
|
|
|
break;
|
|
|
|
case 'REP':
|
|
|
|
fromOptions = repOptions;
|
|
|
|
}
|
|
|
|
switch (to.id) {
|
|
|
|
case 'BTC':
|
|
|
|
toOptions = btcOptions;
|
|
|
|
break;
|
|
|
|
case 'ETH':
|
|
|
|
toOptions = ethOptions;
|
|
|
|
break;
|
|
|
|
case 'REP':
|
|
|
|
toOptions = repOptions;
|
|
|
|
}
|
2017-12-11 17:44:53 +00:00
|
|
|
mappedRates[pairName] = {
|
|
|
|
id: pairName,
|
2018-01-02 18:04:50 +00:00
|
|
|
options: [fromOptions, toOptions],
|
2017-12-11 17:44:53 +00:00
|
|
|
rate: parseFloat(each.rate_we_sell)
|
|
|
|
};
|
|
|
|
}
|
2017-06-19 05:39:07 +00:00
|
|
|
});
|
2017-07-04 00:08:35 +00:00
|
|
|
return mappedRates;
|
|
|
|
});
|
2017-07-31 23:14:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
export function postOrder(amount: number, destAddress: string, mode: number, pair: string) {
|
2017-07-31 23:14:30 +00:00
|
|
|
return fetch(`${bityConfig.serverURL}/order`, {
|
|
|
|
method: 'post',
|
|
|
|
body: JSON.stringify({
|
|
|
|
amount,
|
|
|
|
destAddress,
|
|
|
|
mode,
|
|
|
|
pair
|
|
|
|
}),
|
2017-11-30 05:35:17 +00:00
|
|
|
headers: new Headers(bityConfig.postConfig.headers)
|
2017-07-31 23:14:30 +00:00
|
|
|
})
|
|
|
|
.then(checkHttpStatus)
|
|
|
|
.then(parseJSON);
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export function getOrderStatus(orderId: string) {
|
2017-07-31 23:14:30 +00:00
|
|
|
return fetch(`${bityConfig.serverURL}/status`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
2017-09-25 02:06:28 +00:00
|
|
|
orderid: orderId
|
2017-07-31 23:14:30 +00:00
|
|
|
}),
|
2017-11-30 05:35:17 +00:00
|
|
|
headers: new Headers(bityConfig.postConfig.headers)
|
2017-07-31 23:14:30 +00:00
|
|
|
})
|
|
|
|
.then(checkHttpStatus)
|
|
|
|
.then(parseJSON);
|
2017-07-04 00:08:35 +00:00
|
|
|
}
|
2017-06-12 01:01:27 +00:00
|
|
|
|
2017-07-04 00:08:35 +00:00
|
|
|
function _getAllRates() {
|
2017-09-05 20:07:28 +00:00
|
|
|
return fetch(`${bityConfig.bityURL}/v1/rate2/`)
|
2017-07-31 23:14:30 +00:00
|
|
|
.then(checkHttpStatus)
|
|
|
|
.then(parseJSON);
|
2017-11-30 05:35:17 +00:00
|
|
|
}
|