[FEATURE] add offer autobridging

This commit is contained in:
Bo Chen
2015-03-16 15:42:13 -07:00
committed by Geert Weening
parent 5e2c26a4a2
commit c7bbce8371
8 changed files with 2045 additions and 603 deletions

View File

@@ -24,7 +24,7 @@ module.exports.OTHER_LEDGER_INDEX = 'D3338DA77BA23122FB5647B74B53636AB54BE246D4B
module.exports.TRANSFER_RATE = 1002000000;
module.exports.fiatOffers = function (options) {
module.exports.fiatOffers = function(options) {
options = options || {};
_.defaults(options, {
account_funds: '318.3643710638508',
@@ -156,12 +156,12 @@ module.exports.NATIVE_OFFERS = [
PreviousTxnID: 'CD77500EF28984BFC123E8A257C10E44FF486EA8FC43E1356C42BD6DB853A602',
PreviousTxnLgrSeq: 8265523,
Sequence: 1139002,
TakerGets: {
TakerGets: '972251352',
TakerPays: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '4.9656112525'
},
TakerPays: '972251352',
index: 'D3338DA77BA23122FB5647B74B53636AB54BE246D4B21707C9D6887DEB334252',
owner_funds: '235.0194163432668',
quality: '195796912.5171664'
@@ -403,7 +403,119 @@ module.exports.DECIMAL_TAKER_PAYS_FUNDED_OFFERS = [
}
];
module.exports.bookOffersResponse = function (options) {
module.exports.LEG_ONE_OFFERS = [
{
Account: addresses.ACCOUNT,
BookDirectory: 'DFA3B6DDAB58C7E8E5D944E736DA4B7046C30E4F460FD9DE4D043654A0DBD245',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000078',
PreviousTxnID: '27723DCE3E6DB324DBCE9F0C9110352DBBC04DD6BEFE2A57C4E524FD215144C9',
PreviousTxnLgrSeq: 12024847,
Sequence: 14532890,
TakerGets: '31461561812',
TakerPays: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '373.019921005'
},
index: '7EEE980B0BD43C15504B9A89164D29EF02DBBD3807DA7936F51EA2CE3D0C6324',
owner_funds: '210586312936',
quality: '0.00000001185637010756165'
},
{
Account: addresses.OTHER_ACCOUNT,
BookDirectory: 'DFA3B6DDAB58C7E8E5D944E736DA4B7046C30E4F460FD9DE4D043676B9DEA2FC',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000002',
PreviousTxnID: '1B36F7DE44C96FBDB50F8F80D24D3FA11454CB837BA4E4D667C92E01AE9225F5',
PreviousTxnLgrSeq: 12024788,
Sequence: 244399,
TakerGets: '25299728855',
TakerPays: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '300'
},
index: '5F8BDA3343CB792FA0DD55740F5827C5E050A287C96FDE4F7DFF548693420744',
owner_funds: '1291056089559',
quality: '0.00000001185783459259132'
},
{
Account: addresses.THIRD_ACCOUNT,
BookDirectory: 'DFA3B6DDAB58C7E8E5D944E736DA4B7046C30E4F460FD9DE4D0437FF40E6F02A',
BookNode: '0000000000000000',
Expiration: 478636633,
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000165',
PreviousTxnID: 'D42D81273BDC3ED611ED84DF07EA55E31703F4E05BC70CC12871715FCB58E160',
PreviousTxnLgrSeq: 12024847,
Sequence: 3858033,
TakerGets: '18189943147',
TakerPays: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '216'
},
index: 'FD5E66163DFE67919E64F31D506A8F3E94802E6A0FFEBE7A6FD40A2F1135EDD4',
owner_funds: '490342145233',
quality: '0.0000000118746935190737'
}
];
module.exports.LEG_TWO_OFFERS = [
{
Account: addresses.FOURTH_ACCOUNT,
BookDirectory: 'DA36FDE1B8CE294B214BE4E4C958DAAF9C1F46DE1FCB44115D0A4929E095B160',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000003',
PreviousTxnID: '97A8D6B2135231363EC1B3B509DF052D481A0045684464948E6DF2C2B9FC1E64',
PreviousTxnLgrSeq: 12004045,
Sequence: 384,
TakerGets: {
currency: 'EUR',
issuer: addresses.ISSUER,
value: '17.07639524223001'
},
TakerPays: '4943947661',
index: '5B00ACF35041983F070EAE2219C274D24A11D6FD6FE4306A4C72E7B769D4F914',
owner_funds: '36.40299530003982',
quality: '289519397.75'
},
{
Account: addresses.FOURTH_ACCOUNT,
BookDirectory: 'DA36FDE1B8CE294B214BE4E4C958DAAF9C1F46DE1FCB44115E12B2D070B5DBE0',
BookNode: '0000000000000000',
Flags: 0,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000006',
PreviousTxnID: '425EBA467DD335602BAFBAB5329B1E7FC1ABB325AA5CD4495A5085860D09F2BE',
PreviousTxnLgrSeq: 11802828,
Sequence: 605,
TakerGets: {
currency: 'EUR',
issuer: addresses.ISSUER,
value: '19.99999999954904'
},
TakerPays: '105263157889',
index: '8715E674302D446EBD520FF11B48A0F64822F4F9266D62544987223CA16EDBB1',
quality: '5263157894.7',
taker_gets_funded: {
currency: 'EUR',
issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
value: '19.25393938854825'
},
taker_pays_funded: '101336523096'
}
];
module.exports.bookOffersResponse = function(options) {
options = options || {};
_.defaults(options, {
account_funds: '2010.027702881682',

View File

@@ -0,0 +1,753 @@
/*eslint-disable max-len */
'use strict';
var _ = require('lodash');
var assert = require('assert-diff');
var Remote = require('ripple-lib').Remote;
var Currency = require('ripple-lib').Currency;
var addresses = require('./fixtures/addresses');
var fixtures = require('./fixtures/orderbook');
describe('OrderBook Autobridging', function() {
this.timeout(0);
it('Initialize IOU/IOU', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
assert.deepEqual(book._legOneBook._currencyGets.to_hex(), Currency.from_json('XRP').to_hex());
assert.deepEqual(book._legOneBook._currencyPays.to_hex(), Currency.from_json('USD').to_hex());
assert.deepEqual(book._legTwoBook._currencyGets.to_hex(), Currency.from_json('EUR').to_hex());
assert.deepEqual(book._legTwoBook._currencyPays.to_hex(), Currency.from_json('XRP').to_hex());
});
it('Compute autobridged offers', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1000000000;
var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1));
var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1));
book._legOneBook.setOffers(legOneOffers);
book._legTwoBook.setOffers(legTwoOffers);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 1);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '17.07639524223001');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '58.61727326122974');
assert.strictEqual(book._offersAutobridged[0].taker_gets_funded, '17.07639524223001');
assert.strictEqual(book._offersAutobridged[0].taker_pays_funded, '58.61727326122974');
assert(book._offersAutobridged[0].autobridged);
});
it('Compute autobridged offers - leg one partially funded', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1000000000;
var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1));
var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1));
legOneOffers[0].owner_funds = '2105863129';
book._legOneBook.setOffers(legOneOffers);
book._legTwoBook.setOffers(legTwoOffers);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 1);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '7.273651248813431');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '24.96789265329184');
assert(book._offersAutobridged[0].autobridged);
});
it('Compute autobridged offers - leg two partially funded', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1000000000;
var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1));
var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1));
legTwoOffers[0].owner_funds = '10';
book._legOneBook.setOffers(legOneOffers);
book._legTwoBook.setOffers(legTwoOffers);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 1);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '10');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '34.32649132449533');
assert(book._offersAutobridged[0].autobridged);
});
it('Compute autobridged offers - leg two transfer rate', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1002000000;
var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1));
var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1));
legTwoOffers[0].owner_funds = '10';
book._legOneBook.setOffers(legOneOffers);
book._legTwoBook.setOffers(legTwoOffers);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '9.980039920159681');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '34.25797537722665');
assert(book._offersAutobridged[0].autobridged);
});
it('Compute autobridged offers - taker funds < leg two in', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1000000000;
var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1));
var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1));
legOneOffers[0].owner_funds = '33461561812';
legTwoOffers[0].owner_funds = '360';
legTwoOffers[0].TakerGets.value = '170.7639524223001';
legTwoOffers[0].TakerPays = '49439476610';
book._legOneBook.setOffers(legOneOffers);
book._legTwoBook.setOffers(legTwoOffers);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 1);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '108.6682345172846');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '373.019921005');
assert(book._offersAutobridged[0].autobridged);
});
it('Compute autobridged offers - leg one partially funded - owners equal', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1000000000;
var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1));
var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1));
legOneOffers[0].owner_funds = '2105863129';
legTwoOffers[0].Account = legOneOffers[0].Account;
book._legOneBook.setOffers(legOneOffers);
book._legTwoBook.setOffers(legTwoOffers);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 1);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '17.07639524223001');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '58.61727326122974');
assert(book._offersAutobridged[0].autobridged);
});
it('Compute autobridged offers - leg one partially funded - owners equal - leg two in > leg one out', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1000000000;
var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1));
var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1));
legOneOffers[0].owner_funds = '2105863129';
legTwoOffers[0].Account = legOneOffers[0].Account;
legTwoOffers[0].owner_funds = '360';
legTwoOffers[0].TakerGets.value = '170.7639524223001';
legTwoOffers[0].TakerPays = '49439476610';
book._legOneBook.setOffers(legOneOffers);
book._legTwoBook.setOffers(legTwoOffers);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 1);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '108.6682345172846');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '373.0199210049999');
assert(book._offersAutobridged[0].autobridged);
});
it('Compute autobridged offers - leg one consumes leg two fully', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1000000000;
var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1));
var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 2));
book._legOneBook.setOffers(legOneOffers);
book._legTwoBook.setOffers(legTwoOffers);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 2);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '17.07639524223001');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '58.61727326122974');
assert(book._offersAutobridged[0].autobridged);
assert.strictEqual(book._offersAutobridged[1].TakerGets.value, '5.038346688725268');
assert.strictEqual(book._offersAutobridged[1].TakerPays.value, '314.4026477437702');
assert(book._offersAutobridged[1].autobridged);
});
it('Compute autobridged offers - leg two consumes first leg one offer fully', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1000000000;
var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 2));
var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1));
legTwoOffers[0].TakerGets.value = '170.7639524223001';
legTwoOffers[0].TakerPays = '49439476610';
legTwoOffers[0].owner_funds = '364.0299530003982';
book._legOneBook.setOffers(legOneOffers);
book._legTwoBook.setOffers(legTwoOffers);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 2);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '108.6682345172846');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '373.019921005');
assert(book._offersAutobridged[0].autobridged);
assert.strictEqual(book._offersAutobridged[1].TakerGets.value, '62.0957179050155');
assert.strictEqual(book._offersAutobridged[1].TakerPays.value, '213.1791399943838');
assert(book._offersAutobridged[1].autobridged);
});
it('Compute autobridged offers - owners equal', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1002000000;
var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1));
var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 2));
legOneOffers[0].owner_funds = '2105863129';
legTwoOffers[1].owner_funds = '19.32660005780981';
legTwoOffers[0].Account = legOneOffers[0].Account;
book._legOneBook.setOffers(legOneOffers);
book._legTwoBook.setOffers(legTwoOffers);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 2);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '17.07639524223001');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '58.61727326122974');
assert(book._offersAutobridged[0].autobridged);
assert.strictEqual(book._offersAutobridged[1].TakerGets.value, '0.4001139945128008');
assert.strictEqual(book._offersAutobridged[1].TakerPays.value, '24.96789265329184');
assert(book._offersAutobridged[1].autobridged);
});
it('Compute autobridged offers - owners equal - leg one overfunded', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1002000000;
var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1));
var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 2));
legOneOffers[0].owner_funds = '41461561812';
legTwoOffers[0].Account = legOneOffers[0].Account;
legTwoOffers[1].owner_funds = '30';
book._legOneBook.setOffers(legOneOffers);
book._legTwoBook.setOffers(legTwoOffers);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 2);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '17.07639524223001');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '58.61727326122974');
assert(book._offersAutobridged[0].autobridged);
assert.strictEqual(book._offersAutobridged[1].TakerGets.value, '5.038346688725268');
assert.strictEqual(book._offersAutobridged[1].TakerPays.value, '314.4026477437702');
assert(book._offersAutobridged[1].autobridged);
});
it('Compute autobridged offers - TakerPays < Quality * TakerGets', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1000000000;
book._legOneBook.setOffers([
{
Account: addresses.ACCOUNT,
TakerGets: '75',
TakerPays: {
value: '50',
issuer: addresses.ISSUER,
currency: 'USD'
},
owner_funds: '50',
quality: '1'
}
]);
book._legTwoBook.setOffers([
{
Account: addresses.ACCOUNT,
TakerGets: {
value: '90',
issuer: addresses.ISSUER,
currency: 'EUR'
},
TakerPays: '90',
owner_funds: '150',
quality: '1'
}
]);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 1);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '75');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '75');
assert(book._offersAutobridged[0].autobridged);
});
it('Compute autobridged offers - update funded amount', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1000000000;
book._legOneBook.setOffers([
{
Account: addresses.ACCOUNT,
TakerGets: '100',
TakerPays: {
value: '100',
issuer: addresses.ISSUER,
currency: 'USD'
},
owner_funds: '50',
quality: '1'
},
{
Account: addresses.ACCOUNT,
TakerGets: '50',
TakerPays: {
value: '100',
issuer: addresses.ISSUER,
currency: 'USD'
},
quality: '2'
}
]);
book._legTwoBook.setOffers([
{
Account: addresses.ACCOUNT,
TakerGets: {
value: '90',
issuer: addresses.ISSUER,
currency: 'EUR'
},
TakerPays: '90',
owner_funds: '150',
quality: '1'
},
{
Account: addresses.OTHER_ACCOUNT,
TakerGets: {
value: '30',
issuer: addresses.ISSUER,
currency: 'EUR'
},
TakerPays: '60',
owner_funds: '70',
quality: '2'
}
]);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 3);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '90');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '90');
assert(book._offersAutobridged[0].autobridged);
assert.strictEqual(book._offersAutobridged[1].TakerGets.value, '5');
assert.strictEqual(book._offersAutobridged[1].TakerPays.value, '10');
assert(book._offersAutobridged[1].autobridged);
assert.strictEqual(book._offersAutobridged[2].TakerGets.value, '20');
assert.strictEqual(book._offersAutobridged[2].TakerPays.value, '80');
assert(book._offersAutobridged[2].autobridged);
});
it('Compute autobridged offers - update funded amount - owners equal', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1000000000;
book._legOneBook.setOffers([
{
Account: addresses.ACCOUNT,
TakerGets: '100',
TakerPays: {
value: '100',
issuer: addresses.ISSUER,
currency: 'USD'
},
owner_funds: '50',
quality: '1'
},
{
Account: addresses.ACCOUNT,
TakerGets: '20',
TakerPays: {
value: '100',
issuer: addresses.ISSUER,
currency: 'USD'
},
quality: '5'
}
]);
book._legTwoBook.setOffers([
{
Account: addresses.ACCOUNT,
TakerGets: {
value: '90',
issuer: addresses.ISSUER,
currency: 'EUR'
},
TakerPays: '90',
owner_funds: '150',
quality: '1'
},
{
Account: addresses.OTHER_ACCOUNT,
TakerGets: {
value: '30',
issuer: addresses.ISSUER,
currency: 'EUR'
},
TakerPays: '60',
owner_funds: '70',
quality: '2'
}
]);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 3);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '90');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '90');
assert(book._offersAutobridged[0].autobridged);
assert.strictEqual(book._offersAutobridged[1].TakerGets.value, '5');
assert.strictEqual(book._offersAutobridged[1].TakerPays.value, '10');
assert(book._offersAutobridged[1].autobridged);
assert.strictEqual(book._offersAutobridged[2].TakerGets.value, '10');
assert.strictEqual(book._offersAutobridged[2].TakerPays.value, '100');
assert(book._offersAutobridged[2].autobridged);
});
it('Compute autobridged offers - update funded amount - first two owners equal', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1000000000;
book._legOneBook.setOffers([
{
Account: addresses.ACCOUNT,
TakerGets: '100',
TakerPays: {
value: '100',
issuer: addresses.ISSUER,
currency: 'USD'
},
owner_funds: '50',
quality: '1'
},
{
Account: addresses.ACCOUNT,
TakerGets: '100',
TakerPays: {
value: '200',
issuer: addresses.ISSUER,
currency: 'USD'
},
quality: '2'
}
]);
book._legTwoBook.setOffers([
{
Account: addresses.ACCOUNT,
TakerGets: {
value: '90',
issuer: addresses.ISSUER,
currency: 'EUR'
},
TakerPays: '90',
owner_funds: '150',
quality: '1'
},
{
Account: addresses.ACCOUNT,
TakerGets: {
value: '30',
issuer: addresses.ISSUER,
currency: 'EUR'
},
TakerPays: '60',
quality: '2'
},
{
Account: addresses.OTHER_ACCOUNT,
TakerGets: {
value: '20',
issuer: addresses.ISSUER,
currency: 'EUR'
},
TakerPays: '40',
owner_funds: '70',
quality: '2'
}
]);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 4);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '90');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '90');
assert(book._offersAutobridged[0].autobridged);
assert.strictEqual(book._offersAutobridged[1].TakerGets.value, '5');
assert.strictEqual(book._offersAutobridged[1].TakerPays.value, '10');
assert(book._offersAutobridged[1].autobridged);
assert.strictEqual(book._offersAutobridged[2].TakerGets.value, '25');
assert.strictEqual(book._offersAutobridged[2].TakerPays.value, '100');
assert(book._offersAutobridged[2].autobridged);
assert.strictEqual(book._offersAutobridged[3].TakerGets.value, '20');
assert.strictEqual(book._offersAutobridged[3].TakerPays.value, '80');
assert(book._offersAutobridged[3].autobridged);
});
it('Compute autobridged offers - unfunded offer - owners equal', function() {
var book = new Remote().createOrderBook({
currency_gets: 'EUR',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1000000000;
book._legOneBook._issuerTransferRate = 1000000000;
book._legTwoBook._issuerTransferRate = 1000000000;
book._legOneBook.setOffers([
{
Account: addresses.ACCOUNT,
TakerGets: '75',
TakerPays: {
value: '75',
issuer: addresses.ISSUER,
currency: 'USD'
},
owner_funds: '0',
quality: '1'
}
]);
book._legTwoBook.setOffers([
{
Account: addresses.ACCOUNT,
TakerGets: {
value: '90',
issuer: addresses.ISSUER,
currency: 'EUR'
},
TakerPays: '90',
owner_funds: '150',
quality: '1'
}
]);
book.computeAutobridgedOffers();
assert.strictEqual(book._offersAutobridged.length, 1);
assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '75');
assert.strictEqual(book._offersAutobridged[0].TakerPays.value, '75');
assert(book._offersAutobridged[0].autobridged);
});
});

