Skip to content

Commit

Permalink
Remove demurrage
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Clark committed Oct 15, 2015
1 parent 462e440 commit 512817a
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 769 deletions.
78 changes: 8 additions & 70 deletions src/core/amount.js
Expand Up @@ -190,12 +190,9 @@ Amount.prototype.divide = function(divisor) {
* @return {Amount} The resulting ratio. Unit will be the same as numerator.
*/

Amount.prototype.ratio_human = function(denom, opts) {
const options = extend({ }, opts);

Amount.prototype.ratio_human = function(denom) {
const numerator = this.clone();

let denominator = Amount.from_json(denom);
const denominator = Amount.from_json(denom);

// If either operand is NaN, the result is NaN.
if (!numerator.is_valid() || !denominator.is_valid()) {
Expand All @@ -206,14 +203,6 @@ Amount.prototype.ratio_human = function(denom, opts) {
return new Amount(NaN);
}

// Apply interest/demurrage
//
// We only need to apply it to the second factor, because the currency unit of
// the first factor will carry over into the result.
if (options.reference_date) {
denominator = denominator.applyInterest(options.reference_date);
}

// Special case: The denominator is a native (XRP) amount.
//
// In that case, it's going to be expressed as base units (1 XRP =
Expand Down Expand Up @@ -252,23 +241,14 @@ Amount.prototype.ratio_human = function(denom, opts) {
* for Ripple epoch.
* @return {Amount} The product. Unit will be the same as the first factor.
*/
Amount.prototype.product_human = function(factor, options = {}) {

let fac = Amount.from_json(factor);
Amount.prototype.product_human = function(factor) {
const fac = Amount.from_json(factor);

// If either operand is NaN, the result is NaN.
if (!this.is_valid() || !fac.is_valid()) {
return new Amount();
}

// Apply interest/demurrage
//
// We only need to apply it to the second factor, because the currency unit of
// the first factor will carry over into the result.
if (options.reference_date) {
fac = fac.applyInterest(options.reference_date);
}

const product = this.multiply(fac);

// Special case: The second factor is a native (XRP) amount expressed as base
Expand Down Expand Up @@ -464,9 +444,7 @@ Amount.prototype.negate = function() {
* $
*/

Amount.prototype.parse_human = function(j, options) {
const opts = options || {};

Amount.prototype.parse_human = function(j) {
const hex_RE = /^[a-fA-F0-9]{40}$/;
const currency_RE = /^([a-zA-Z]{3}|[0-9]{3})$/;

Expand Down Expand Up @@ -516,14 +494,6 @@ Amount.prototype.parse_human = function(j, options) {
(this._is_native ? new XRPValue(value) :
new IOUValue(value));
this._set_value(newValue);

// Apply interest/demurrage
if (opts.reference_date && this._currency.has_interest()) {
const interest = this._currency.get_interest_at(opts.reference_date);
this._set_value(
this._value.divide(new IOUValue(interest.toString())));
}

return this;
};

Expand Down Expand Up @@ -627,12 +597,6 @@ function(quality, counterCurrency, counterIssuer, opts) {
this._set_value(nativeAdjusted);
}

if (options.reference_date && baseCurrency.is_valid()
&& baseCurrency.has_interest()) {
const interest = baseCurrency.get_interest_at(options.reference_date);
this._set_value(
this._value.divide(new IOUValue(interest.toString())));
}
return this;
};

Expand Down Expand Up @@ -782,27 +746,6 @@ Amount.prototype.to_text = function() {
+ (s_post ? '.' + post.substring(0, 1 + post.length - s_post[0].length) : '');
};

/**
* Calculate present value based on currency and a reference date.
*
* This only affects demurraging and interest-bearing currencies.
*
* User should not store amount objects after the interest is applied. This is
* intended by display functions such as toHuman().
*
* @param {Date|Number} referenceDate Date based on which demurrage/interest
* should be applied. Can be given as JavaScript Date or int for Ripple epoch.
* @return {Amount} The amount with interest applied.
*/
Amount.prototype.applyInterest = function(referenceDate) {
if (!this._currency.has_interest()) {
return this;
}
const interest = this._currency.get_interest_at(referenceDate);
return this._copy(
this._value.multiply(new IOUValue(interest.toString())));
};

/**
* Format only value in a human-readable format.
*
Expand Down Expand Up @@ -836,13 +779,9 @@ Amount.prototype.to_human = function(options) {

/* eslint-disable consistent-this */
// Apply demurrage/interest
let ref = this;
const ref = this;
/* eslint-enable consistent-this */

if (opts.reference_date) {
ref = this.applyInterest(opts.reference_date);
}

const isNegative = ref._value.isNegative();
const valueString = ref._value.abs().toFixed();
const parts = valueString.split('.');
Expand Down Expand Up @@ -930,7 +869,7 @@ Amount.prototype.to_human = function(options) {
Amount.prototype.to_human_full = function(options) {
const opts = options || {};
const value = this.to_human(opts);
const currency = this._currency.to_human();
const currency = this._currency.to_json();
const issuer = this._issuer;
const base = value + '/' + currency;
return this.is_native() ? base : (base + '/' + issuer);
Expand All @@ -943,8 +882,7 @@ Amount.prototype.to_json = function() {

const amount_json = {
value: this.to_text(),
currency: this._currency.has_interest() ?
this._currency.to_hex() : this._currency.to_json()
currency: this._currency.to_json()
};

if (isValidAddress(this._issuer)) {
Expand Down
8 changes: 4 additions & 4 deletions src/core/autobridgecalculator.js
Expand Up @@ -157,7 +157,7 @@ function(legOneOffer, legTwoOffer) {
this._currencyPays, this._issuerPays);
const legTwoTakerPaysFunded = Utils.getOfferTakerPaysFunded(legTwoOffer,
this._currencyGets, this._issuerGets);
const legOneQuality = Utils.getOfferQuality(legOneOffer, this._currencyGets,
const legOneQuality = Utils.getOfferQuality(legOneOffer,
this._currencyPays, this._issuerPays);

const autobridgedTakerGets = Utils.getOfferTakerGetsFunded(legTwoOffer,
Expand Down Expand Up @@ -202,7 +202,7 @@ function(legOneOffer, legTwoOffer) {
this._currencyPays, this._issuerPays);
const legTwoTakerPaysFunded = Utils.getOfferTakerPaysFunded(legTwoOffer,
this._currencyGets, this._issuerGets);
const legTwoQuality = Utils.getOfferQuality(legTwoOffer, this._currencyGets,
const legTwoQuality = Utils.getOfferQuality(legTwoOffer,
this._currencyGets, this._issuerGets);

const autobridgedTakerGets = legOneTakerGetsFunded.divide(legTwoQuality);
Expand Down Expand Up @@ -463,7 +463,7 @@ function setLegOneTakerGetsFunded(legOneOffer, takerGetsFunded) {

legOneOffer.taker_gets_funded = takerGetsFunded.to_text();
legOneOffer.taker_pays_funded = takerGetsFunded
.multiply(Utils.getOfferQuality(legOneOffer, this._currencyGets,
.multiply(Utils.getOfferQuality(legOneOffer,
this._currencyPays, this._issuerPays))
.to_text();

Expand All @@ -485,7 +485,7 @@ function(legOneOffer, takerGets) {
assertValidLegOneOffer(legOneOffer, 'Leg one offer is invalid');
assert(takerGets instanceof Amount, 'Taker gets funded is invalid');

const legOneQuality = Utils.getOfferQuality(legOneOffer, this._currencyGets,
const legOneQuality = Utils.getOfferQuality(legOneOffer,
this._currencyPays, this._issuerPays);

legOneOffer.TakerGets = takerGets.to_text();
Expand Down

0 comments on commit 512817a

Please sign in to comment.