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) {
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
@@ -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) {
var serverConfig = typeof obj === 'string' ? config.servers[obj] : obj;
@@ -561,7 +553,7 @@ Remote.prototype._getServer = function() {
Remote.prototype.request = function(request) {
if (typeof request === 'string') {
if (!/^request_/.test(request)) request = 'request_' + request;
if (this[request]) {
if (typeof this[request] === 'function') {
var args = Array.prototype.slice.call(arguments, 1);
return this[request].apply(this, args);
} else {
@@ -577,7 +569,7 @@ Remote.prototype.request = function(request) {
} else if (request.server === null) {
request.emit('error', new Error('Server does not exist'));
} else {
var server = request.server || this._get_server();
var server = request.server || this._getServer();
if (server) {
server.request(request);
} else {
@@ -1045,56 +1037,64 @@ Remote.prototype.ledgerAccept = function(callback) {
return this;
};
Remote.accountRootRequest = function(account, ledger, callback) {
Remote.accountRootRequest = function(type, responseFilter, account, ledger, callback) {
if (typeof account === 'object') {
callback = ledger;
ledger = account.ledger;
account = account.account;
}
var lastArg = arguments[arguments.length - 1];
if (typeof lastArg === 'function') {
callback = lastArg;
}
var request = this.requestLedgerEntry('account_root');
request.accountRoot(account);
request.ledgerChoose(ledger);
request.once('success', function(message) {
request.emit(type, responseFilter(message));
});
request.callback(callback, type);
return request;
};
// Return a request to refresh the account balance.
Remote.prototype.requestAccountBalance = function(account, ledger, callback) {
function responseFilter(message) {
return Amount.from_json(message.node.Balance);
};
var request = Remote.accountRootRequest.apply(this, arguments);
request.once('success', function(message) {
request.emit('account_balance', Amount.from_json(message.node.Balance));
});
request.callback(callback, 'account_balance');
var args = Array.prototype.concat.apply(['account_balance', responseFilter], arguments);
var request = Remote.accountRootRequest.apply(this, args);
return request;
};
// Return a request to return the account flags.
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) {
request.emit('account_flags', message.node.Flags);
});
request.callback(callback, 'account_flags');
var args = Array.prototype.concat.apply(['account_flags', responseFilter], arguments);
var request = Remote.accountRootRequest.apply(this, args);
return request;
};
// Return a request to emit the owner count.
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) {
request.emit('owner_count', message.node.OwnerCount);
});
request.callback(callback, 'owner_count');
var args = Array.prototype.concat.apply(['owner_count', responseFilter], arguments);
var request = Remote.accountRootRequest.apply(this, args);
return request;
};
@@ -1389,8 +1389,7 @@ Remote.prototype.requestUnlAdd = function(addr, comment, callback) {
if (comment) {
// note is not specified anywhere, should remove?
var note = undefined;
request.message.comment = note;
request.message.comment = void(0);
}
request.callback(callback);

View File

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