Skip to content

Commit

Permalink
feat(vm): get broadcasts (#627)
Browse files Browse the repository at this point in the history
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
    }
}
```
  • Loading branch information
yash-atreya authored Nov 4, 2024
1 parent 1eea5ba commit da591f5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
65 changes: 65 additions & 0 deletions src/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/Vm.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ contract VmTest is Test {
}

function test_VmSafeInterfaceId() public pure {
assertEq(type(VmSafe).interfaceId, bytes4(0xb09496a4), "VmSafe");
assertEq(type(VmSafe).interfaceId, bytes4(0x590bc2b4), "VmSafe");
}
}

0 comments on commit da591f5

Please sign in to comment.