How to get Smart Contract address when it is deployed with web3.js
Asked Answered
H

4

9

I have tried to deploy a SmartContract from web3.js node library, I am getting a transaction hash from it but how would I get the contract address after It's been mined by a miner?

Haberdashery answered 10/10, 2018 at 11:5 Comment(1)
Welcome to StackOverflow! Please, provide a Minimal, Complete, and Verifiable example of code that you already have.Ritualize
H
10

finally i got the answer

var Tx=require('ethereumjs-tx')
const Web3=require('web3')
const web3 = new Web3('https://rinkeby.infura.io/xxxxxxxxxxxxxxxxxx')

const account1='0xf2b6xxxxxxxxxxxxxxxxxxx83e9d52d934e5c'
const privateKey1=Buffer.from('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx','hex')    


web3.eth.getTransactionCount(account1,(err,txCount)=>{
//smart contract data
const data = 'your data here'

//create transaction object
const txObject={
nonce:web3.utils.toHex(txCount),
gasLimit:web3.utils.toHex(1000000),
gasPrice:web3.utils.toHex(web3.utils.toWei('10','gwei')),
data: data
}

//sign the transaction
const tx = new Tx(txObject)
tx.sign(privateKey1)
const serializedTx = tx.serialize()
const raw='0x'+serializedTx.toString('hex')

//broadcast the transaction
web3.eth.sendSignedTransaction(raw,(err,txHash)=>{
console.log('err : ',err,'txHash : ',txHash)
//use this hash to find smartcontract on etherscan
}).on('receipt', console.log,);

})

.on() method wait till the end of block mining and returns the address of transaction(here contract address). This method is applicable if you don't want to use metamask to sign your transaction and broadcast to the network.

Haberdashery answered 17/10, 2018 at 11:5 Comment(2)
What if I just want to get the smart contract address by transaction hash which deployed the smart contract?Titration
Just found one: If you just want to get the smart contract by transaction hash which deployed the smart contract, you can use the the [web3.eth.getTransactionReceipt][1] fetches the receipt for a transaction hash. The receipt has a contactAddress field filled in if the transaction was a deployment. Check this out: github.com/ChainSafe/web3.js/issues/3515 [1]: web3js.readthedocs.io/en/v1.4.0/…Titration
H
3

This returns the contract address...

MyContract is the .json file in the build/contracts folder that is created by migration.

const netId = await web3.eth.net.getId();
const deployedNetwork = MyContract.networks[netId];


const contract = new web3.eth.Contract(
    MyContract.abi,
    deployedNetwork.address
);
Harrovian answered 25/6, 2021 at 6:13 Comment(0)
P
2

Add .address after the object.

var contact = web3.eth.contract.new(abi,{from: web3.eth.accounts[0], data: bc});
console.log(contract.address); // Prints address
Patronize answered 10/10, 2018 at 14:28 Comment(2)
what is the bc variable?Quota
This has been changed in latter versions to be contract.options.addressTann
T
2

If you just want to get the smart contract by transaction hash which deployed the smart contract, you can use the the web3.eth.getTransactionReceipt fetches the receipt for a transaction hash. The receipt has a contactAddress field filled in if the transaction was a deployment. Check this out: https://github.com/ChainSafe/web3.js/issues/3515

Titration answered 17/9, 2021 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.