mirror of
https://github.com/Xahau/xahau.js.git
synced 2026-04-29 15:37:50 +00:00
Merge pull request #111 from geertweening/feature/order_book_hex
[FEATURE] use hex format for currencies in orderbook
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -45,4 +45,7 @@ test/config.js
|
||||
/coverage
|
||||
|
||||
# Ignore IntelliJ files
|
||||
.idea
|
||||
.idea
|
||||
|
||||
# Ignore npm-debug
|
||||
npm-debug.log
|
||||
@@ -617,8 +617,8 @@ Amount.prototype.parse_human = function(j, opts) {
|
||||
var m = String(j).match(Amount.human_RE);
|
||||
|
||||
if (m) {
|
||||
var currency = m[1] || m[5] || 'XRP';
|
||||
var integer = m[3] || '0';
|
||||
var currency = m[5] || m[1] || 'XRP';
|
||||
var integer = m[5] && m[1] ? m[1] + '' + m[3] : (m[3] || '0');
|
||||
var fraction = m[4] || '';
|
||||
var precision = null;
|
||||
|
||||
|
||||
@@ -313,16 +313,18 @@ Currency.prototype.to_json = function(opts) {
|
||||
return 'XRP';
|
||||
}
|
||||
|
||||
var opts = opts || {};
|
||||
|
||||
var currency;
|
||||
var fullName = opts && opts.full_name ? " - " + opts.full_name : "";
|
||||
|
||||
// Any currency with standard properties and a valid code can be abbreviated
|
||||
// in the JSON wire format as the three character code.
|
||||
if (/^[A-Z0-9]{3}$/.test(this._iso_code) && !this.has_interest()) {
|
||||
if (!opts.force_hex && /^[A-Z0-9]{3}$/.test(this._iso_code) && !this.has_interest()) {
|
||||
currency = this._iso_code + fullName;
|
||||
|
||||
// If there is interest, append the annual interest to the full currency name
|
||||
} else if (this.has_interest()) {
|
||||
} else if (!opts.force_hex && this.has_interest()) {
|
||||
var decimals = opts ? opts.decimals : undefined;
|
||||
currency = this._iso_code + fullName + " (" + this.get_interest_percentage_at(this._interest_start + 3600 * 24 * 365, decimals) + "%pa)";
|
||||
} else {
|
||||
|
||||
@@ -21,9 +21,9 @@ function OrderBook(remote, currency_gets, issuer_gets, currency_pays, issuer_pay
|
||||
var self = this;
|
||||
|
||||
this._remote = remote;
|
||||
this._currency_gets = currency_gets;
|
||||
this._currency_gets = Currency.from_json(currency_gets);
|
||||
this._issuer_gets = issuer_gets;
|
||||
this._currency_pays = currency_pays;
|
||||
this._currency_pays = Currency.from_json(currency_pays);
|
||||
this._issuer_pays = issuer_pays;
|
||||
this._subs = 0;
|
||||
this._key = key;
|
||||
@@ -132,18 +132,18 @@ OrderBook.prototype._prepareSubscribe = function (request) {
|
||||
OrderBook.prototype.to_json = function () {
|
||||
var json = {
|
||||
taker_gets: {
|
||||
currency: this._currency_gets
|
||||
currency: this._currency_gets.to_hex()
|
||||
},
|
||||
taker_pays: {
|
||||
currency: this._currency_pays
|
||||
currency: this._currency_pays.to_hex()
|
||||
}
|
||||
};
|
||||
|
||||
if (this._currency_gets !== 'XRP') {
|
||||
if (!this._currency_gets.is_native()) {
|
||||
json.taker_gets.issuer = this._issuer_gets;
|
||||
}
|
||||
|
||||
if (this._currency_pays !== 'XRP') {
|
||||
if (!this._currency_pays.is_native()) {
|
||||
json.taker_pays.issuer = this._issuer_pays;
|
||||
}
|
||||
|
||||
@@ -159,17 +159,19 @@ OrderBook.prototype.to_json = function () {
|
||||
OrderBook.prototype.is_valid = function () {
|
||||
// XXX Should check for same currency (non-native) && same issuer
|
||||
return (
|
||||
Currency.is_valid(this._currency_pays) &&
|
||||
(this._currency_pays === 'XRP' || UInt160.is_valid(this._issuer_pays)) &&
|
||||
Currency.is_valid(this._currency_gets) &&
|
||||
(this._currency_gets === 'XRP' || UInt160.is_valid(this._issuer_gets)) &&
|
||||
!(this._currency_pays === 'XRP' && this._currency_gets === 'XRP')
|
||||
this._currency_pays && this._currency_pays.is_valid() &&
|
||||
(this._currency_pays.is_native() || UInt160.is_valid(this._issuer_pays)) &&
|
||||
|
||||
this._currency_gets && this._currency_gets.is_valid() &&
|
||||
(this._currency_gets.is_native() || UInt160.is_valid(this._issuer_gets)) &&
|
||||
|
||||
!(this._currency_pays.is_native() && this._currency_gets.is_native())
|
||||
);
|
||||
};
|
||||
|
||||
OrderBook.prototype.trade = function(type) {
|
||||
var tradeStr = '0'
|
||||
+ ((this['_currency_' + type] === 'XRP') ? '' : '/'
|
||||
+ ((Currency.from_json(this['_currency_' + type]).is_native()) ? '' : '/'
|
||||
+ this['_currency_' + type ] + '/'
|
||||
+ this['_issuer_' + type]);
|
||||
return Amount.from_json(tradeStr);
|
||||
|
||||
@@ -1436,18 +1436,18 @@ Remote.prototype.requestBookOffers = function(gets, pays, taker, callback) {
|
||||
var request = new Request(this, 'book_offers');
|
||||
|
||||
request.message.taker_gets = {
|
||||
currency: Currency.json_rewrite(gets.currency)
|
||||
currency: Currency.json_rewrite(gets.currency, {force_hex:true})
|
||||
};
|
||||
|
||||
if (request.message.taker_gets.currency !== 'XRP') {
|
||||
if (!Currency.from_json(request.message.taker_gets.currency).is_native()) {
|
||||
request.message.taker_gets.issuer = UInt160.json_rewrite(gets.issuer);
|
||||
}
|
||||
|
||||
request.message.taker_pays = {
|
||||
currency: Currency.json_rewrite(pays.currency)
|
||||
currency: Currency.json_rewrite(pays.currency, {force_hex:true})
|
||||
};
|
||||
|
||||
if (request.message.taker_pays.currency !== 'XRP') {
|
||||
if (!Currency.from_json(request.message.taker_pays.currency).is_native()) {
|
||||
request.message.taker_pays.issuer = UInt160.json_rewrite(pays.issuer);
|
||||
}
|
||||
|
||||
@@ -1757,7 +1757,7 @@ Remote.prototype.createPathFind = function(src_account, dst_account, dst_amount,
|
||||
};
|
||||
|
||||
Remote.prepareTrade = function(currency, issuer) {
|
||||
return currency + (currency === 'XRP' ? '' : ('/' + issuer));
|
||||
return currency + (Currency.from_json(currency).is_native() ? '' : ('/' + issuer));
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -340,10 +340,10 @@ Request.prototype.addBook = function(book, snapshot) {
|
||||
}
|
||||
|
||||
var obj = json[side] = {
|
||||
currency: Currency.json_rewrite(book[side].currency)
|
||||
currency: Currency.json_rewrite(book[side].currency, {force_hex: true})
|
||||
};
|
||||
|
||||
if (obj.currency !== 'XRP') {
|
||||
if (!Currency.from_json(obj.currency).is_native()) {
|
||||
obj.issuer = UInt160.json_rewrite(book[side].issuer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,24 @@ describe('Amount', function() {
|
||||
it('0.1 USD', function() {
|
||||
assert.strictEqual(Amount.from_human("0.1 USD").to_text_full(), '0.1/USD/NaN');
|
||||
});
|
||||
it('10000 USD', function() {
|
||||
assert.strictEqual(Amount.from_human("10000 USD").to_text_full(), '10000/USD/NaN');
|
||||
});
|
||||
it('USD 10000', function() {
|
||||
assert.strictEqual(Amount.from_human("USD 10000").to_text_full(), '10000/USD/NaN');
|
||||
});
|
||||
it('12345.6789 XAU', function() {
|
||||
assert.strictEqual(Amount.from_human("12345.6789 XAU").to_text_full(), '12345.6789/XAU/NaN');
|
||||
});
|
||||
it('XAU 12345.6789', function() {
|
||||
assert.strictEqual(Amount.from_human("XAU 12345.6789").to_text_full(), '12345.6789/XAU/NaN');
|
||||
});
|
||||
it('101 12345.6789', function() {
|
||||
assert.strictEqual(Amount.from_human("101 12345.6789").to_text_full(), '12345.6789/101/NaN');
|
||||
});
|
||||
it('12345.6789 101', function() {
|
||||
assert.strictEqual(Amount.from_human("12345.6789 101").to_text_full(), '12345.6789/101/NaN');
|
||||
});
|
||||
});
|
||||
describe('from_json', function() {
|
||||
it('1 XRP', function() {
|
||||
@@ -117,6 +135,12 @@ describe('Amount', function() {
|
||||
assert.strictEqual(typeof Amount.from_json('x').to_text(true), 'number');
|
||||
assert(isNaN(Amount.from_json('x').to_text(true)));
|
||||
});
|
||||
it('parse dem', function() {
|
||||
assert.strictEqual(Amount.from_json('10/015841551A748AD2C1F76FF6ECB0CCCD00000000/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_text_full(), '10/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
|
||||
});
|
||||
it('parse dem', function() {
|
||||
assert.strictEqual(Amount.from_json('10/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_text_full(), '10/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
|
||||
});
|
||||
it('Parse 800/USD/mtgox', function () {
|
||||
assert.strictEqual('800/USD/'+config.accounts['mtgox'].account, Amount.from_json('800/USD/mtgox').to_text_full());
|
||||
});
|
||||
|
||||
@@ -27,6 +27,12 @@ describe('Currency', function() {
|
||||
assert(r.is_native());
|
||||
assert.strictEqual('XRP', r.to_json());
|
||||
});
|
||||
it('from_json("0000000000000000000000000000000000000000").to_json() == "XRP"', function() {
|
||||
var r = currency.from_json('0000000000000000000000000000000000000000');
|
||||
assert(r.is_valid());
|
||||
assert(r.is_native());
|
||||
assert.strictEqual('XRP', r.to_json());
|
||||
});
|
||||
it('from_json("111").to_human()', function() {
|
||||
var r = currency.from_json("111");
|
||||
assert(r.is_valid());
|
||||
@@ -37,6 +43,22 @@ describe('Currency', function() {
|
||||
assert(!r.is_valid());
|
||||
assert.strictEqual('XRP', r.to_json());
|
||||
});
|
||||
it('from_json("XAU").to_json() hex', function() {
|
||||
var r = currency.from_json("XAU");
|
||||
assert.strictEqual('0000000000000000000000005841550000000000', r.to_json({force_hex: true}));
|
||||
});
|
||||
it('from_json("XAU (0.5%pa").to_json() hex', function() {
|
||||
var r = currency.from_json("XAU (0.5%pa)");
|
||||
assert.strictEqual('015841550000000041F78E0A28CBF19200000000', r.to_json({force_hex: true}));
|
||||
});
|
||||
it('json_rewrite("015841550000000041F78E0A28CBF19200000000").to_json() hex', function() {
|
||||
var r = currency.json_rewrite('015841550000000041F78E0A28CBF19200000000');
|
||||
assert.strictEqual('XAU (0.5%pa)', r);
|
||||
});
|
||||
it('json_rewrite("015841550000000041F78E0A28CBF19200000000") hex', function() {
|
||||
var r = currency.json_rewrite('015841550000000041F78E0A28CBF19200000000', {force_hex: true});
|
||||
assert.strictEqual('015841550000000041F78E0A28CBF19200000000', r);
|
||||
});
|
||||
});
|
||||
|
||||
describe('from_human', function() {
|
||||
@@ -98,6 +120,14 @@ describe('Currency', function() {
|
||||
it('to_human with full_name "XRP - Ripples"', function() {
|
||||
assert.strictEqual('XRP - Ripples', currency.from_json('XRP').to_human({full_name:'Ripples'}));
|
||||
});
|
||||
it('to_human human "TIM" without full_name', function() {
|
||||
var cur = currency.from_json("TIM");
|
||||
assert.strictEqual(cur.to_human(), "TIM");
|
||||
});
|
||||
it('to_human "TIM" with null full_name', function() {
|
||||
var cur = currency.from_json("TIM");
|
||||
assert.strictEqual(cur.to_human({full_name: null}), "TIM");
|
||||
});
|
||||
});
|
||||
|
||||
describe('from_hex', function() {
|
||||
|
||||
@@ -3,6 +3,7 @@ var utils = require('./testutils');
|
||||
var Request = utils.load_module('request').Request;
|
||||
var Remote = utils.load_module('remote').Remote;
|
||||
var Server = utils.load_module('server').Server;
|
||||
var Currency = utils.load_module('currency').Currency;
|
||||
|
||||
function makeServer(url) {
|
||||
var server = new Server(new process.EventEmitter(), url);
|
||||
@@ -554,6 +555,9 @@ describe('Request', function() {
|
||||
|
||||
request.books(books);
|
||||
|
||||
books[0]['taker_gets'].currency = Currency.from_json('EUR').to_hex();
|
||||
books[0]['taker_pays'].currency = Currency.from_json('USD').to_hex();
|
||||
|
||||
assert.deepEqual(request.message.books, books);
|
||||
});
|
||||
|
||||
@@ -577,11 +581,11 @@ describe('Request', function() {
|
||||
assert.deepEqual(request.message.books, [
|
||||
{
|
||||
'taker_gets': {
|
||||
'currency': 'CNY',
|
||||
'currency': Currency.from_json('CNY').to_hex(),
|
||||
'issuer': 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
|
||||
},
|
||||
'taker_pays': {
|
||||
'currency': 'USD',
|
||||
'currency': Currency.from_json('USD').to_hex(),
|
||||
'issuer': 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
|
||||
}
|
||||
}
|
||||
@@ -605,11 +609,11 @@ describe('Request', function() {
|
||||
assert.deepEqual(request.message.books, [
|
||||
{
|
||||
'taker_gets': {
|
||||
'currency': 'EUR',
|
||||
'currency': '0000000000000000000000004555520000000000', // EUR hex
|
||||
'issuer': 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
|
||||
},
|
||||
'taker_pays': {
|
||||
'currency': 'USD',
|
||||
'currency': '0000000000000000000000005553440000000000', // USD hex
|
||||
'issuer': 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
|
||||
}
|
||||
},
|
||||
@@ -662,11 +666,11 @@ describe('Request', function() {
|
||||
|
||||
assert.deepEqual(request.message.books, [{
|
||||
'taker_gets': {
|
||||
'currency': 'EUR',
|
||||
'currency': Currency.from_json('EUR').to_hex(),
|
||||
'issuer': 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
|
||||
},
|
||||
'taker_pays': {
|
||||
'currency': 'USD',
|
||||
'currency': Currency.from_json('USD').to_hex(),
|
||||
'issuer': 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
|
||||
},
|
||||
'snapshot': true,
|
||||
|
||||
Reference in New Issue
Block a user