View File

@@ -2119,7 +2119,7 @@ describe('OrderBook', function() {
assert.strictEqual(book._offers[2].taker_pays_funded, '101533965');
});
it('Insert offer - worst quality - insufficient funds for all orders', function () {
it('Insert offer - worst quality - insufficient funds for all orders', function() {
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
@@ -2193,159 +2193,6 @@ describe('OrderBook', function() {
assert.strictEqual(book._offers[2].taker_pays_funded, '0');
});
it('Request offers', function(done) {
var remote = new Remote();
var offers = {
offers: fixtures.REQUEST_OFFERS
};
remote.request = function(request) {
switch (request.message.command) {
case 'book_offers':
assert.deepEqual(request.message, {
command: 'book_offers',
id: undefined,
taker_gets: {
currency: '0000000000000000000000004254430000000000',
issuer: addresses.ISSUER
},
taker_pays: {
currency: '0000000000000000000000005553440000000000',
issuer: addresses.ISSUER
},
taker: 'rrrrrrrrrrrrrrrrrrrrBZbvji'
});
setImmediate(function() {
request.emit('success', offers);
});
break;
}
};
var book = remote.createOrderBook({
currency_gets: 'BTC',
issuer_gets: addresses.ISSUER,
currency_pays: 'USD',
issuer_pays: addresses.ISSUER
});
book._issuerTransferRate = 1002000000;
var expected = [
{
Account: addresses.ACCOUNT,
BookDirectory: '6EAB7C172DEFA430DBFAD120FDC373B5F5AF8B191649EC985711A3A4254F5000',
BookNode: '0000000000000000',
Flags: 131072,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000000',
Sequence: 195,
TakerGets: {
currency: 'BTC',
issuer: addresses.ISSUER,
value: '0.1129232560043778'
},
TakerPays: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '56.06639660617357'
},
index: 'B6BC3B0F87976370EE11F5575593FE63AA5DC1D602830DC96F04B2D597F044BF',
owner_funds: '0.1129267125000245',
taker_gets_funded: '0.112701309880264',
taker_pays_funded: '55.95620035555106',
is_fully_funded: false,
quality: '496.4999999999999'
},
{
Account: addresses.OTHER_ACCOUNT,
BookDirectory: '6EAB7C172DEFA430DBFAD120FDC373B5F5AF8B191649EC985711B6D8C62EF414',
BookNode: '0000000000000000',
Expiration: 461498565,
Flags: 131072,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000144',
Sequence: 29354,
TakerGets: {
currency: 'BTC',
issuer: addresses.ISSUER,
value: '0.2'
},
TakerPays: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '99.72233516476456'
},
index: 'A437D85DF80D250F79308F2B613CF5391C7CF8EE9099BC4E553942651CD9FA86',
owner_funds: '0.950363009783092',
is_fully_funded: true,
taker_gets_funded: '0.2',
taker_pays_funded: '99.72233516476456',
quality: '498.6116758238228'
},
{
Account: addresses.THIRD_ACCOUNT,
BookDirectory: '6EAB7C172DEFA430DBFAD120FDC373B5F5AF8B191649EC985711B6D8C62EF414',
BookNode: '0000000000000000',
Expiration: 461498565,
Flags: 131072,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000144',
Sequence: 29356,
TakerGets: {
currency: 'BTC',
issuer: addresses.ISSUER,
value: '0.5'
},
TakerPays: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '99.72233516476456'
},
index: 'A437D85DF80D250F79308F2B613CF5391C7CF8EE9099BC4E553942651CD9FA86',
owner_funds: '0.950363009783092',
is_fully_funded: true,
taker_gets_funded: '0.5',
taker_pays_funded: '99.72233516476456',
quality: '498.6116758238228'
},
{
Account: addresses.THIRD_ACCOUNT,
BookDirectory: '6EAB7C172DEFA430DBFAD120FDC373B5F5AF8B191649EC985711B6D8C62EF414',
BookNode: '0000000000000000',
Expiration: 461498565,
Flags: 131078,
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000144',
Sequence: 29354,
TakerGets: {
currency: 'BTC',
issuer: addresses.ISSUER,
value: '0.5'
},
TakerPays: {
currency: 'USD',
issuer: addresses.ISSUER,
value: '99.72233516476456'
},
index: 'A437D85DF80D250F79308F2B613CF5391C7CF8EE9099BC4E553942651CD9FA86',
owner_funds: '0.950363009783092',
is_fully_funded: false,
taker_gets_funded: '0.4484660776278363',
taker_pays_funded: '89.44416900646082',
quality: '199.4446703295291'
}
];
book.on('model', function(model) {
assert.deepEqual(model, expected);
assert.strictEqual(book._synchronized, true);
done();
});
});
it('Request offers - native currency', function(done) {
var remote = new Remote();

File diff suppressed because it is too large Load Diff