mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-12-01 01:25:48 +00:00
Fix orderbook notification while disconnected
This commit is contained in:
@@ -62,9 +62,9 @@ function OrderBook(remote,
|
|||||||
this._ownerFunds = {};
|
this._ownerFunds = {};
|
||||||
this._ownerOffersTotal = {};
|
this._ownerOffersTotal = {};
|
||||||
|
|
||||||
// We consider ourselves synchronized if we have a current
|
// We consider ourselves synced if we have a current
|
||||||
// copy of the offers, we are online and subscribed to updates
|
// copy of the offers, we are online and subscribed to updates
|
||||||
this._synchronized = false;
|
this._synced = false;
|
||||||
|
|
||||||
// Transfer rate of the taker gets currency issuer
|
// Transfer rate of the taker gets currency issuer
|
||||||
this._issuerTransferRate = null;
|
this._issuerTransferRate = null;
|
||||||
@@ -278,7 +278,6 @@ OrderBook.prototype.requestOffers = function(callback=function() {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.setOffers(res.offers);
|
self.setOffers(res.offers);
|
||||||
self._synchronized = true;
|
|
||||||
self.notifyDirectOffersChanged();
|
self.notifyDirectOffersChanged();
|
||||||
|
|
||||||
callback(null, self._offers);
|
callback(null, self._offers);
|
||||||
@@ -293,7 +292,8 @@ OrderBook.prototype.requestOffers = function(callback=function() {}) {
|
|||||||
callback(err);
|
callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
const request = this._remote.requestBookOffers(this.toJSON());
|
const requestOptions = _.merge({}, this.toJSON(), {ledger: 'validated'});
|
||||||
|
const request = this._remote.requestBookOffers(requestOptions);
|
||||||
request.once('success', handleOffers);
|
request.once('success', handleOffers);
|
||||||
request.once('error', handleError);
|
request.once('error', handleError);
|
||||||
request.request();
|
request.request();
|
||||||
@@ -408,7 +408,7 @@ OrderBook.prototype.resetCache = function() {
|
|||||||
this._ownerFunds = {};
|
this._ownerFunds = {};
|
||||||
this._ownerOffersTotal = {};
|
this._ownerOffersTotal = {};
|
||||||
this._offerCounts = {};
|
this._offerCounts = {};
|
||||||
this._synchronized = false;
|
this._synced = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -754,9 +754,15 @@ OrderBook.prototype.updateFundedAmounts = function(transaction) {
|
|||||||
log.info('waiting for transfer rate');
|
log.info('waiting for transfer rate');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.requestTransferRate(function() {
|
this.requestTransferRate(function(err) {
|
||||||
// Defer until transfer rate is requested
|
if (err) {
|
||||||
self.updateFundedAmounts(transaction);
|
log.error(
|
||||||
|
'Failed to request transfer rate, will not update funded amounts: '
|
||||||
|
+ err.toString());
|
||||||
|
} else {
|
||||||
|
// Defer until transfer rate is requested
|
||||||
|
self.updateFundedAmounts(transaction);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -842,7 +848,7 @@ OrderBook.prototype.updateOwnerOffersFundedAmount = function(account) {
|
|||||||
OrderBook.prototype.notify = function(transaction) {
|
OrderBook.prototype.notify = function(transaction) {
|
||||||
const self = this;
|
const self = this;
|
||||||
|
|
||||||
if (!this._subscribed) {
|
if (!(this._subscribed && this._synced)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1083,6 +1089,7 @@ OrderBook.prototype.setOffers = function(offers) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this._offers = newOffers;
|
this._offers = newOffers;
|
||||||
|
this._synced = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1101,7 +1108,7 @@ OrderBook.prototype.offers =
|
|||||||
OrderBook.prototype.getOffers = function(callback) {
|
OrderBook.prototype.getOffers = function(callback) {
|
||||||
assert.strictEqual(typeof callback, 'function', 'Callback missing');
|
assert.strictEqual(typeof callback, 'function', 'Callback missing');
|
||||||
|
|
||||||
if (this._synchronized) {
|
if (this._synced) {
|
||||||
callback(null, this._offers);
|
callback(null, this._offers);
|
||||||
} else {
|
} else {
|
||||||
this.once('model', function(m) {
|
this.once('model', function(m) {
|
||||||
|
|||||||
63
test/fixtures/orderbook.js
vendored
63
test/fixtures/orderbook.js
vendored
@@ -1,10 +1,10 @@
|
|||||||
/*eslint-disable max-len */
|
/*eslint-disable max-len, no-param-reassign*/
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var _ = require('lodash');
|
const _ = require('lodash');
|
||||||
var addresses = require('./addresses');
|
const addresses = require('./addresses');
|
||||||
var Meta = require('ripple-lib').Meta;
|
const Meta = require('ripple-lib').Meta;
|
||||||
|
|
||||||
module.exports.FIAT_BALANCE = '10';
|
module.exports.FIAT_BALANCE = '10';
|
||||||
module.exports.NATIVE_BALANCE = '55';
|
module.exports.NATIVE_BALANCE = '55';
|
||||||
@@ -816,7 +816,7 @@ module.exports.transactionWithCreatedOffer = function(options) {
|
|||||||
amount: '1.9951'
|
amount: '1.9951'
|
||||||
});
|
});
|
||||||
|
|
||||||
var meta = new Meta({
|
const meta = new Meta({
|
||||||
AffectedNodes: [
|
AffectedNodes: [
|
||||||
{
|
{
|
||||||
CreatedNode: {
|
CreatedNode: {
|
||||||
@@ -856,7 +856,7 @@ module.exports.transactionWithDeletedOffer = function(options) {
|
|||||||
transaction_type: 'OfferCreate'
|
transaction_type: 'OfferCreate'
|
||||||
});
|
});
|
||||||
|
|
||||||
var meta = new Meta({
|
const meta = new Meta({
|
||||||
AffectedNodes: [
|
AffectedNodes: [
|
||||||
{
|
{
|
||||||
DeletedNode: {
|
DeletedNode: {
|
||||||
@@ -895,8 +895,53 @@ module.exports.transactionWithDeletedOffer = function(options) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.transactionWithDeletedOfferR = function(options) {
|
||||||
|
options = options || {};
|
||||||
|
_.defaults(options, {
|
||||||
|
transaction_type: 'OfferCreate'
|
||||||
|
});
|
||||||
|
|
||||||
|
const meta = new Meta({
|
||||||
|
AffectedNodes: [
|
||||||
|
{
|
||||||
|
DeletedNode: {
|
||||||
|
FinalFields: {
|
||||||
|
Account: addresses.ACCOUNT,
|
||||||
|
BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517',
|
||||||
|
BookNode: '0000000000000000',
|
||||||
|
Expiration: 477086871,
|
||||||
|
Flags: 131072,
|
||||||
|
OwnerNode: '0000000000000979',
|
||||||
|
PreviousTxnID: 'DDD2AB60A2AA1690A6CB99B094BFD2E39A81AFF2AA91B5E4049D2C96A4BC8EBA',
|
||||||
|
PreviousTxnLgrSeq: 11674760,
|
||||||
|
Sequence: 85006,
|
||||||
|
TakerPays: {
|
||||||
|
currency: 'USD',
|
||||||
|
issuer: addresses.ISSUER,
|
||||||
|
value: module.exports.TAKER_GETS
|
||||||
|
},
|
||||||
|
TakerGets: module.exports.TAKER_PAYS
|
||||||
|
},
|
||||||
|
LedgerEntryType: 'Offer',
|
||||||
|
LedgerIndex: 'B6BC3B0F87976370EE11F5575593FE63AA5DC1D602830DC96F04B2D597F044BF'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
meta.getAffectedBooks();
|
||||||
|
|
||||||
|
return {
|
||||||
|
mmeta: meta,
|
||||||
|
transaction: {
|
||||||
|
TransactionType: options.transaction_type,
|
||||||
|
owner_funds: '2010.027702881682'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
module.exports.transactionWithModifiedOffer = function() {
|
module.exports.transactionWithModifiedOffer = function() {
|
||||||
var meta = new Meta({
|
const meta = new Meta({
|
||||||
AffectedNodes: module.exports.MODIFIED_NODES.slice(0, 1)
|
AffectedNodes: module.exports.MODIFIED_NODES.slice(0, 1)
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -912,7 +957,7 @@ module.exports.transactionWithModifiedOffer = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
module.exports.transactionWithModifiedOffers = function() {
|
module.exports.transactionWithModifiedOffers = function() {
|
||||||
var meta = new Meta({
|
const meta = new Meta({
|
||||||
AffectedNodes: module.exports.MODIFIED_NODES
|
AffectedNodes: module.exports.MODIFIED_NODES
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -928,7 +973,7 @@ module.exports.transactionWithModifiedOffers = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
module.exports.transactionWithNoNodes = function() {
|
module.exports.transactionWithNoNodes = function() {
|
||||||
var meta = new Meta({
|
const meta = new Meta({
|
||||||
AffectedNodes: []
|
AffectedNodes: []
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -712,6 +712,8 @@ describe('Remote', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
orderbook._subscribed = true;
|
orderbook._subscribed = true;
|
||||||
|
orderbook._synced = true;
|
||||||
|
|
||||||
orderbook.once('transaction', function(t) {
|
orderbook.once('transaction', function(t) {
|
||||||
assert.deepEqual(t.transaction, message.transaction);
|
assert.deepEqual(t.transaction, message.transaction);
|
||||||
assert.deepEqual(t.meta, message.meta);
|
assert.deepEqual(t.meta, message.meta);
|
||||||
|
|||||||
Reference in New Issue
Block a user