0

Using Hardhat. Unfortunately I deployed my contracts without asking for the contract address to be returned properly. Now with ethers.js I am struggling to find how or where to pull that information from. The contracts are deployed and I have artifacts in the folder but don't know how to interact with abi without contract address.

Here is the code I used to deploy:

async function main() {

    const GenTest1 = await ethers.getContractFactory("GenTest1")
  
  
    // Start deployment, returning a promise that resolves to a contract object
  
    const genTest1 = await GenTest1.deploy()
  
    await genTest1.waitForDeployment()
  
    console.log("Contract deployed to address:", genTest1.address)
  
  }
  
  
  main()
  
    .then(() => process.exit(0))
  
    .catch((error) => {
  
      console.error(error)
  
      process.exit(1)
  
    })
4
  • Which command do you use to execute the script?
    – CT95
    Sep 19, 2023 at 2:01
  • npx hardhat run scripts/myscript.js
    – Meltxx
    Sep 19, 2023 at 2:11
  • That command will execute your script on default network is hardhat There are 2 options for you: 1. another command: npx hardhat run --network [your-network-name] scripts/myscript.js 2. Update hardhat config, add this in module exports: defaultNetwork: 'your_network'. And execute script again
    – CT95
    Sep 19, 2023 at 2:36
  • Ok this seems to be the answer. I deployed on a local and what I really needed to do was specify the network and this made me see that I had the wrong URL as well. I am running into new errors now so onto the next thread! Thank you!!
    – Meltxx
    Sep 19, 2023 at 2:51

2 Answers 2

0

You must have used some wallet to pay for this transaction. Go to etherscan (or appropriate 'scan' for your chain), find your wallet there and look for transactions executed by it. There will be a transaction for creation of a new contract and you can get the address from there

1
  • It seems like Hardhat maybe just deployed it on a local node because even though I have my network and account info in my hardhat.config I did not see a transaction on that account.
    – Meltxx
    Sep 19, 2023 at 1:55
0

To find the contract address of a contract that has already been deployed, you can use the following steps:

  1. Get the ABI of the contract. You can find this in the artifacts folder of your Hardhat project.
  2. Use the ABI to get the contract factory.
  3. Use the contract factory to get the contract object.
  4. Get the contract address from the contract object.

Here is an example of how to do this in JavaScript:

const ethers = require('ethers');

const ABI = require('./artifacts/contracts/GenTest1.sol/GenTest1.json').abi;

const contractFactory = new ethers.ContractFactory(ABI, provider);

const contract = await contractFactory.deploy();

const contractAddress = contract.address;

Once you have the contract address, you can use it to interact with the contract using the ABI.

Note: If you deployed your contract to a testnet or mainnet, you will need to use a provider that is connected to that network. For example, if you deployed your contract to Sepolia, you can use the following provider:

const provider = new ethers.providers.AlchemyProvider('sepolia');

Use code with your different network you are using.

You can also get the contract address from the Hardhat deployment information. To do this, run the following command:

npx hardhat show-deployments

This will show you a list of all the contracts that have been deployed, along with their addresses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.