MyCrypto/common/api/bity.js

63 lines
2.0 KiB
JavaScript
Raw Normal View History

2017-06-11 18:01:27 -07:00
import axios from 'axios';
2017-06-18 17:56:11 -07:00
import bityConfig from 'config/bity';
2017-06-11 18:01:27 -07:00
// https://stackoverflow.com/questions/9828684/how-to-get-all-arguments-of-a-callback-function
export function combineAndUpper() {
2017-06-18 22:39:07 -07:00
const args = [];
let newString = '';
for (let i = 0; i < arguments.length; ++i) args[i] = arguments[i];
args.forEach(each => {
newString = newString.concat(each.toUpperCase());
});
return newString;
2017-06-11 18:01:27 -07:00
}
export default class Bity {
2017-06-18 22:39:07 -07:00
findRateFromBityRateList(rateObjects, pairName) {
return rateObjects.find(x => x.pair === pairName);
}
2017-06-11 18:01:27 -07:00
2017-06-18 22:39:07 -07:00
_getRate(bityRates, origin, destination) {
const pairName = combineAndUpper(origin, destination);
const rateObjects = bityRates.data.objects;
return this.findRateFromBityRateList(rateObjects, pairName);
}
2017-06-11 18:01:27 -07:00
2017-06-18 22:39:07 -07:00
/**
2017-06-11 18:01:27 -07:00
* Gives you multiple rates from Bitys API without making multiple API calls
* @param arrayOfOriginAndDestinationDicts - [{origin: 'BTC', destination: 'ETH'}, {origin: 'BTC', destination: 'REP}]
*/
2017-06-18 22:39:07 -07:00
getMultipleRates(arrayOfOriginAndDestinationDicts) {
const mappedRates = {};
return this.requestAllRates().then(bityRates => {
arrayOfOriginAndDestinationDicts.forEach(each => {
const origin = each.origin;
const destination = each.destination;
const pairName = combineAndUpper(origin, destination);
const rate = this._getRate(bityRates, origin, destination);
mappedRates[pairName] = parseFloat(rate.rate_we_sell);
});
return mappedRates;
});
// TODO - catch errors
}
2017-06-11 18:01:27 -07:00
2017-06-18 22:39:07 -07:00
getAllRates() {
const mappedRates = {};
return this.requestAllRates().then(bityRates => {
bityRates.data.objects.forEach(each => {
const pairName = each.pair;
mappedRates[pairName] = parseFloat(each.rate_we_sell);
});
return mappedRates;
});
// TODO - catch errors
}
2017-06-11 18:01:27 -07:00
2017-06-18 22:39:07 -07:00
requestAllRates() {
const path = '/v1/rate2/';
const bityURL = bityConfig.bityAPI + path;
return axios.get(bityURL);
}
2017-06-11 18:01:27 -07:00
}