mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-22 01:02:05 -07:00
feat(validateBulk): Changed output. Passing true, null, and false values
This commit is contained in:
+21
-5
@@ -1006,7 +1006,7 @@ class Slp {
|
||||
|
||||
let formattedTokens = []
|
||||
|
||||
// Combine the arrays. Why? Generally there is nothing in the u array.
|
||||
// Combine the confirmed and unconfirmed collections.
|
||||
const concatArray = tokenRes.data.c.concat(tokenRes.data.u)
|
||||
|
||||
const tokenIds = []
|
||||
@@ -1028,12 +1028,15 @@ class Slp {
|
||||
})
|
||||
|
||||
// If a user-provided txid doesn't exist in the data, add it with
|
||||
// valid:false property.
|
||||
// valid:null property.
|
||||
// 'null' indicates that SLPDB does not know about the transaction. It
|
||||
// either has not seen it or has not processed it yet. A determination
|
||||
// can not be made.
|
||||
txids.forEach((txid) => {
|
||||
if (!tokenIds.includes(txid)) {
|
||||
formattedTokens.push({
|
||||
txid: txid,
|
||||
valid: false
|
||||
valid: null
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -1056,8 +1059,21 @@ class Slp {
|
||||
formattedTokens = newOutput
|
||||
}
|
||||
|
||||
// Put the output array in the same order as the input array.
|
||||
const outAry = []
|
||||
for (let i = 0; i < txids.length; i++) {
|
||||
const thisTxid = txids[i]
|
||||
|
||||
// Need to use Array.find() because the returned output array is out
|
||||
// of order with respect to the txid input array.
|
||||
const output = formattedTokens.find((elem) => elem.txid === thisTxid)
|
||||
// console.log(`output: ${JSON.stringify(output, null, 2)}`)
|
||||
|
||||
outAry.push(output)
|
||||
}
|
||||
|
||||
res.status(200)
|
||||
return res.json(formattedTokens)
|
||||
return res.json(outAry)
|
||||
} catch (err) {
|
||||
wlogger.error('Error in slp.ts/validateBulk().', err)
|
||||
|
||||
@@ -1137,7 +1153,7 @@ class Slp {
|
||||
res.status(200)
|
||||
return res.json(result)
|
||||
} catch (err) {
|
||||
wlogger.error('Error in slp.ts/validateSingle().', err)
|
||||
wlogger.error('Error in slp.js/validateSingle().', err)
|
||||
|
||||
return _this.errorHandler(err, res)
|
||||
}
|
||||
|
||||
@@ -258,4 +258,86 @@ describe('#slp', () => {
|
||||
// )
|
||||
// })
|
||||
})
|
||||
|
||||
describe('#validateBulk', () => {
|
||||
it('should handle a mix of valid, invalid, and non-SLP txs', async () => {
|
||||
const txids = [
|
||||
// Malformed SLP tx
|
||||
'f7e5199ef6669ad4d078093b3ad56e355b6ab84567e59ad0f08a5ad0244f783a',
|
||||
// Normal TX (non-SLP)
|
||||
'01cdaec2f8b311fc2d6ecc930247bd45fa696dc204ab684596e281fe1b06c1f0',
|
||||
// Valid PSF SLP tx
|
||||
'daf4d8b8045e7a90b7af81bfe2370178f687da0e545511bce1c9ae539eba5ffd',
|
||||
// Valid SLP token not in whitelist
|
||||
'3a4b628cbcc183ab376d44ce5252325f042268307ffa4a53443e92b6d24fb488',
|
||||
// Token send on BCHN network.
|
||||
'402c663379d9699b6e2dd38737061e5888c5a49fca77c97ab98e79e08959e019',
|
||||
// Token send on ABC network.
|
||||
'336bfe2168aac4c3303508a9e8548a0d33797a83b85b76a12d845c8d6674f79d',
|
||||
// Known invalid SLP token send of PSF tokens.
|
||||
'2bf691ad3679d928fef880b8a45b93b233f8fa0d0a92cf792313dbe77b1deb74'
|
||||
]
|
||||
|
||||
req.body.txids = txids
|
||||
const result = await slp.validateBulk(req, res)
|
||||
console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
// assert.equal(result.txid, txid)
|
||||
// assert.equal(result.isValid, false)
|
||||
|
||||
// BCHN expected results
|
||||
if (process.env.ISBCHN) {
|
||||
assert.equal(result[0].txid, txids[0])
|
||||
assert.equal(result[0].valid, null)
|
||||
|
||||
assert.equal(result[1].txid, txids[1])
|
||||
assert.equal(result[1].valid, null)
|
||||
|
||||
assert.equal(result[2].txid, txids[2])
|
||||
assert.equal(result[2].valid, true)
|
||||
|
||||
assert.equal(result[3].txid, txids[3])
|
||||
assert.equal(result[3].valid, true)
|
||||
|
||||
// Note: This should change from null to true once SLPDB finishes indexing.
|
||||
assert.equal(result[4].txid, txids[4])
|
||||
assert.equal(result[4].valid, null)
|
||||
|
||||
assert.equal(result[5].txid, txids[5])
|
||||
assert.equal(result[5].valid, null)
|
||||
|
||||
assert.equal(result[6].txid, txids[6])
|
||||
assert.equal(result[6].valid, false)
|
||||
assert.include(
|
||||
result[6].invalidReason,
|
||||
'Token outputs are greater than valid token inputs'
|
||||
)
|
||||
} else {
|
||||
assert.equal(result[0].txid, txids[0])
|
||||
assert.equal(result[0].valid, null)
|
||||
|
||||
assert.equal(result[1].txid, txids[1])
|
||||
assert.equal(result[1].valid, null)
|
||||
|
||||
assert.equal(result[2].txid, txids[2])
|
||||
assert.equal(result[2].valid, true)
|
||||
|
||||
assert.equal(result[3].txid, txids[3])
|
||||
assert.equal(result[3].valid, true)
|
||||
|
||||
assert.equal(result[4].txid, txids[4])
|
||||
assert.equal(result[4].valid, null)
|
||||
|
||||
assert.equal(result[5].txid, txids[5])
|
||||
assert.equal(result[5].valid, true)
|
||||
|
||||
assert.equal(result[6].txid, txids[6])
|
||||
assert.equal(result[6].valid, false)
|
||||
assert.include(
|
||||
result[6].invalidReason,
|
||||
'Token outputs are greater than valid token inputs'
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user