Skip to content

Commit

Permalink
feat: makeAddr and makeAddrAndKey (#146)
Browse files Browse the repository at this point in the history
* feat: makeAddr and makeAddrAndKey

Two small quality-of-life functions to easily create labeled addresses.

Inspired by https://twitter.com/eth_call/status/1549792921976803328

* ♻️ Remove bytes conversion

Co-authored-by: ZeroEkkusu <94782988+ZeroEkkusu@users.noreply.github.com>
  • Loading branch information
karmacoma-eth and ZeroEkkusu authored Aug 3, 2022
1 parent 1e6f1d2 commit 2c7cbfc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ abstract contract Test is DSTest, Script {
vm.startPrank(who);
}

// creates a labeled address and the corresponding private key
function makeAddrAndKey(string memory name) internal returns(address addr, uint256 privateKey) {
privateKey = uint256(keccak256(abi.encodePacked(name)));
addr = vm.addr(privateKey);
vm.label(addr, name);
}

// creates a labeled address
function makeAddr(string memory name) internal returns(address addr) {
(addr,) = makeAddrAndKey(name);
}

// DEPRECATED: Use `deal` instead
function tip(address token, address to, uint256 give) internal {
emit log_named_string("WARNING", "Test tip(address,address,uint256): The `tip` stdcheat has been deprecated. Use `deal` instead.");
Expand Down
15 changes: 14 additions & 1 deletion src/test/StdCheats.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ contract StdCheatsTest is Test {
vm.stopPrank();
}

function testMakeAddrEquivalence() public {
(address addr, ) = makeAddrAndKey("1337");
assertEq(makeAddr("1337"), addr);
}

function testMakeAddrSigning() public {
(address addr, uint256 key) = makeAddrAndKey("1337");
bytes32 hash = keccak256("some_message");

(uint8 v, bytes32 r, bytes32 s) = vm.sign(key, hash);
assertEq(ecrecover(hash, v, r, s), addr);
}

function testDeal() public {
deal(address(this), 1 ether);
assertEq(address(this).balance, 1 ether);
Expand Down Expand Up @@ -157,7 +170,7 @@ contract StdCheatsTest is Test {
function deployCodeHelper(string memory what) external {
deployCode(what);
}

function testDeployCodeFail() public {
vm.expectRevert(bytes("Test deployCode(string): Deployment failed."));
this.deployCodeHelper("StdCheats.t.sol:RevertingContract");
Expand Down

4 comments on commit 2c7cbfc

@PaulRBerg
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this also be added to the v0.3 branch?

@ZeroEkkusu
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can resolve it when we decide to merge v0.3?

@PaulRBerg
Copy link
Contributor

Choose a reason for hiding this comment

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

Fair enough.

@ZeroEkkusu
Copy link
Collaborator

Choose a reason for hiding this comment

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

I also wanted to use v0.3 the other day but stayed on master to get new features. Should be soon.

Please sign in to comment.