mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 12:15:51 +00:00
JS and UT: XNS -> XRP
This commit is contained in:
committed by
Stefan Thomas
parent
10ce6f3a12
commit
19ec41e7bd
@@ -218,11 +218,11 @@ UInt160.prototype.to_json = function () {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var Currency = function () {
|
var Currency = function () {
|
||||||
// Internal form: 0 = XNS. 3 letter-code.
|
// Internal form: 0 = XRP. 3 letter-code.
|
||||||
// XXX Internal should be 0 or hex with three letter annotation when valid.
|
// XXX Internal should be 0 or hex with three letter annotation when valid.
|
||||||
|
|
||||||
// Json form:
|
// Json form:
|
||||||
// '', 'XNS', '0': 0
|
// '', 'XRP', '0': 0
|
||||||
// 3-letter code: ...
|
// 3-letter code: ...
|
||||||
// XXX Should support hex, C++ doesn't currently allow it.
|
// XXX Should support hex, C++ doesn't currently allow it.
|
||||||
|
|
||||||
@@ -253,7 +253,7 @@ Currency.prototype.copyTo = function(d) {
|
|||||||
|
|
||||||
// this._value = NaN on error.
|
// this._value = NaN on error.
|
||||||
Currency.prototype.parse_json = function(j) {
|
Currency.prototype.parse_json = function(j) {
|
||||||
if ("" === j || "0" === j || "XNS" === j) {
|
if ("" === j || "0" === j || "XRP" === j) {
|
||||||
this._value = 0;
|
this._value = 0;
|
||||||
}
|
}
|
||||||
else if ('string' != typeof j || 3 !== j.length) {
|
else if ('string' != typeof j || 3 !== j.length) {
|
||||||
@@ -267,22 +267,22 @@ Currency.prototype.parse_json = function(j) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Currency.prototype.to_json = function () {
|
Currency.prototype.to_json = function () {
|
||||||
return this._value ? this._value : "XNS";
|
return this._value ? this._value : "XRP";
|
||||||
};
|
};
|
||||||
|
|
||||||
Currency.prototype.to_human = function() {
|
Currency.prototype.to_human = function() {
|
||||||
return this._value ? this._value : "XNS";
|
return this._value ? this._value : "XRP";
|
||||||
};
|
};
|
||||||
|
|
||||||
var Amount = function () {
|
var Amount = function () {
|
||||||
// Json format:
|
// Json format:
|
||||||
// integer : XNS
|
// integer : XRP
|
||||||
// { 'value' : ..., 'currency' : ..., 'issuer' : ...}
|
// { 'value' : ..., 'currency' : ..., 'issuer' : ...}
|
||||||
|
|
||||||
this._value = new BigInteger(); // NaN for bad value. Always positive for non-XNS.
|
this._value = new BigInteger(); // NaN for bad value. Always positive for non-XRP.
|
||||||
this._offset = undefined; // For non-XNS.
|
this._offset = undefined; // For non-XRP.
|
||||||
this._is_native = true; // Default to XNS. Only valid if value is not NaN.
|
this._is_native = true; // Default to XRP. Only valid if value is not NaN.
|
||||||
this._is_negative = undefined; // Undefined for XNS.
|
this._is_negative = undefined; // Undefined for XRP.
|
||||||
|
|
||||||
this.currency = new Currency();
|
this.currency = new Currency();
|
||||||
this.issuer = new UInt160();
|
this.issuer = new UInt160();
|
||||||
@@ -432,11 +432,11 @@ Amount.prototype.to_text_full = function() {
|
|||||||
return isNaN(this._value)
|
return isNaN(this._value)
|
||||||
? NaN
|
? NaN
|
||||||
: this._is_native
|
: this._is_native
|
||||||
? this.to_text() + "/XNS"
|
? this.to_text() + "/XRP"
|
||||||
: this.to_text() + "/" + this.currency.to_json() + "/" + this.issuer.to_json();
|
: this.to_text() + "/" + this.currency.to_json() + "/" + this.issuer.to_json();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Parse a XNS value from untrusted input.
|
// Parse a XRP value from untrusted input.
|
||||||
// - integer = raw units
|
// - integer = raw units
|
||||||
// - float = with precision 6
|
// - float = with precision 6
|
||||||
// XXX Improvements: disallow leading zeros.
|
// XXX Improvements: disallow leading zeros.
|
||||||
@@ -566,7 +566,7 @@ Amount.prototype.parse_json = function(j) {
|
|||||||
// Parse the passed value to sanitize and copy it.
|
// Parse the passed value to sanitize and copy it.
|
||||||
|
|
||||||
this.parse_value(j.value);
|
this.parse_value(j.value);
|
||||||
this.currency.parse_json(j.currency); // Never XNS.
|
this.currency.parse_json(j.currency); // Never XRP.
|
||||||
this.issuer.parse_json(j.issuer);
|
this.issuer.parse_json(j.issuer);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ require("../src/js/amount.js").config = require("./config.js");
|
|||||||
|
|
||||||
var config = require('./config.js');
|
var config = require('./config.js');
|
||||||
|
|
||||||
|
// XXX Add test cases that push XRP vs non-XRP behavior.
|
||||||
|
|
||||||
buster.testCase("Amount", {
|
buster.testCase("Amount", {
|
||||||
"UInt160" : {
|
"UInt160" : {
|
||||||
"Parse 0" : function () {
|
"Parse 0" : function () {
|
||||||
@@ -38,25 +40,25 @@ buster.testCase("Amount", {
|
|||||||
buster.assert.equals("800/USD/"+config.accounts["mtgox"].account, Amount.from_json("800/USD/mtgox").to_text_full());
|
buster.assert.equals("800/USD/"+config.accounts["mtgox"].account, Amount.from_json("800/USD/mtgox").to_text_full());
|
||||||
},
|
},
|
||||||
"Parse native 0" : function () {
|
"Parse native 0" : function () {
|
||||||
buster.assert.equals("0/XNS", Amount.from_json("0").to_text_full());
|
buster.assert.equals("0/XRP", Amount.from_json("0").to_text_full());
|
||||||
},
|
},
|
||||||
"Parse native 0.0" : function () {
|
"Parse native 0.0" : function () {
|
||||||
buster.assert.equals("0/XNS", Amount.from_json("0.0").to_text_full());
|
buster.assert.equals("0/XRP", Amount.from_json("0.0").to_text_full());
|
||||||
},
|
},
|
||||||
"Parse native -0" : function () {
|
"Parse native -0" : function () {
|
||||||
buster.assert.equals("0/XNS", Amount.from_json("-0").to_text_full());
|
buster.assert.equals("0/XRP", Amount.from_json("-0").to_text_full());
|
||||||
},
|
},
|
||||||
"Parse native -0.0" : function () {
|
"Parse native -0.0" : function () {
|
||||||
buster.assert.equals("0/XNS", Amount.from_json("-0.0").to_text_full());
|
buster.assert.equals("0/XRP", Amount.from_json("-0.0").to_text_full());
|
||||||
},
|
},
|
||||||
"Parse native 1000" : function () {
|
"Parse native 1000" : function () {
|
||||||
buster.assert.equals("1000/XNS", Amount.from_json("1000").to_text_full());
|
buster.assert.equals("1000/XRP", Amount.from_json("1000").to_text_full());
|
||||||
},
|
},
|
||||||
"Parse native 12.3" : function () {
|
"Parse native 12.3" : function () {
|
||||||
buster.assert.equals("12300000/XNS", Amount.from_json("12.3").to_text_full());
|
buster.assert.equals("12300000/XRP", Amount.from_json("12.3").to_text_full());
|
||||||
},
|
},
|
||||||
"Parse native -12.3" : function () {
|
"Parse native -12.3" : function () {
|
||||||
buster.assert.equals("-12300000/XNS", Amount.from_json("-12.3").to_text_full());
|
buster.assert.equals("-12300000/XRP", Amount.from_json("-12.3").to_text_full());
|
||||||
},
|
},
|
||||||
"Parse 123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" : function () {
|
"Parse 123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" : function () {
|
||||||
buster.assert.equals("123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
|
buster.assert.equals("123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
|
||||||
@@ -79,10 +81,10 @@ buster.testCase("Amount", {
|
|||||||
},
|
},
|
||||||
"Amount operations" : {
|
"Amount operations" : {
|
||||||
"Negate native 123" : function () {
|
"Negate native 123" : function () {
|
||||||
buster.assert.equals("-123/XNS", Amount.from_json("123").negate().to_text_full());
|
buster.assert.equals("-123/XRP", Amount.from_json("123").negate().to_text_full());
|
||||||
},
|
},
|
||||||
"Negate native -123" : function () {
|
"Negate native -123" : function () {
|
||||||
buster.assert.equals("123/XNS", Amount.from_json("-123").negate().to_text_full());
|
buster.assert.equals("123/XRP", Amount.from_json("-123").negate().to_text_full());
|
||||||
},
|
},
|
||||||
"Negate non-native 123" : function () {
|
"Negate non-native 123" : function () {
|
||||||
buster.assert.equals("-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").negate().to_text_full());
|
buster.assert.equals("-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").negate().to_text_full());
|
||||||
|
|||||||
Reference in New Issue
Block a user