2

I am using a local testnet node (Geth 1.7.3) in light mode, and in such case the transaction receipt does not contain the contractAddress when a new contract is deployed.

Therefore I am looking to determine the contract address myself. From what I read it is determined from the nonce and the account: How is the address of an Ethereum contract computed?

I am trying to repliate the logic explained in this post in Python:

def mk_contract_address(sender, nonce):
    return sha3(rlp.encode([normalize_address(sender), nonce]))[12:] 

In Javascript using web3 1.0.0-beta.26 and RLP package https://github.com/ethereum/wiki/wiki/RLP My attempt is this:

var mainAccount = "0x5e03df21b770c783b1641bb3b49924e2fed0b3a7"
var Web3 = require("web3")
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
var RLP = require('rlp');  

web3.eth.getTransactionCount(mainAccount, (err, nonce) => {

    console.log(web3.utils.sha3(RLP.encode[sender,nonce]))) ;
});

But I do not get the same address once the contract is deployed

Renaud
  • 349
  • 2
  • 11
  • Just to check: are you setting the sender address and the nonce to the same values when you run each of the two methods above? – Richard Horrocks Dec 05 '17 at 17:43
  • Where is sender defined? Should it be mainAccount instead (since that's the account you're fetching the nonce for)? – user19510 Dec 05 '17 at 22:00

1 Answers1

3

I think I finally got it. I am using the example in the original response (in Python):

For sender 0x6ac7ea33f8831ea9dcc53393aaa88b25a785dbf0, the contract addresses that it will create are the following:

nonce0= "0xcd234a471b72ba2f1ccf0a70fcaba648a5eecd8d"

By running this I get the same result:

var Web3 = require("web3")
var web3 = new Web3()
var RLP = require('rlp');  
var nonce = 0;
var sender= "0x6ac7ea33f8831ea9dcc53393aaa88b25a785dbf0"

var address = "0x" + web3.utils.sha3(RLP.encode([sender,nonce])).slice(12).substring(14)

address is:

'0xcd234a471b72ba2f1ccf0a70fcaba648a5eecd8d'

as the nonce0 example above

Renaud
  • 349
  • 2
  • 11
  • yeah, you can calculate it yourself, but only getting the transaction status will give you the safety that the contract has actually created and exist on the blockchain – Nulik Mar 19 '18 at 18:57