.getContractAddress() returning an incorrect address.
See original GitHub issueNote: 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:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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. 😃
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.