1

Is it possible to detect the block number that the transaction included within solidity? For example somebody transfer me some eth or token, can I know the block number of the transaction?

2 Answers 2

1

You can get the current block number inside a Solidity contract by calling block.number, this also corresponds to the block number of the block which includes the transaction of ETH sent to the contract function.

0

Any Solidity method call that changes the state is a transaction. That transaction is composed of possibly multiple atomic operations. To log or return the block info of that transaction, you could use block.number. You also have access to many other handy global properties. This is possible because that method call can only take effect on the state once the block that the transaction belongs to is mined. And upon mining, those properties expose all that info about that block. If you want to figure out the block number of any TX outside the realms of Solidity, you could use EthersJS or Web3JS and get that info as follows:

EthersJS:

const provider = new ethers.providers...;
provider.getTransactionReceipt(txHash).then(tx => console.log(tx.blockNumber))

Web3JS:

web3.eth.getTransactionReceipt(txHash, tx => console.log(tx.blockNumber))

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.