I am trying to sign and send some transactions using the batch
function.
Here are some code snippets for reproduction.
const nftMinting = async (item) => {
try {
let collection = 1
const fromAcct = await getFromAcct()
let txs = []
txs.push(api.tx.uniques.create(collection, polkadotAccount))
const nftAmount = 100
const price = nftAmount > 0? Math.round(Number(item?.price) / nftAmount) : 0
if (price > 0) {
for (let index = 0; index < 100; index++) {
txs.push(api.tx.uniques.mint(collection, index + 1, polkadotAccount))
txs.push(api.tx.uniques.setPrice(collection, index + 1, price, polkadotAccount))
}
}
await api.tx.utility.batch(txs).signAndSend(...fromAcct, ({ events = [], status, txHash }) =>{
status.isFinalized
? toast.success(`😉 Finalized. Block hash: ${status.asFinalized.toString()}`)
: toast.info(`Current transaction status: ${status.type}`)
// Loop through Vec<EventRecord> to display all events
events.forEach(async ({ _, event: { data, method, section } }) => {
if ((section + ":" + method) === 'system:ExtrinsicFailed' ) {
// extract the data for this event
const [dispatchError, dispatchInfo] = data
console.log(`dispatchinfo: ${dispatchInfo}`)
let errorInfo
// decode the error
if (dispatchError.isModule) {
// for module errors, we have the section indexed, lookup
// (For specific known errors, we can also do a check against the
// api.errors.<module>.<ErrorName>.is(dispatchError.asModule) guard)
const mod = dispatchError.asModule
const error = api.registry.findMetaError(
new Uint8Array([mod.index.toNumber(), bnFromHex(mod.error.toHex().slice(0, 4)).toNumber()])
)
let message = `${error.section}.${error.name}${
Array.isArray(error.docs) ? `(${error.docs.join('')})` : error.docs || ''
}`
errorInfo = `${message}`
console.log(`Error-info::${JSON.stringify(error)}`)
} else {
// Other, CannotLookup, BadOrigin, no extra info
errorInfo = dispatchError.toString()
}
toast.warn(`😞 Transaction Failed! ${section}.${method}::${errorInfo}`)
} else if (section + ":" + method === 'system:ExtrinsicSuccess' ) {
toast.success(`❤️️ Transaction successful! tx hash: ${txHash}, Block hash: ${status.asFinalized.toString()}`)
}
})
})
} catch (error) {
console.log('error :: ', error)
}
}
But I got this error, unfortunately.
Uncaught (in promise) Error: Cannot convert 'InBlock' via asFinalized
Any kind of help would be appreciated. Thank you in advance.
toast.success(
😉 Finalized. Block hash: ${status.asFinalized.toString()})
one? If so, I'd have a look at the generated JS; perhaps thestatus.asFinalized.toString()
is moved outside the ternary or something?