Skip to content

Commit

Permalink
Merge branch 'main' into anvil-server-config
Browse files Browse the repository at this point in the history
  • Loading branch information
Romsters authored Dec 12, 2024
2 parents d54b085 + 1c42504 commit f620ac4
Show file tree
Hide file tree
Showing 14 changed files with 1,459 additions and 1,117 deletions.
6 changes: 2 additions & 4 deletions e2e-tests/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { HardhatUserConfig } from "hardhat/config";

import "@matterlabs/hardhat-zksync-deploy";
import "@matterlabs/hardhat-zksync-solc";
import "@nomiclabs/hardhat-ethers";
import "@matterlabs/hardhat-zksync";

const config: HardhatUserConfig = {
zksolc: {
version: "1.5.7",
version: "1.5.8",
settings: {},
},
defaultNetwork: "zkSyncTestnet",
Expand Down
4 changes: 2 additions & 2 deletions e2e-tests/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Deployer } from "@matterlabs/hardhat-zksync-deploy";
import { Deployer } from "@matterlabs/hardhat-zksync";
import { expect } from "chai";
import { Contract, Provider } from "zksync-web3";
import { Contract, Provider } from "zksync-ethers";
import * as hre from "hardhat";
import { HttpNetworkUserConfig } from "hardhat/types";

