Skip to content

Commit

Permalink
feat(cheatcodes): getArtifactPathByCode and `getArtifactPathByDep…
Browse files Browse the repository at this point in the history
…loyedCode` (#8938)

* feat(`cheatcodes`): vm.getArtifactPath

* cargo cheats

* nit

* nit

* fix

* test: vm.getArtifactPath

* feat: vm.getArtifactPath(creationCode)

* cheats

* nit

* change seed

* rm vm.getArtifactPath(contractName)

* fmt

* nit

* fix

* nit

* rename

* nit

* fix

---------

Co-authored-by: grandizzy <[email protected]>
  • Loading branch information
yash-atreya and grandizzy authored Sep 26, 2024
1 parent 9a0f66e commit c59d97e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
40 changes: 40 additions & 0 deletions crates/cheatcodes/assets/cheatcodes.json

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

8 changes: 8 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,14 @@ interface Vm {
#[cheatcode(group = Filesystem)]
function writeLine(string calldata path, string calldata data) external;

/// Gets the artifact path from code (aka. creation code).
#[cheatcode(group = Filesystem)]
function getArtifactPathByCode(bytes calldata code) external view returns (string memory path);

/// Gets the artifact path from deployed code (aka. runtime code).
#[cheatcode(group = Filesystem)]
function getArtifactPathByDeployedCode(bytes calldata deployedCode) external view returns (string memory path);

/// Gets the creation bytecode from an artifact file. Takes in the relative path to the json file or the path to the
/// artifact in the form of <path>:<contract>:<version> where <contract> and <version> parts are optional.
#[cheatcode(group = Filesystem)]
Expand Down
28 changes: 28 additions & 0 deletions crates/cheatcodes/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,34 @@ impl Cheatcode for writeLineCall {
}
}

impl Cheatcode for getArtifactPathByCodeCall {
fn apply(&self, state: &mut Cheatcodes) -> Result {
let Self { code } = self;
let (artifact_id, _) = state
.config
.available_artifacts
.as_ref()
.and_then(|artifacts| artifacts.find_by_creation_code(code))
.ok_or_else(|| fmt_err!("no matching artifact found"))?;

Ok(artifact_id.path.to_string_lossy().abi_encode())
}
}

impl Cheatcode for getArtifactPathByDeployedCodeCall {
fn apply(&self, state: &mut Cheatcodes) -> Result {
let Self { deployedCode } = self;
let (artifact_id, _) = state
.config
.available_artifacts
.as_ref()
.and_then(|artifacts| artifacts.find_by_deployed_code(deployedCode))
.ok_or_else(|| fmt_err!("no matching artifact found"))?;

Ok(artifact_id.path.to_string_lossy().abi_encode())
}
}

impl Cheatcode for getCodeCall {
fn apply(&self, state: &mut Cheatcodes) -> Result {
let Self { artifactPath: path } = self;
Expand Down
2 changes: 2 additions & 0 deletions testdata/cheats/Vm.sol

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

37 changes: 37 additions & 0 deletions testdata/default/cheats/GetArtifactPath.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity 0.8.18;

import "ds-test/test.sol";
import "cheats/Vm.sol";

contract DummyForGetArtifactPath {}

contract GetArtifactPathTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

function testGetArtifactPathByCode() public {
DummyForGetArtifactPath dummy = new DummyForGetArtifactPath();
bytes memory dummyCreationCode = type(DummyForGetArtifactPath).creationCode;

string memory root = vm.projectRoot();
string memory path = vm.getArtifactPathByCode(dummyCreationCode);

string memory expectedPath =
string.concat(root, "/out/default/GetArtifactPath.t.sol/DummyForGetArtifactPath.json");

assertEq(path, expectedPath);
}

function testGetArtifactPathByDeployedCode() public {
DummyForGetArtifactPath dummy = new DummyForGetArtifactPath();
bytes memory dummyRuntimeCode = address(dummy).code;

string memory root = vm.projectRoot();
string memory path = vm.getArtifactPathByDeployedCode(dummyRuntimeCode);

string memory expectedPath =
string.concat(root, "/out/default/GetArtifactPath.t.sol/DummyForGetArtifactPath.json");

assertEq(path, expectedPath);
}
}

0 comments on commit c59d97e

Please sign in to comment.