1

I am aware that different versions of this question were asked but my interest is specifically working out the contract address following dynamic deployment in a UI using web3 or something similar. I can recover the transaction hash and I want the contract address before it is fully deployed.

What happens is the UI session ends before the return to it by Metamask.

I think the best answer is to compute the contract address manually and then store that off chain (that is needed for the use case). Another option is to somehow wait for the return by Metamask.

This is my code -

 status("compiling");
    document.getElementById("compile-output").value = "";
    var result = compiler.compile(getSourceCode(), optimize);
    var stringResult = JSON.stringify(result);

    var bytecode = result.contracts.greeter.bytecode;
    var abi = result.contracts.greeter.interface;

    web3 = new Web3(web3.currentProvider);

    // Our future code here..
     web3.eth.defaultAccount = web3.eth.accounts[0];

    var contract = web3.eth.contract(JSON.parse(abi));
    var hash = web3.sha3(getSourceCode());

    contract.new(
       {
         from: web3.eth.accounts[0],
         data: "0x"+bytecode,
         gas: '4500000'
       }, function (e, contract){
          console.log(e, contract);
          if (typeof contract.address !== 'undefined') {
             console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
           }
    });

  • 1
    The contract address is the last 20 bytes of the keccak256 hash of the RLP encoded deploying address and nonce. In pseudocode: last20Bytes(keccak256(rlp(fromAddress, nonce))). You don't need the transaction hash; you just need to know the from address and the nonce. – user19510 Aug 31 '18 at 16:35