I created kusama transfer function using polkadot.js/api.
I followed https://polkadot.js.org/docs/api/examples/promise/make-transfer.
I can get extrinsic hash from above. I want to get extrinsic detail to check if transaction is success or not.
According to my check, it is impossible to check extrinsic detail with hash only.
Extrinsic Hash and Block number are both needed.
https://polkadot.js.org/docs/api/examples/promise/transfer-events
I also tried callback, but it doesn't show blockhash for me.
When I log status, it is just {ready: null}
How can I get block number or block hash from transfer?
Add a comment
|
1 Answer
To get the blockHash where it has been included your transaction, you can send it and then wait until it has been included in the block using a subscription.
const unsub = await api.tx.balances.transfer(BOB, 12345).signAndSend(alice, (result) => {
console.log(`Current status is ${result.status}`);
if (result.status.isInBlock) {
console.log(`Transaction included at blockHash ${result.status.asInBlock}`);
} else if (result.status.isFinalized) {
console.log(`Transaction finalized at blockHash ${result.status.asFinalized}`);
unsub();
}
});
Then you can get the block information(number, hash...) and the extrinsics in that block:
const blockHash = result.status.asFinalized;
const signedBlock = await api.rpc.chain.getBlock(blockHash);
console.log(signedBlock.toHuman());
const extrinsics = signedBlock.block.extrinsics;
console.log(extrinsics.toHuman())
-
Hey. Thank you for your answer. I also tried it but it doesn't work for me. When I log
result.status
, it is justready
and if condition log does not happen Commented Nov 11, 2022 at 9:34 -
I have tried it in the Polkadot network and works, make sure you are importing you are creating the Keyring propertly: polkadot.js.org/docs/keyring/start/create and specific for Kusama: polkadot.js.org/docs/keyring/start/ss58 Commented Nov 11, 2022 at 10:10
-
Yes. Tranasaction is done. This mean, I am doing correctly, just I can't get block hash Commented Nov 11, 2022 at 10:16
-
Do you close your connection after the transfer? you may have to wait a bit to get the message that is included in a block, and check you have the await Commented Nov 11, 2022 at 11:45
-
Yes, I think it is logical. I will try to add setTimeout. Commented Nov 11, 2022 at 12:11