Skip to content

Commit 2f43c7e

Browse files
ZeroEkkusumds1
andauthored
feat(cheats): deployCodeTo (#401)
* feat(cheats): `deployCodeTo` * Update src/StdCheats.sol Co-authored-by: Matt Solomon <[email protected]> * test: fix test --------- Co-authored-by: Matt Solomon <[email protected]>
1 parent 27035c8 commit 2f43c7e

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/StdCheats.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,4 +697,20 @@ abstract contract StdCheats is StdCheatsSafe {
697697
// update owner
698698
stdstore.target(token).sig(0x6352211e).with_key(id).checked_write(to);
699699
}
700+
701+
function deployCodeTo(string memory what, address where) internal virtual {
702+
deployCodeTo(what, "", 0, where);
703+
}
704+
705+
function deployCodeTo(string memory what, bytes memory args, address where) internal virtual {
706+
deployCodeTo(what, args, 0, where);
707+
}
708+
709+
function deployCodeTo(string memory what, bytes memory args, uint256 value, address where) internal virtual {
710+
bytes memory creationCode = vm.getCode(what);
711+
vm.etch(where, abi.encodePacked(creationCode, args));
712+
(bool success, bytes memory runtimeBytecode) = where.call{value: value}("");
713+
require(success, "StdCheats deployCodeTo(string,bytes,uint256,address): Failed to create runtime bytecode.");
714+
vm.etch(where, runtimeBytecode);
715+
}
700716
}

test/StdCheats.t.sol

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,33 @@ contract StdCheatsTest is Test {
366366
&& addr != 0x4e59b44847b379578588920cA78FbF26c0B4956C
367367
);
368368
}
369+
370+
function testCannotDeployCodeTo() external {
371+
vm.expectRevert("StdCheats deployCodeTo(string,bytes,uint256,address): Failed to create runtime bytecode.");
372+
this._revertDeployCodeTo();
373+
}
374+
375+
function _revertDeployCodeTo() external {
376+
deployCodeTo("StdCheats.t.sol:RevertingContract", address(0));
377+
}
378+
379+
function testDeployCodeTo() external {
380+
address arbitraryAddress = makeAddr("arbitraryAddress");
381+
382+
deployCodeTo(
383+
"StdCheats.t.sol:MockContractWithConstructorArgs",
384+
abi.encode(uint256(6), true, bytes20(arbitraryAddress)),
385+
1 ether,
386+
arbitraryAddress
387+
);
388+
389+
MockContractWithConstructorArgs ct = MockContractWithConstructorArgs(arbitraryAddress);
390+
391+
assertEq(arbitraryAddress.balance, 1 ether);
392+
assertEq(ct.x(), 6);
393+
assertTrue(ct.y());
394+
assertEq(ct.z(), bytes20(arbitraryAddress));
395+
}
369396
}
370397

371398
contract StdCheatsMock is StdCheats {
@@ -505,3 +532,15 @@ contract RevertingContract {
505532
revert();
506533
}
507534
}
535+
536+
contract MockContractWithConstructorArgs {
537+
uint256 public immutable x;
538+
bool public y;
539+
bytes20 public z;
540+
541+
constructor(uint256 _x, bool _y, bytes20 _z) payable {
542+
x = _x;
543+
y = _y;
544+
z = _z;
545+
}
546+
}

0 commit comments

Comments
 (0)