From 626164a8ab2648cbe4899dd9fba1884a11f82034 Mon Sep 17 00:00:00 2001 From: wltsmrz Date: Tue, 7 Jan 2014 01:23:54 -0800 Subject: [PATCH] Fix requestTransactionEntry arguments, add flexibility to remote.transaction --- src/js/ripple/remote.js | 68 ++++++++++++++++++++++++++++++------ src/js/ripple/transaction.js | 1 + 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/src/js/ripple/remote.js b/src/js/ripple/remote.js index 8e0431fc..fa0386fa 100644 --- a/src/js/ripple/remote.js +++ b/src/js/ripple/remote.js @@ -513,7 +513,7 @@ Remote.isValidLedgerData = function(ledger) { && (typeof ledger.ledger_time === 'number') && (typeof ledger.reserve_base === 'number') && (typeof ledger.reserve_inc === 'number') - && (typeof ledger.txn_count === 'number') + && (typeof ledger.txn_count === 'number'); }; Remote.isLoadStatus = function(message) { @@ -781,6 +781,14 @@ Remote.prototype.requestTransactionEntry = function(hash, ledger_hash, callback) request.ledgerHash(ledger_hash); break; + case 'number': + request.ledgerIndex(ledger_hash); + break; + + case 'function': + request.callback(ledger_hash); + break; + default: request.ledgerIndex('validated'); callback = ledger_hash; @@ -795,7 +803,7 @@ Remote.prototype.requestTransactionEntry = function(hash, ledger_hash, callback) Remote.prototype.requestTx = function(hash, callback) { var request = new Request(this, 'tx'); - request.message.transaction = hash; + request.message.transaction = hash; request.callback(callback); return request; @@ -1450,17 +1458,57 @@ Remote.prototype.requestConnect = function(ip, port, callback) { return request; }; -Remote.prototype.transaction = function(source, destination, amount, callback) { - var tx = new Transaction(this); +Remote.prototype.createTransaction = +Remote.prototype.transaction = function(source, options, callback) { + var transaction = new Transaction(this); - if (arguments.length >= 3) { - tx = tx.payment(source, destination, amount); - if (typeof callback === 'function') { - tx.submit(callback); - } + var transactionTypes = { + payment: 'payment', + accountset: 'accountSet', + trustset: 'trustSet', + offercreate: 'offerCreate', + offercancel: 'offerCancel', + sign: 'sign' } - return tx; + var transactionType; + + switch (typeof source) { + case 'object': + if (typeof source.type !== 'string') { + throw new Error('Missing transaction type'); + } + + transactionType = transactionTypes[source.type.toLowerCase()]; + + if (!transactionType) { + throw new Error('Invalid transaction type: ' + transactionType); + } + + transaction = transaction[transactionType](source); + break; + + case 'string': + transactionType = source.toLowerCase(); + + if (!transactionType) { + throw new Error('Invalid transaction type: ' + transactionType); + } + + transaction = transaction[transactionType](options); + break; + + default: + throw new Error('Argument must be string or object: ' + source); + } + + var lastArg = arguments[arguments.length - 1]; + + if (typeof lastArg === 'function') { + transaction.submit(lastArg); + } + + return transaction; }; /** diff --git a/src/js/ripple/transaction.js b/src/js/ripple/transaction.js index 0cca25d8..f3f993d3 100644 --- a/src/js/ripple/transaction.js +++ b/src/js/ripple/transaction.js @@ -621,6 +621,7 @@ Transaction.prototype.payment = function(src, dst, amount) { return this; }; +Transaction.prototype.trustSet = Transaction.prototype.rippleLineSet = function(src, limit, quality_in, quality_out) { if (typeof src === 'object') { var options = src;