[FIX] fix order funded amount calculation

This commit is contained in:
Bo Chen
2015-02-18 13:15:47 -08:00
parent 77d5db168b
commit b2cdb1a6ae
3 changed files with 698 additions and 371 deletions

View File

@@ -20,74 +20,82 @@ module.exports.OTHER_LEDGER_INDEX = 'D3338DA77BA23122FB5647B74B53636AB54BE246D4B
module.exports.TRANSFER_RATE = 1002000000;
module.exports.FIAT_OFFERS = [
{
Account: addresses.ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5D06F15E821839FB',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000001897',
PreviousTxnID: '11BA57676711A42C2FC2191EAEE98023B04627DFA84926B0C8E9D61A9CAF13AD',
PreviousTxnLgrSeq: 8265601,
Sequence: 531927,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: module.exports.TAKER_GETS
module.exports.fiatOffers = function (options) {
options = options || {};
_.defaults(options, {
account_funds: '318.3643710638508',
other_account_funds: '235.0194163432668'
});
return [
{
Account: addresses.ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5D06F15E821839FB',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000001897',
PreviousTxnID: '11BA57676711A42C2FC2191EAEE98023B04627DFA84926B0C8E9D61A9CAF13AD',
PreviousTxnLgrSeq: 8265601,
Sequence: 531927,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: module.exports.TAKER_GETS
},
taker_gets_funded: '100',
is_fully_funded: true,
TakerPays: module.exports.TAKER_PAYS,
index: module.exports.LEDGER_INDEX,
owner_funds: options.account_funds,
quality: '195423807.2109563'
},
taker_gets_funded: '100',
is_fully_funded: true,
TakerPays: module.exports.TAKER_PAYS,
index: module.exports.LEDGER_INDEX,
owner_funds: '318.3643710638508',
quality: '195423807.2109563'
},
{
Account: addresses.ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5D06F4C3362FE1D0',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '00000000000063CC',
PreviousTxnID: 'CD77500EF28984BFC123E8A257C10E44FF486EA8FC43E1356C42BD6DB853A602',
PreviousTxnLgrSeq: 8265523,
Sequence: 1139002,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '4.9656112525'
{
Account: addresses.ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5D06F4C3362FE1D0',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '00000000000063CC',
PreviousTxnID: 'CD77500EF28984BFC123E8A257C10E44FF486EA8FC43E1356C42BD6DB853A602',
PreviousTxnLgrSeq: 8265523,
Sequence: 1139002,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '4.9656112525'
},
taker_gets_funded: '100',
is_fully_funded: true,
TakerPays: '972251352',
index: 'X2K98DB77BA23122FB5647B74B53636AB54BE246D4B21707C9D6887DEB334252',
owner_funds: options.account_funds,
quality: '195796912.5171664'
},
taker_gets_funded: '100',
is_fully_funded: true,
TakerPays: '972251352',
index: 'X2K98DB77BA23122FB5647B74B53636AB54BE246D4B21707C9D6887DEB334252',
owner_funds: '318.3643710638508',
quality: '195796912.5171664'
},
{
Account: addresses.OTHER_ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5D06F4C3362FE1D0',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '00000000000063CC',
PreviousTxnID: 'CD77500EF28984BFC123E8A257C10E44FF486EA8FC43E1356C42BD6DB853A602',
PreviousTxnLgrSeq: 8265523,
Sequence: 1139002,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: module.exports.OTHER_TAKER_GETS
{
Account: addresses.OTHER_ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5D06F4C3362FE1D0',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '00000000000063CC',
PreviousTxnID: 'CD77500EF28984BFC123E8A257C10E44FF486EA8FC43E1356C42BD6DB853A602',
PreviousTxnLgrSeq: 8265523,
Sequence: 1139002,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: module.exports.OTHER_TAKER_GETS
},
taker_gets_funded: '100',
is_fully_funded: true,
TakerPays: module.exports.OTHER_TAKER_PAYS,
index: module.exports.OTHER_LEDGER_INDEX,
owner_funds: options.other_account_funds,
quality: '195796912.5171664'
},
taker_gets_funded: '100',
is_fully_funded: true,
TakerPays: module.exports.OTHER_TAKER_PAYS,
index: module.exports.OTHER_LEDGER_INDEX,
owner_funds: '235.0194163432668',
quality: '195796912.5171664'
},
];
];
};
module.exports.NATIVE_OFFERS = [
{
@@ -179,17 +187,7 @@ module.exports.REQUEST_OFFERS = [
},
index: 'B6BC3B0F87976370EE11F5575593FE63AA5DC1D602830DC96F04B2D597F044BF',
owner_funds: '0.1129267125000245',
quality: '496.5',
taker_gets_funded: {
currency: 'BTC',
issuer: addresses.ISSUER,
value: '0.1127013098802639'
},
taker_pays_funded: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '55.95620035555103'
}
quality: '496.5'
},
{
Account: addresses.OTHER_ACCOUNT,
@@ -344,114 +342,124 @@ module.exports.REQUEST_OFFERS_NATIVE = [
}
];
module.exports.BOOK_OFFERS_RESPONSE = {
offers: [
{
Account: addresses.ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5C188F5B29EE1C14',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000006762',
PreviousTxnID: '5F08192C82CD3A598D29B51FCCDE29B6709EBCB454A3CD540C32F7A79EE7CB26',
PreviousTxnLgrSeq: 11558364,
Sequence: 1689777,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '79.39192374'
module.exports.bookOffersResponse = function (options) {
options = options || {};
_.defaults(options, {
account_funds: '2010.027702881682',
other_account_funds: '24.06086596039299',
third_account_funds: '9071.40090264774',
fourth_account_funds: '7244.053477923128'
});
return {
offers: [
{
Account: addresses.ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5C188F5B29EE1C14',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000006762',
PreviousTxnID: '5F08192C82CD3A598D29B51FCCDE29B6709EBCB454A3CD540C32F7A79EE7CB26',
PreviousTxnLgrSeq: 11558364,
Sequence: 1689777,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '79.39192374'
},
TakerPays: '5488380479',
index: 'D9F821C8687E0D0EDEFF05EBB53CFDC81C5F9C4C354DAACB11F6676B5E14AEF5',
owner_funds: options.account_funds,
quality: '69130211.4932226'
},
TakerPays: '5488380479',
index: 'D9F821C8687E0D0EDEFF05EBB53CFDC81C5F9C4C354DAACB11F6676B5E14AEF5',
owner_funds: '2010.027702881682',
quality: '69130211.4932226'
},
{
Account: addresses.OTHER_ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5C18949C72B26C2A',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000001',
PreviousTxnID: '038ED9ACC10211A8F6768729F36B74729CECCD33057837E160131675B272E532',
PreviousTxnLgrSeq: 11558374,
Sequence: 931088,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '24.060765960393'
{
Account: addresses.OTHER_ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5C18949C72B26C2A',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000001',
PreviousTxnID: '038ED9ACC10211A8F6768729F36B74729CECCD33057837E160131675B272E532',
PreviousTxnLgrSeq: 11558374,
Sequence: 931088,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '24.060765960393'
},
TakerPays: '1664716059',
index: '8845F212A8B53004A14C8C029FAF51B53266C66B49281A72F6A8F41CD92FDE99',
owner_funds: options.other_account_funds,
quality: '69187991.0116049',
taker_gets_funded: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '24.01284027983332'
},
taker_pays_funded: '1661400177'
},
TakerPays: '1664716059',
index: '8845F212A8B53004A14C8C029FAF51B53266C66B49281A72F6A8F41CD92FDE99',
owner_funds: '24.06086596039299',
quality: '69187991.0116049',
taker_gets_funded: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '24.01284027983332'
{
Account: addresses.THIRD_ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5C18949C764EA14E',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000009',
PreviousTxnID: '62B96C0E0D86827BCE59ABDCAD146CC0B09404FE5BC86E712FB6F4E473016C63',
PreviousTxnLgrSeq: 11558234,
Sequence: 617872,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '712.60995'
},
TakerPays: '49304051247',
index: '9E5C13908F67146AC35A711A17E5EB75771FDDA816C9532891DC90F29A6A4C10',
owner_funds: options.third_account_funds,
quality: '69187991.61729358'
},
taker_pays_funded: '1661400177'
},
{
Account: addresses.THIRD_ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5C18949C764EA14E',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000009',
PreviousTxnID: '62B96C0E0D86827BCE59ABDCAD146CC0B09404FE5BC86E712FB6F4E473016C63',
PreviousTxnLgrSeq: 11558234,
Sequence: 617872,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '712.60995'
{
Account: addresses.FOURTH_ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5C18AA2E761B7EE6',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000511',
PreviousTxnID: 'F18AED5EC1E7529EF03AF23ADA85F7625AA308278BACE1851F336443AA3DAAEA',
PreviousTxnLgrSeq: 11558336,
Sequence: 662712,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '288.08'
},
TakerPays: '20000000000',
index: '606B3FC9199D5122F1DCC73EC1629E40C8A838D58AC67315A78D76699D960705',
owner_funds: options.fourth_account_funds,
quality: '69425159.67786726'
},
TakerPays: '49304051247',
index: '9E5C13908F67146AC35A711A17E5EB75771FDDA816C9532891DC90F29A6A4C10',
owner_funds: '9071.40090264774',
quality: '69187991.61729358'
},
{
Account: addresses.FOURTH_ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5C18AA2E761B7EE6',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000511',
PreviousTxnID: 'F18AED5EC1E7529EF03AF23ADA85F7625AA308278BACE1851F336443AA3DAAEA',
PreviousTxnLgrSeq: 11558336,
Sequence: 662712,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '288.08'
},
TakerPays: '20000000000',
index: '606B3FC9199D5122F1DCC73EC1629E40C8A838D58AC67315A78D76699D960705',
owner_funds: '7244.053477923128',
quality: '69425159.67786726'
},
{
Account: addresses.ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5C18C3D9EF58005A',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000006762',
PreviousTxnID: 'E3A0240001B03E4F16C4BA6C2B0CB00C01413BE331ABE9E782B6A975DC936618',
PreviousTxnLgrSeq: 11558318,
Sequence: 1689755,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '196.460002'
},
TakerPays: '13694716399',
index: '9A5D0AA37EA0889B876E9A3E552CACDB28BA5A3CD482A528992CD0CCFC09F6E8',
quality: '69707402.31897178'
}
]
{
Account: addresses.ACCOUNT,
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5C18C3D9EF58005A',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000006762',
PreviousTxnID: 'E3A0240001B03E4F16C4BA6C2B0CB00C01413BE331ABE9E782B6A975DC936618',
PreviousTxnLgrSeq: 11558318,
Sequence: 1689755,
TakerGets: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '196.460002'
},
TakerPays: '13694716399',
index: '9A5D0AA37EA0889B876E9A3E552CACDB28BA5A3CD482A528992CD0CCFC09F6E8',
quality: '69707402.31897178'
}
]
};
};
module.exports.MODIFIED_NODES = [

View File

@@ -7,6 +7,8 @@ var addresses = require('./fixtures/addresses');
var fixtures = require('./fixtures/orderbook');
describe('OrderBook', function() {
this.timeout(0);
it('toJSON', function() {
var book = new Remote().createOrderBook({
currency_gets: 'XRP',
@@ -37,7 +39,7 @@ describe('OrderBook', function() {
},
taker_pays: {
currency: Currency.from_json('XRP').to_hex()
},
}
});
});
@@ -345,13 +347,7 @@ describe('OrderBook', function() {
issuer: addresses.ISSUER
});
var offerTotal = Amount.from_json({
value: 3,
currency: 'BTC',
issuer: addresses.ISSUER
});
assert(offerTotal.equals(book.getOwnerOfferTotal(addresses.ACCOUNT)));
assert.strictEqual(book.getOwnerOfferTotal(addresses.ACCOUNT).to_text(), '3');
});
it('Get owner offer total - native', function() {
@@ -363,9 +359,7 @@ describe('OrderBook', function() {
book._ownerOffersTotal[addresses.ACCOUNT] = Amount.from_json('3');
var offerTotal = Amount.from_json('3');
assert(offerTotal.equals(book.getOwnerOfferTotal(addresses.ACCOUNT)));
assert.strictEqual(book.getOwnerOfferTotal(addresses.ACCOUNT).to_text(), '3');
});
it('Get owner offer total - no total', function() {
@@ -375,13 +369,7 @@ describe('OrderBook', function() {
currency_pays: 'XRP'
});
var offerTotal = Amount.from_json({
value: 0,
currency: 'BTC',
issuer: addresses.ISSUER
});
assert(offerTotal.equals(book.getOwnerOfferTotal(addresses.ACCOUNT)));
assert.strictEqual(book.getOwnerOfferTotal(addresses.ACCOUNT).to_text(), '0');
});
it('Get owner offer total - native - no total', function() {
@@ -391,9 +379,7 @@ describe('OrderBook', function() {
currency_pays: 'BTC'
});
var offerTotal = Amount.from_json('0');
assert(offerTotal.equals(book.getOwnerOfferTotal(addresses.ACCOUNT)));
assert(book.getOwnerOfferTotal(addresses.ACCOUNT).to_text(), '0');
});
it('Apply transfer rate - cached transfer rate', function() {
@@ -1124,7 +1110,7 @@ describe('OrderBook', function() {
book._issuerTransferRate = 1000000000;
book._synchronized = true;
book._offers = fixtures.FIAT_OFFERS;
book._offers = fixtures.fiatOffers();
book.on('offer_changed', function(offer) {
receivedChangedEvents += 1;
@@ -1136,15 +1122,19 @@ describe('OrderBook', function() {
assert.notStrictEqual(previousFunds, newFunds);
switch (++receivedFundsChangedEvents) {
case 1:
assert(!offer.is_fully_funded);
assert.strictEqual(offer.is_fully_funded, false);
assert.strictEqual(offer.taker_gets_funded, '10');
assert.strictEqual(offer.taker_pays_funded, '1954238072');
break;
case 2:
assert(offer.is_fully_funded);
assert.strictEqual(offer.is_fully_funded, false);
assert.strictEqual(offer.taker_gets_funded, '0');
assert.strictEqual(offer.taker_pays_funded, '0');
break;
}
});
book._ownerFunds[addresses.ACCOUNT] = '100';
book._ownerFunds[addresses.ACCOUNT] = '20';
book.updateFundedAmounts(message);
setImmediate(function() {
@@ -1155,6 +1145,51 @@ describe('OrderBook', function() {
});
});
it('Update funded amounts - increase funds', function() {
var receivedChangedEvents = 0;
var receivedFundsChangedEvents = 0;
var remote = new Remote();
var message = fixtures.transactionWithRippleState({
balance: '50'
});
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
currency_pays: 'XRP'
});
book._issuerTransferRate = 1000000000;
book._synchronized = true;
book.setOffers(fixtures.fiatOffers({
account_funds: '19'
}));
book.on('offer_funds_changed', function(offer, previousFunds, newFunds) {
assert.strictEqual(newFunds, offer.taker_gets_funded);
assert.notStrictEqual(previousFunds, newFunds);
switch (++receivedFundsChangedEvents) {
case 1:
assert.strictEqual(previousFunds, '19');
assert.strictEqual(offer.is_fully_funded, true);
assert.strictEqual(offer.taker_gets_funded, fixtures.TAKER_GETS);
assert.strictEqual(offer.taker_pays_funded, fixtures.TAKER_PAYS);
break;
case 2:
assert.strictEqual(previousFunds, '0');
assert.strictEqual(offer.is_fully_funded, true);
assert.strictEqual(offer.taker_gets_funded, '4.9656112525');
assert.strictEqual(offer.taker_pays_funded, '972251352');
break;
}
});
book.updateFundedAmounts(message);
});
it('Update funded amounts - owner_funds', function(done) {
var remote = new Remote();
@@ -1169,7 +1204,7 @@ describe('OrderBook', function() {
book._issuerTransferRate = 1002000000;
book._synchronized = true;
book._offers = fixtures.FIAT_OFFERS;
book._offers = fixtures.fiatOffers();
book._ownerFunds[addresses.ACCOUNT] = '100';
book.updateFundedAmounts(message);
@@ -1197,7 +1232,7 @@ describe('OrderBook', function() {
book._synchronized = true;
book._ownerFunds[addresses.ACCOUNT] = '100';
book._offers = fixtures.FIAT_OFFERS;
book._offers = fixtures.fiatOffers();
book.updateFundedAmounts(message);
@@ -1351,7 +1386,7 @@ describe('OrderBook', function() {
book.updateFundedAmounts(message);
});
it('Set offers - issuer transfer rate set', function() {
it('Set offers - issuer transfer rate set - iou/xrp', function() {
var remote = new Remote();
var book = remote.createOrderBook({
@@ -1362,12 +1397,119 @@ describe('OrderBook', function() {
book._issuerTransferRate = 1002000000;
var offers = fixtures.BOOK_OFFERS_RESPONSE.offers;
var offers = fixtures.bookOffersResponse().offers;
book.setOffers(offers);
assert.strictEqual(book._offers.length, 5);
assert.strictEqual(book.getOwnerOfferTotal(addresses.ACCOUNT).to_text(), '275.85192574');
assert.strictEqual(book.getOwnerOfferTotal(addresses.OTHER_ACCOUNT).to_text(), '24.060765960393');
assert.strictEqual(book.getOwnerOfferTotal(addresses.THIRD_ACCOUNT).to_text(), '712.60995');
assert.strictEqual(book.getOwnerOfferTotal(addresses.FOURTH_ACCOUNT).to_text(), '288.08');
assert.strictEqual(book.getOwnerOfferCount(addresses.ACCOUNT), 2);
assert.strictEqual(book.getOwnerOfferCount(addresses.OTHER_ACCOUNT), 1);
assert.strictEqual(book.getOwnerOfferCount(addresses.THIRD_ACCOUNT), 1);
assert.strictEqual(book.getOwnerOfferCount(addresses.FOURTH_ACCOUNT), 1);
assert.strictEqual(book.getOwnerFunds(addresses.ACCOUNT), '2006.015671538605');
assert.strictEqual(book.getOwnerFunds(addresses.OTHER_ACCOUNT), '24.01284027983332');
assert.strictEqual(book.getOwnerFunds(addresses.THIRD_ACCOUNT), '9053.294314019701');
assert.strictEqual(book.getOwnerFunds(addresses.FOURTH_ACCOUNT), '7229.594289344439');
});
it('Set offers - issuer transfer rate set - iou/xrp - funded amounts', function() {
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
currency_pays: 'XRP'
});
book._issuerTransferRate = 1002000000;
var offers = fixtures.bookOffersResponse({
account_funds: '233.13532'
}).offers;
book.setOffers(offers);
var offerOneTakerGetsFunded = Amount.from_json({
value: book._offers[0].taker_gets_funded,
currency: 'USD',
issuer: addresses.ISSUER
});
var offerOneTakerGetsFundedExpected = Amount.from_json({
value: '79.39192374',
currency: 'USD',
issuer: addresses.ISSUER
});
assert.strictEqual(offerOneTakerGetsFunded.equals(offerOneTakerGetsFundedExpected), true);
assert.strictEqual(book._offers[0].is_fully_funded, true);
var offerTwoTakerGetsFunded = Amount.from_json({
value: book._offers[1].taker_gets_funded,
currency: 'USD',
issuer: addresses.ISSUER
});
var offerTwoTakerGetsFundedExpected = Amount.from_json({
value: '24.01284027983332',
currency: 'USD',
issuer: addresses.ISSUER
});
var offerTwoTakerPaysFunded = Amount.from_json(book._offers[1].taker_pays_funded);
var offerTwoTakerPaysFundedExpected = Amount.from_json('1661400177');
assert.strictEqual(offerTwoTakerGetsFunded.equals(offerTwoTakerGetsFundedExpected), true);
assert.strictEqual(offerTwoTakerPaysFunded.equals(offerTwoTakerPaysFundedExpected), true);
assert.strictEqual(book._offers[1].is_fully_funded, false);
var offerFiveTakerGetsFunded = Amount.from_json({
value: book._offers[4].taker_gets_funded,
currency: 'USD',
issuer: addresses.ISSUER
});
var offerFiveTakerGetsFundedExpected = Amount.from_json({
value: '153.2780562999202',
currency: 'USD',
issuer: addresses.ISSUER
});
var offerFiveTakerPaysFunded = Amount.from_json(book._offers[4].taker_pays_funded);
var offerFiveTakerPaysFundedExpected = Amount.from_json('10684615137');
assert.strictEqual(offerFiveTakerGetsFunded.equals(offerFiveTakerGetsFundedExpected), true);
assert.strictEqual(offerFiveTakerPaysFunded.equals(offerFiveTakerPaysFundedExpected), true);
assert.strictEqual(book._offers[4].is_fully_funded, false);
});
it('Set offers - multiple calls', function() {
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
currency_pays: 'XRP'
});
book._issuerTransferRate = 1002000000;
var offers = fixtures.bookOffersResponse().offers;
book.setOffers(offers);
book.setOffers(offers);
assert.strictEqual(book._offers.length, 5);
var accountOfferTotal = Amount.from_json({
value: 275.85192574,
currency: 'USD',
@@ -1537,20 +1679,14 @@ describe('OrderBook', function() {
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.FIAT_OFFERS);
book.setOffers(fixtures.fiatOffers());
var message = fixtures.transactionWithDeletedOffer();
book.notify(message);
var accountOfferTotal = Amount.from_json({
value: 4.9656112525,
currency: 'USD',
issuer: addresses.ISSUER
});
assert.strictEqual(book._offers.length, 2);
assert(book.getOwnerOfferTotal(addresses.ACCOUNT).equals(accountOfferTotal));
assert.strictEqual(book.getOwnerOfferTotal(addresses.ACCOUNT).to_text(), '4.9656112525');
assert.strictEqual(book.getOwnerOfferCount(addresses.ACCOUNT), 1);
});
@@ -1573,7 +1709,7 @@ describe('OrderBook', function() {
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.FIAT_OFFERS.slice(0, 1));
book.setOffers(fixtures.fiatOffers().slice(0, 1));
var message = fixtures.transactionWithDeletedOffer();
@@ -1615,7 +1751,7 @@ describe('OrderBook', function() {
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.FIAT_OFFERS);
book.setOffers(fixtures.fiatOffers());
var message = fixtures.transactionWithDeletedOffer();
@@ -1652,7 +1788,7 @@ describe('OrderBook', function() {
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.FIAT_OFFERS);
book.setOffers(fixtures.fiatOffers());
var message = fixtures.transactionWithDeletedOffer();
@@ -1678,7 +1814,7 @@ describe('OrderBook', function() {
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.FIAT_OFFERS);
book.setOffers(fixtures.fiatOffers());
var message = fixtures.transactionWithDeletedOffer({
transaction_type: 'OfferCancel'
@@ -1686,14 +1822,8 @@ describe('OrderBook', function() {
book.notify(message);
var accountOfferTotal = Amount.from_json({
value: 4.9656112525,
currency: 'USD',
issuer: addresses.ISSUER
});
assert.strictEqual(book._offers.length, 2);
assert(book.getOwnerOfferTotal(addresses.ACCOUNT).equals(accountOfferTotal));
assert.strictEqual(book.getOwnerOfferTotal(addresses.ACCOUNT).to_text(), '4.9656112525');
assert.strictEqual(book.getOwnerOfferCount(addresses.ACCOUNT), 1);
});
@@ -1716,7 +1846,7 @@ describe('OrderBook', function() {
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.FIAT_OFFERS.slice(0, 1));
book.setOffers(fixtures.fiatOffers().slice(0, 1));
var message = fixtures.transactionWithDeletedOffer({
transaction_type: 'OfferCancel'
@@ -1747,21 +1877,23 @@ describe('OrderBook', function() {
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.FIAT_OFFERS);
book.setOffers(fixtures.fiatOffers());
var message = fixtures.transactionWithModifiedOffer();
book.notify(message);
var accountOfferTotal = Amount.from_json({
value: 23.8114145625,
currency: 'USD',
issuer: addresses.ISSUER
});
assert.strictEqual(book._offers.length, 3);
assert(book.getOwnerOfferTotal(addresses.ACCOUNT).equals(accountOfferTotal));
assert.strictEqual(book.getOwnerOfferTotal(addresses.ACCOUNT).to_text(), '23.8114145625');
assert.strictEqual(book.getOwnerOfferCount(addresses.ACCOUNT), 2);
assert.strictEqual(book._offers[0].is_fully_funded, true);
assert.strictEqual(book._offers[0].taker_gets_funded, fixtures.TAKER_GETS_FINAL);
assert.strictEqual(book._offers[0].taker_pays_funded, fixtures.TAKER_PAYS_FINAL);
assert.strictEqual(book._offers[1].is_fully_funded, true);
assert.strictEqual(book._offers[1].taker_gets_funded, '4.9656112525');
assert.strictEqual(book._offers[1].taker_pays_funded, '972251352');
});
it('Notify - modified node - events', function() {
@@ -1796,7 +1928,7 @@ describe('OrderBook', function() {
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.FIAT_OFFERS);
book.setOffers(fixtures.fiatOffers());
var message = fixtures.transactionWithModifiedOffer();
@@ -1833,7 +1965,7 @@ describe('OrderBook', function() {
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.FIAT_OFFERS);
book.setOffers(fixtures.fiatOffers());
var message = fixtures.transactionWithModifiedOffer();
@@ -1865,7 +1997,7 @@ describe('OrderBook', function() {
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.FIAT_OFFERS);
book.setOffers(fixtures.fiatOffers());
var message = fixtures.transactionWithModifiedOffers();
@@ -1904,7 +2036,7 @@ describe('OrderBook', function() {
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.FIAT_OFFERS);
book.setOffers(fixtures.fiatOffers());
var message = fixtures.transactionWithNoNodes();
@@ -1916,6 +2048,171 @@ describe('OrderBook', function() {
assert.strictEqual(numOfferChangedEvents, 0);
});
it('Delete offer - offer cancel - funded after delete', function() {
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
currency_pays: 'XRP'
});
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.fiatOffers({
account_funds: '20'
}));
book.deleteOffer(fixtures.transactionWithDeletedOffer({
transaction_type: 'OfferCancel'
}).mmeta.getNodes()[0], true);
assert.strictEqual(book._offers.length, 2);
assert.strictEqual(book.getOwnerOfferCount(addresses.ACCOUNT), 1);
assert.strictEqual(book.getOwnerOfferCount(addresses.OTHER_ACCOUNT), 1);
assert.strictEqual(book.getOwnerOfferTotal(addresses.ACCOUNT).to_text(), '4.9656112525');
assert.strictEqual(book._offers[0].is_fully_funded, true);
});
it('Delete offer - offer cancel - not fully funded after delete', function() {
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
currency_pays: 'XRP'
});
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.fiatOffers({
account_funds: '4.5'
}));
book.deleteOffer(fixtures.transactionWithDeletedOffer({
transaction_type: 'OfferCancel'
}).mmeta.getNodes()[0], true);
assert.strictEqual(book._offers.length, 2);
assert.strictEqual(book.getOwnerOfferCount(addresses.ACCOUNT), 1);
assert.strictEqual(book.getOwnerOfferCount(addresses.OTHER_ACCOUNT), 1);
assert.strictEqual(book.getOwnerOfferTotal(addresses.ACCOUNT).to_text(), '4.9656112525');
assert.strictEqual(book._offers[0].is_fully_funded, false);
assert.strictEqual(book._offers[0].taker_gets_funded, '4.5');
assert.strictEqual(book._offers[0].taker_pays_funded, '881086106');
});
it('Insert offer - best quality - insufficient funds for all offers', function() {
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
currency_pays: 'XRP'
});
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.fiatOffers());
book.insertOffer(fixtures.transactionWithCreatedOffer({
amount: '298'
}).mmeta.getNodes()[0]);
assert.strictEqual(book._offers.length, 4);
assert.strictEqual(book.getOwnerOfferCount(addresses.ACCOUNT), 3);
assert.strictEqual(book.getOwnerOfferCount(addresses.OTHER_ACCOUNT), 1);
assert.strictEqual(book.getOwnerOfferTotal(addresses.ACCOUNT).to_text(), '322.8114145625');
assert.strictEqual(book._offers[0].is_fully_funded, true);
assert.strictEqual(book._offers[0].taker_gets_funded, '298');
assert.strictEqual(book._offers[0].taker_pays_funded, fixtures.TAKER_PAYS);
assert.strictEqual(book._offers[1].is_fully_funded, true);
assert.strictEqual(book._offers[1].taker_gets_funded, fixtures.TAKER_GETS);
assert.strictEqual(book._offers[1].taker_pays_funded, fixtures.TAKER_PAYS);
assert.strictEqual(book._offers[2].is_fully_funded, false);
assert.strictEqual(book._offers[2].taker_gets_funded, '0.5185677538508');
assert.strictEqual(book._offers[2].taker_pays_funded, '101533965');
});
it('Insert offer - worst quality - insufficient funds for all orders', function () {
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
currency_pays: 'XRP'
});
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.fiatOffers({
account_funds: '25'
}));
book.insertOffer(fixtures.transactionWithCreatedOffer({
amount: '5'
}).mmeta.getNodes()[0]);
assert.strictEqual(book._offers.length, 4);
assert.strictEqual(book.getOwnerOfferCount(addresses.ACCOUNT), 3);
assert.strictEqual(book.getOwnerOfferCount(addresses.OTHER_ACCOUNT), 1);
assert.strictEqual(book.getOwnerOfferTotal(addresses.ACCOUNT).to_text(), '29.8114145625');
assert.strictEqual(book._offers[0].is_fully_funded, true);
assert.strictEqual(book._offers[0].taker_gets_funded, fixtures.TAKER_GETS);
assert.strictEqual(book._offers[0].taker_pays_funded, fixtures.TAKER_PAYS);
assert.strictEqual(book._offers[1].is_fully_funded, true);
assert.strictEqual(book._offers[1].taker_gets_funded, '4.9656112525');
assert.strictEqual(book._offers[1].taker_pays_funded, '972251352');
assert.strictEqual(book._offers[3].is_fully_funded, false);
assert.strictEqual(book._offers[3].taker_gets_funded, '0.1885854375');
assert.strictEqual(book._offers[3].taker_pays_funded, '146279781');
});
it('Insert offer - middle quality - insufficient funds for all offers', function() {
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
currency_pays: 'XRP'
});
book._subscribed = true;
book._issuerTransferRate = 1000000000;
book.setOffers(fixtures.fiatOffers({
account_funds: '30'
}));
book.insertOffer(fixtures.transactionWithCreatedOffer({
amount: '19.84080331'
}).mmeta.getNodes()[0]);
assert.strictEqual(book._offers.length, 4);
assert.strictEqual(book.getOwnerOfferCount(addresses.ACCOUNT), 3);
assert.strictEqual(book.getOwnerOfferCount(addresses.OTHER_ACCOUNT), 1);
assert.strictEqual(book.getOwnerOfferTotal(addresses.ACCOUNT).to_text(), '44.6522178725');
assert.strictEqual(book._offers[0].is_fully_funded, true);
assert.strictEqual(book._offers[0].taker_gets_funded, fixtures.TAKER_GETS);
assert.strictEqual(book._offers[0].taker_pays_funded, fixtures.TAKER_PAYS);
assert.strictEqual(book._offers[1].is_fully_funded, false);
assert.strictEqual(book._offers[1].taker_gets_funded, '10.15419669');
assert.strictEqual(book._offers[1].taker_pays_funded, '1984871849');
assert.strictEqual(book._offers[2].is_fully_funded, false);
assert.strictEqual(book._offers[2].taker_gets_funded, '0');
assert.strictEqual(book._offers[2].taker_pays_funded, '0');
});
it('Request offers', function(done) {
var remote = new Remote();
@@ -1978,9 +2275,9 @@ describe('OrderBook', function() {
owner_funds: '0.1129267125000245',
taker_gets_funded: '0.112701309880264',
taker_pays_funded: '55.95620035555106',
is_fully_funded: false
is_fully_funded: false
},
{
{
Account: addresses.OTHER_ACCOUNT,
BookDirectory: '6EAB7C172DEFA430DBFAD120FDC373B5F5AF8B191649EC985711B6D8C62EF414',
BookNode: '0000000000000000',
@@ -2025,9 +2322,9 @@ describe('OrderBook', function() {
},
index: 'A437D85DF80D250F79308F2B613CF5391C7CF8EE9099BC4E553942651CD9FA86',
owner_funds: '0.950363009783092',
is_fully_funded: false,
is_fully_funded: true,
taker_gets_funded: '0.5',
taker_pays_funded: '94.58325208561269'
taker_pays_funded: '99.72233516476456'
},
{
Account: addresses.THIRD_ACCOUNT,
@@ -2051,8 +2348,8 @@ describe('OrderBook', function() {
index: 'A437D85DF80D250F79308F2B613CF5391C7CF8EE9099BC4E553942651CD9FA86',
owner_funds: '0.950363009783092',
is_fully_funded: false,
taker_gets_funded: '0.5',
taker_pays_funded: '94.58325208561269'
taker_gets_funded: '0.4484660776278363',
taker_pays_funded: '89.44416900646082'
}
];
@@ -2129,9 +2426,9 @@ describe('OrderBook', function() {
},
index: 'A437D85DF80D250F79308F2B613CF5391C7CF8EE9099BC4E553942651CD9FA86',
owner_funds: '3900',
is_fully_funded: false,
is_fully_funded: true,
taker_gets_funded: '2000',
taker_pays_funded: '97.22927678564545'
taker_pays_funded: '99.72233516476456'
},
{
Account: addresses.THIRD_ACCOUNT,
@@ -2150,8 +2447,8 @@ describe('OrderBook', function() {
},
index: 'A437D85DF80D250F79308F2B613CF5391C7CF8EE9099BC4E553942651CD9FA86',
is_fully_funded: false,
taker_gets_funded: '2000',
taker_pays_funded: '97.22927678564545',
taker_gets_funded: '1900',
taker_pays_funded: '94.73621840652633',
owner_funds: '3900'
}
];
@@ -2191,4 +2488,4 @@ describe('OrderBook', function() {
done();
});
});
});
});