0

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

enter image description here

Any kind of help would be appreciated. Thank you in advance.

2
  • Which line of the code posted does the error point at; the toast.success(😉 Finalized. Block hash: ${status.asFinalized.toString()}) one? If so, I'd have a look at the generated JS; perhaps the status.asFinalized.toString() is moved outside the ternary or something?
    – jsdw
    Aug 1, 2023 at 14:17
  • The error occurs once I accept by entering the password on the polkadot js extension. The other words, I think it happens before running the status.asFinalized.toString() function.
    – Alan Ma
    Aug 1, 2023 at 14:24

1 Answer 1

0

I added status?.type !== 'InBlock' like this.

} else if (section + ":" + method === 'system:ExtrinsicSuccess' && status?.type !== 'InBlock' ) {

And I fixed it.

2
  • Ah, yeah, because you call status.asFinalized.toString() after that, and the status may well not be finalized yet.
    – jsdw
    Aug 2, 2023 at 14:37
  • By the way, why it occurs for InBlock? It has also Ready.
    – Alan Ma
    Aug 3, 2023 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.