Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for create2 in ethers.utils.getContractAddress ? #697

Closed
julien51 opened this issue Jan 3, 2020 · 5 comments
Closed

Support for create2 in ethers.utils.getContractAddress ? #697

julien51 opened this issue Jan 3, 2020 · 5 comments
Labels
enhancement New feature or improvement.

Comments

@julien51
Copy link

julien51 commented Jan 3, 2020

We are using ethers.utils.getContractAddress in order to determine the address of an upcoming contract (using the sender's address and its nonce).

As we are moving to using create2, this approach does not yield the correct address.
Is there any plan to support create2 for getContractAddress?

@ricmoo
Copy link
Member

ricmoo commented Jan 3, 2020

Oh yes! I meant to add this earlier.

I have the necessary function in another project and will add it to both v4 and v5 today.

Thanks for the reminder. :)

@ricmoo ricmoo added enhancement New feature or improvement. on-deck This Enhancement or Bug is currently being worked on. labels Jan 3, 2020
@ricmoo
Copy link
Member

ricmoo commented Jan 4, 2020

I've added utils.getCreate2Address. Let me know if you have any problems:

const addr = utils.getCreate2Address({
    from: fromAddress, // Probably the contract. address
    salt: someSalt,
    initCode: initCode
});

Or if you have the initCode hash instead:

const addr = utils.getCreate2Address({
    from: fromAddress,
    salt: someSalt,
    initCodeHash: initCodeHash
});

The v5 version will only support the latter option, I think, but I'd be curious which you use more often.

@ricmoo ricmoo removed the on-deck This Enhancement or Bug is currently being worked on. label Jan 4, 2020
@ricmoo
Copy link
Member

ricmoo commented Jan 6, 2020

Closing this now, but please feel free to re-open (or open a new issue) if you have any problems.

Thanks! :)

@b4vtech
Copy link

b4vtech commented Dec 8, 2021

Hi, I am trying the getCreate2Address function, but it is not giving me the addresses that are finally created.
I think it has to do because of the constructor arguments. Checking in the Solidity docs, the address is also calculated by the constructor arguments: https://docs.soliditylang.org/en/v0.8.0/control-structures.html#salted-contract-creations-create2

With the ethers function getCreate2Address we can not pass the arguments and I think that's why it doesn't coincide.
Is there anyway I can pass those arguments or do you know how to create the init code with constructor arguments, please?

Thanks a lot!

@ricmoo
Copy link
Member

ricmoo commented Dec 8, 2021

You have to pass in the initcode, which is the deployment bytecode concatenated with the ABI encoded arguments. You can use the defaultAbiCoder.encode(types, values) to compute this.

That should give you the address you are expecting.

Let me know if that works for you. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improvement.
Projects
None yet
Development

No branches or pull requests

3 participants