Fix/refactor account_root request

This commit is contained in:
wltsmrz
2013-11-20 12:32:03 -08:00
parent dfedc86fb7
commit bcd5e38976
2 changed files with 56 additions and 57 deletions

View File

@@ -167,7 +167,7 @@ function Remote(opts, trace) {
opts.servers.forEach(function(server) { opts.servers.forEach(function(server) {
var pool = Number(server.pool) || 1; var pool = Number(server.pool) || 1;
while (pool--) { self.add_server(server); }; while (pool--) { self.addServer(server); };
}); });
// This is used to remove Node EventEmitter warnings // This is used to remove Node EventEmitter warnings
@@ -229,14 +229,6 @@ Remote.flags = {
} }
}; };
function isTemMalformed(engine_result_code) {
return (engine_result_code >= -299 && engine_result_code < 199);
};
function isTefFailure(engine_result_code) {
return (engine_result_code >= -299 && engine_result_code < 199);
};
Remote.from_config = function(obj, trace) { Remote.from_config = function(obj, trace) {
var serverConfig = typeof obj === 'string' ? config.servers[obj] : obj; var serverConfig = typeof obj === 'string' ? config.servers[obj] : obj;
@@ -561,7 +553,7 @@ Remote.prototype._getServer = function() {
Remote.prototype.request = function(request) { Remote.prototype.request = function(request) {
if (typeof request === 'string') { if (typeof request === 'string') {
if (!/^request_/.test(request)) request = 'request_' + request; if (!/^request_/.test(request)) request = 'request_' + request;
if (this[request]) { if (typeof this[request] === 'function') {
var args = Array.prototype.slice.call(arguments, 1); var args = Array.prototype.slice.call(arguments, 1);
return this[request].apply(this, args); return this[request].apply(this, args);
} else { } else {
@@ -577,7 +569,7 @@ Remote.prototype.request = function(request) {
} else if (request.server === null) { } else if (request.server === null) {
request.emit('error', new Error('Server does not exist')); request.emit('error', new Error('Server does not exist'));
} else { } else {
var server = request.server || this._get_server(); var server = request.server || this._getServer();
if (server) { if (server) {
server.request(request); server.request(request);
} else { } else {
@@ -1045,56 +1037,64 @@ Remote.prototype.ledgerAccept = function(callback) {
return this; return this;
}; };
Remote.accountRootRequest = function(account, ledger, callback) { Remote.accountRootRequest = function(type, responseFilter, account, ledger, callback) {
if (typeof account === 'object') { if (typeof account === 'object') {
callback = ledger; callback = ledger;
ledger = account.ledger; ledger = account.ledger;
account = account.account; account = account.account;
} }
var lastArg = arguments[arguments.length - 1];
if (typeof lastArg === 'function') {
callback = lastArg;
}
var request = this.requestLedgerEntry('account_root'); var request = this.requestLedgerEntry('account_root');
request.accountRoot(account); request.accountRoot(account);
request.ledgerChoose(ledger); request.ledgerChoose(ledger);
request.once('success', function(message) {
request.emit(type, responseFilter(message));
});
request.callback(callback, type);
return request; return request;
}; };
// Return a request to refresh the account balance. // Return a request to refresh the account balance.
Remote.prototype.requestAccountBalance = function(account, ledger, callback) { Remote.prototype.requestAccountBalance = function(account, ledger, callback) {
function responseFilter(message) {
return Amount.from_json(message.node.Balance);
};
var request = Remote.accountRootRequest.apply(this, arguments); var args = Array.prototype.concat.apply(['account_balance', responseFilter], arguments);
var request = Remote.accountRootRequest.apply(this, args);
request.once('success', function(message) {
request.emit('account_balance', Amount.from_json(message.node.Balance));
});
request.callback(callback, 'account_balance');
return request; return request;
}; };
// Return a request to return the account flags. // Return a request to return the account flags.
Remote.prototype.requestAccountFlags = function(account, ledger, callback) { Remote.prototype.requestAccountFlags = function(account, ledger, callback) {
var request = Remote.accountRootRequest.apply(this, arguments); function responseFilter(message) {
return message.node.Flags;
};
request.once('success', function(message) { var args = Array.prototype.concat.apply(['account_flags', responseFilter], arguments);
request.emit('account_flags', message.node.Flags); var request = Remote.accountRootRequest.apply(this, args);
});
request.callback(callback, 'account_flags');
return request; return request;
}; };
// Return a request to emit the owner count. // Return a request to emit the owner count.
Remote.prototype.requestOwnerCount = function(account, ledger, callback) { Remote.prototype.requestOwnerCount = function(account, ledger, callback) {
var request = Remote.accountRootRequest.apply(this, arguments); function responseFilter(message) {
return message.node.OwnerCount;
};
request.once('success', function(message) { var args = Array.prototype.concat.apply(['owner_count', responseFilter], arguments);
request.emit('owner_count', message.node.OwnerCount); var request = Remote.accountRootRequest.apply(this, args);
});
request.callback(callback, 'owner_count');
return request; return request;
}; };
@@ -1389,8 +1389,7 @@ Remote.prototype.requestUnlAdd = function(addr, comment, callback) {
if (comment) { if (comment) {
// note is not specified anywhere, should remove? // note is not specified anywhere, should remove?
var note = undefined; request.message.comment = void(0);
request.message.comment = note;
} }
request.callback(callback); request.callback(callback);

View File

@@ -269,38 +269,42 @@ Transaction.prototype.destinationTag = function (tag) {
}; };
Transaction._pathRewrite = function (path) { Transaction._pathRewrite = function (path) {
var path_new = path.map(function(node) { var pathProperties = {
var node_new = { }; account: UInt160.json_rewrite,
issuer: UInt160.json_rewrite,
if (node.hasOwnProperty('account')) { currency: Currency.json_rewrite
node_new.account = UInt160.json_rewrite(node.account);
} }
if (node.hasOwnProperty('issuer')) { return path.map(function(node) {
node_new.issuer = UInt160.json_rewrite(node.issuer); var newNode = { };
for (var prop in node) {
if (pathProperties.hasOwnProperty(prop)) {
newNode[prop] = pathProperties[prop](node[prop]);
}
} }
if (node.hasOwnProperty('currency')) { return newNode
node_new.currency = Currency.json_rewrite(node.currency);
}
return node_new;
}); });
return path_new;
}; };
Transaction.prototype.pathAdd = function (path) { Transaction.prototype.pathAdd = function (path) {
this.tx_json.Paths = (this.tx_json.Paths || []).push(Transaction._pathRewrite(path)); if (Array.isArray(path)) {
this.tx_json.Paths = this.tx_json.Paths || [];
this.tx_json.Paths.push(Transaction._pathRewrite(path));
}
return this; return this;
}; };
// --> paths: undefined or array of path // --> paths: undefined or array of path
// A path is an array of objects containing some combination of: account, currency, issuer // // A path is an array of objects containing some combination of: account, currency, issuer
Transaction.prototype.paths = function (paths) { Transaction.prototype.paths = function (paths) {
if (Array.isArray(paths)) {
for (var i=0, l=paths.length; i<l; i++) { for (var i=0, l=paths.length; i<l; i++) {
this.pathAdd(paths[i]); this.pathAdd(paths[i]);
} }
}
return this; return this;
}; };
@@ -326,7 +330,7 @@ Transaction.prototype.sourceTag = function (tag) {
}; };
// --> rate: In billionths. // --> rate: In billionths.
Transaction.prototype.transfer_rate = function (rate) { Transaction.prototype.transferRate = function (rate) {
this.tx_json.TransferRate = Number(rate); this.tx_json.TransferRate = Number(rate);
if (this.tx_json.TransferRate < 1e9) { if (this.tx_json.TransferRate < 1e9) {
@@ -361,10 +365,6 @@ Transaction.prototype.setFlags = function (flags) {
return this; return this;
}; };
//
// Transactions
//
Transaction.prototype._accountSecret = function (account) { Transaction.prototype._accountSecret = function (account) {
// Fill in secret from remote, if available. // Fill in secret from remote, if available.
return this.remote.secrets[account]; return this.remote.secrets[account];