.getContractAddress() returning an incorrect address. #2309
Replies: 7 comments 13 replies
-
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. |
Beta Was this translation helpful? Give feedback.
-
Hi Richard, thanks for your reply! Just wanted to tell you real quick that I love the work that you do & I appreciate you! My Contract A has a factory function that creates ERC20 tokens (Contract Bs). I want to get the contract address of the new Contract B that A created. How would I do this? I'm unsure of how to get the nonce of Contract A, I was getting the |
Beta Was this translation helpful? Give feedback.
-
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 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. :) |
Beta Was this translation helpful? Give feedback.
-
Awesome! Thank you! Something weird that I noticed that I'm not sure is the right behavior, but when I set up the event listener it logs the correct address before the transaction call is even executed/created. I actually put it in the 2nd code snippet. Is this proper behavior? A workaround that I did to get the returned address was to use const newTokenAddress = await factoryContract.callStatic.createERC20(
tokenName,
tokenSymbol.toUpperCase(),
parseEther(mintAmount)
);
const tx = await factoryContract.createERC20(
tokenName,
tokenSymbol,
parseEther(mintAmount)
); |
Beta Was this translation helpful? Give feedback.
-
Hmmm. I’m not totally sure what you mean by “before the call is even executed/created”. Are you using Ganache? Because it does lots of strange things (since some operations that would be asynchronous are handled internally synchronously)? Using callStatic is fine, but keep front running and consistency in mind; things can change on the blockchain between your callStatic and your transaction being mined. :) |
Beta Was this translation helpful? Give feedback.
-
(also, moving to discussions in case future people have related questions ;)) |
Beta Was this translation helpful? Give feedback.
-
I have similar issue related to contract deployment. Im using hardhat, and it seems to provide an address prior to submitting tx to blockchain. If i do contract.deployed() it permanently hangs. If i search address on polygonscan (or mumbai version) they are invalid/never used addresses. The weird thing is it only happens on Polygon networks (Mainnet and Mumbai)
it creates a new address each time but no tx hash bc its not a valid transaction |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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).
In the frontend, when I try to run
getContractAddress()
usingtx.from
&tx.nonce
, I get an address back, but it's not the one that got created.Environment:
Hardhat, MetaMask, Ethers.js, Next.js
Beta Was this translation helpful? Give feedback.
All reactions