I understand how the below web3js code block works, adapted from here:


But how does one save the contract address programmatically? One could, I suppose, open a file and persist it within the callback, but not that. =:)

And I don't believe the myContractReturned identifier helps us; given the asynchronous nature of JavaScript (not to mention the mining delay). How does one even use that?

It's kind of an academic question (because there's no guarantee an address will even be generated), but curious as to what friends in the community have done (or thought). Thank you!


var myContractReturned = MyContract.new(param1,param2, {
   function(error, myContract){
     if(!error) {
        if(!myContract.address) {
            // Step-1: Runs on contract submission/deployment.
        else {
            // Step-2: Runs after contract is deployed.

  • Thank you "@Mikko Ohtamaa" and "@Xavier Leprêtre B9lab" for your respective answers. Both are quite good answers and I encourage readers to view both! Xavier's answer reminds us that -- by pre-computing it -- we can know the Contract Address even before it is submitted. – NYCeyes Feb 1 '17 at 15:30

The contract's address is computed out of the deployer's address and the transaction nonce. You need not wait for the contract to be mined to get it.

In NodeJs, something like that will do:

var ethUtil = require('ethereumjs-util');

var currentNonce = web3.eth.getTransactionCount(myAccount);
var futureAddress = ethUtil.bufferToHex(ethUtil.generateAddress(myAccount, currentNonce));
// futureAddress is the address of the contract that you deploy below

var MyContract = web3.eth.contract(abiArray);
var contractInstance = MyContract.new([contructorParam1] [, contructorParam2], {data: '0x12345...', from: myAccount, gas: 1000000});

// Here you can confirm that its address is indeed the one you calculated earlier.

Don't forget to improve this demo-code by using asynchronous calls.

  • 1
    Too bad people who come to the accepted answer will believe that you cannot have the address before it has been mined. – Xavier Leprêtre B9lab Jan 31 '17 at 15:19
  • 1
    @"Xavier Leprêtre B9lab" True, and is why I updated my accepted answer, so people can see it. The accepted answer was changed since your above comment :) – NYCeyes Jan 4 at 20:56

The contract constructor will return a transaction hash where the contract is being deployed. The final contract address can be deterministically determined from deployer address and deployer address nonce (see another answer). This information is also available through web3.eth.getTransactionReceipt after the deployment transaction has been mined.

Note that you don't know if the contract deployment will success or fail before the transaction has been mined. You cannot interact with the contract before the transaction has been mined.

Here is a sample deployment script for Node 7 (read full tutorial):

// Copyright 2017 https://tokenmarket.net - MIT licensed
// Run with Node 7.x as:
// node --harmony-async-await  deploy.js

let fs = require("fs");

// http://localhost:8545
// For geth VPS server + SSH tunneling see
// https://gist.github.com/miohtama/ce612b35415e74268ff243af645048f4
let web3 = new Web3();

// Read the compiled contract code
// Compile with
let source = fs.readFileSync("contracts.json");

// ABI description as JSON structure
let abi = JSON.parse(contracts.SampleContract.abi);

// Smart contract EVM bytecode as hex
let code = contracts.SampleContract.bin;

// Create Contract proxy class
let SampleContract = web3.eth.contract(abi);

// Unlock the coinbase account to make transactions out of it
console.log("Unlocking coinbase account");
var password = "";
try {
  web3.personal.unlockAccount(web3.eth.coinbase, password);
} catch(e) {

console.log("Deploying the contract");

// Transaction has entered to geth memory pool

function sleep(ms) {

// in a block to get the address of the contract
async function waitBlock() {
  while (true) {
    let receipt = web3.eth.getTransactionReceipt(contract.transactionHash);
    if (receipt && receipt.contractAddress) {
    await sleep(4000);

  • Thank you for this answer "@Mikko Ohtamaa". Appreciated. Below "@Xavier Leprêtre B9lab" provides an alternative approach, and readers should leverage both answers (appropriate to their use-case). :) – NYCeyes Jan 31 '17 at 15:51
  • Please, clarify that the provided code is not compatible with the geth JavaScript console (the code uses ES6 features like arrow functions, while geth only supports ES5). I made the mistake myself, and it looks like I am not the first one: ethereum.stackexchange.com/q/33978/36845 – Gallaecio Apr 17 '18 at 10:05

Your Answer

