mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-29 08:35:49 +00:00
[CHORE] Improved Amount#parse_quality w/ demurrage support, drops->XRP, etc.
Amount#parse_quality is made currency-aware. This allows it to adjust for XRP as the base currency, as well as for interest-bearing or demurring base currencies.
This commit is contained in:
@@ -71,8 +71,8 @@ Amount.from_json = function (j) {
|
||||
return (new Amount()).parse_json(j);
|
||||
};
|
||||
|
||||
Amount.from_quality = function (quality, currency, issuer) {
|
||||
return (new Amount()).parse_quality(quality, currency, issuer);
|
||||
Amount.from_quality = function (quality, currency, issuer, opts) {
|
||||
return (new Amount()).parse_quality(quality, currency, issuer, opts);
|
||||
};
|
||||
|
||||
Amount.from_human = function (j, opts) {
|
||||
@@ -667,17 +667,81 @@ Amount.prototype.parse_issuer = function (issuer) {
|
||||
return this;
|
||||
};
|
||||
|
||||
// --> h: 8 hex bytes quality or 32 hex bytes directory index.
|
||||
Amount.prototype.parse_quality = function (quality, currency, issuer) {
|
||||
/**
|
||||
* Decode a price from a BookDirectory index.
|
||||
*
|
||||
* BookDirectory ledger entries each encode the offer price in their index. This
|
||||
* method can decode that information and populate an Amount object with it.
|
||||
*
|
||||
* It is possible not to provide a currency or issuer, but be aware that Amount
|
||||
* objects behave differently based on the currency, so you may get incorrect
|
||||
* results.
|
||||
*
|
||||
* Prices involving demurraging currencies are tricky, since they depend on the
|
||||
* base and counter currencies.
|
||||
*
|
||||
* @param quality {String} 8 hex bytes quality or 32 hex bytes BookDirectory
|
||||
* index.
|
||||
* @param counterCurrency {Currency|String} Currency of the resulting Amount
|
||||
* object.
|
||||
* @param counterIssuer {Issuer|String} Issuer of the resulting Amount object.
|
||||
* @param opts Additional options
|
||||
* @param opts.inverse {Boolean} If true, return the inverse of the price
|
||||
* encoded in the quality.
|
||||
* @param opts.base_currency {Currency|String} The other currency. This plays a
|
||||
* role with interest-bearing or demurrage currencies. In that case the
|
||||
* demurrage has to be applied when the quality is decoded, otherwise the
|
||||
* price will be false.
|
||||
* @param opts.reference_date {Date|Number} Date based on which demurrage/interest
|
||||
* should be applied. Can be given as JavaScript Date or int for Ripple epoch.
|
||||
* @param opts.xrp_as_drops {Boolean} Whether XRP amount should be treated as
|
||||
* drops. When the base currency is XRP, the quality is calculated in drops.
|
||||
* For human use however, we want to think of 1000000 drops as 1 XRP and
|
||||
* prices as per-XRP instead of per-drop.
|
||||
*/
|
||||
Amount.prototype.parse_quality = function (quality, counterCurrency, counterIssuer, opts)
|
||||
{
|
||||
opts = opts || {};
|
||||
|
||||
var baseCurrency = Currency.from_json(opts.base_currency);
|
||||
|
||||
this._is_negative = false;
|
||||
this._value = new BigInteger(quality.substring(quality.length-14), 16);
|
||||
this._offset = parseInt(quality.substring(quality.length-16, quality.length-14), 16)-100;
|
||||
this._currency = Currency.from_json(currency);
|
||||
this._issuer = UInt160.from_json(issuer);
|
||||
this._currency = Currency.from_json(counterCurrency);
|
||||
this._issuer = UInt160.from_json(counterIssuer);
|
||||
this._is_native = this._currency.is_native();
|
||||
|
||||
// Correct offset if xrp_as_drops option is not set and base currency is XRP
|
||||
if (!opts.xrp_as_drops &&
|
||||
baseCurrency.is_valid() &&
|
||||
baseCurrency.is_native()) {
|
||||
if (opts.inverse) {
|
||||
this._offset -= 6;
|
||||
} else {
|
||||
this._offset += 6;
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.inverse) {
|
||||
this._invert();
|
||||
}
|
||||
|
||||
this.canonicalize();
|
||||
|
||||
if (opts.reference_date && baseCurrency.is_valid() && baseCurrency.has_interest()) {
|
||||
var interest = baseCurrency.get_interest_at(opts.reference_date);
|
||||
|
||||
// XXX If we had better math utilities, we wouldn't need this hack.
|
||||
var interestTempAmount = Amount.from_json(""+interest+"/1/1");
|
||||
|
||||
if (interestTempAmount.is_valid()) {
|
||||
var v = this.divide(interestTempAmount);
|
||||
this._value = v._value;
|
||||
this._offset = v._offset;
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -225,11 +225,12 @@ OrderBook.prototype.notify = function (message) {
|
||||
break;
|
||||
|
||||
case 'CreatedNode':
|
||||
var price = Amount.from_json(an.fields.TakerPays).ratio_human(an.fields.TakerGets);
|
||||
// XXX Should use Amount#from_quality
|
||||
var price = Amount.from_json(an.fields.TakerPays).ratio_human(an.fields.TakerGets, {reference_date: new Date()});
|
||||
|
||||
for (i = 0, l = self._offers.length; i < l; i++) {
|
||||
offer = self._offers[i];
|
||||
var priceItem = Amount.from_json(offer.TakerPays).ratio_human(offer.TakerGets);
|
||||
var priceItem = Amount.from_json(offer.TakerPays).ratio_human(offer.TakerGets, {reference_date: new Date()});
|
||||
|
||||
if (price.compareTo(priceItem) <= 0) {
|
||||
var obj = an.fields;
|
||||
|
||||
@@ -524,4 +524,37 @@ describe('Amount', function() {
|
||||
assert.strictEqual(Amount.from_json('0.02/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').invert().to_text_full(), '50/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
|
||||
});
|
||||
});
|
||||
|
||||
describe('from_quality', function() {
|
||||
it('BTC/XRP', function () {
|
||||
assert.strictEqual(Amount.from_quality('7B73A610A009249B0CC0D4311E8BA7927B5A34D86634581C5F0FF9FF678E1000', 'XRP', NaN, {base_currency: 'BTC'}).to_text_full(), '44,970/XRP');
|
||||
});
|
||||
it('BTC/XRP inverse', function () {
|
||||
assert.strictEqual(Amount.from_quality('37AAC93D336021AE94310D0430FFA090F7137C97D473488C4A0918D0DEF8624E', 'XRP', NaN, {inverse: true, base_currency: 'BTC'}).to_text_full(), '39,053.954453/XRP');
|
||||
});
|
||||
it('XRP/USD', function () {
|
||||
assert.strictEqual(Amount.from_quality('DFA3B6DDAB58C7E8E5D944E736DA4B7046C30E4F460FD9DE4D05DCAA8FE12000', 'USD', 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', {base_currency: 'XRP'}).to_text_full(), '0.0165/USD/rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B');
|
||||
});
|
||||
it('XRP/USD inverse', function () {
|
||||
assert.strictEqual(Amount.from_quality('4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5C22A840E27DCA9B', 'USD', 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', {inverse: true, base_currency: 'XRP'}).to_text_full(), '0.010251/USD/rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B');
|
||||
});
|
||||
it('BTC/USD', function () {
|
||||
assert.strictEqual(Amount.from_quality('6EAB7C172DEFA430DBFAD120FDC373B5F5AF8B191649EC9858038D7EA4C68000', 'USD', 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', {base_currency: 'BTC'}).to_text_full(), '1000/USD/rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B');
|
||||
});
|
||||
it('BTC/USD inverse', function () {
|
||||
assert.strictEqual(Amount.from_quality('20294C923E80A51B487EB9547B3835FD483748B170D2D0A455071AFD498D0000', 'USD', 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', {inverse: true, base_currency: 'BTC'}).to_text_full(), '0.5/USD/rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B');
|
||||
});
|
||||
it('XAU(dem)/XRP', function () {
|
||||
assert.strictEqual(Amount.from_quality('587322CCBDE0ABD01704769A73A077C32FB39057D813D4165F1FF973CAF997EF', 'XRP', NaN, {base_currency: '015841551A748AD2C1F76FF6ECB0CCCD00000000', reference_date: 443845330 + 31535000}).to_text_full(), '90,452.246928/XRP');
|
||||
});
|
||||
it('XAU(dem)/XRP inverse', function () {
|
||||
assert.strictEqual(Amount.from_quality('F72C7A9EAE4A45ED1FB547AD037D07B9B965C6E662BEBAFA4A03F2A976804235', 'XRP', NaN, {inverse: true, base_currency: '015841551A748AD2C1F76FF6ECB0CCCD00000000', reference_date: 443845330 + 31535000}).to_text_full(), '90,442.196677/XRP');
|
||||
});
|
||||
it('USD/XAU(dem)', function () {
|
||||
assert.strictEqual(Amount.from_quality('4743E58E44974B325D42FD2BB683A6E36950F350EE46DD3A521B644B99782F5F', '015841551A748AD2C1F76FF6ECB0CCCD00000000', 'rUyPiNcSFFj6uMR2gEaD8jUerQ59G1qvwN', {base_currency: 'USD', reference_date: 443845330 + 31535000}).to_text_full(), '0.007710100231303007/015841551A748AD2C1F76FF6ECB0CCCD00000000/rUyPiNcSFFj6uMR2gEaD8jUerQ59G1qvwN');
|
||||
});
|
||||
it('USD/XAU(dem) inverse', function () {
|
||||
assert.strictEqual(Amount.from_quality('CDFD3AFB2F8C5DBEF75B081F7C957FF5509563266F28F36C5704A0FB0BAD8800', '015841551A748AD2C1F76FF6ECB0CCCD00000000', 'rUyPiNcSFFj6uMR2gEaD8jUerQ59G1qvwN', {inverse: true, base_currency: 'USD', reference_date: 443845330 + 31535000}).to_text_full(), '0.007675186123263489/015841551A748AD2C1F76FF6ECB0CCCD00000000/rUyPiNcSFFj6uMR2gEaD8jUerQ59G1qvwN');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user