mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-28 06:55:50 +00:00
UT: Lots of improvements and fixes.
- Parse negative amounts. - Make Amount.negate() return a new value. - Add Amount.equals(). - Rename Remote.trace() to set_trace(). - Fix request_ripple_balnce. - Add more tests to send-test.js. - Add more tests to amount-test.js. - Add helper functions create_accounts and credit_limits to testutils.js.
This commit is contained in:
60
js/amount.js
60
js/amount.js
@@ -38,6 +38,10 @@ UInt160.prototype.copyTo = function(d) {
|
||||
return d;
|
||||
};
|
||||
|
||||
UInt160.prototype.equals = function(d) {
|
||||
return this.value === d.value;
|
||||
};
|
||||
|
||||
// value = NaN on error.
|
||||
UInt160.prototype.parse_json = function (j) {
|
||||
// Canonicalize and validate
|
||||
@@ -166,6 +170,12 @@ var Amount = function () {
|
||||
this.issuer = new UInt160();
|
||||
};
|
||||
|
||||
// Given "100/USD/mtgox" return the a string with mtgox remapped.
|
||||
Amount.text_full_rewrite = function (j) {
|
||||
return Amount.from_json(j).to_text_full();
|
||||
}
|
||||
|
||||
// Given "100/USD/mtgox" return the json.
|
||||
Amount.json_rewrite = function(j) {
|
||||
return Amount.from_json(j).to_json();
|
||||
};
|
||||
@@ -174,15 +184,18 @@ Amount.from_json = function(j) {
|
||||
return (new Amount()).parse_json(j);
|
||||
};
|
||||
|
||||
Amount.prototype.clone = function() {
|
||||
return this.copyTo(new Amount());
|
||||
Amount.prototype.clone = function(negate) {
|
||||
return this.copyTo(new Amount(), negate);
|
||||
};
|
||||
|
||||
// Returns copy.
|
||||
Amount.prototype.copyTo = function(d) {
|
||||
Amount.prototype.copyTo = function(d, negate) {
|
||||
if ('object' === typeof this.value)
|
||||
{
|
||||
this.value.copyTo(d.value);
|
||||
if (this.is_native && negate)
|
||||
this.value.negate.copyTo(d.value);
|
||||
else
|
||||
this.value.copyTo(d.value);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -191,7 +204,7 @@ Amount.prototype.copyTo = function(d) {
|
||||
|
||||
d.offset = this.offset;
|
||||
d.is_native = this.is_native;
|
||||
d.is_negative = this.is_negative;
|
||||
d.is_negative = this.is_negative ? undefined : !this.is_negative;
|
||||
|
||||
this.currency.copyTo(d.currency);
|
||||
this.issuer.copyTo(d.issuer);
|
||||
@@ -266,15 +279,13 @@ Amount.prototype.canonicalize = function() {
|
||||
this.offset += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
// Return a new value.
|
||||
Amount.prototype.negate = function () {
|
||||
if (this.is_native) {
|
||||
this.value.negate();
|
||||
}
|
||||
else {
|
||||
this.is_negative = !this.is_negative;
|
||||
}
|
||||
return this.clone('NEGATE');
|
||||
};
|
||||
|
||||
Amount.prototype.to_json = function() {
|
||||
@@ -307,23 +318,26 @@ Amount.prototype.parse_native = function(j) {
|
||||
var m;
|
||||
|
||||
if ('string' === typeof j)
|
||||
m = j.match(/^(\d+)(\.\d{1,6})?$/);
|
||||
m = j.match(/^(-?)(\d+)(\.\d{1,6})?$/);
|
||||
|
||||
if (m) {
|
||||
if (undefined === m[2]) {
|
||||
if (undefined === m[3]) {
|
||||
// Integer notation
|
||||
|
||||
this.value = new BigInteger(m[1]);
|
||||
this.value = new BigInteger(m[2]);
|
||||
}
|
||||
else {
|
||||
// Decimal notation
|
||||
|
||||
var int_part = (new BigInteger(m[1])).multiply(exports.consts.bi_xns_unit);
|
||||
var fraction_part = (new BigInteger(m[2])).multiply(new BigInteger(String(Math.pow(10, 1+exports.consts.xns_precision-m[2].length))));
|
||||
var int_part = (new BigInteger(m[2])).multiply(exports.consts.bi_xns_unit);
|
||||
var fraction_part = (new BigInteger(m[3])).multiply(new BigInteger(String(Math.pow(10, 1+exports.consts.xns_precision-m[3].length))));
|
||||
|
||||
this.value = int_part.add(fraction_part);
|
||||
}
|
||||
|
||||
if (m[1])
|
||||
this.value = this.value.negate();
|
||||
|
||||
this.is_native = true;
|
||||
this.offset = undefined;
|
||||
this.is_negative = undefined;
|
||||
@@ -442,6 +456,20 @@ Amount.prototype.parse_issuer = function (issuer) {
|
||||
return this;
|
||||
};
|
||||
|
||||
// Check BigInteger NaN
|
||||
Amount.prototype.equals = function (d) {
|
||||
return 'string' === typeof (d)
|
||||
? this.equals(Amount.from_json(d))
|
||||
: this === d
|
||||
|| (d.constructor === Amount
|
||||
&& this.is_native === d.is_native
|
||||
&& (this.is_native
|
||||
? this.value.equals(d.value)
|
||||
: this.is_negative === d.is_negative
|
||||
? this.value.equals(d.value)
|
||||
: this.value.equals(BigInteger.ZERO) && d.value.equals(BigInteger.ZERO)));
|
||||
};
|
||||
|
||||
exports.Amount = Amount;
|
||||
exports.Currency = Currency;
|
||||
exports.UInt160 = UInt160;
|
||||
|
||||
Reference in New Issue
Block a user