Skip to content

Instantly share code, notes, and snippets.

@nerdylocks
Last active August 29, 2015 14:05
Show Gist options
  • Save nerdylocks/de9faf37a51f7d6befbb to your computer and use it in GitHub Desktop.
Save nerdylocks/de9faf37a51f7d6befbb to your computer and use it in GitHub Desktop.
'use strict';
var config = require('../config/nconf'),
request = require('request');
var Convert = function(){
this.saneMax = null;
this.saneMin = null;
};
Convert.prototype.discount = function(amount, discount){
var btc_units = 100000000;
var m = amount * btc_units;
var discountedAmount = (m + (m * discount));
return discountedAmount/ btc_units;
};
Convert.prototype.convert = function(amount, exchangeRate, toCurrency, discount) {
var a = this.discount(amount, discount);
return {
amount: a * exchangeRate,
currency: toCurrency
}
};
Convert.prototype.getRate = function(callback) {
var url = config.get('RIPPLE_CHARTS_API');
var json = {
"pairs" : [{
"base":{"currency":"BTC","issuer": config.get('EXCHANGE_ISSUER_ADDRESS')},
"trade":{"currency":"XRP"}
}]
};
request.post({ url: url, json: json }, callback);
};
Convert.prototype.sanePrice = function(exchangeRate){
this.saneMax = config.get('SANE_PRICE_BASE') + (config.get('SANE_PRICE_BASE') * .05);
this.saneMin = config.get('SANE_PRICE_BASE') - (config.get('SANE_PRICE_BASE') * .05);
if(exchangeRate < this.saneMax) {
if(exchangeRate > this.saneMin) {
return true;
}
}
};
Convert.prototype.toXrp = function(amount, callback){
var self = this;
self.getRate(function(error, response, body){
if(error){
return callback(new Error(error));
}
var exchangeRate = body[0]['last'];
var discountBy = config.get('DISCOUNT_PERCENTAGE') / 100;
var converted = self.convert(amount, exchangeRate, 'XRP', discountBy);
callback(null, converted);
});
};
module.exports = new Convert();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment