diff --git a/test/orderbook-test.js b/test/orderbook-test.js index 81fbebeb..a4a5aac2 100644 --- a/test/orderbook-test.js +++ b/test/orderbook-test.js @@ -21,6 +21,20 @@ describe('OrderBook', function() { 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() { @@ -51,19 +65,13 @@ describe('OrderBook', function() { currency_pays: 'BTC' }); - var requestedTransferRate = false; var requestedOffers = false; book.subscribeTransactions = function() { - assert(requestedTransferRate); assert(requestedOffers); done(); }; - book.requestTransferRate = function(callback) { - requestedTransferRate = true; - }; - book.requestOffers = function() { requestedOffers = true; var em = new process.EventEmitter(); @@ -440,6 +448,34 @@ describe('OrderBook', function() { }); }); + it('Set funded amount - zero funds', function() { + var remote = new Remote(); + var book = remote.createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + currency_pays: 'BTC' + }); + + var offer = { + TakerGets: { + value: '100', + currency: 'BTC', + issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji' + }, + TakerPays: '123456' + }; + + book.setFundedAmount(offer, '0'); + + assert.deepEqual(offer, { + TakerGets: offer.TakerGets, + TakerPays: offer.TakerPays, + is_fully_funded: false, + taker_gets_funded: '0', + taker_pays_funded: '0' + }); + }); + it('Check is balance change', function() { var remote = new Remote(); @@ -708,7 +744,8 @@ describe('OrderBook', function() { }); var meta = new Meta({ - AffectedNodes: [{ + AffectedNodes: [ + { ModifiedNode: { FinalFields: { Balance: { @@ -742,7 +779,43 @@ describe('OrderBook', function() { PreviousTxnID: '53354D84BAE8FDFC3F4DA879D984D24B929E7FEB9100D2AD9EFCD2E126BCCDC8', PreviousTxnLgrSeq: 343570 } - }] + }, + { + ModifiedNode: { + FinalFields: { + Balance: { + currency: 'USD', + issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + value: '-10' + }, + Flags: 131072, + HighLimit: { + currency: 'USD', + issuer: 'r3PDtZSa5LiYp1Ysn1vMuMzB59RzV3W9QH', + value: '100' + }, + HighNode: '0000000000000000', + LowLimit: { + currency: 'USD', + issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + value: '0' + }, + LowNode: '0000000000000000' + }, + LedgerEntryType: 'RippleState', + LedgerIndex: 'EA4BF03B4700123CDFFB6EB09DC1D6E28D5CEB7F680FB00FC24BC1C3BB2DB959', + PreviousFields: { + Balance: { + currency: 'USD', + issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + value: '0' + } + }, + PreviousTxnID: '53354D84BAE8FDFC3F4DA879D984D24B929E7FEB9100D2AD9EFCD2E126BCCDC8', + PreviousTxnLgrSeq: 343570 + } + } + ] }); assert.deepEqual(book.getBalanceChange(meta.getNodes()[0]), { @@ -750,6 +823,12 @@ describe('OrderBook', function() { balance: '10', isValid: true }); + + assert.deepEqual(book.getBalanceChange(meta.getNodes()[1]), { + account: 'r3PDtZSa5LiYp1Ysn1vMuMzB59RzV3W9QH', + balance: '10', + isValid: true + }); }); it('Get balance change - native currency', function() { @@ -1076,4 +1155,393 @@ describe('OrderBook', function() { done(); }); }); + + it('Update funded amounts - no affected account', function(done) { + var remote = new Remote(); + + var book = remote.createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + currency_pays: 'USD' + }); + + var message = { + mmeta: new Meta({ + AffectedNodes: [{ + ModifiedNode: { + FinalFields: { + Account: 'r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59', + Balance: '25', + Flags: 0, + OwnerCount: 1, + Sequence: 2 + }, + LedgerEntryType: 'AccountRoot', + LedgerIndex: '4F83A2CF7E70F77F79A307E6A472BFC2585B806A70833CCD1C26105BAE0D6E05', + PreviousFields: { + Balance: '100', + OwnerCount: 0, + Sequence: 1 + }, + PreviousTxnID: 'B24159F8552C355D35E43623F0E5AD965ADBF034D482421529E2703904E1EC09', + PreviousTxnLgrSeq: 16154 + } + }] + }) + }; + + book._synchronized = true; + + book._offers = [ + { + Account: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + BookDirectory: 'DFA3B6DDAB58C7E8E5D944E736DA4B7046C30E4F460FD9DE4C124AF94ED1781B', + BookNode: '0000000000000000', + Flags: 0, + LedgerEntryType: 'Offer', + OwnerNode: '00000000000063CA', + PreviousTxnID: '51C64E0B300E9C0E877BA3E79B4ED1DBD5FDDCE58FA1A8FDA5F8DDF139787A24', + PreviousTxnLgrSeq: 8265275, + Sequence: 1138918, + TakerGets: '50', + taker_gets_funded: '100', + is_fully_funded: true, + TakerPays: { + currency: 'USD', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + value: '5' + }, + index: 'DC003E09AD1306FBBD1957C955EE668E429CC85B0EC0EC17297F6676E6108DE7', + owner_funds: '162110617177', + quality: '0.000000005148984210454555' + } + ]; + + book._offers.__defineGetter__(0, function() { + assert(false, 'Iteration of offers for unaffected account'); + }); + + book.on('offer_changed', function() { + assert(false, 'offer_changed event emitted'); + }); + + book.on('offer_funds_changed', function() { + assert(false, 'offer_funds_changed event emitted'); + }); + + book.updateFundedAmounts(message); + + setImmediate(done); + }); + + it('Update funded amounts - no balance change', function(done) { + var remote = new Remote(); + + var book = remote.createOrderBook({ + currency_gets: 'XRP', + issuer_pays: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + currency_pays: 'USD' + }); + + var message = { + mmeta: new Meta({ + AffectedNodes: [{ + ModifiedNode: { + FinalFields: { + Account: 'r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV', + Balance: '78991384535796', + Flags: 0, + OwnerCount: 3, + Sequence: 188 + }, + LedgerEntryType: 'AccountRoot', + LedgerIndex: 'B33FDD5CF3445E1A7F2BE9B06336BEBD73A5E3EE885D3EF93F7E3E2992E46F1A', + PreviousTxnID: 'E9E1988A0F061679E5D14DE77DB0163CE0BBDC00F29E396FFD1DA0366E7D8904', + PreviousTxnLgrSeq: 195455 + } + }] + }) + }; + + book._synchronized = true; + + book._offers = [ + { + Account: 'r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59', + BookDirectory: 'DFA3B6DDAB58C7E8E5D944E736DA4B7046C30E4F460FD9DE4C124AF94ED1781B', + BookNode: '0000000000000000', + Flags: 0, + LedgerEntryType: 'Offer', + OwnerNode: '00000000000063CA', + PreviousTxnID: '51C64E0B300E9C0E877BA3E79B4ED1DBD5FDDCE58FA1A8FDA5F8DDF139787A24', + PreviousTxnLgrSeq: 8265275, + Sequence: 1138918, + TakerGets: '50', + taker_gets_funded: '100', + is_fully_funded: true, + TakerPays: { + currency: 'USD', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + value: '5' + }, + index: 'DC003E09AD1306FBBD1957C955EE668E429CC85B0EC0EC17297F6676E6108DE7', + owner_funds: '162110617177', + quality: '0.000000005148984210454555' + } + ]; + + book.on('offer_changed', function() { + assert(false, 'offer_changed event emitted'); + }); + + book.on('offer_funds_changed', function() { + assert(false, 'offer_funds_changed event emitted'); + }); + + assert.strictEqual(typeof book.getBalanceChange, 'function'); + + book.getBalanceChange = function() { + assert(false, 'getBalanceChange should not be called'); + }; + + book.addCachedFunds('r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59', '100'); + book.updateFundedAmounts(message); + + setImmediate(done); + }); + + it('Update funded amounts - deferred TransferRate', function(done) { + var remote = new Remote(); + + var book = remote.createOrderBook({ + currency_gets: 'USD', + issuer_gets: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + currency_pays: 'XRP' + }); + + var message = { + mmeta: new Meta({ + AffectedNodes: [{ + ModifiedNode: { + FinalFields: { + Balance: { + currency: 'USD', + issuer: 'rrrrrrrrrrrrrrrrrrrrBZbvji', + value: '10' + }, + Flags: 131072, + HighLimit: { + currency: 'USD', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + value: '100' + }, + HighNode: '0000000000000000', + LowLimit: { + currency: 'USD', + issuer: 'r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59', + value: '0' + }, + LowNode: '0000000000000000' + }, + LedgerEntryType: 'RippleState', + LedgerIndex: 'EA4BF03B4700123CDFFB6EB09DC1D6E28D5CEB7F680FB00FC24BC1C3BB2DB959', + PreviousTxnID: '53354D84BAE8FDFC3F4DA879D984D24B929E7FEB9100D2AD9EFCD2E126BCCDC8', + PreviousTxnLgrSeq: 343570 + } + }] + }) + }; + + remote.request = function(request) { + assert.deepEqual(request.message, { + command: 'account_info', + id: undefined, + ident: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + account: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' + }); + + request.emit('success', { + account_data: { + Account: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + Balance: '6156166959471', + Domain: '6269747374616D702E6E6574', + EmailHash: '5B33B93C7FFE384D53450FC666BB11FB', + Flags: 131072, + LedgerEntryType: 'AccountRoot', + OwnerCount: 0, + PreviousTxnID: '6A7D0AB36CBA6884FDC398254BC67DE7E0B4887E9B0252568391102FBB854C09', + PreviousTxnLgrSeq: 8344426, + Sequence: 561, + TransferRate: 1002000000, + index: 'B7D526FDDF9E3B3F95C3DC97C353065B0482302500BBB8051A5C090B596C6133', + urlgravatar: 'http://www.gravatar.com/avatar/5b33b93c7ffe384d53450fc666bb11fb' + } + }); + + assert.strictEqual(book._issuerTransferRate, 1002000000); + done(); + }; + + book.addCachedFunds('r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59', '100'); + book.updateFundedAmounts(message); + }); + + it('Request offers', function(done) { + var remote = new Remote(); + + var offers = { + offers: [ + { + Account: 'rGCHV41NxoK7wHQJhmao2RqjWZvBrTUhW1', + BookDirectory: '6EAB7C172DEFA430DBFAD120FDC373B5F5AF8B191649EC985711A3A4254F5000', + BookNode: '0000000000000000', + Flags: 131072, + LedgerEntryType: 'Offer', + OwnerNode: '0000000000000000', + PreviousTxnID: '9BB337CC8B34DC8D1A3FFF468556C8BA70977C37F7436439D8DA19610F214AD1', + PreviousTxnLgrSeq: 8342933, + Sequence: 195, + TakerGets: { + currency: 'BTC', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + value: '0.1129232560043778' + }, + TakerPays: { + currency: 'USD', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + value: '56.06639660617357' + }, + index: 'B6BC3B0F87976370EE11F5575593FE63AA5DC1D602830DC96F04B2D597F044BF', + owner_funds: '0.1129267125000245', + quality: '496.5', + taker_gets_funded: { + currency: 'BTC', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + value: '0.1127013098802639' + }, + taker_pays_funded: { + currency: 'USD', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + value: '55.95620035555103' + } + }, + { + Account: 'raudnGKfTK23YKfnS7ixejHrqGERTYNFXk', + BookDirectory: '6EAB7C172DEFA430DBFAD120FDC373B5F5AF8B191649EC985711B6D8C62EF414', + BookNode: '0000000000000000', + Expiration: 461498565, + Flags: 131072, + LedgerEntryType: 'Offer', + OwnerNode: '0000000000000144', + PreviousTxnID: 'C8296B9CCA6DC594C7CD271C5D8FD11FEE380021A07768B25935642CDB37048A', + PreviousTxnLgrSeq: 8342469, + Sequence: 29354, + TakerGets: { + currency: 'BTC', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + value: '0.2' + }, + TakerPays: { + currency: 'USD', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + value: '99.72233516476456' + }, + index: 'A437D85DF80D250F79308F2B613CF5391C7CF8EE9099BC4E553942651CD9FA86', + owner_funds: '0.950363009783092', + quality: '498.6116758238228' + } + ] + }; + + remote.request = function(request) { + switch (request.message.command) { + case 'book_offers': + assert.deepEqual(request.message, { + command: 'book_offers', + id: void(0), + taker_gets: { + currency: '0000000000000000000000004254430000000000', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' + }, + taker_pays: { + currency: '0000000000000000000000005553440000000000', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' + }, + taker: 'rrrrrrrrrrrrrrrrrrrrBZbvji' + }); + + setImmediate(function() { + request.emit('success', offers); + }); + break; + } + }; + + var book = remote.createOrderBook({ + currency_gets: 'BTC', + issuer_gets: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + currency_pays: 'USD', + issuer_pays: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' + }); + + book._issuerTransferRate = 1002000000; + + book.once('model', function(model) { + assert.deepEqual(model, [ + { Account: 'rGCHV41NxoK7wHQJhmao2RqjWZvBrTUhW1', + BookDirectory: '6EAB7C172DEFA430DBFAD120FDC373B5F5AF8B191649EC985711A3A4254F5000', + BookNode: '0000000000000000', + Flags: 131072, + LedgerEntryType: 'Offer', + OwnerNode: '0000000000000000', + PreviousTxnID: '9BB337CC8B34DC8D1A3FFF468556C8BA70977C37F7436439D8DA19610F214AD1', + PreviousTxnLgrSeq: 8342933, + Sequence: 195, + TakerGets: { currency: 'BTC', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + value: '0.1129232560043778' + }, + TakerPays: { + currency: 'USD', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + value: '56.06639660617357' + }, + index: 'B6BC3B0F87976370EE11F5575593FE63AA5DC1D602830DC96F04B2D597F044BF', + owner_funds: '0.1129267125000245', + quality: '496.5', + taker_gets_funded: '0.1127013098802639', + taker_pays_funded: '55.95620035555102', + is_fully_funded: false }, + + { Account: 'raudnGKfTK23YKfnS7ixejHrqGERTYNFXk', + BookDirectory: '6EAB7C172DEFA430DBFAD120FDC373B5F5AF8B191649EC985711B6D8C62EF414', + BookNode: '0000000000000000', + Expiration: 461498565, + Flags: 131072, + LedgerEntryType: 'Offer', + OwnerNode: '0000000000000144', + PreviousTxnID: 'C8296B9CCA6DC594C7CD271C5D8FD11FEE380021A07768B25935642CDB37048A', + PreviousTxnLgrSeq: 8342469, + Sequence: 29354, + TakerGets: { + currency: 'BTC', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + value: '0.2' + }, + TakerPays: { + currency: 'USD', + issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + value: '99.72233516476456' + }, + index: 'A437D85DF80D250F79308F2B613CF5391C7CF8EE9099BC4E553942651CD9FA86', + owner_funds: '0.950363009783092', + quality: '498.6116758238228', + is_fully_funded: true, + taker_gets_funded: '0.2', + taker_pays_funded: '99.72233516476456' + }]) + assert.strictEqual(book._synchronized, true); + done(); + }); + }); });