From 01903cc6d2a6885da4de2aaec1b7d71524d31ce0 Mon Sep 17 00:00:00 2001 From: wltsmrz Date: Wed, 20 Aug 2014 14:33:00 -0700 Subject: [PATCH 1/3] Update unfunded order tracking --- src/js/ripple/orderbook.js | 51 ++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/src/js/ripple/orderbook.js b/src/js/ripple/orderbook.js index 2cd7ace3..75cb838b 100644 --- a/src/js/ripple/orderbook.js +++ b/src/js/ripple/orderbook.js @@ -369,40 +369,31 @@ OrderBook.prototype.setFundedAmount = function(offer, fundedAmount) { return offer; } - function sixFigures(str) { - return str.substring(0, str.indexOf('.') + 7); - }; + var iouSuffix = '/' + this._currencyGets.to_json() + + '/' + this._issuerGets; - var takerGetsValue = (typeof offer.TakerGets === 'object') - ? offer.TakerGets.value - : offer.TakerGets; - - var takerPaysValue = (typeof offer.TakerPays === 'object') - ? offer.TakerPays.value - : offer.TakerPays; - - var iouSuffix = '/USD/rrrrrrrrrrrrrrrrrrrrBZbvji'; - var fundedTakerGets = Amount.from_json(fundedAmount + iouSuffix); - var takerGets = Amount.from_json(takerGetsValue + iouSuffix); - var takerPays = Amount.from_json(takerPaysValue + iouSuffix); - - offer.is_fully_funded = fundedTakerGets.compareTo(takerGets) >= 0; + offer.is_fully_funded = Amount.from_json( + this._currencyGets.is_native() ? fundedAmount : fundedAmount + iouSuffix + ).compareTo(Amount.from_json(offer.TakerGets)) >= 0; if (offer.is_fully_funded) { - offer.taker_gets_funded = takerGetsValue; - offer.taker_pays_funded = takerPaysValue; - return offer; - } - - offer.taker_gets_funded = fundedAmount; - - var rate = Amount.from_json(offer.TakerPays).divide(offer.TakerGets); - var takerPaysFunded = fundedTakerGets.multiply(rate); - - if (takerPaysFunded.compareTo(takerPays) <= 0) { - offer.taker_pays_funded = takerPaysFunded.to_text(); + offer.taker_gets_funded = Amount.from_json(offer.TakerGets).to_text(); + offer.taker_pays_funded = Amount.from_json(offer.TakerPays).to_text(); } else { - offer.taker_pays_funded = takerPaysValue; + offer.taker_gets_funded = fundedAmount; + + var rate = Amount.from_json(offer.TakerPays) + .divide(Amount.from_json(offer.TakerGets)); + + var fundedPays = Amount.from_json(offer.TakerGets).multiply(rate); + + fundedPays.set_issuer(Amount.from_json(offer.TakerPays).issuer()); + + if (fundedPays.compareTo(Amount.from_json(offer.TakerPays)) < 0) { + offer.taker_pays_funded = fundedPays.to_text(); + } else { + offer.taker_pays_funded = Amount.from_json(offer.TakerPays).to_text(); + } } return offer; From c29f92f05b8c3d03577cf5e06c43220ea33fc394 Mon Sep 17 00:00:00 2001 From: wltsmrz Date: Wed, 20 Aug 2014 15:50:20 -0700 Subject: [PATCH 2/3] Fix native/non-native currency discrepancies --- src/js/ripple/orderbook.js | 10 +- test/orderbook-test.js | 742 +++++++++++++++++++------------------ 2 files changed, 382 insertions(+), 370 deletions(-) diff --git a/src/js/ripple/orderbook.js b/src/js/ripple/orderbook.js index 75cb838b..c6b163be 100644 --- a/src/js/ripple/orderbook.js +++ b/src/js/ripple/orderbook.js @@ -385,11 +385,13 @@ OrderBook.prototype.setFundedAmount = function(offer, fundedAmount) { var rate = Amount.from_json(offer.TakerPays) .divide(Amount.from_json(offer.TakerGets)); - var fundedPays = Amount.from_json(offer.TakerGets).multiply(rate); + var fundedPays = Amount.from_json( + this._currencyPays.is_native() + ? fundedAmount + : fundedAmount + '/' + this._currencyPays.to_json() + '/' + this._issuerPays + ).multiply(rate); - fundedPays.set_issuer(Amount.from_json(offer.TakerPays).issuer()); - - if (fundedPays.compareTo(Amount.from_json(offer.TakerPays)) < 0) { + if (fundedPays.compareTo(Amount.from_json(offer.TakerPays)) <= 0) { offer.taker_pays_funded = fundedPays.to_text(); } else { offer.taker_pays_funded = Amount.from_json(offer.TakerPays).to_text(); diff --git a/test/orderbook-test.js b/test/orderbook-test.js index 5294aab9..96cfb515 100644 --- a/test/orderbook-test.js +++ b/test/orderbook-test.js @@ -6,357 +6,359 @@ var Amount = utils.load_module('amount').Amount; var Meta = utils.load_module('meta').Meta; describe('OrderBook', function() { - it('toJSON', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'XRP', - issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'BTC' - }); - assert.deepEqual(book.toJSON(), { - taker_gets: { - currency: Currency.from_json('XRP').to_hex() - }, - taker_pays: { - currency: Currency.from_json('BTC').to_hex(), - issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' - } - }); - book = new Remote().createOrderBook({ - issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_gets: 'BTC', - currency_pays: 'XRP' - }); - assert.deepEqual(book.toJSON(), { - taker_gets: { - currency: Currency.from_json('BTC').to_hex(), - issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' - }, - taker_pays: { - currency: Currency.from_json('XRP').to_hex() - }, - }); - }); - - it('Check orderbook validity', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'XRP', - issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'BTC' - }); - assert(book.isValid()); - }); - - it('Automatic subscription (based on listeners)', function(done) { - var book = new Remote().createOrderBook({ - currency_gets: 'XRP', - issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'BTC' - }); - book.subscribe = function() { - done(); - }; - book.on('model', function(){}); - }); - - it('Subscribe', function(done) { - var book = new Remote().createOrderBook({ - currency_gets: 'XRP', - issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'BTC' - }); - - var requestedOffers = false; - - book.subscribeTransactions = function() { - assert(requestedOffers); - done(); - }; - - book.requestOffers = function(callback) { - requestedOffers = true; - callback(); - }; - - book.subscribe(); - }); - - it('Unsubscribe', function(done) { - var book = new Remote().createOrderBook({ - currency_gets: 'XRP', - issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'BTC' - }); - - book.once('unsubscribe', function() { - done(); - }); - - book.on('model', function(){}); - - book.unsubscribe(); - - assert(!book._subscribed); - assert(!book._shouldConnect); - assert.deepEqual(book.listeners(), []); - }); - - it('Automatic unsubscription (based on listeners)', function(done) { - var book = new Remote().createOrderBook({ - currency_gets: 'XRP', - issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'BTC' - }); - book.unsubscribe = function() { - done(); - }; - book.on('model', function(){}); - book.removeAllListeners('model'); - }); - - it('Add cached owner funds', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'XRP', - issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'BTC' - }); - book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); - assert.strictEqual(book.getCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji'), '1'); - }); - - it('Add cached owner funds - invalid account', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'XRP', - issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'BTC' - }); - assert.throws(function() { - book.addCachedFunds('0rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); - }); - }); - - it('Has cached owner funds', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'XRP', - issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'BTC' - }); - book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); - assert(book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); - }); - - it('Has cached owner funds - invalid account', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'XRP', - issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'BTC' - }); - assert.throws(function() { - book.addCachedFunds('0rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); - }); - }); - - it('Remove cached owner funds', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'XRP', - issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'BTC' - }); - book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); - assert(book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); - book.removeCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji'); - assert(!book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); - }); - - it('Remove cached owner funds', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'BTC', - issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'XRP' - }); - book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); - assert(book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); - assert.throws(function() { - book.removeCachedFunds('0rrrrrrrrrrrrrrrrrrrrBZbvji'); - }); - }); - - it('Increment offer count', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'BTC', - issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'XRP' - }); - assert.strictEqual(book.incrementOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 1); - assert.strictEqual(book.getOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 1); - }); - - it('Increment offer count - invalid address', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'BTC', - issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'XRP' - }); - assert.throws(function() { - book.incrementOfferCount('zrrrrrrrrrrrrrrrrrrrBZbvji'); - }); - }); - - it('Decrement offer count', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'BTC', - issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'XRP' - }); - book.incrementOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'); - assert.strictEqual(book.decrementOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 0); - assert.strictEqual(book.getOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 0); - }); - - it('Decrement offer count - invalid address', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'BTC', - issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'XRP' - }); - assert.throws(function() { - book.decrementOfferCount('zrrrrrrrrrrrrrrrrrrrBZbvji'); - }); - }); - - it('Apply transfer rate', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'BTC', - issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'XRP' - }); - assert.strictEqual(book.applyTransferRate('1', 1002000000), '0.9980039920159681'); - }); - - it('Apply transfer rate - cached transfer rate', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'BTC', - issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'XRP' - }); - book._issuerTransferRate = 1002000000; - assert.strictEqual(book.applyTransferRate('1'), '0.9980039920159681'); - }); - - it('Apply transfer rate - invalid balance', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'BTC', - issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'XRP' - }); - assert.throws(function() { - book.applyTransferRate('asdf'); - }); - }); - - it('Apply transfer rate - invalid transfer rate', function() { - var book = new Remote().createOrderBook({ - currency_gets: 'BTC', - issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'XRP' - }); - assert.throws(function() { - book.applyTransferRate('1', 'asdf'); - }); - }); - - it('Request transfer rate', function() { - var remote = new Remote(); - var book = remote.createOrderBook({ - currency_gets: 'BTC', - issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'XRP' - }); - - remote.request = function(request) { - assert.deepEqual(request.message, { - command: 'account_info', - id: void(0), - ident: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - account: 'rrrrrrrrrrrrrrrrrrrrBZbvji' - }); - request.emit('success', { - account_data: { - TransferRate: 1002000000 - } - }); - }; - - book.requestTransferRate(function(err, rate) { - assert.ifError(err); - assert.strictEqual(rate, 1002000000); - }); - }); - - it('Request transfer rate - cached transfer rate', function() { - var remote = new Remote(); - var book = remote.createOrderBook({ - currency_gets: 'BTC', - issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'XRP' - }); - - book._issuerTransferRate = 1002000000; - - remote.request = function(request) { - assert(false); - }; - - book.requestTransferRate(function(err, rate) { - assert.ifError(err); - assert.strictEqual(rate, 1002000000); - }); - }); - - it('Request transfer rate - native currency', function() { - var remote = new Remote(); - var book = remote.createOrderBook({ - currency_gets: 'XRP', - issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'BTC' - }); - - remote.request = function(request) { - assert(false); - }; - - book.requestTransferRate(function(err, rate) { - assert.ifError(err); - assert.strictEqual(rate, 1000000000); - }); - }); - - it('Set funded amount - funded', function() { - var remote = new Remote(); - var book = remote.createOrderBook({ - currency_pays: 'XRP', - issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_gets: 'BTC' - }); - - var offer = { - TakerGets: { - value: '100', - currency: 'BTC', - issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' - }, - TakerPays: '123456' - }; - - book.setFundedAmount(offer, '100.1234'); - - assert.deepEqual(offer, { - TakerGets: offer.TakerGets, - TakerPays: offer.TakerPays, - is_fully_funded: true, - taker_gets_funded: '100', - taker_pays_funded: '123456' - }); - }); +// it('toJSON', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'XRP', +// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'BTC' +// }); +// assert.deepEqual(book.toJSON(), { +// taker_gets: { +// currency: Currency.from_json('XRP').to_hex() +// }, +// taker_pays: { +// currency: Currency.from_json('BTC').to_hex(), +// issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' +// } +// }); +// book = new Remote().createOrderBook({ +// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_gets: 'BTC', +// currency_pays: 'XRP' +// }); +// assert.deepEqual(book.toJSON(), { +// taker_gets: { +// currency: Currency.from_json('BTC').to_hex(), +// issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' +// }, +// taker_pays: { +// currency: Currency.from_json('XRP').to_hex() +// }, +// }); +// }); +// +// it('Check orderbook validity', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'XRP', +// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'BTC' +// }); +// assert(book.isValid()); +// }); +// +// it('Automatic subscription (based on listeners)', function(done) { +// var book = new Remote().createOrderBook({ +// currency_gets: 'XRP', +// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'BTC' +// }); +// book.subscribe = function() { +// done(); +// }; +// book.on('model', function(){}); +// }); +// +// it('Subscribe', function(done) { +// var book = new Remote().createOrderBook({ +// currency_gets: 'XRP', +// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'BTC' +// }); +// +// var requestedOffers = false; +// +// book.subscribeTransactions = function() { +// assert(requestedOffers); +// done(); +// }; +// +// book.requestOffers = function(callback) { +// requestedOffers = true; +// callback(); +// }; +// +// book.subscribe(); +// }); +// +// it('Unsubscribe', function(done) { +// var book = new Remote().createOrderBook({ +// currency_gets: 'XRP', +// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'BTC' +// }); +// +// book.once('unsubscribe', function() { +// done(); +// }); +// +// book.on('model', function(){}); +// +// book.unsubscribe(); +// +// assert(!book._subscribed); +// assert(!book._shouldConnect); +// assert.deepEqual(book.listeners(), []); +// }); +// +// it('Automatic unsubscription (based on listeners)', function(done) { +// var book = new Remote().createOrderBook({ +// currency_gets: 'XRP', +// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'BTC' +// }); +// book.unsubscribe = function() { +// done(); +// }; +// book.on('model', function(){}); +// book.removeAllListeners('model'); +// }); +// +// it('Add cached owner funds', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'XRP', +// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'BTC' +// }); +// book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); +// assert.strictEqual(book.getCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji'), '1'); +// }); +// +// it('Add cached owner funds - invalid account', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'XRP', +// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'BTC' +// }); +// assert.throws(function() { +// book.addCachedFunds('0rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); +// }); +// }); +// +// it('Has cached owner funds', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'XRP', +// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'BTC' +// }); +// book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); +// assert(book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); +// }); +// +// it('Has cached owner funds - invalid account', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'XRP', +// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'BTC' +// }); +// assert.throws(function() { +// book.addCachedFunds('0rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); +// }); +// }); +// +// it('Remove cached owner funds', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'XRP', +// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'BTC' +// }); +// book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); +// assert(book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); +// book.removeCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji'); +// assert(!book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); +// }); +// +// it('Remove cached owner funds', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'BTC', +// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'XRP' +// }); +// book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); +// assert(book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); +// assert.throws(function() { +// book.removeCachedFunds('0rrrrrrrrrrrrrrrrrrrrBZbvji'); +// }); +// }); +// +// it('Increment offer count', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'BTC', +// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'XRP' +// }); +// assert.strictEqual(book.incrementOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 1); +// assert.strictEqual(book.getOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 1); +// }); +// +// it('Increment offer count - invalid address', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'BTC', +// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'XRP' +// }); +// assert.throws(function() { +// book.incrementOfferCount('zrrrrrrrrrrrrrrrrrrrBZbvji'); +// }); +// }); +// +// it('Decrement offer count', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'BTC', +// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'XRP' +// }); +// book.incrementOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'); +// assert.strictEqual(book.decrementOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 0); +// assert.strictEqual(book.getOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 0); +// }); +// +// it('Decrement offer count - invalid address', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'BTC', +// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'XRP' +// }); +// assert.throws(function() { +// book.decrementOfferCount('zrrrrrrrrrrrrrrrrrrrBZbvji'); +// }); +// }); +// +// it('Apply transfer rate', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'BTC', +// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'XRP' +// }); +// assert.strictEqual(book.applyTransferRate('1', 1002000000), '0.9980039920159681'); +// }); +// +// it('Apply transfer rate - cached transfer rate', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'BTC', +// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'XRP' +// }); +// book._issuerTransferRate = 1002000000; +// assert.strictEqual(book.applyTransferRate('1'), '0.9980039920159681'); +// }); +// +// it('Apply transfer rate - invalid balance', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'BTC', +// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'XRP' +// }); +// assert.throws(function() { +// book.applyTransferRate('asdf'); +// }); +// }); +// +// it('Apply transfer rate - invalid transfer rate', function() { +// var book = new Remote().createOrderBook({ +// currency_gets: 'BTC', +// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'XRP' +// }); +// assert.throws(function() { +// book.applyTransferRate('1', 'asdf'); +// }); +// }); +// +// it('Request transfer rate', function() { +// var remote = new Remote(); +// var book = remote.createOrderBook({ +// currency_gets: 'BTC', +// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'XRP' +// }); +// +// remote.request = function(request) { +// assert.deepEqual(request.message, { +// command: 'account_info', +// id: void(0), +// ident: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// account: 'rrrrrrrrrrrrrrrrrrrrBZbvji' +// }); +// request.emit('success', { +// account_data: { +// TransferRate: 1002000000 +// } +// }); +// }; +// +// book.requestTransferRate(function(err, rate) { +// assert.ifError(err); +// assert.strictEqual(rate, 1002000000); +// }); +// }); +// +// it('Request transfer rate - cached transfer rate', function() { +// var remote = new Remote(); +// var book = remote.createOrderBook({ +// currency_gets: 'BTC', +// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'XRP' +// }); +// +// book._issuerTransferRate = 1002000000; +// +// remote.request = function(request) { +// assert(false); +// }; +// +// book.requestTransferRate(function(err, rate) { +// assert.ifError(err); +// assert.strictEqual(rate, 1002000000); +// }); +// }); +// +// it('Request transfer rate - native currency', function() { +// var remote = new Remote(); +// var book = remote.createOrderBook({ +// currency_gets: 'XRP', +// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_pays: 'BTC' +// }); +// +// remote.request = function(request) { +// assert(false); +// }; +// +// book.requestTransferRate(function(err, rate) { +// assert.ifError(err); +// assert.strictEqual(rate, 1000000000); +// }); +// }); +// +// it('Set funded amount - funded', function() { +// var remote = new Remote(); +// var book = remote.createOrderBook({ +// currency_pays: 'XRP', +// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', +// currency_gets: 'BTC' +// }); +// +// var offer = { +// TakerGets: { +// value: '100', +// currency: 'BTC', +// issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' +// }, +// TakerPays: '123456' +// }; +// +// book.setFundedAmount(offer, '100.1234'); +// +// var expected = { +// TakerGets: offer.TakerGets, +// TakerPays: offer.TakerPays, +// is_fully_funded: true, +// taker_gets_funded: '100', +// taker_pays_funded: '123456' +// }; +// +// assert.deepEqual(offer, expected); +// }); it('Set funded amount - unfunded', function() { var remote = new Remote(); @@ -367,23 +369,25 @@ describe('OrderBook', function() { }); var offer = { - TakerGets: { - value: '100', + TakerPays: { + value: '123456', currency: 'BTC', issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' }, - TakerPays: '123456' + TakerGets: '100' }; book.setFundedAmount(offer, '99'); - assert.deepEqual(offer, { + var expected = { TakerGets: offer.TakerGets, TakerPays: offer.TakerPays, is_fully_funded: false, taker_gets_funded: '99', - taker_pays_funded: '122166' - }); + taker_pays_funded: '122221.44' + }; + + assert.deepEqual(offer, expected); }); it('Set funded amount - native currency - funded', function() { @@ -421,7 +425,7 @@ describe('OrderBook', function() { var book = remote.createOrderBook({ currency_gets: 'XRP', issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', - currency_pays: 'BTC' + currency_pays: 'USD' }); var offer = { @@ -435,13 +439,15 @@ describe('OrderBook', function() { book.setFundedAmount(offer, '99'); - assert.deepEqual(offer, { + var expected = { TakerGets: offer.TakerGets, TakerPays: offer.TakerPays, is_fully_funded: false, taker_gets_funded: '99', taker_pays_funded: '99.122166' - }); + }; + + assert.deepEqual(offer, expected); }); it('Set funded amount - zero funds', function() { @@ -1484,9 +1490,8 @@ describe('OrderBook', function() { book._issuerTransferRate = 1002000000; - book.once('model', function(model) { - assert.deepEqual(model, [ - { Account: 'rGCHV41NxoK7wHQJhmao2RqjWZvBrTUhW1', + var expected = [ + { Account: 'rGCHV41NxoK7wHQJhmao2RqjWZvBrTUhW1', BookDirectory: '6EAB7C172DEFA430DBFAD120FDC373B5F5AF8B191649EC985711A3A4254F5000', BookNode: '0000000000000000', Flags: 131072, @@ -1537,9 +1542,14 @@ describe('OrderBook', function() { is_fully_funded: true, taker_gets_funded: '0.2', taker_pays_funded: '99.72233516476456' - }]) - assert.strictEqual(book._synchronized, true); - done(); + } + ] + + + book.once('model', function(model) { + assert.deepEqual(model, expected); + assert.strictEqual(book._synchronized, true); + done(); }); }); }); From 3cb337e7ec38ea58c72e69255ac6307e39f220ba Mon Sep 17 00:00:00 2001 From: wltsmrz Date: Wed, 20 Aug 2014 15:53:03 -0700 Subject: [PATCH 3/3] Uncomment tests --- test/orderbook-test.js | 708 ++++++++++++++++++++--------------------- 1 file changed, 354 insertions(+), 354 deletions(-) diff --git a/test/orderbook-test.js b/test/orderbook-test.js index 96cfb515..bba06327 100644 --- a/test/orderbook-test.js +++ b/test/orderbook-test.js @@ -6,359 +6,359 @@ var Amount = utils.load_module('amount').Amount; var Meta = utils.load_module('meta').Meta; describe('OrderBook', function() { -// it('toJSON', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'XRP', -// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'BTC' -// }); -// assert.deepEqual(book.toJSON(), { -// taker_gets: { -// currency: Currency.from_json('XRP').to_hex() -// }, -// taker_pays: { -// currency: Currency.from_json('BTC').to_hex(), -// issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' -// } -// }); -// book = new Remote().createOrderBook({ -// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_gets: 'BTC', -// currency_pays: 'XRP' -// }); -// assert.deepEqual(book.toJSON(), { -// taker_gets: { -// currency: Currency.from_json('BTC').to_hex(), -// issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' -// }, -// taker_pays: { -// currency: Currency.from_json('XRP').to_hex() -// }, -// }); -// }); -// -// it('Check orderbook validity', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'XRP', -// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'BTC' -// }); -// assert(book.isValid()); -// }); -// -// it('Automatic subscription (based on listeners)', function(done) { -// var book = new Remote().createOrderBook({ -// currency_gets: 'XRP', -// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'BTC' -// }); -// book.subscribe = function() { -// done(); -// }; -// book.on('model', function(){}); -// }); -// -// it('Subscribe', function(done) { -// var book = new Remote().createOrderBook({ -// currency_gets: 'XRP', -// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'BTC' -// }); -// -// var requestedOffers = false; -// -// book.subscribeTransactions = function() { -// assert(requestedOffers); -// done(); -// }; -// -// book.requestOffers = function(callback) { -// requestedOffers = true; -// callback(); -// }; -// -// book.subscribe(); -// }); -// -// it('Unsubscribe', function(done) { -// var book = new Remote().createOrderBook({ -// currency_gets: 'XRP', -// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'BTC' -// }); -// -// book.once('unsubscribe', function() { -// done(); -// }); -// -// book.on('model', function(){}); -// -// book.unsubscribe(); -// -// assert(!book._subscribed); -// assert(!book._shouldConnect); -// assert.deepEqual(book.listeners(), []); -// }); -// -// it('Automatic unsubscription (based on listeners)', function(done) { -// var book = new Remote().createOrderBook({ -// currency_gets: 'XRP', -// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'BTC' -// }); -// book.unsubscribe = function() { -// done(); -// }; -// book.on('model', function(){}); -// book.removeAllListeners('model'); -// }); -// -// it('Add cached owner funds', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'XRP', -// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'BTC' -// }); -// book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); -// assert.strictEqual(book.getCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji'), '1'); -// }); -// -// it('Add cached owner funds - invalid account', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'XRP', -// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'BTC' -// }); -// assert.throws(function() { -// book.addCachedFunds('0rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); -// }); -// }); -// -// it('Has cached owner funds', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'XRP', -// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'BTC' -// }); -// book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); -// assert(book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); -// }); -// -// it('Has cached owner funds - invalid account', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'XRP', -// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'BTC' -// }); -// assert.throws(function() { -// book.addCachedFunds('0rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); -// }); -// }); -// -// it('Remove cached owner funds', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'XRP', -// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'BTC' -// }); -// book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); -// assert(book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); -// book.removeCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji'); -// assert(!book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); -// }); -// -// it('Remove cached owner funds', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'BTC', -// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'XRP' -// }); -// book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); -// assert(book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); -// assert.throws(function() { -// book.removeCachedFunds('0rrrrrrrrrrrrrrrrrrrrBZbvji'); -// }); -// }); -// -// it('Increment offer count', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'BTC', -// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'XRP' -// }); -// assert.strictEqual(book.incrementOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 1); -// assert.strictEqual(book.getOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 1); -// }); -// -// it('Increment offer count - invalid address', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'BTC', -// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'XRP' -// }); -// assert.throws(function() { -// book.incrementOfferCount('zrrrrrrrrrrrrrrrrrrrBZbvji'); -// }); -// }); -// -// it('Decrement offer count', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'BTC', -// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'XRP' -// }); -// book.incrementOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'); -// assert.strictEqual(book.decrementOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 0); -// assert.strictEqual(book.getOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 0); -// }); -// -// it('Decrement offer count - invalid address', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'BTC', -// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'XRP' -// }); -// assert.throws(function() { -// book.decrementOfferCount('zrrrrrrrrrrrrrrrrrrrBZbvji'); -// }); -// }); -// -// it('Apply transfer rate', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'BTC', -// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'XRP' -// }); -// assert.strictEqual(book.applyTransferRate('1', 1002000000), '0.9980039920159681'); -// }); -// -// it('Apply transfer rate - cached transfer rate', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'BTC', -// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'XRP' -// }); -// book._issuerTransferRate = 1002000000; -// assert.strictEqual(book.applyTransferRate('1'), '0.9980039920159681'); -// }); -// -// it('Apply transfer rate - invalid balance', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'BTC', -// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'XRP' -// }); -// assert.throws(function() { -// book.applyTransferRate('asdf'); -// }); -// }); -// -// it('Apply transfer rate - invalid transfer rate', function() { -// var book = new Remote().createOrderBook({ -// currency_gets: 'BTC', -// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'XRP' -// }); -// assert.throws(function() { -// book.applyTransferRate('1', 'asdf'); -// }); -// }); -// -// it('Request transfer rate', function() { -// var remote = new Remote(); -// var book = remote.createOrderBook({ -// currency_gets: 'BTC', -// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'XRP' -// }); -// -// remote.request = function(request) { -// assert.deepEqual(request.message, { -// command: 'account_info', -// id: void(0), -// ident: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// account: 'rrrrrrrrrrrrrrrrrrrrBZbvji' -// }); -// request.emit('success', { -// account_data: { -// TransferRate: 1002000000 -// } -// }); -// }; -// -// book.requestTransferRate(function(err, rate) { -// assert.ifError(err); -// assert.strictEqual(rate, 1002000000); -// }); -// }); -// -// it('Request transfer rate - cached transfer rate', function() { -// var remote = new Remote(); -// var book = remote.createOrderBook({ -// currency_gets: 'BTC', -// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'XRP' -// }); -// -// book._issuerTransferRate = 1002000000; -// -// remote.request = function(request) { -// assert(false); -// }; -// -// book.requestTransferRate(function(err, rate) { -// assert.ifError(err); -// assert.strictEqual(rate, 1002000000); -// }); -// }); -// -// it('Request transfer rate - native currency', function() { -// var remote = new Remote(); -// var book = remote.createOrderBook({ -// currency_gets: 'XRP', -// issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_pays: 'BTC' -// }); -// -// remote.request = function(request) { -// assert(false); -// }; -// -// book.requestTransferRate(function(err, rate) { -// assert.ifError(err); -// assert.strictEqual(rate, 1000000000); -// }); -// }); -// -// it('Set funded amount - funded', function() { -// var remote = new Remote(); -// var book = remote.createOrderBook({ -// currency_pays: 'XRP', -// issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', -// currency_gets: 'BTC' -// }); -// -// var offer = { -// TakerGets: { -// value: '100', -// currency: 'BTC', -// issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' -// }, -// TakerPays: '123456' -// }; -// -// book.setFundedAmount(offer, '100.1234'); -// -// var expected = { -// TakerGets: offer.TakerGets, -// TakerPays: offer.TakerPays, -// is_fully_funded: true, -// taker_gets_funded: '100', -// taker_pays_funded: '123456' -// }; -// -// assert.deepEqual(offer, expected); -// }); + it('toJSON', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'BTC' + }); + assert.deepEqual(book.toJSON(), { + taker_gets: { + currency: Currency.from_json('XRP').to_hex() + }, + taker_pays: { + currency: Currency.from_json('BTC').to_hex(), + issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' + } + }); + book = new Remote().createOrderBook({ + issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_gets: 'BTC', + currency_pays: 'XRP' + }); + assert.deepEqual(book.toJSON(), { + taker_gets: { + currency: Currency.from_json('BTC').to_hex(), + issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' + }, + taker_pays: { + currency: Currency.from_json('XRP').to_hex() + }, + }); + }); + + it('Check orderbook validity', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'BTC' + }); + assert(book.isValid()); + }); + + it('Automatic subscription (based on listeners)', function(done) { + var book = new Remote().createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'BTC' + }); + book.subscribe = function() { + done(); + }; + book.on('model', function(){}); + }); + + it('Subscribe', function(done) { + var book = new Remote().createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'BTC' + }); + + var requestedOffers = false; + + book.subscribeTransactions = function() { + assert(requestedOffers); + done(); + }; + + book.requestOffers = function(callback) { + requestedOffers = true; + callback(); + }; + + book.subscribe(); + }); + + it('Unsubscribe', function(done) { + var book = new Remote().createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'BTC' + }); + + book.once('unsubscribe', function() { + done(); + }); + + book.on('model', function(){}); + + book.unsubscribe(); + + assert(!book._subscribed); + assert(!book._shouldConnect); + assert.deepEqual(book.listeners(), []); + }); + + it('Automatic unsubscription (based on listeners)', function(done) { + var book = new Remote().createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'BTC' + }); + book.unsubscribe = function() { + done(); + }; + book.on('model', function(){}); + book.removeAllListeners('model'); + }); + + it('Add cached owner funds', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'BTC' + }); + book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); + assert.strictEqual(book.getCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji'), '1'); + }); + + it('Add cached owner funds - invalid account', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'BTC' + }); + assert.throws(function() { + book.addCachedFunds('0rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); + }); + }); + + it('Has cached owner funds', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'BTC' + }); + book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); + assert(book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); + }); + + it('Has cached owner funds - invalid account', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'BTC' + }); + assert.throws(function() { + book.addCachedFunds('0rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); + }); + }); + + it('Remove cached owner funds', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'BTC' + }); + book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); + assert(book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); + book.removeCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji'); + assert(!book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); + }); + + it('Remove cached owner funds', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'BTC', + issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'XRP' + }); + book.addCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji', '1'); + assert(book.hasCachedFunds('rrrrrrrrrrrrrrrrrrrrBZbvji')); + assert.throws(function() { + book.removeCachedFunds('0rrrrrrrrrrrrrrrrrrrrBZbvji'); + }); + }); + + it('Increment offer count', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'BTC', + issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'XRP' + }); + assert.strictEqual(book.incrementOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 1); + assert.strictEqual(book.getOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 1); + }); + + it('Increment offer count - invalid address', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'BTC', + issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'XRP' + }); + assert.throws(function() { + book.incrementOfferCount('zrrrrrrrrrrrrrrrrrrrBZbvji'); + }); + }); + + it('Decrement offer count', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'BTC', + issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'XRP' + }); + book.incrementOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'); + assert.strictEqual(book.decrementOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 0); + assert.strictEqual(book.getOfferCount('rrrrrrrrrrrrrrrrrrrrBZbvji'), 0); + }); + + it('Decrement offer count - invalid address', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'BTC', + issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'XRP' + }); + assert.throws(function() { + book.decrementOfferCount('zrrrrrrrrrrrrrrrrrrrBZbvji'); + }); + }); + + it('Apply transfer rate', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'BTC', + issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'XRP' + }); + assert.strictEqual(book.applyTransferRate('1', 1002000000), '0.9980039920159681'); + }); + + it('Apply transfer rate - cached transfer rate', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'BTC', + issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'XRP' + }); + book._issuerTransferRate = 1002000000; + assert.strictEqual(book.applyTransferRate('1'), '0.9980039920159681'); + }); + + it('Apply transfer rate - invalid balance', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'BTC', + issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'XRP' + }); + assert.throws(function() { + book.applyTransferRate('asdf'); + }); + }); + + it('Apply transfer rate - invalid transfer rate', function() { + var book = new Remote().createOrderBook({ + currency_gets: 'BTC', + issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'XRP' + }); + assert.throws(function() { + book.applyTransferRate('1', 'asdf'); + }); + }); + + it('Request transfer rate', function() { + var remote = new Remote(); + var book = remote.createOrderBook({ + currency_gets: 'BTC', + issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'XRP' + }); + + remote.request = function(request) { + assert.deepEqual(request.message, { + command: 'account_info', + id: void(0), + ident: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + account: 'rrrrrrrrrrrrrrrrrrrrBZbvji' + }); + request.emit('success', { + account_data: { + TransferRate: 1002000000 + } + }); + }; + + book.requestTransferRate(function(err, rate) { + assert.ifError(err); + assert.strictEqual(rate, 1002000000); + }); + }); + + it('Request transfer rate - cached transfer rate', function() { + var remote = new Remote(); + var book = remote.createOrderBook({ + currency_gets: 'BTC', + issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'XRP' + }); + + book._issuerTransferRate = 1002000000; + + remote.request = function(request) { + assert(false); + }; + + book.requestTransferRate(function(err, rate) { + assert.ifError(err); + assert.strictEqual(rate, 1002000000); + }); + }); + + it('Request transfer rate - native currency', function() { + var remote = new Remote(); + var book = remote.createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'BTC' + }); + + remote.request = function(request) { + assert(false); + }; + + book.requestTransferRate(function(err, rate) { + assert.ifError(err); + assert.strictEqual(rate, 1000000000); + }); + }); + + it('Set funded amount - funded', function() { + var remote = new Remote(); + var book = remote.createOrderBook({ + currency_pays: 'XRP', + issuer_gets: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_gets: 'BTC' + }); + + var offer = { + TakerGets: { + value: '100', + currency: 'BTC', + issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' + }, + TakerPays: '123456' + }; + + book.setFundedAmount(offer, '100.1234'); + + var expected = { + TakerGets: offer.TakerGets, + TakerPays: offer.TakerPays, + is_fully_funded: true, + taker_gets_funded: '100', + taker_pays_funded: '123456' + }; + + assert.deepEqual(offer, expected); + }); it('Set funded amount - unfunded', function() { var remote = new Remote(); @@ -1378,7 +1378,7 @@ describe('OrderBook', function() { Sequence: 561, TransferRate: 1002000000, index: 'B7D526FDDF9E3B3F95C3DC97C353065B0482302500BBB8051A5C090B596C6133', - urlgravatar: 'http://www.gravatar.com/avatar/5b33b93c7ffe384d53450fc666bb11fb' + urlgravatar: 'http:www.gravatar.com/avatar/5b33b93c7ffe384d53450fc666bb11fb' } });