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

feat(cheatcodes): access broadcast artifacts #9107

Merged
merged 56 commits into from
Oct 30, 2024

Conversation

yash-atreya
Copy link
Member

@yash-atreya yash-atreya commented Oct 14, 2024

Motivation

Builds upon #9098

Closes #4732 + partially addresses #9083
Closes #6807

#9083 requires access to the broadcast artifacts while the script is running. This isn't possible with the current script design as txs are broadcasted and their respective artifacts are written after the execution of the script is complete.

Solution

// Returns the most recent broadcast for the given contract on `chainId` matching `txType`.
function getBroadcast(string memory contractName, uint64 chainId, BroadcastTxType txType) external returns (BroadcastTxSummary memory);

//  Returns all broadcasts for the given contract on `chainId` with the specified `txType`.
//  Sorted such that the most recent broadcast is the first element, and the oldest is the last. i.e descending order of BroadcastTxSummary.blockNumber.
function getBroadcasts(string memory contractName, uint64 chainId, BroadcastTxType txType) external returns (BroadcastTxSummary[] memory);

// Returns all broadcasts for the given contract on `chainId`.
// Sorted such that the most recent broadcast is the first element, and the oldest is the last. i.e descending order of BroadcastTxSummary.blockNumber.
function getBroadcasts(string memory contractName, uint64 chainId) external returns (BroadcastTxSummary[] memory);

// Returns the most recent deployment for the current `chainId`.
function getDeployment(string memory contractName) external returns (address deployedAddress);

// Returns the most recent deployment for the given contract on `chainId`
function getDeployment(string memory contractName, uint64 chainId) external returns (address deployedAddress);

// Returns all deployments for the given contract on `chainId`
// Sorted in descending order of deployment time i.e descending order of BroadcastTxSummary.blockNumber.
// The most recent deployment is the first element, and the oldest is the last.
function getDeployments(string memory contractName, uint64 chainId) external returns (address[] memory deployedAddresses);

These cheatcodes enable the following behaviour:

  • Deploy a contract forge script CounterDeployScript --broadcast
  • Access the broadcast artifacts forge script BroadcastCollector
contract BroadcastCollector is Script {
    function run() public {
        // Get the most recent counter deployment
        Vm.BroadcastTxSummary memory broadcast = Vm(address(vm)).getBroadcast(
            "Counter",
            31337,
            Vm.BroadcastTxType.Create
        );

        console.logBytes32(broadcast.txHash);
        console.logAddress(broadcast.contractAddress); // New contract address
    }
}
  • Tests

@yash-atreya yash-atreya changed the base branch from master to yash/refac-script-sequence October 14, 2024 13:11
@yash-atreya yash-atreya changed the title Yash/get deployment data feat(cheatcodes): access broadcast artifacts Oct 14, 2024
@yash-atreya yash-atreya marked this pull request as draft October 14, 2024 13:12
@yash-atreya yash-atreya self-assigned this Oct 14, 2024
Base automatically changed from yash/refac-script-sequence to master October 15, 2024 11:06
@yash-atreya yash-atreya marked this pull request as ready for review October 16, 2024 08:56
@yash-atreya yash-atreya marked this pull request as draft October 16, 2024 09:04
@yash-atreya
Copy link
Member Author

I don't think this will close #9083 because it mentions the need for access of transaction hashes during script execution, which is not really feasible with current script design. This PR only adds access to artifacts after script was broadcasted

And #4732 mentions access of deployed contract addresses vs broadcasted transactions data, so I think we should either follow its design where you are getting address by string memory contractName ot at least add helpers matching the proposed getDeployment cheats to make API simpler

@klkvr added the getDeployment helpers.

@yash-atreya yash-atreya enabled auto-merge (squash) October 28, 2024 10:57
Copy link
Member

@klkvr klkvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, looking good

just have nits

crates/script-sequence/src/reader.rs Outdated Show resolved Hide resolved
crates/cheatcodes/src/fs.rs Outdated Show resolved Hide resolved
crates/script-sequence/src/reader.rs Outdated Show resolved Hide resolved
crates/cheatcodes/src/fs.rs Outdated Show resolved Hide resolved
crates/cheatcodes/spec/src/vm.rs Show resolved Hide resolved
crates/cheatcodes/spec/src/vm.rs Show resolved Hide resolved
@yash-atreya yash-atreya merged commit 2bb446e into master Oct 30, 2024
22 checks passed
@yash-atreya yash-atreya deleted the yash/get-deployment-data branch October 30, 2024 11:32
yash-atreya added a commit to foundry-rs/forge-std that referenced this pull request Nov 4, 2024
Add cheatcodes introduced in
foundry-rs/foundry#9107.

```solidity

// Returns the most recent broadcast for the given contract on `chainId` matching `txType`.
function getBroadcast(string memory contractName, uint64 chainId, BroadcastTxType txType) external returns (BroadcastTxSummary memory);

//  Returns all broadcasts for the given contract on `chainId` with the specified `txType`.
//  Sorted such that the most recent broadcast is the first element, and the oldest is the last. i.e descending order of BroadcastTxSummary.blockNumber.
function getBroadcasts(string memory contractName, uint64 chainId, BroadcastTxType txType) external returns (BroadcastTxSummary[] memory);

// Returns all broadcasts for the given contract on `chainId`.
// Sorted such that the most recent broadcast is the first element, and the oldest is the last. i.e descending order of BroadcastTxSummary.blockNumber.
function getBroadcasts(string memory contractName, uint64 chainId) external returns (BroadcastTxSummary[] memory);

// Returns the most recent deployment for the current `chainId`.
function getDeployment(string memory contractName) external returns (address deployedAddress);

// Returns the most recent deployment for the given contract on `chainId`
function getDeployment(string memory contractName, uint64 chainId) external returns (address deployedAddress);

// Returns all deployments for the given contract on `chainId`
// Sorted in descending order of deployment time i.e descending order of BroadcastTxSummary.blockNumber.
// The most recent deployment is the first element, and the oldest is the last.
function getDeployments(string memory contractName, uint64 chainId) external returns (address[] memory deployedAddresses);

```

These cheatcodes enable the following behaviour:

- Deploy a contract `forge script CounterDeployScript --broadcast`
- Access the broadcast artifacts `forge script BroadcastCollector`
```solidity
contract BroadcastCollector is Script {
    function run() public {
        // Get the most recent counter deployment
        Vm.BroadcastTxSummary memory broadcast = Vm(address(vm)).getBroadcast(
            "Counter",
            31337,
            Vm.BroadcastTxType.Create
        );

        console.logBytes32(broadcast.txHash);
        console.logAddress(broadcast.contractAddress); // New contract address
    }
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Completed
3 participants