|
|
@ -46,6 +46,38 @@ class Transactions { |
|
|
|
this.rpcClient = new RpcClient() |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get the transactions for a given array of txids |
|
|
|
* @param {string[]} txids - txids of the transaction to be retrieved |
|
|
|
* @param {boolean} fees - true if fees must be computed, false otherwise |
|
|
|
* @returns {Promise} return an array of transactions (object[]) |
|
|
|
*/ |
|
|
|
async getTransactions(txids, fees) { |
|
|
|
try { |
|
|
|
const rpcCalls = txids.map(txid => { |
|
|
|
return { |
|
|
|
'method': 'getrawtransaction', |
|
|
|
'params': [txid, true] |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
const txs = await this.rpcClient.batch(rpcCalls) |
|
|
|
|
|
|
|
return await util.seriesCall(txs, async tx => { |
|
|
|
if (tx.result == null) { |
|
|
|
Logger.info(` got null for ${txids[tx.id]}`) |
|
|
|
return null |
|
|
|
} else { |
|
|
|
return this._prepareTxResult(tx.result, fees) |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
} catch(e) { |
|
|
|
Logger.error(e, 'Transaction.getTransactions()') |
|
|
|
return Promise.reject(errors.generic.GEN) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get the transaction for a given txid |
|
|
|
* @param {string} txid - txid of the transaction to be retrieved |
|
|
@ -61,7 +93,24 @@ class Transactions { |
|
|
|
|
|
|
|
try { |
|
|
|
const tx = await this.rpcClient.getrawtransaction(txid, true) |
|
|
|
const ret = await this._prepareTxResult(tx) |
|
|
|
// Store the result in cache
|
|
|
|
if (ret.block && ret.block.hash) |
|
|
|
this.txCache.set(txid, ret) |
|
|
|
return ret |
|
|
|
} catch(e) { |
|
|
|
Logger.error(e, 'Transaction.getTransaction()') |
|
|
|
return Promise.reject(errors.generic.GEN) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Formats a transaction object returned by the RPC API |
|
|
|
* @param {object} tx - transaction |
|
|
|
* @param {boolean} fees - true if fees must be computed, false otherwise |
|
|
|
* @returns {Promise} return an array of inputs (object[]) |
|
|
|
*/ |
|
|
|
async _prepareTxResult(tx, fees) { |
|
|
|
const ret = { |
|
|
|
txid: tx.txid, |
|
|
|
size: tx.size, |
|
|
@ -107,18 +156,10 @@ class Transactions { |
|
|
|
ret.vfeerate = Math.round(ret.fees / ret.vsize) |
|
|
|
} |
|
|
|
|
|
|
|
// Store in cache
|
|
|
|
if (ret.block && ret.block.hash) |
|
|
|
this.txCache.set(keyCache, ret) |
|
|
|
|
|
|
|
return ret |
|
|
|
|
|
|
|
} catch(e) { |
|
|
|
Logger.error(e, 'Transaction.getTransaction()') |
|
|
|
return Promise.reject(errors.generic.GEN) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Extract information about the inputs of a transaction |
|
|
|
* @param {object} tx - transaction |
|
|
@ -180,7 +221,6 @@ class Transactions { |
|
|
|
/** |
|
|
|
* Extract information about the outputs of a transaction |
|
|
|
* @param {object} tx - transaction |
|
|
|
* @param {boolean} fees - true if fees must be computed, false otherwise |
|
|
|
* @returns {Promise} return an array of outputs (object[]) |
|
|
|
*/ |
|
|
|
async _getOutputs(tx) { |
|
|
|