mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-12-01 09:35:48 +00:00
[FIX] fix order funded amount calculation
This commit is contained in:
@@ -490,6 +490,26 @@ OrderBook.prototype.getOwnerOfferTotal = function(account) {
|
||||
return amount;
|
||||
};
|
||||
|
||||
/**
|
||||
* Reset offers amount sum for owner to 0
|
||||
*
|
||||
* @param {String} account - owner's account address
|
||||
* @return {Amount}
|
||||
*/
|
||||
OrderBook.prototype.resetOwnerOfferTotal = function(account) {
|
||||
var amount;
|
||||
|
||||
if (this._currencyGets.is_native()) {
|
||||
amount = Amount.from_json('0');
|
||||
} else {
|
||||
amount = Amount.from_json('0' + OrderBook.IOU_SUFFIX);
|
||||
}
|
||||
|
||||
this._ownerOffersTotal[account] = amount;
|
||||
|
||||
return amount;
|
||||
};
|
||||
|
||||
/**
|
||||
* Compute adjusted balance that would be left after issuer's transfer fee is deducted
|
||||
*
|
||||
@@ -545,86 +565,59 @@ OrderBook.prototype.requestTransferRate = function(callback) {
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Set funded amount on offer with its owner's cached funds
|
||||
*
|
||||
* Offers have taker_gets_funded, reflecting the amount this account can afford to offer.
|
||||
* Offers have is_fully_funded, indicating whether these funds are sufficient for the offer placed.
|
||||
* Offers have taker_gets_funded, reflecting the amount this account can afford to offer.
|
||||
* Offers have taker_pays_funded, reflecting an adjusted TakerPays in the case of a partially funded order.
|
||||
*
|
||||
* @param {Object} offer
|
||||
* @param {String} funds
|
||||
* @return offer
|
||||
*/
|
||||
|
||||
OrderBook.prototype.setOfferFundedAmount = function(offer) {
|
||||
assert.strictEqual(typeof offer, 'object', 'Offer is invalid');
|
||||
assert(!isNaN(this.getOwnerFunds(offer.Account)), 'Funds is invalid');
|
||||
|
||||
var fundedAmount = this.getOwnerFunds(offer.Account);
|
||||
var fundedAmount = Amount.from_json(
|
||||
this._currencyGets.is_native()
|
||||
? this.getOwnerFunds(offer.Account)
|
||||
: this.getOwnerFunds(offer.Account) + OrderBook.IOU_SUFFIX
|
||||
);
|
||||
var previousOfferSum = this.getOwnerOfferTotal(offer.Account);
|
||||
var currentOfferSum = Amount.from_json(offer.TakerGets).add(previousOfferSum);
|
||||
|
||||
offer.owner_funds = this.getUnadjustedOwnerFunds(offer.Account);
|
||||
|
||||
if (fundedAmount === '0') {
|
||||
offer.taker_gets_funded = '0';
|
||||
offer.taker_pays_funded = '0';
|
||||
offer.is_fully_funded = false;
|
||||
return offer;
|
||||
}
|
||||
|
||||
var offerSum = this.getOwnerOfferTotal(offer.Account);
|
||||
|
||||
if (offerSum.is_zero()) {
|
||||
offerSum = Amount.from_json(offer.TakerGets);
|
||||
}
|
||||
|
||||
offer.is_fully_funded = Amount.from_json(
|
||||
this._currencyGets.is_native()
|
||||
? fundedAmount
|
||||
: fundedAmount + OrderBook.IOU_SUFFIX
|
||||
).compareTo(offerSum) >= 0;
|
||||
offer.is_fully_funded = fundedAmount.compareTo(currentOfferSum) >= 0;
|
||||
|
||||
if (offer.is_fully_funded) {
|
||||
offer.taker_gets_funded = Amount.from_json(offer.TakerGets).to_text();
|
||||
offer.taker_pays_funded = Amount.from_json(offer.TakerPays).to_text();
|
||||
return offer;
|
||||
}
|
||||
} else if (previousOfferSum.compareTo(fundedAmount) < 0) {
|
||||
var takerGetsFunded = fundedAmount.subtract(previousOfferSum);
|
||||
|
||||
var isOfferGetsExceeded = Amount.from_json(
|
||||
this._currencyGets.is_native()
|
||||
? fundedAmount
|
||||
: fundedAmount + OrderBook.IOU_SUFFIX
|
||||
)
|
||||
.compareTo(offer.TakerGets) > 0;
|
||||
var takerPaysValue = this._currencyPays.is_native()
|
||||
? offer.TakerPays
|
||||
: offer.TakerPays.value;
|
||||
var takerPays = Amount.from_json(takerPaysValue + OrderBook.IOU_SUFFIX);
|
||||
|
||||
if (isOfferGetsExceeded) {
|
||||
offer.taker_gets_funded = Amount.from_json(offer.TakerGets).to_text();
|
||||
var takerGetsValue = this._currencyGets.is_native()
|
||||
? offer.TakerGets
|
||||
: offer.TakerGets.value;
|
||||
var takerGets = Amount.from_json(takerGetsValue + OrderBook.IOU_SUFFIX);
|
||||
|
||||
var quality = takerPays.divide(takerGets);
|
||||
var takerPaysFunded = Amount.from_json(takerGetsFunded.to_text() + OrderBook.IOU_SUFFIX).multiply(quality);
|
||||
|
||||
offer.taker_gets_funded = takerGetsFunded.to_text();
|
||||
offer.taker_pays_funded = this._currencyPays.is_native() ? String(parseInt(takerPaysFunded.to_json().value, 10)) : takerPaysFunded.to_json().value;
|
||||
} else {
|
||||
offer.taker_gets_funded = fundedAmount;
|
||||
offer.taker_gets_funded = '0';
|
||||
offer.taker_pays_funded = '0';
|
||||
}
|
||||
|
||||
var takerPaysValue = this._currencyPays.is_native()
|
||||
? offer.TakerPays
|
||||
: offer.TakerPays.value;
|
||||
var takerPays = Amount.from_json(takerPaysValue + OrderBook.IOU_SUFFIX);
|
||||
var takerGets = Amount.from_json(offerSum);
|
||||
var fundedPays = Amount.from_json(fundedAmount + OrderBook.IOU_SUFFIX);
|
||||
var rate = takerPays.divide(takerGets);
|
||||
|
||||
fundedPays = fundedPays.multiply(rate);
|
||||
|
||||
if (fundedPays.compareTo(takerPays) < 0) {
|
||||
if (this._currencyPays.is_native()) {
|
||||
fundedPays = String(parseInt(fundedPays.to_json().value, 10));
|
||||
} else {
|
||||
fundedPays = fundedPays.to_json().value;
|
||||
}
|
||||
} else {
|
||||
fundedPays = takerPays.to_json().value;
|
||||
}
|
||||
|
||||
offer.taker_pays_funded = fundedPays;
|
||||
|
||||
return offer;
|
||||
};
|
||||
|
||||
@@ -752,7 +745,6 @@ OrderBook.prototype.updateFundedAmounts = function(transaction) {
|
||||
|
||||
OrderBook.prototype.updateOwnerOffersFundedAmount = function(account) {
|
||||
assert(UInt160.is_valid(account), 'Account is invalid');
|
||||
assert(!isNaN(this.getOwnerFunds(account)), 'Funded amount is invalid');
|
||||
|
||||
var self = this;
|
||||
|
||||
@@ -760,6 +752,8 @@ OrderBook.prototype.updateOwnerOffersFundedAmount = function(account) {
|
||||
log.info('updating offer funds', this._key, account, this.getOwnerFunds(account));
|
||||
}
|
||||
|
||||
this.resetOwnerOfferTotal(account);
|
||||
|
||||
_.each(this._offers, function (offer) {
|
||||
if (offer.Account !== account) {
|
||||
return;
|
||||
@@ -767,13 +761,19 @@ OrderBook.prototype.updateOwnerOffersFundedAmount = function(account) {
|
||||
|
||||
// Save a copy of the old offer so we can show how the offer has changed
|
||||
var previousOffer = extend({}, offer);
|
||||
var previousFundedGets = Amount.from_json(
|
||||
offer.taker_gets_funded + OrderBook.IOU_SUFFIX
|
||||
);
|
||||
var previousFundedGetsValue = offer.taker_gets_funded;
|
||||
var previousFundedGets;
|
||||
|
||||
if (_.isString(previousFundedGetsValue)) {
|
||||
previousFundedGets = Amount.from_json(
|
||||
offer.taker_gets_funded + OrderBook.IOU_SUFFIX
|
||||
);
|
||||
}
|
||||
|
||||
self.setOfferFundedAmount(offer);
|
||||
self.addOwnerOfferTotal(offer.Account, offer.TakerGets);
|
||||
|
||||
var areFundsChanged = !previousFundedGets.equals(
|
||||
var areFundsChanged = previousFundedGets && !previousFundedGets.equals(
|
||||
Amount.from_json(offer.taker_gets_funded + OrderBook.IOU_SUFFIX)
|
||||
);
|
||||
|
||||
@@ -828,26 +828,26 @@ OrderBook.prototype.notify = function(transaction) {
|
||||
);
|
||||
|
||||
function handleNode(node) {
|
||||
var isDeletedNode = node.nodeType === 'DeletedNode';
|
||||
var isOfferCancel = transaction.transaction.TransactionType === 'OfferCancel';
|
||||
|
||||
switch (node.nodeType) {
|
||||
case 'DeletedNode':
|
||||
case 'ModifiedNode':
|
||||
self.modifyOffer(node);
|
||||
self.deleteOffer(node, isOfferCancel);
|
||||
|
||||
// We don't want to count an OfferCancel as a trade
|
||||
if (!isOfferCancel) {
|
||||
if (isDeletedNode) {
|
||||
takerGetsTotal = takerGetsTotal.add(node.fieldsFinal.TakerGets);
|
||||
takerPaysTotal = takerPaysTotal.add(node.fieldsFinal.TakerPays);
|
||||
} else {
|
||||
takerGetsTotal = takerGetsTotal.add(node.fieldsPrev.TakerGets).subtract(node.fieldsFinal.TakerGets);
|
||||
takerPaysTotal = takerPaysTotal.add(node.fieldsPrev.TakerPays).subtract(node.fieldsFinal.TakerPays);
|
||||
}
|
||||
takerGetsTotal = takerGetsTotal.add(node.fieldsFinal.TakerGets);
|
||||
takerPaysTotal = takerPaysTotal.add(node.fieldsFinal.TakerPays);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'ModifiedNode':
|
||||
self.modifyOffer(node);
|
||||
|
||||
takerGetsTotal = takerGetsTotal.add(node.fieldsPrev.TakerGets).subtract(node.fieldsFinal.TakerGets);
|
||||
takerPaysTotal = takerPaysTotal.add(node.fieldsPrev.TakerPays).subtract(node.fieldsFinal.TakerPays);
|
||||
break;
|
||||
|
||||
case 'CreatedNode':
|
||||
self.setOwnerFunds(node.fields.Account, transaction.transaction.owner_funds);
|
||||
self.insertOffer(node);
|
||||
@@ -867,6 +867,9 @@ OrderBook.prototype.notify = function(transaction) {
|
||||
/**
|
||||
* Insert an offer into the orderbook
|
||||
*
|
||||
* NOTE: We *MUST* update offers' funded amounts when a new offer is placed because funds go
|
||||
* to the highest quality offers first.
|
||||
*
|
||||
* @param {Object} node - Offer node
|
||||
*/
|
||||
|
||||
@@ -877,14 +880,9 @@ OrderBook.prototype.insertOffer = function(node) {
|
||||
|
||||
var offer = OrderBook.offerRewrite(node.fields);
|
||||
|
||||
this.addOwnerOfferTotal(offer.Account, offer.TakerGets);
|
||||
this.incrementOwnerOfferCount(offer.Account);
|
||||
|
||||
offer.LedgerEntryType = node.entryType;
|
||||
offer.index = node.ledgerIndex;
|
||||
|
||||
this.setOfferFundedAmount(offer);
|
||||
|
||||
var DATE_REF = {
|
||||
reference_date: new Date()
|
||||
};
|
||||
@@ -894,23 +892,30 @@ OrderBook.prototype.insertOffer = function(node) {
|
||||
offer.TakerPays
|
||||
).ratio_human(offer.TakerGets, DATE_REF);
|
||||
|
||||
for (var i=0, offersLength=this._offers.length; i<offersLength + 1; i++) {
|
||||
if (i === offersLength) {
|
||||
this._offers.push(offer);
|
||||
} else {
|
||||
var currentOffer = this._offers[i];
|
||||
var originalLength = this._offers.length;
|
||||
|
||||
var priceItem = Amount.from_json(
|
||||
currentOffer.TakerPays
|
||||
).ratio_human(currentOffer.TakerGets, DATE_REF);
|
||||
for (var i=0; i<originalLength; i++) {
|
||||
var currentOffer = this._offers[i];
|
||||
|
||||
if (price.compareTo(priceItem) <= 0) {
|
||||
this._offers.splice(i, 0, offer);
|
||||
break;
|
||||
}
|
||||
var priceItem = Amount.from_json(
|
||||
currentOffer.TakerPays
|
||||
).ratio_human(currentOffer.TakerGets, DATE_REF);
|
||||
|
||||
if (price.compareTo(priceItem) <= 0) {
|
||||
this._offers.splice(i, 0, offer);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (this._offers.length === originalLength) {
|
||||
this._offers.push(offer);
|
||||
}
|
||||
|
||||
this.incrementOwnerOfferCount(offer.Account);
|
||||
|
||||
this.updateOwnerOffersFundedAmount(offer.Account);
|
||||
|
||||
this.emit('offer_added', offer);
|
||||
};
|
||||
|
||||
@@ -922,41 +927,59 @@ OrderBook.prototype.insertOffer = function(node) {
|
||||
|
||||
OrderBook.prototype.modifyOffer = function(node) {
|
||||
if (this._remote.trace) {
|
||||
if (node.nodeType === 'DeletedNode') {
|
||||
log.info('deleting offer', this._key, node.fields);
|
||||
} else {
|
||||
log.info('modifying offer', this._key, node.fields);
|
||||
}
|
||||
log.info('modifying offer', this._key, node.fields);
|
||||
}
|
||||
|
||||
for (var i=0, l=this._offers.length; i<l; i++) {
|
||||
for (var i=0; i<this._offers.length; i++) {
|
||||
var offer = this._offers[i];
|
||||
|
||||
if (offer.index === node.ledgerIndex) {
|
||||
if (node.nodeType === 'DeletedNode') {
|
||||
// Remove offer amount from sum for account
|
||||
this.subtractOwnerOfferTotal(offer.Account, offer.TakerGets);
|
||||
this._offers.splice(i, 1);
|
||||
this.decrementOwnerOfferCount(offer.Account);
|
||||
|
||||
this.emit('offer_removed', offer);
|
||||
} else {
|
||||
// TODO: This assumes no fields are deleted, which is
|
||||
// probably a safe assumption, but should be checked.
|
||||
var previousOffer = extend({}, offer);
|
||||
extend(offer, node.fieldsFinal);
|
||||
|
||||
// Remove offer amount from sum for account
|
||||
this.subtractOwnerOfferTotal(offer.Account, previousOffer.TakerGets);
|
||||
// Add offer amount from sum for account
|
||||
this.addOwnerOfferTotal(offer.Account, offer.TakerGets);
|
||||
|
||||
this.emit('offer_changed', previousOffer, offer);
|
||||
}
|
||||
// TODO: This assumes no fields are deleted, which is
|
||||
// probably a safe assumption, but should be checked.
|
||||
extend(offer, node.fieldsFinal);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this.updateOwnerOffersFundedAmount(node.fields.Account);
|
||||
};
|
||||
|
||||
/**
|
||||
* Delete an existing offer in the orderbook
|
||||
*
|
||||
* NOTE: We only update funded amounts when the node comes from an OfferCancel
|
||||
* transaction because when offers are deleted, it frees up funds to fund
|
||||
* other existing offers in the book
|
||||
*
|
||||
* @param {Object} node - Offer node
|
||||
* @param {Boolean} isOfferCancel - whether node came from an OfferCancel transaction
|
||||
*/
|
||||
|
||||
OrderBook.prototype.deleteOffer = function(node, isOfferCancel) {
|
||||
if (this._remote.trace) {
|
||||
log.info('deleting offer', this._key, node.fields);
|
||||
}
|
||||
|
||||
for (var i=0; i<this._offers.length; i++) {
|
||||
var offer = this._offers[i];
|
||||
|
||||
if (offer.index === node.ledgerIndex) {
|
||||
// Remove offer amount from sum for account
|
||||
this.subtractOwnerOfferTotal(offer.Account, offer.TakerGets);
|
||||
|
||||
this._offers.splice(i, 1);
|
||||
this.decrementOwnerOfferCount(offer.Account);
|
||||
|
||||
this.emit('offer_removed', offer);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isOfferCancel) {
|
||||
this.updateOwnerOffersFundedAmount(node.fields.Account);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -971,10 +994,7 @@ OrderBook.prototype.setOffers = function(offers) {
|
||||
|
||||
var self = this;
|
||||
|
||||
// Must calculate offer totals before we can set funded amounts on offers
|
||||
_.each(offers, function (rawOffer) {
|
||||
self.addOwnerOfferTotal(rawOffer.Account, rawOffer.TakerGets);
|
||||
});
|
||||
this.resetCache();
|
||||
|
||||
var newOffers = _.map(offers, function (rawOffer) {
|
||||
var offer = OrderBook.offerRewrite(rawOffer);
|
||||
@@ -985,7 +1005,9 @@ OrderBook.prototype.setOffers = function(offers) {
|
||||
}
|
||||
|
||||
self.incrementOwnerOfferCount(offer.Account);
|
||||
|
||||
self.setOfferFundedAmount(offer);
|
||||
self.addOwnerOfferTotal(rawOffer.Account, rawOffer.TakerGets);
|
||||
|
||||
return offer;
|
||||
});
|
||||
|
||||
368
test/fixtures/orderbook.js
vendored
368
test/fixtures/orderbook.js
vendored
@@ -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 = [
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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'
|
||||
}
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user