0

We deploy our contracts with truffle migrate.

We need the blockNumber of the contract deployment transaction in the webapp.

We could use web3.eth.getTransaction(txhash).blockNumber() but can't get txhash in the app because JSON files generated by truffle doesn't contain it.

How can I retrieve contract deployment blockNumber on front end app?

szerte
  • 1,231
  • 1
  • 14
  • 32

4 Answers4

1

My solution would be this (if you use web3 prior version 1.0.):

  1. You can calculate the deployed contract's address by knowing the address from which you sent the contract creation transaction and from the corresponding nonce. Check this out!
  2. Now you can create your contract instance since you have the abi and the contract address.
  3. From the created contract's instance you can simply have txHash of the transaction which created it by accessing this field: myContractInstance.transactionHash
0

Here is a solution:

module.exports = function(deployer) {
  const MyContract = artifacts.require("MyContract");
  await deployer.deploy(MyContract);
  const instance = await MyContract.deployed();
  const address = instance.address;
  const txHash = MyContract.transactionHash;
  const blockNumber = (await MyContract.web3.eth.getTransaction(hash)).blockNumber;
};
daniel liu
  • 13
  • 3
0

Solution tested with truffle 4.x, use in your js: let block = await web3.eth.getBlock("latest") console.log(block.number)

Ellis
  • 2,354
  • 14
  • 14
  • 3
    This retrieves the latest block number, and may or may not be related to when the transaction creating the contract was mined. – hayesgm Aug 24 '18 at 04:08
0

You can get the information from the networks section of the json generated from the truffle migraton. By fetching the receipt for the transactionHash, you can get the blockNumber among other information.

[Contract].json > networks > [networkID] > transactionHash
alternatively from the truffle-contract instance.
[truffle-contract instance].contractArtifact.networks.[networkId].transactionHash

https://github.com/trufflesuite/truffle-contract-schema/pull/19

Adibas03
  • 388
  • 2
  • 7