MyCrypto/common/api/bity.js

53 lines
1.6 KiB
JavaScript
Raw Normal View History

// @flow
2017-06-19 00:56:11 +00:00
import bityConfig from 'config/bity';
import {combineAndUpper} from 'utils/formatters'
2017-06-12 01:01:27 +00:00
function findRateFromBityRateList(rateObjects, pairName) {
return rateObjects.find(x => x.pair === pairName);
}
2017-06-12 01:01:27 +00:00
// FIXME better types
function _getRate(bityRates, origin: string, destination: string) {
const pairName = combineAndUpper(origin, destination);
const rateObjects = bityRates.objects;
return findRateFromBityRateList(rateObjects, pairName);
}
2017-06-12 01:01:27 +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
});
return mappedRates;
});
// TODO - catch errors
}
2017-06-12 01:01:27 +00:00
export function getAllRates() {
const mappedRates = {};
return _getAllRates().then(bityRates => {
bityRates.objects.forEach(each => {
const pairName = each.pair;
mappedRates[pairName] = parseFloat(each.rate_we_sell);
2017-06-19 05:39:07 +00:00
});
return mappedRates;
});
// TODO - catch errors
}
2017-06-12 01:01:27 +00:00
function _getAllRates() {
return fetch(`${bityConfig.bityAPI}/v1/rate2/`).then(r => r.json());
2017-06-12 01:01:27 +00:00
}
function requestStatus() {}