2

In my test I would like to get the block or block number of when the contract was deployed.

const instance = await MyContract.deployed()
const contractDeploymentBlockNumber = await instance.?
0
0

When using

truffle develop
> migrate

or

truffle migrate

It returns the transaction hash for a specific object, i.e.

0x95aadab5fdd4c06dbf0e27601bd3a4c2da2ea244a7506189864f4465351fe3dc

So, you can get the block number for that contract deployment using

const txBlock = await Migrations.web3.eth.getTransaction('0x95aadab5fdd4c06dbf0e27601bd3a4c2da2ea244a7506189864f4465351fe3dc');
console.log(txBlock.blockNumber);
2
  • He is referring to truffle test for a specific deployed contract, not common web3.js code for current block. – Sergi Juanati Jan 17 at 9:24
  • I see... Thank you! – Einar César Jan 18 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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