2017-06-12 01:01:27 +00:00
|
|
|
import axios from 'axios';
|
2017-06-19 00:56:11 +00:00
|
|
|
import bityConfig from 'config/bity';
|
2017-06-12 01:01:27 +00:00
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/9828684/how-to-get-all-arguments-of-a-callback-function
|
|
|
|
export function combineAndUpper() {
|
2017-06-19 05:39:07 +00: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-12 01:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class Bity {
|
2017-06-19 05:39:07 +00:00
|
|
|
findRateFromBityRateList(rateObjects, pairName) {
|
|
|
|
return rateObjects.find(x => x.pair === pairName);
|
|
|
|
}
|
2017-06-12 01:01:27 +00:00
|
|
|
|
2017-06-19 05:39:07 +00:00
|
|
|
_getRate(bityRates, origin, destination) {
|
|
|
|
const pairName = combineAndUpper(origin, destination);
|
|
|
|
const rateObjects = bityRates.data.objects;
|
|
|
|
return this.findRateFromBityRateList(rateObjects, pairName);
|
|
|
|
}
|
2017-06-12 01:01:27 +00:00
|
|
|
|
2017-06-19 05:39:07 +00:00
|
|
|
/**
|
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}]
|
|
|
|
*/
|
2017-06-19 05:39:07 +00: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-12 01:01:27 +00:00
|
|
|
|
2017-06-19 05:39:07 +00: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-12 01:01:27 +00:00
|
|
|
|
2017-06-19 05:39:07 +00:00
|
|
|
requestAllRates() {
|
|
|
|
const path = '/v1/rate2/';
|
|
|
|
const bityURL = bityConfig.bityAPI + path;
|
|
|
|
return axios.get(bityURL);
|
|
|
|
}
|
2017-06-12 01:01:27 +00:00
|
|
|
}
|