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: add vm.addr cheatcode #25

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions e2e-tests/contracts/TestCheatcodes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ pragma solidity ^0.8.0;
contract TestCheatcodes {
address constant CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D;

function testAddr(uint256 privateKey, address addr) external {
(bool success, bytes memory data) = CHEATCODE_ADDRESS.call(abi.encodeWithSignature("addr(uint256)", privateKey));
require(success, "addr failed");
address recovered = abi.decode(data, (address));
require(recovered == addr, "address mismatch");
}

function testDeal(address account, uint256 amount) external {
uint balanceBefore = address(account).balance;
(bool success, ) = CHEATCODE_ADDRESS.call(abi.encodeWithSignature("deal(address,uint256)", account, amount));
Expand Down
17 changes: 17 additions & 0 deletions e2e-tests/test/cheatcodes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ import { deployContract, getTestProvider } from "../helpers/utils";
const provider = getTestProvider();

describe("Cheatcodes", function () {
it("Should test vm.addr", async function () {
// Arrange
const wallet = new Wallet(RichAccounts[0].PrivateKey);
const deployer = new Deployer(hre, wallet);
const randomWallet = Wallet.createRandom().connect(provider);

// Act
const greeter = await deployContract(deployer, "TestCheatcodes", []);
const tx = await greeter.testAddr(randomWallet.privateKey, randomWallet.address, {
gasLimit: 1000000,
});
const receipt = await tx.wait();

// Assert
expect(receipt.status).to.eq(1);
});

it("Should test vm.deal", async function () {
// Arrange
const wallet = new Wallet(RichAccounts[0].PrivateKey);
Expand Down
13 changes: 12 additions & 1 deletion src/cheatcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
abigen!(
CheatcodeContract,
r#"[
function addr(uint256 privateKey)
function deal(address who, uint256 newBalance)
function etch(address who, bytes calldata code)
function setNonce(address account, uint64 nonce)
Expand Down Expand Up @@ -170,6 +171,16 @@
) {
use CheatcodeContractCalls::*;
match call {
Addr(AddrCall { private_key }) => {
tracing::info!("Getting address for private key");
let Ok(address) = zksync_types::PackedEthSignature::address_from_private_key(
&u256_to_h256(private_key),
) else {
tracing::error!("Failed generating address for private key");
return;
};
self.returndata = Some(vec![h256_to_u256(address.into())]);
}
Deal(DealCall { who, new_balance }) => {
tracing::info!("Setting balance for {who:?} to {new_balance}");
storage
Expand Down Expand Up @@ -205,7 +216,7 @@
account_nonce.as_u64()
);
tracing::info!("👷 Setting returndata",);
self.returndata = Some(vec![account_nonce.into()]);
self.returndata = Some(vec![account_nonce]);
}
SetNonce(SetNonceCall { account, nonce }) => {
tracing::info!("Setting nonce for {account:?} to {nonce}");
Expand Down Expand Up @@ -318,14 +329,14 @@
use crate::{
deps::system_contracts::bytecode_from_slice,
http_fork_source::HttpForkSource,
node::{InMemoryNode, TransactionResult},

Check failure on line 332 in src/cheatcodes.rs

View workflow job for this annotation

GitHub Actions / unit-tests (macos-latest)

unused imports: `LogBuilder`, `TransactionBuilder`, `TransactionResult`
testing::{self, LogBuilder, TransactionBuilder},
};
use ethers::abi::{short_signature, AbiEncode, HumanReadableParser, ParamType, Token};

Check failure on line 335 in src/cheatcodes.rs

View workflow job for this annotation

GitHub Actions / unit-tests (macos-latest)

unused imports: `AbiEncode`, `HumanReadableParser`, `ParamType`, `Token`
use zksync_basic_types::{Address, L2ChainId, Nonce, H160, H256, U256};
use zksync_core::api_server::web3::backend_jsonrpc::namespaces::eth::EthNamespaceT;
use zksync_types::{
api::{Block, CallTracerConfig, SupportedTracers, TransactionReceipt},

Check failure on line 339 in src/cheatcodes.rs

View workflow job for this annotation

GitHub Actions / unit-tests (macos-latest)

unused imports: `Block`, `CallTracerConfig`, `SupportedTracers`, `TransactionReceipt`, `transaction_request::CallRequestBuilder`
fee::Fee,
l2::L2Tx,
transaction_request::CallRequestBuilder,
Expand Down
Loading