2017-07-08 02:00:09 +00:00
|
|
|
// @flow
|
2017-06-19 00:56:11 +00:00
|
|
|
import bityConfig from 'config/bity';
|
2017-07-22 21:24:03 +00:00
|
|
|
import {combineAndUpper} from 'utils/formatters'
|
2017-06-12 01:01:27 +00:00
|
|
|
|
|
|
|
|
2017-07-04 00:08:35 +00:00
|
|
|
function findRateFromBityRateList(rateObjects, pairName) {
|
|
|
|
return rateObjects.find(x => x.pair === pairName);
|
|
|
|
}
|
2017-06-12 01:01:27 +00:00
|
|
|
|
2017-07-08 02:00:09 +00:00
|
|
|
// FIXME better types
|
|
|
|
function _getRate(bityRates, origin: string, destination: string) {
|
2017-07-04 00:08:35 +00:00
|
|
|
const pairName = combineAndUpper(origin, destination);
|
2017-07-08 02:00:09 +00:00
|
|
|
const rateObjects = bityRates.objects;
|
2017-07-04 00:08:35 +00:00
|
|
|
return findRateFromBityRateList(rateObjects, pairName);
|
|
|
|
}
|
2017-06-12 01:01:27 +00:00
|
|
|
|
2017-07-04 00:08:35 +00:00
|
|
|
/**
|
|
|
|
* Gives you multiple rates from Bitys API without making multiple API calls
|
|
|
|
* @param arrayOfOriginAndDestinationDicts - [{origin: 'BTC', destination: 'ETH'}, {origin: 'BTC', destination: 'REP}]
|
|
|
|
*/
|
|
|
|
function getMultipleRates(arrayOfOriginAndDestinationDicts) {
|
|
|
|
const mappedRates = {};
|
|
|
|
return _getAllRates().then(bityRates => {
|
|
|
|
arrayOfOriginAndDestinationDicts.forEach(each => {
|
|
|
|
const origin = each.origin;
|
|
|
|
const destination = each.destination;
|
|
|
|
const pairName = combineAndUpper(origin, destination);
|
|
|
|
const rate = _getRate(bityRates, origin, destination);
|
|
|
|
mappedRates[pairName] = parseFloat(rate.rate_we_sell);
|
2017-06-19 05:39:07 +00:00
|
|
|
});
|
2017-07-04 00:08:35 +00:00
|
|
|
return mappedRates;
|
|
|
|
});
|
|
|
|
// TODO - catch errors
|
|
|
|
}
|
2017-06-12 01:01:27 +00:00
|
|
|
|
2017-07-04 00:08:35 +00:00
|
|
|
export function getAllRates() {
|
|
|
|
const mappedRates = {};
|
|
|
|
return _getAllRates().then(bityRates => {
|
2017-07-08 02:00:09 +00:00
|
|
|
bityRates.objects.forEach(each => {
|
2017-07-04 00:08:35 +00:00
|
|
|
const pairName = each.pair;
|
|
|
|
mappedRates[pairName] = parseFloat(each.rate_we_sell);
|
2017-06-19 05:39:07 +00:00
|
|
|
});
|
2017-07-04 00:08:35 +00:00
|
|
|
return mappedRates;
|
|
|
|
});
|
|
|
|
// TODO - catch errors
|
|
|
|
}
|
2017-06-12 01:01:27 +00:00
|
|
|
|
2017-07-04 00:08:35 +00:00
|
|
|
function _getAllRates() {
|
2017-07-08 02:00:09 +00:00
|
|
|
return fetch(`${bityConfig.bityAPI}/v1/rate2/`).then(r => r.json());
|
2017-06-12 01:01:27 +00:00
|
|
|
}
|
2017-07-04 00:08:35 +00:00
|
|
|
|
|
|
|
function requestStatus() {}
|