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

feature(cheatcode): vm.predictContractAddr(); pre-determine next deployed addresses #6270

Closed
Anish-Agnihotri opened this issue Nov 9, 2023 · 5 comments · Fixed by #6296
Closed
Assignees
Labels
first issue A good way to start contributing T-feature Type: feature

Comments

@Anish-Agnihotri
Copy link
Contributor

Component

Forge

Describe the feature you would like

Would be useful to have a cheatcode that can pre-compute future contract deployment addresses based on deployer address and nonce:

function predictContractAddr(address deployer, uint256 nonce) external returns (address);

Frequently run into use-cases where the contract ordering is dependent and I need to serialize some address/call for a future-deployed contract.

Additional context

Today, I just copy-paste @k06a's handy addressFrom function into a test-suite library.

function addressFrom(address _origin, uint _nonce) public pure returns (address) {
    bytes memory data;
    if (_nonce == 0x00)          data = abi.encodePacked(bytes1(0xd6), bytes1(0x94), _origin, bytes1(0x80));
    else if (_nonce <= 0x7f)     data = abi.encodePacked(bytes1(0xd6), bytes1(0x94), _origin, uint8(_nonce));
    else if (_nonce <= 0xff)     data = abi.encodePacked(bytes1(0xd7), bytes1(0x94), _origin, bytes1(0x81), uint8(_nonce));
    else if (_nonce <= 0xffff)   data = abi.encodePacked(bytes1(0xd8), bytes1(0x94), _origin, bytes1(0x82), uint16(_nonce));
    else if (_nonce <= 0xffffff) data = abi.encodePacked(bytes1(0xd9), bytes1(0x94), _origin, bytes1(0x83), uint24(_nonce));
    else                         data = abi.encodePacked(bytes1(0xda), bytes1(0x94), _origin, bytes1(0x84), uint32(_nonce));
    return address(uint160(uint256(keccak256(data))));
}
@Anish-Agnihotri Anish-Agnihotri added the T-feature Type: feature label Nov 9, 2023
@Anish-Agnihotri Anish-Agnihotri changed the title feature(cheatcode): vm.predictContractAddr() cheatcode; pre-determine next deployed addresses feature(cheatcode): vm.predictContractAddr(); pre-determine next deployed addresses Nov 9, 2023
@gakonst gakonst added this to Foundry Nov 9, 2023
@github-project-automation github-project-automation bot moved this to Todo in Foundry Nov 9, 2023
@DaniPopes DaniPopes added the first issue A good way to start contributing label Nov 9, 2023
@mattsse
Copy link
Member

mattsse commented Nov 11, 2023

sgtm, we can do the same for create2 with salt.
this is just a single call in rust

@qiweiii
Copy link
Contributor

qiweiii commented Nov 12, 2023

Hi, I would like to try implement this! Any hint on which category I should put this cheatcode under?

@mds1
Copy link
Collaborator

mds1 commented Nov 12, 2023

Thanks @qiweiii, I’ll assign you

We should match the function names and signatures of the forge-std methods that do this: https://github.com/foundry-rs/forge-std/blob/37a37ab73364d6644bfe11edf88a07880f99bd56/src/StdUtils.sol#L97-L134

Additionally, we should PR in a warning event emission to the existing forge-std methods that they’re deprecated in favor of the native cheats

@qiweiii
Copy link
Contributor

qiweiii commented Nov 14, 2023

There is one more task to do😅

Additionally, we should PR in a warning event emission to the existing forge-std methods that they’re deprecated in favor of the native cheats

Does this mean do something like this? https://github.com/foundry-rs/forge-std/blob/37a37ab73364d6644bfe11edf88a07880f99bd56/src/StdCheats.sol#L700

@mds1
Copy link
Collaborator

mds1 commented Nov 14, 2023

Yes, just like that! So the forge-std PR will need that change + an update to Vm.sol to add the new cheats. I think Vm.sol should be organized roughly the same as the foundry cheat definitions, so choose whatever location within VmSafe you think makes the most sense and I can always move it if needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
first issue A good way to start contributing T-feature Type: feature
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants