4

I'm new and I'm trying to get the transaction hash but I'm only getting undefined. I used this tutorial and this and below is my code.

const txHash = await polkadot.tx.balances
  .transfer('5F1pZe.....GmZRSZuK', 100000000000)
  .signAndSend(SENDER, { signer: injector.signer }, (status) => {

        if (status.isInBlock) {
            console.log(`Completed at block hash #${status.isInBlock}`);
        } else {
            console.log(`Current status: ${status.type}`);
        }


    }).catch((error) => {
        console.log(':( transaction failed', error);
    });

console.log("Submitted with hash", txHash.hash);

I'm getting undefined for txHash.hash, status.type. I have another code that works but I wonder what's wrong with the above.

1 Answer 1

7

The txhash is only returned when you use the non-subscribe form.

In code for the non-sub form -

// don't subscribe, txhash is returned
const txHash = await polkadot.tx.balances
  .transfer('5F1pZe.....GmZRSZuK', 100000000000)
  .signAndSend(SENDER, { signer: injector.signer });

console.log(txhash);

And for the subscription form -

// subscription, unsub is returned, txHash internal
const unsub = await polkadot.tx.balances
  .transfer('5F1pZe.....GmZRSZuK', 100000000000)
  .signAndSend(SENDER, { signer: injector.signer }, (status) => {
    console.log(status.txHash);
  });

// unsubscribe
unsub();

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.