Skip to content

Commit

Permalink
Add more e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MexicanAce committed Sep 22, 2023
1 parent c7d9e73 commit 2d6b2da
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 19 deletions.
3 changes: 3 additions & 0 deletions e2e-tests/contracts/Greeter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "hardhat/console.sol";

contract Greeter is Ownable {
string private greeting;
event LogString(string value);

constructor(string memory _greeting) {
greeting = _greeting;
Expand All @@ -18,6 +19,8 @@ contract Greeter is Ownable {
function setGreeting(string memory _greeting) public onlyOwner {
console.log("setGreeting called");
console.log(_greeting);
emit LogString(string.concat("Greeting is being updated to ", _greeting));

require(
keccak256(abi.encodePacked((_greeting))) != keccak256(abi.encodePacked(("test"))),
"Received a test value"
Expand Down
19 changes: 0 additions & 19 deletions e2e-tests/test/evm-apis.test copy.ts

This file was deleted.

92 changes: 92 additions & 0 deletions e2e-tests/test/evm-apis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { expect } from "chai";
import { getTestProvider } from "../helpers/utils";
import { Wallet } from "zksync-web3";
import { RichAccounts } from "../helpers/constants";
import { ethers } from "ethers";

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);
});
});

describe("evm_increaseTime", function () {
it("Should increase current timestamp of the node", async function () {
// Arrange
const timeIncreaseInSeconds = 13;
let expectedTimestamp = (await provider.getBlock("latest")).timestamp;
expectedTimestamp += timeIncreaseInSeconds * 1000;
const wallet = new Wallet(RichAccounts[0].PrivateKey, provider);
const userWallet = Wallet.createRandom().connect(provider);

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

await wallet.sendTransaction({
to: userWallet.address,
value: ethers.utils.parseEther("0.1"),
});
expectedTimestamp += 1; // New transaction will increase timestamp by 1

// Assert
const currentBlockTimestamp = (await provider.getBlock("latest")).timestamp;
expect(currentBlockTimestamp).to.equal(expectedTimestamp);
});
});

describe("evm_setNextBlockTimestamp", function () {
it("Should set current timestamp of the node to specific value", async function () {
// Arrange
const timeIncreaseInMS = 123;
let newTimestamp = (await provider.getBlock("latest")).timestamp;
newTimestamp += timeIncreaseInMS;
const wallet = new Wallet(RichAccounts[0].PrivateKey, provider);
const userWallet = Wallet.createRandom().connect(provider);

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

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

// Assert
const currentBlockTimestamp = (await provider.getBlock("latest")).timestamp;
expect(currentBlockTimestamp).to.equal(newTimestamp);
});
});

describe("evm_setTime", function () {
it("Should set current timestamp of the node to specific value", async function () {
// Arrange
const timeIncreaseInMS = 123;
let newTimestamp = (await provider.getBlock("latest")).timestamp;
newTimestamp += timeIncreaseInMS;
const wallet = new Wallet(RichAccounts[0].PrivateKey, provider);
const userWallet = Wallet.createRandom().connect(provider);

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

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

// Assert
const currentBlockTimestamp = (await provider.getBlock("latest")).timestamp;
expect(currentBlockTimestamp).to.equal(newTimestamp);
});
});
16 changes: 16 additions & 0 deletions e2e-tests/test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,20 @@ describe("Greeter Smart Contract", function () {

await expectThrowsAsync(action, "Ownable: caller is not the owner");
});

it("Should produce event logs", async function () {
// TODO: Figure out a test for this
const wallet = new Wallet(RichAccounts[0].PrivateKey);
const deployer = new Deployer(hre, wallet);

const greeter = await deployContract(deployer, "Greeter", ["Hi"]);

expect(await greeter.greet()).to.eq("Hi");

const setGreetingTx = await greeter.setGreeting("Hola, mundo!");
// wait until the transaction is mined
await setGreetingTx.wait();

expect(await greeter.greet()).to.equal("Hola, mundo!");
});
});
66 changes: 66 additions & 0 deletions e2e-tests/test/zks-apis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { expect } from "chai";
import { getTestProvider } from "../helpers/utils";
import { Wallet } from "zksync-web3";
import { RichAccounts } from "../helpers/constants";
import { ethers } from "ethers";
import { TransactionRequest } from "zksync-web3/build/src/types";

const provider = getTestProvider();

interface Fee {
gas_limit: ethers.BigNumber;
gas_per_pubdata_limit: ethers.BigNumber;
max_fee_per_gas: ethers.BigNumber;
max_priority_fee_per_gas: ethers.BigNumber;
}

describe("zks_estimateFee", function () {
it("Should return fee estimation data for transfer of 1 ETH", async function () {
// Arrange
const wallet = new Wallet(RichAccounts[0].PrivateKey, provider);
const userWallet = Wallet.createRandom().connect(provider);
const transaction: TransactionRequest = {
from: wallet.address,
to: userWallet.address,
value: ethers.utils.parseEther("1")._hex,
};

// Act
const response: Fee = await provider.send("zks_estimateFee", [transaction]);

// Assert
expect(
ethers.BigNumber.from(response.gas_limit).eq(
ethers.BigNumber.from("1228893")
)
).to.true;
expect(
ethers.BigNumber.from(response.gas_per_pubdata_limit).eq(
ethers.BigNumber.from("4080")
)
).to.true;
expect(
ethers.BigNumber.from(response.max_fee_per_gas).eq(
ethers.BigNumber.from("250000000")
)
).to.true;
expect(
ethers.BigNumber.from(response.max_priority_fee_per_gas).eq(
ethers.BigNumber.from("0")
)
).to.true;
});
});

describe("zks_getTokenPrice", function () {
it("Should return fake token Price for ETH", async function () {
// Arrange
const ethAddress = "0x0000000000000000000000000000000000000000";

// Act
const response: string = await provider.send("zks_getTokenPrice", [ethAddress]);

// Assert
expect(response).to.equal("1500");
});
});

0 comments on commit 2d6b2da

Please sign in to comment.