Skip to content

Commit

Permalink
chore: add more e2e testsd
Browse files Browse the repository at this point in the history
  • Loading branch information
MexicanAce committed Sep 22, 2023
1 parent a6b9675 commit ad62746
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
1 change: 1 addition & 0 deletions e2e-tests/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HardhatUserConfig } from "hardhat/config";

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

const config: HardhatUserConfig = {
zksolc: {
Expand Down
1 change: 1 addition & 0 deletions e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"devDependencies": {
"@matterlabs/hardhat-zksync-deploy": "^0.6.5",
"@matterlabs/hardhat-zksync-solc": "^0.4.2",
"@nomiclabs/hardhat-ethers": "^2.2.3",
"@openzeppelin/contracts": "^4.9.3",
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
Expand Down
24 changes: 24 additions & 0 deletions e2e-tests/test/evm-apis.test copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from "chai";
import {
getTestProvider,
} from "../helpers/utils";

const provider = getTestProvider();

// TODO: Investigate why deploying a smart contract after this crashes the bootloader/VM
xdescribe("evm_mine", function () {
it("Should mine one block", async function () {
// Arrange
const startingBlock = await provider.getBlock("latest");

// Act
await provider.send(
"evm_mine",
[]
);

// Assert
const latestBlock = await provider.getBlock("latest");
expect(latestBlock.number).to.equal(startingBlock.number + 1);
});
});
109 changes: 109 additions & 0 deletions e2e-tests/test/hardhat-apis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { expect } from "chai";
import { Wallet } from "zksync-web3";
import {
getTestProvider,
} from "../helpers/utils";
import { RichAccounts } from "../helpers/constants";
import { ethers } from "hardhat";

const provider = getTestProvider();

describe("hardhat_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");

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

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

describe("hardhat_setNonce", function () {
it("Should update the nonce of an account", async function () {
// Arrange
const userWallet = Wallet.createRandom().connect(provider);
const newNonce = 42;

// Act
await provider.send(
"hardhat_setNonce",
[
userWallet.address,
ethers.utils.hexlify(newNonce),
]
);

// Assert
const nonce = await userWallet.getNonce();
expect(nonce).to.equal(newNonce);
});
});

// TODO: Investigate why deploying a smart contract after this crashes the bootloader/VM
xdescribe("hardhat_mine", function () {
it("Should mine multiple blocks with a given interval", async function () {
// Arrange
const numberOfBlocks = 10;
const intervalInSeconds = 1000;
const startingBlock = await provider.getBlock("latest");

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

// Assert
const latestBlock = await provider.getBlock("latest");
expect(latestBlock.number).to.equal(startingBlock.number + numberOfBlocks, "Block number mismatch");
expect(latestBlock.timestamp).to.equal(startingBlock.timestamp + (numberOfBlocks * intervalInSeconds * 1000), "Timestamp mismatch");
});
});

// TODO: Run this test once eth_sendTransaction has been implemented or when different tests have been added
xdescribe("hardhat_impersonateAccount & hardhat_stopImpersonatingAccount", function () {
it("Should allow transfers of funds without knowing the Private Key", async function () {
// Arrange
const userWallet = Wallet.createRandom().connect(provider);
const beforeBalance = await provider.getBalance(RichAccounts[0].Account);

const actionToTransferWithoutPrivateKey = async () => {

};

// Act
await provider.send(
"hardhat_impersonateAccount",
[
RichAccounts[0].Account,
]
);

const signer = await ethers.getSigner(RichAccounts[0].Account);
const tx = {
to: userWallet.address,
value: ethers.utils.parseEther("0.42"),
};

const recieptTx = await signer.sendTransaction(tx);
await recieptTx.wait();

// Assert
expect(await userWallet.getBalance()).to.equal(ethers.utils.parseEther("0.42"));
expect(await provider.getBalance(RichAccounts[0].Account)).to.equal(beforeBalance.sub(0.42));
});
});
5 changes: 5 additions & 0 deletions e2e-tests/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,11 @@
fs-extra "^7.0.1"
node-fetch "^2.6.0"

"@nomiclabs/hardhat-ethers@^2.2.3":
version "2.2.3"
resolved "https://registry.yarnpkg.com/@nomiclabs/hardhat-ethers/-/hardhat-ethers-2.2.3.tgz#b41053e360c31a32c2640c9a45ee981a7e603fe0"
integrity sha512-YhzPdzb612X591FOe68q+qXVXGG2ANZRvDo0RRUtimev85rCrAlv/TLMEZw5c+kq9AbzocLTVX/h2jVIFPL9Xg==

"@openzeppelin/contracts@^4.9.3":
version "4.9.3"
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.9.3.tgz#00d7a8cf35a475b160b3f0293a6403c511099364"
Expand Down

0 comments on commit ad62746

Please sign in to comment.