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?

5
  • FE? If you have transaction hash of contract deployment or even contract address. You can paste the transaction hash on etherscan.io to get the details like block number. timestamp, gas, gasPrice etc. If you are strict to use web3, use getTrsancation web3.eth.getTransaction(txhahs).blockNumber() Feb 20, 2018 at 9:43
  • thanks. FE = front end (updated question) Indeed I can see on etherescan. But I need to get it in my webapp. we deploy with truffle, I couldn't find txhash in generated JSON artifacts
    – szerte
    Feb 20, 2018 at 9:46
  • Was it deployed on testnet? Feb 20, 2018 at 9:50
  • 1
    (clarified the question) it's a deploy on rinkeby.
    – szerte
    Feb 20, 2018 at 9:54
  • Do you have contract address? Feb 20, 2018 at 10:19

4 Answers 4

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

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

1
  • 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, 2018 at 4: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

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;
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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