MyCrypto/common/api/bity.js

65 lines
2.2 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 12:47:00 -07:00
const args = [];
2017-06-11 18:01:27 -07:00
let newString = '';
for (let i = 0; i < arguments.length; ++i) args[i] = arguments[i];
args.forEach((each) => {
newString = newString.concat(each.toUpperCase())
});
return newString
}
export default class Bity {
findRateFromBityRateList(rateObjects, pairName) {
return rateObjects.find(x => x.pair === pairName);
}
_getRate(bityRates, origin, destination) {
2017-06-18 12:47:00 -07:00
const pairName = combineAndUpper(origin, destination);
const rateObjects = bityRates.data.objects;
2017-06-11 18:01:27 -07:00
return this.findRateFromBityRateList(rateObjects, pairName);
}
/**
* Gives you multiple rates from Bitys API without making multiple API calls
* @param arrayOfOriginAndDestinationDicts - [{origin: 'BTC', destination: 'ETH'}, {origin: 'BTC', destination: 'REP}]
*/
getMultipleRates(arrayOfOriginAndDestinationDicts) {
2017-06-18 12:47:00 -07:00
const mappedRates = {};
2017-06-11 18:01:27 -07:00
return this.requestAllRates()
.then((bityRates) => {
arrayOfOriginAndDestinationDicts.forEach((each) => {
2017-06-18 12:47:00 -07:00
const origin = each.origin;
const destination = each.destination;
const pairName = combineAndUpper(origin, destination);
const rate = this._getRate(bityRates, origin, destination);
2017-06-11 18:01:27 -07:00
mappedRates[pairName] = parseFloat(rate.rate_we_sell)
});
return mappedRates
})
// TODO - catch errors
}
getAllRates() {
2017-06-18 12:47:00 -07:00
const mappedRates = {};
2017-06-11 18:01:27 -07:00
return this.requestAllRates()
.then((bityRates) => {
bityRates.data.objects.forEach((each) => {
2017-06-18 12:47:00 -07:00
const pairName = each.pair;
2017-06-11 18:01:27 -07:00
mappedRates[pairName] = parseFloat(each.rate_we_sell)
});
return mappedRates
})
// TODO - catch errors
}
requestAllRates() {
2017-06-18 12:47:00 -07:00
const path = '/v1/rate2/';
2017-06-18 17:56:11 -07:00
const bityURL = bityConfig.bityAPI + path;
2017-06-11 18:01:27 -07:00
return axios.get(bityURL)
}
}