[Ethereum] Determining Contract Address with web3 1.0

contract-deploymentgo-ethereumweb3js

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

Best Answer

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