Expand Down
10 changes: 4 additions & 6 deletions e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
"license": "MIT",
"devDependencies": {
"@matterlabs/eslint-config-vue": "^1.1.4",
"@matterlabs/hardhat-zksync-deploy": "^0.6.5",
"@matterlabs/hardhat-zksync-solc": "^0.4.2",
"@matterlabs/hardhat-zksync": "^1.3.0",
"@matterlabs/prettier-config": "^1.0.3",
"@matterlabs/zksync-contracts": "0.6.1",
"@nomiclabs/hardhat-ethers": "^2.2.3",
"@openzeppelin/contracts": "^4.9.3",
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
Expand All @@ -22,14 +20,14 @@
"eslint": "^8.48.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-mocha": "^10.2.0",
"ethers": "^5.7.2",
"hardhat": "^2.12.4",
"ethers": "^6.13.4",
"hardhat": "^2.22.17",
"mocha": "^10.2.0",
"mocha-multi": "^1.1.7",
"prettier": "^3.0.3",
"ts-node": "^10.9.1",
"typescript": "^4.9.4",
"zksync-web3": "^0.14.3"
"zksync-ethers": "^6.15.3"
},
"prettier": "@matterlabs/prettier-config",
"scripts": {
Expand Down
100 changes: 49 additions & 51 deletions e2e-tests/test/anvil-apis.test.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
import { expect } from "chai";
import { Wallet } from "zksync-web3";
import { Wallet } from "zksync-ethers";
import { deployContract, expectThrowsAsync, getTestProvider } from "../helpers/utils";
import { RichAccounts } from "../helpers/constants";
import { ethers } from "hardhat";
import { Deployer } from "@matterlabs/hardhat-zksync-deploy";
import * as hre from "hardhat";
import { keccak256 } from "ethers/lib/utils";
import { BigNumber } from "ethers";
import * as fs from "node:fs";

const provider = getTestProvider();

describe("anvil_setNextBlockBaseFeePerGas", function () {
it("Should change gas price", async function () {
const oldBaseFee = (await provider.getGasPrice()).toNumber();
const expectedNewBaseFee = oldBaseFee + 42;
const oldBaseFee = await provider.getGasPrice();
const expectedNewBaseFee = oldBaseFee + 42n;

// Act
await provider.send("anvil_setNextBlockBaseFeePerGas", [ethers.utils.hexlify(expectedNewBaseFee)]);
await provider.send("anvil_setNextBlockBaseFeePerGas", [ethers.toBeHex(expectedNewBaseFee)]);

// Assert
const newBaseFee = (await provider.getGasPrice()).toNumber();
const newBaseFee = await provider.getGasPrice();
expect(newBaseFee).to.equal(expectedNewBaseFee);

// Revert
await provider.send("anvil_setNextBlockBaseFeePerGas", [ethers.utils.hexlify(oldBaseFee)]);
await provider.send("anvil_setNextBlockBaseFeePerGas", [ethers.toBeHex(oldBaseFee)]);
});

it("Should produce a block with new gas price", async function () {
const wallet = new Wallet(RichAccounts[0].PrivateKey, provider);
const userWallet = Wallet.createRandom().connect(provider);
const oldBaseFee = (await provider.getGasPrice()).toNumber();
const expectedNewBaseFee = oldBaseFee + 42;
const oldBaseFee = await provider.getGasPrice();
const expectedNewBaseFee = oldBaseFee + 42n;

// Act
await provider.send("anvil_setNextBlockBaseFeePerGas", [ethers.utils.hexlify(expectedNewBaseFee)]);
await provider.send("anvil_setNextBlockBaseFeePerGas", [ethers.toBeHex(expectedNewBaseFee)]);

const txResponse = await wallet.sendTransaction({
to: userWallet.address,
value: ethers.utils.parseEther("0.1"),
value: ethers.parseEther("0.1"),
});
const txReceipt = await txResponse.wait();
const newBlock = await provider.getBlock(txReceipt.blockNumber);
expect(newBlock.baseFeePerGas?.toNumber()).to.equal(expectedNewBaseFee);
expect(newBlock.baseFeePerGas).to.equal(expectedNewBaseFee);

// Revert
await provider.send("anvil_setNextBlockBaseFeePerGas", [ethers.utils.hexlify(oldBaseFee)]);
await provider.send("anvil_setNextBlockBaseFeePerGas", [ethers.toBeHex(oldBaseFee)]);
});
});

Expand All @@ -63,7 +61,7 @@ describe("anvil_setBlockTimestampInterval & anvil_removeBlockTimestampInterval",

const txResponse = await wallet.sendTransaction({
to: userWallet.address,
value: ethers.utils.parseEther("0.1"),
value: ethers.parseEther("0.1"),
});
const txReceipt = await txResponse.wait();

Expand All @@ -80,7 +78,7 @@ describe("anvil_setBlockTimestampInterval & anvil_removeBlockTimestampInterval",

const txResponse2 = await wallet.sendTransaction({
to: userWallet.address,
value: ethers.utils.parseEther("0.1"),
value: ethers.parseEther("0.1"),
});
const txReceipt2 = await txResponse2.wait();

Expand All @@ -102,7 +100,7 @@ describe("anvil_setLoggingEnabled", function () {

await wallet.sendTransaction({
to: userWallet.address,
value: ethers.utils.parseEther("0.1"),
value: ethers.parseEther("0.1"),
});
const logSizeAfter = fs.statSync("../anvil-zksync.log").size;

Expand All @@ -127,7 +125,7 @@ describe("anvil_snapshot", function () {

// Assert
expect(await greeter.greet()).to.eq("Hi");
expect(BigNumber.from(snapshotId2).toString()).to.eq(BigNumber.from(snapshotId1).add(1).toString());
expect(BigInt(snapshotId2)).to.eq(BigInt(snapshotId1) + 1n);
});
});

Expand Down Expand Up @@ -165,7 +163,7 @@ describe("anvil_increaseTime", function () {

const txResponse = await wallet.sendTransaction({
to: userWallet.address,
value: ethers.utils.parseEther("0.1"),
value: ethers.parseEther("0.1"),
});
await txResponse.wait();
expectedTimestamp += 2; // New transaction will add two blocks
Expand All @@ -190,7 +188,7 @@ describe("anvil_setNextBlockTimestamp", function () {

const txResponse = await wallet.sendTransaction({
to: userWallet.address,
value: ethers.utils.parseEther("0.1"),
value: ethers.parseEther("0.1"),
});
await txResponse.wait();
expectedTimestamp += 1; // After executing a transaction, the node puts it into a block and increases its current timestamp
Expand All @@ -215,7 +213,7 @@ describe("anvil_setTime", function () {

const txResponse = await wallet.sendTransaction({
to: userWallet.address,
value: ethers.utils.parseEther("0.1"),
value: ethers.parseEther("0.1"),
});
await txResponse.wait();
expectedTimestamp += 2; // New transaction will add two blocks
Expand All @@ -229,15 +227,15 @@ describe("anvil_setTime", function () {
describe("anvil_setBalance", function () {
it("Should update the balance of an account", async function () {
// Arrange
const userWallet = Wallet.createRandom().connect(provider);
const newBalance = ethers.utils.parseEther("42");
const userWallet = new Wallet(Wallet.createRandom().privateKey).connect(provider);
const newBalance = ethers.parseEther("42");

// Act
await provider.send("anvil_setBalance", [userWallet.address, newBalance._hex]);
await provider.send("anvil_setBalance", [userWallet.address, ethers.toBeHex(newBalance)]);

// Assert
const balance = await userWallet.getBalance();
expect(balance.eq(newBalance)).to.true;
expect(balance).to.eq(newBalance);
});
});

Expand All @@ -251,7 +249,7 @@ describe("anvil_setNonce", function () {
async function assertCanSendTx() {
const tx = {
to: userWallet.address,
value: ethers.utils.parseEther("0.42"),
value: ethers.parseEther("0.42"),
};

const txResponse = await richWallet.sendTransaction(tx);
Expand All @@ -262,14 +260,14 @@ describe("anvil_setNonce", function () {
const newNonce = 42;

// Advance nonce to 42
await provider.send("anvil_setNonce", [richWallet.address, ethers.utils.hexlify(newNonce)]);
await provider.send("anvil_setNonce", [richWallet.address, ethers.toBeHex(newNonce)]);

// Assert
expect(await richWallet.getNonce()).to.equal(newNonce);
await assertCanSendTx();

// Rollback nonce to 0
await provider.send("anvil_setNonce", [richWallet.address, ethers.utils.hexlify(0)]);
await provider.send("anvil_setNonce", [richWallet.address, ethers.toBeHex(0)]);

// Assert
expect(await richWallet.getNonce()).to.equal(0);
Expand All @@ -286,7 +284,7 @@ describe("anvil_mine", function () {
const startingTimestamp: number = await provider.send("config_getCurrentTimestamp", []);

// Act
await provider.send("anvil_mine", [ethers.utils.hexlify(numberOfBlocks), ethers.utils.hexlify(intervalInSeconds)]);
await provider.send("anvil_mine", [ethers.toBeHex(numberOfBlocks), ethers.toBeHex(intervalInSeconds)]);

// Assert
const latestBlock = await provider.getBlock("latest");
Expand All @@ -301,7 +299,7 @@ describe("anvil_mine", function () {
describe("anvil_impersonateAccount & anvil_stopImpersonatingAccount", function () {
it("Should allow transfers of funds without knowing the Private Key", async function () {
// Arrange
const userWallet = Wallet.createRandom().connect(provider);
const userWallet = new Wallet(Wallet.createRandom().privateKey).connect(provider);
const richAccount = RichAccounts[5].Account;
const beforeBalance = await provider.getBalance(richAccount);

Expand All @@ -311,7 +309,7 @@ describe("anvil_impersonateAccount & anvil_stopImpersonatingAccount", function (
const signer = await ethers.getSigner(richAccount);
const tx = {
to: userWallet.address,
value: ethers.utils.parseEther("0.42"),
value: ethers.parseEther("0.42"),
};

const recieptTx = await signer.sendTransaction(tx);
Expand All @@ -320,15 +318,15 @@ describe("anvil_impersonateAccount & anvil_stopImpersonatingAccount", function (
await provider.send("anvil_stopImpersonatingAccount", [richAccount]);

// Assert
expect((await userWallet.getBalance()).eq(ethers.utils.parseEther("0.42"))).to.true;
expect((await provider.getBalance(richAccount)).eq(beforeBalance.sub(ethers.utils.parseEther("0.42")))).to.true;
expect(await userWallet.getBalance()).to.eq(ethers.parseEther("0.42"));
expect(await provider.getBalance(richAccount)).to.eq(beforeBalance - ethers.parseEther("0.42"));
});
});

describe("anvil_autoImpersonateAccount", function () {
it("Should allow transfers of funds without knowing the Private Key", async function () {
// Arrange
const userWallet = Wallet.createRandom().connect(provider);
const userWallet = new Wallet(Wallet.createRandom().privateKey).connect(provider);
const richAccount = RichAccounts[6].Account;
const beforeBalance = await provider.getBalance(richAccount);

Expand All @@ -338,7 +336,7 @@ describe("anvil_autoImpersonateAccount", function () {
const signer = await ethers.getSigner(richAccount);
const tx = {
to: userWallet.address,
value: ethers.utils.parseEther("0.42"),
value: ethers.parseEther("0.42"),
};

const recieptTx = await signer.sendTransaction(tx);
Expand All @@ -347,8 +345,8 @@ describe("anvil_autoImpersonateAccount", function () {
await provider.send("anvil_autoImpersonateAccount", [false]);

// Assert
expect((await userWallet.getBalance()).eq(ethers.utils.parseEther("0.42"))).to.true;
expect((await provider.getBalance(richAccount)).eq(beforeBalance.sub(ethers.utils.parseEther("0.42")))).to.true;
expect(await userWallet.getBalance()).to.eq(ethers.parseEther("0.42"));
expect(await provider.getBalance(richAccount)).to.eq(beforeBalance - ethers.parseEther("0.42"));
});
});

Expand All @@ -370,7 +368,7 @@ describe("anvil_setCode", function () {
const result = await provider.send("eth_call", [
{
to: address,
data: keccak256(ethers.utils.toUtf8Bytes("value()")).substring(0, 10),
data: ethers.keccak256(ethers.toUtf8Bytes("value()")).substring(0, 10),
from: wallet.address,
gas: "0x1000",
gasPrice: "0x0ee6b280",
Expand All @@ -379,7 +377,7 @@ describe("anvil_setCode", function () {
},
"latest",
]);
expect(BigNumber.from(result).toNumber()).to.eq(5);
expect(BigInt(result)).to.eq(5n);
});

it("Should reject invalid code", async function () {
Expand Down Expand Up @@ -411,13 +409,13 @@ describe("anvil_setCode", function () {
const newContractCode = artifact.deployedBytecode;

// Act
await provider.send("anvil_setCode", [greeter.address, newContractCode]);
await provider.send("anvil_setCode", [await greeter.getAddress(), newContractCode]);

// Assert
const result = await provider.send("eth_call", [
{
to: greeter.address,
data: keccak256(ethers.utils.toUtf8Bytes("value()")).substring(0, 10),
to: await greeter.getAddress(),
data: ethers.keccak256(ethers.toUtf8Bytes("value()")).substring(0, 10),
from: wallet.address,
gas: "0x1000",
gasPrice: "0x0ee6b280",
Expand All @@ -426,30 +424,30 @@ describe("anvil_setCode", function () {
},
"latest",
]);
expect(BigNumber.from(result).toNumber()).to.eq(5);
expect(BigInt(result)).to.eq(5n);
});
});

describe("anvil_setStorageAt", function () {
it("Should set storage at an address", async function () {
const wallet = new Wallet(RichAccounts[0].PrivateKey, provider);
const userWallet = Wallet.createRandom().connect(provider);
const userWallet = new Wallet(Wallet.createRandom().privateKey).connect(provider);
await wallet.sendTransaction({
to: userWallet.address,
value: ethers.utils.parseEther("3"),
value: ethers.parseEther("3"),
});

const deployer = new Deployer(hre, userWallet);
const artifact = await deployer.loadArtifact("MyERC20");
const token = await deployer.deploy(artifact, ["MyToken", "MyToken", 18]);

const before = await provider.send("eth_getStorageAt", [token.address, "0x0", "latest"]);
expect(BigNumber.from(before).toNumber()).to.eq(0);
const before = await provider.send("eth_getStorageAt", [await token.getAddress(), "0x0", "latest"]);
expect(BigInt(before)).to.eq(0n);

const value = ethers.utils.hexlify(ethers.utils.zeroPad("0x10", 32));
await provider.send("anvil_setStorageAt", [token.address, "0x0", value]);
const value = ethers.hexlify(ethers.zeroPadValue("0x10", 32));
await provider.send("anvil_setStorageAt", [await token.getAddress(), "0x0", value]);

const after = await provider.send("eth_getStorageAt", [token.address, "0x0", "latest"]);
expect(BigNumber.from(after).toNumber()).to.eq(16);
const after = await provider.send("eth_getStorageAt", [await token.getAddress(), "0x0", "latest"]);
expect(BigInt(after)).to.eq(16n);
});
});
Loading

0 comments on commit f620ac4

Please sign in to comment.