question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.getContractAddress() returning an incorrect address.

See original GitHub issue

Note: Not all sections may be relevant, but please be as thorough while remaining concise as possible. Remove this Notice and any sections that don’t feel pertinent.

If you are unsure if something is a bug, start a thread in the “discussions” tab above…

Describe the bug I have a Contract (A) that creates a Contract (B).

// Creates a new ERC20 contract
function createERC20(
  string memory _name,
  string memory _symbol,
  uint _mintAmount
) external returns (address newToken) {
  newToken = address(
	new ERC20Token(_name, _symbol, address(this), msg.sender, _mintAmount)
  );
  emit TokenCreated(newToken, _name, _symbol);
}

In the frontend, when I try to run getContractAddress() using tx.from & tx.nonce, I get an address back, but it’s not the one that got created.

// Surprisingly this line returns the correct address BEFORE the contract is even accepted in MetaMask.
factoryContract.on('TokenCreated', e => console.log(e));

const tx = await factoryContract.createERC20(
  tokenName,
  tokenSymbol,
  mintAmount
);
// This returns an incorrect contract address.
const newTokenAddress = getContractAddress({
  from: tx.from,
  nonce: tx.nonce
});
await tx.wait();
			

Environment: Hardhat, MetaMask, Ethers.js, Next.js

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ricmoocommented, Nov 19, 2021

Thanks! Glad to helps. 😃

I haven’t actually ever done this myself, but every address’ nonce is stored in the state trie, and should be accessible using provider.getTransactionCount(contractA.address).

Generally I would recommend emitting an event in your ContractA that indicates the new address created, since then the transaction receipt can be queried more reliably, otherwise you might compute the new B’s address and someone might front-run you and sneak in ahead of you create that contract.

Using create2 also gives more control over the address being created, which does not take the sending nonce into account. But emitting an event is still recommended, if possible. Obviously sometimes this isn’t possible, such as counterfactual instantiating where the contract wont be deployed for quite some time (if ever).

Let me know if that works. 😃

1reaction
ricmoocommented, Nov 19, 2021

Are you trying to compute the address of contract A or contract B? For contract B you will need to use the nonce of contract A, not the EOA that sent the transaction.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Addresses - Ethers.js
Returns address as a Checksum Address. If address is an invalid 40-nibble HexString or if it contains mixed case and the checksum is...
Read more >
Contract function returns address?
I just verified I can call the constant function and get the expect return value, 4545. Question, how do I get the response...
Read more >
Utilities — ethers.js 4.0.0 documentation
getContractAddress ( transaction ) => Address: Computes the contract address of a contract deployed by transaction. The only properties used are from and ......
Read more >
Transactions and Smart Contracts - Web3j
get contract address EthGetTransactionReceipt transactionReceipt = web3j. ... invalid function call is made, or a null result is obtained, the return value ...
Read more >
Cant get a smart contract address balance with standard ...
pragma solidity ^0.6.0; contract HelloWorld { function getContractAddress() public returns (address) { return address(this); } function ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found