Skip to content

Commit 50a1d60

Browse files
committed
fix(Transaction.get()): Handling coinbase txs
1 parent f3e25a0 commit 50a1d60

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

src/psf-slp-indexer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,20 @@ class PsfSlpIndexer {
288288
*/
289289
async tx (txid) {
290290
try {
291+
// console.log('txid: ', txid)
292+
291293
// Handle single address.
292294
if (typeof txid === 'string') {
293295
const response = await axios.post(
294296
`${this.restURL}psf/slp/txid`,
295297
{ txid },
296298
this.axiosOptions
297299
)
300+
// console.log('response: ', response)
301+
298302
return response.data
299303
}
304+
300305
throw new Error('Input txid must be a string.')
301306
} catch (error) {
302307
// console.log('error: ', error)
@@ -316,6 +321,7 @@ class PsfSlpIndexer {
316321

317322
// Check if this txid belongs to a blacklisted token.
318323
const isInBlacklist = await this.checkBlacklist(txid)
324+
// console.log('isInBlacklist: ', isInBlacklist)
319325

320326
// Get the TX Details from the full node.
321327
const txDetails = await this.rawTransaction.getTxData(txid)

src/raw-transactions.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ class RawTransactions {
381381
// Appends the BCH address to the inputs of the transaction.
382382
async getTxData (txid) {
383383
try {
384+
// console.log('getTxData() txid: ', txid)
385+
384386
if (typeof txid !== 'string') {
385387
throw new Error(
386388
'Input to raw-transaction.js/getTxData() must be a string containg a TXID.'
@@ -391,17 +393,24 @@ class RawTransactions {
391393
const txDetails = await this.getRawTransaction(txid, true)
392394
// console.log(`txDetails: ${JSON.stringify(txDetails, null, 2)}`)
393395

394-
const inAddrs = await this._getInputAddrs(txDetails)
395-
// console.log(`inAddrs: ${JSON.stringify(inAddrs, null, 2)}`)
396+
try {
397+
const inAddrs = await this._getInputAddrs(txDetails)
398+
// console.log(`inAddrs: ${JSON.stringify(inAddrs, null, 2)}`)
396399

397-
// Add the input address to the transaction data.
398-
for (let i = 0; i < inAddrs.length; i++) {
399-
txDetails.vin[i].address = inAddrs[i].address
400-
txDetails.vin[i].value = inAddrs[i].value
400+
// Add the input address to the transaction data.
401+
for (let i = 0; i < inAddrs.length; i++) {
402+
txDetails.vin[i].address = inAddrs[i].address
403+
txDetails.vin[i].value = inAddrs[i].value
404+
}
405+
} catch (err) {
406+
// Coinbase transactions will throw an error. Just ignore them and
407+
// pass back the raw transaction data.
408+
/* exit quietly */
401409
}
402410

403411
return txDetails
404412
} catch (error) {
413+
// console.log('error: ', error)
405414
if (error.error) throw new Error(error.error)
406415

407416
// This case handles rate limit errors.

src/transaction.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Transaction {
4141
* })()
4242
*/
4343
async get (txid) {
44+
// console.log('transaction.get() txid: ', txid)
4445
return await this.psfSlpIndexer.tx(txid)
4546
}
4647

test/integration/chains/bchn/transaction-integration.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,14 @@ describe('#Transaction', () => {
2424
assert.property(result.txData, 'vout')
2525
assert.equal(result.txData.isValidSlp, false)
2626
})
27+
28+
it('should handle a coinbase transaction', async () => {
29+
const txid = 'cca1d2dd3a533d2f501448dec03face2cb2814afd59a533a611e9a2909f2302b'
30+
31+
const details = await bchjs.Transaction.get(txid)
32+
// console.log(`details: ${JSON.stringify(details, null, 2)}`)
33+
34+
assert.property(details.txData, 'txid')
35+
})
2736
})
2837
})

0 commit comments

Comments
 (0)