mirror of
https://github.com/Permissionless-Software-Foundation/bch-js.git
synced 2026-09-21 16:51:59 -07:00
Making progress on Transaction.get3()
This commit is contained in:
+19
-19
@@ -437,7 +437,7 @@ class Transaction {
|
||||
}
|
||||
|
||||
// Refactoring code in a more structured way
|
||||
async get3(txid) {
|
||||
async get3 (txid) {
|
||||
try {
|
||||
if (typeof txid !== 'string') {
|
||||
throw new Error(
|
||||
@@ -463,7 +463,7 @@ class Transaction {
|
||||
console.log(`txTokenData: ${JSON.stringify(txTokenData, null, 2)}`)
|
||||
|
||||
// If not a token, return the tx data. Processing is complete.
|
||||
if(!txTokenData) return txDetails
|
||||
if (!txTokenData) return txDetails
|
||||
|
||||
// Mark TX as an SLP tx. This does not mean it's valid, it just means
|
||||
// the OP_RETURN passes a basic check.
|
||||
@@ -492,20 +492,20 @@ class Transaction {
|
||||
console.log(`txTokenData: ${JSON.stringify(txTokenData, null, 2)}`)
|
||||
|
||||
// First output is OP_RETURN, so tokenQty is null.
|
||||
if(i === 0) {
|
||||
if (i === 0) {
|
||||
thisVout.tokenQty = null
|
||||
thisVout.tokenQtyStr = null
|
||||
continue
|
||||
}
|
||||
|
||||
// Non SLP outputs.
|
||||
if(i > txTokenData.amounts.length) {
|
||||
if (i > txTokenData.amounts.length) {
|
||||
thisVout.tokenQty = null
|
||||
thisVout.tokenQtyStr = null
|
||||
continue
|
||||
}
|
||||
|
||||
const rawQty = txTokenData.amounts[i-1]
|
||||
const rawQty = txTokenData.amounts[i - 1]
|
||||
|
||||
// Calculate the real quantity using a BigNumber, then convert it to a
|
||||
// floating point number.
|
||||
@@ -521,7 +521,7 @@ class Transaction {
|
||||
console.log(
|
||||
`thisVout ${i}: ${JSON.stringify(txDetails.vout[i], null, 2)}`
|
||||
)
|
||||
} else if(txTokenData.txType === 'GENESIS' || txTokenData.txType === 'MINT') {
|
||||
} else if (txTokenData.txType === 'GENESIS' || txTokenData.txType === 'MINT') {
|
||||
console.log(`txTokenData: ${JSON.stringify(txTokenData, null, 2)}`)
|
||||
|
||||
let tokenQty = 0 // Default value
|
||||
@@ -543,13 +543,13 @@ class Transaction {
|
||||
thisVout.tokenQtyStr = realQty
|
||||
thisVout.tokenQty = parseFloat(realQty)
|
||||
console.log(`thisVout[${i}]: ${JSON.stringify(thisVout, null, 2)}`)
|
||||
} else if(i === txTokenData.mintBatonVout) {
|
||||
} else if (i === txTokenData.mintBatonVout) {
|
||||
// Optional Mint baton
|
||||
thisVout.tokenQtyStr = "0"
|
||||
thisVout.tokenQtyStr = '0'
|
||||
thisVout.tokenQty = 0
|
||||
thisVout.isMintBaton = true
|
||||
} else {
|
||||
thisVout.tokenQtyStr = "0"
|
||||
thisVout.tokenQtyStr = '0'
|
||||
thisVout.tokenQty = 0
|
||||
}
|
||||
} else {
|
||||
@@ -558,7 +558,7 @@ class Transaction {
|
||||
}
|
||||
|
||||
// Process TX inputs
|
||||
for(let i=0; i < txDetails.vin.length; i++) {
|
||||
for (let i = 0; i < txDetails.vin.length; i++) {
|
||||
const thisVin = txDetails.vin[i]
|
||||
|
||||
const vinTokenData = await this.getTokenInfo(thisVin.txid)
|
||||
@@ -569,14 +569,14 @@ class Transaction {
|
||||
|
||||
// If the input is not a token input, or if the tokenID is not the same,
|
||||
// then mark the token output as null.
|
||||
if(!vinTokenData || !vinTokenIdIsTheSame) {
|
||||
if (!vinTokenData || !vinTokenIdIsTheSame) {
|
||||
thisVin.tokenQty = 0
|
||||
thisVin.tokenQtyStr = "0"
|
||||
thisVin.tokenQtyStr = '0'
|
||||
thisVin.tokenId = null
|
||||
continue
|
||||
}
|
||||
|
||||
if(vinTokenData.txType === 'SEND') {
|
||||
if (vinTokenData.txType === 'SEND') {
|
||||
console.log(`vinTokenData: ${JSON.stringify(vinTokenData, null, 2)}`)
|
||||
|
||||
const tokenQty = vinTokenData.amounts[thisVin.vout - 1]
|
||||
@@ -593,7 +593,7 @@ class Transaction {
|
||||
thisVin.tokenQtyStr = realQty
|
||||
thisVin.tokenQty = parseFloat(realQty)
|
||||
thisVin.tokenId = vinTokenData.tokenId
|
||||
} else if(vinTokenData.txType === 'GENESIS' || vinTokenData.txType === 'MINT') {
|
||||
} else if (vinTokenData.txType === 'GENESIS' || vinTokenData.txType === 'MINT') {
|
||||
console.log(`vinTokenData: ${JSON.stringify(vinTokenData, null, 2)}`)
|
||||
|
||||
let tokenQty = 0 // Default value
|
||||
@@ -615,14 +615,14 @@ class Transaction {
|
||||
thisVin.tokenQtyStr = realQty
|
||||
thisVin.tokenQty = parseFloat(realQty)
|
||||
thisVin.tokenId = vinTokenData.tokenId
|
||||
} else if(thisVin.vout === vinTokenData.mintBatonVout) {
|
||||
} else if (thisVin.vout === vinTokenData.mintBatonVout) {
|
||||
// Optional Mint baton
|
||||
thisVin.tokenQtyStr = "0"
|
||||
thisVin.tokenQtyStr = '0'
|
||||
thisVin.tokenQty = 0
|
||||
thisVin.tokenId = vinTokenData.tokenId
|
||||
thisVin.isMintBaton = true
|
||||
} else {
|
||||
thisVin.tokenQtyStr = "0"
|
||||
thisVin.tokenQtyStr = '0'
|
||||
thisVin.tokenQty = 0
|
||||
thisVin.tokenId = null
|
||||
}
|
||||
@@ -643,7 +643,7 @@ class Transaction {
|
||||
// if MINT
|
||||
|
||||
return txDetails
|
||||
} catch(err) {
|
||||
} catch (err) {
|
||||
console.error('Error in get3()')
|
||||
|
||||
// This case handles rate limit errors.
|
||||
@@ -662,7 +662,7 @@ class Transaction {
|
||||
try {
|
||||
const tokenData = await this.slpUtils.decodeOpReturn(txid)
|
||||
return tokenData
|
||||
} catch(err) {
|
||||
} catch (err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -657,11 +657,11 @@ const mintTestInputTx02 = {
|
||||
}
|
||||
|
||||
const mintTestOpReturnData04 = {
|
||||
"tokenType": 1,
|
||||
"txType": "MINT",
|
||||
"tokenId": "938cc18e618967d787897bbc64b9a8d201b94ec7c69b1a9949eab0433ba5cdf8",
|
||||
"mintBatonVout": 2,
|
||||
"qty": "234123"
|
||||
tokenType: 1,
|
||||
txType: 'MINT',
|
||||
tokenId: '938cc18e618967d787897bbc64b9a8d201b94ec7c69b1a9949eab0433ba5cdf8',
|
||||
mintBatonVout: 2,
|
||||
qty: '234123'
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -297,7 +297,7 @@ describe('#TransactionLib', () => {
|
||||
sandbox
|
||||
.stub(bchjs.Transaction.blockchain, 'getBlockHeader')
|
||||
.resolves({ height: 602405 })
|
||||
sandbox.stub(bchjs.Transaction,'getTokenInfo').resolves(false)
|
||||
sandbox.stub(bchjs.Transaction, 'getTokenInfo').resolves(false)
|
||||
|
||||
const result = await bchjs.Transaction.get3(txid)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
@@ -354,17 +354,17 @@ describe('#TransactionLib', () => {
|
||||
assert.equal(result.vout[0].tokenQty, null)
|
||||
assert.equal(result.vout[0].tokenQtyStr, null)
|
||||
assert.equal(result.vout[1].tokenQty, 1)
|
||||
assert.equal(result.vout[1].tokenQtyStr, "1")
|
||||
assert.equal(result.vout[1].tokenQtyStr, '1')
|
||||
assert.equal(result.vout[2].tokenQty, 998833)
|
||||
assert.equal(result.vout[2].tokenQtyStr, "998833")
|
||||
assert.equal(result.vout[2].tokenQtyStr, '998833')
|
||||
assert.equal(result.vout[3].tokenQty, null)
|
||||
assert.equal(result.vout[3].tokenQtyStr, null)
|
||||
|
||||
// Assert that inputs have expected properties
|
||||
assert.equal(result.vin[0].tokenQtyStr, "998834")
|
||||
assert.equal(result.vin[0].tokenQtyStr, '998834')
|
||||
assert.equal(result.vin[0].tokenQty, 998834)
|
||||
assert.equal(result.vin[0].tokenId, "497291b8a1dfe69c8daea50677a3d31a5ef0e9484d8bebb610dac64bbc202fb7")
|
||||
assert.equal(result.vin[1].tokenQtyStr, "0")
|
||||
assert.equal(result.vin[0].tokenId, '497291b8a1dfe69c8daea50677a3d31a5ef0e9484d8bebb610dac64bbc202fb7')
|
||||
assert.equal(result.vin[1].tokenQtyStr, '0')
|
||||
assert.equal(result.vin[1].tokenQty, 0)
|
||||
assert.equal(result.vin[1].tokenId, null)
|
||||
|
||||
@@ -430,18 +430,18 @@ describe('#TransactionLib', () => {
|
||||
assert.equal(result.vout[0].tokenQty, null)
|
||||
assert.equal(result.vout[0].tokenQtyStr, null)
|
||||
assert.equal(result.vout[1].tokenQty, 5000000)
|
||||
assert.equal(result.vout[1].tokenQtyStr, "5000000")
|
||||
assert.equal(result.vout[1].tokenQtyStr, '5000000')
|
||||
assert.equal(result.vout[2].tokenQty, 5000000)
|
||||
assert.equal(result.vout[2].tokenQtyStr, "5000000")
|
||||
assert.equal(result.vout[2].tokenQtyStr, '5000000')
|
||||
assert.equal(result.vout[3].tokenQty, null)
|
||||
assert.equal(result.vout[3].tokenQtyStr, null)
|
||||
|
||||
// Assert inputs have expected properties and values
|
||||
assert.equal(result.vin[0].tokenQty, 10000000)
|
||||
assert.equal(result.vin[0].tokenQtyStr, "10000000")
|
||||
assert.equal(result.vin[0].tokenId, "323a1e35ae0b356316093d20f2d9fbc995d19314b5c0148b78dc8d9c0dab9d35")
|
||||
assert.equal(result.vin[0].tokenQtyStr, '10000000')
|
||||
assert.equal(result.vin[0].tokenId, '323a1e35ae0b356316093d20f2d9fbc995d19314b5c0148b78dc8d9c0dab9d35')
|
||||
assert.equal(result.vin[1].tokenQty, 0)
|
||||
assert.equal(result.vin[1].tokenQtyStr, "0")
|
||||
assert.equal(result.vin[1].tokenQtyStr, '0')
|
||||
assert.equal(result.vin[1].tokenId, null)
|
||||
|
||||
// Assert blockheight is added
|
||||
@@ -486,18 +486,18 @@ describe('#TransactionLib', () => {
|
||||
// Assert expected output properties and values exist.
|
||||
assert.equal(result.vout[0].tokenQty, null)
|
||||
assert.equal(result.vout[1].tokenQty, 43547.68657)
|
||||
assert.equal(result.vout[1].tokenQtyStr, "43547.68657")
|
||||
assert.equal(result.vout[1].tokenQtyStr, '43547.68657')
|
||||
assert.equal(result.vout[2].tokenQty, null)
|
||||
|
||||
// Assert expected input properties and values exist.
|
||||
assert.equal(result.vin[0].tokenQty, 43545.34534)
|
||||
assert.equal(result.vin[0].tokenQtyStr, "43545.34534")
|
||||
assert.equal(result.vin[0].tokenId, "938cc18e618967d787897bbc64b9a8d201b94ec7c69b1a9949eab0433ba5cdf8")
|
||||
assert.equal(result.vin[0].tokenQtyStr, '43545.34534')
|
||||
assert.equal(result.vin[0].tokenId, '938cc18e618967d787897bbc64b9a8d201b94ec7c69b1a9949eab0433ba5cdf8')
|
||||
assert.equal(result.vin[1].tokenQty, 2.34123)
|
||||
assert.equal(result.vin[1].tokenQtyStr, "2.34123")
|
||||
assert.equal(result.vin[1].tokenId, "938cc18e618967d787897bbc64b9a8d201b94ec7c69b1a9949eab0433ba5cdf8")
|
||||
assert.equal(result.vin[1].tokenQtyStr, '2.34123')
|
||||
assert.equal(result.vin[1].tokenId, '938cc18e618967d787897bbc64b9a8d201b94ec7c69b1a9949eab0433ba5cdf8')
|
||||
assert.equal(result.vin[2].tokenQty, 0)
|
||||
assert.equal(result.vin[2].tokenQtyStr, "0")
|
||||
assert.equal(result.vin[2].tokenQtyStr, '0')
|
||||
assert.equal(result.vin[2].tokenId, null)
|
||||
|
||||
// Assert blockheight is added
|
||||
@@ -544,21 +544,21 @@ describe('#TransactionLib', () => {
|
||||
// Assert the outputs have expected properties and values.
|
||||
assert.equal(result.vout[0].tokenQty, null)
|
||||
assert.equal(result.vout[1].tokenQty, 1000000)
|
||||
assert.equal(result.vout[1].tokenQtyStr, "1000000")
|
||||
assert.equal(result.vout[1].tokenQtyStr, '1000000')
|
||||
assert.equal(result.vout[2].tokenQty, 198000000)
|
||||
assert.equal(result.vout[2].tokenQtyStr, "198000000")
|
||||
assert.equal(result.vout[2].tokenQtyStr, '198000000')
|
||||
assert.equal(result.vout[3].tokenQty, null)
|
||||
|
||||
// Assert the inputs have expected properties and values.
|
||||
assert.equal(result.vin[0].tokenQty, 100000000)
|
||||
assert.equal(result.vin[0].tokenQtyStr, "100000000")
|
||||
assert.equal(result.vin[0].tokenId, "550d19eb820e616a54b8a73372c4420b5a0567d8dc00f613b71c5234dc884b35")
|
||||
assert.equal(result.vin[0].tokenQtyStr, '100000000')
|
||||
assert.equal(result.vin[0].tokenId, '550d19eb820e616a54b8a73372c4420b5a0567d8dc00f613b71c5234dc884b35')
|
||||
assert.equal(result.vin[1].tokenQty, 0)
|
||||
assert.equal(result.vin[1].tokenQtyStr, 0)
|
||||
assert.equal(result.vin[1].tokenId, null)
|
||||
assert.equal(result.vin[2].tokenQty, 99000000)
|
||||
assert.equal(result.vin[2].tokenQtyStr, "99000000")
|
||||
assert.equal(result.vin[2].tokenId, "550d19eb820e616a54b8a73372c4420b5a0567d8dc00f613b71c5234dc884b35")
|
||||
assert.equal(result.vin[2].tokenQtyStr, '99000000')
|
||||
assert.equal(result.vin[2].tokenId, '550d19eb820e616a54b8a73372c4420b5a0567d8dc00f613b71c5234dc884b35')
|
||||
|
||||
// Assert blockheight is added
|
||||
assert.equal(result.blockheight, 543957)
|
||||
|
||||
Reference in New Issue
Block a user