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 erc20 paymaster test and refactor testing #38

Merged
merged 4 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 8 additions & 2 deletions .github/workflows/playwright.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ jobs:
strategy:
matrix:
tutorial:
- "tests/how-to-test-contracts.spec.ts"
- "tests/erc20-paymaster.spec.ts"
- "tests/how-to-test-contracts.spec.ts"

steps:
- uses: actions/checkout@v4
Expand All @@ -21,4 +22,9 @@ jobs:
- name: Install Playwright Browsers
run: bun playwright install chromium --with-deps
- name: Run test for ${{ matrix.tutorial }}
run: bun test:github ${{ matrix.tutorial }}
run: |
export TERM=xterm-256color
export COLUMNS=80
export LINES=24
script -q -c "bun test:github ${{ matrix.tutorial }}"

Binary file modified bun.lockb
Binary file not shown.
111 changes: 96 additions & 15 deletions content/tutorials/erc20-paymaster/10.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,26 @@ Atlas is a smart contract IDE that lets you write, deploy, and interact with con

## Setup the Project

1. Initiate a new project by running the command:
First, initialize a new project by running the command:

```sh
npx zksync-cli create custom-paymaster-tutorial --template hardhat_solidity
```
<span id="initialize-hardhat-project" data-test-action></span>

This creates a new ZKsync Era project called `custom-paymaster-tutorial` with a basic `Greeter` contract.
```sh
npx zksync-cli create custom-paymaster-tutorial --template hardhat_solidity
```

1. Navigate into the project directory:
This creates a new ZKsync Era project called `custom-paymaster-tutorial` with a basic `Greeter` contract.

```sh
cd custom-paymaster-tutorial
```
Next, navigate into the project directory and install the dependencies:

<span id="wait-for-init" data-test-action></span>
<span id="npm-install" data-test-action></span>
sarahschwartz marked this conversation as resolved.
Show resolved Hide resolved

```shb
cd custom-paymaster-tutorial && npm install
```

<span id="wait-for-install" data-test-action></span>

::callout{icon="i-heroicons-exclamation-circle"}
The template project includes multiple example contracts. Feel free to delete them.
Expand Down Expand Up @@ -213,7 +220,17 @@ _first_ check that the user provided enough allowance before calling `transferFr

## Paymaster Contract Full Code

Create the `contracts/MyPaymaster.sol` file and copy/paste the following:
Create the `contracts/MyPaymaster.sol` file with

<span id="create-contract-file" data-test-action></span>

```sh
touch contracts/MyPaymaster.sol
```

and copy/paste the following:

<span id="add-paymaster-contract" data-test-action></span>

```solidity
// SPDX-License-Identifier: MIT
Expand Down Expand Up @@ -338,10 +355,18 @@ contract MyPaymaster is IPaymaster {

## Create ERC20 Contract

For the sake of simplicity we will use a modified OpenZeppelin ERC20 implementation:
For the sake of simplicity we will use a modified OpenZeppelin ERC20 implementation.

<span id="create-erc20-contract-file" data-test-action></span>

```sh
touch contracts/MyERC20.sol
```

Create the `contracts/MyERC20.sol` file and copy/paste the following:

<span id="add-erc20-contract" data-test-action></span>

```solidity [contracts/MyERC20.sol]
// SPDX-License-Identifier: UNLICENSED

Expand Down Expand Up @@ -378,7 +403,17 @@ It also mints some `MyERC20` tokens into the account we use to deploy the contra
In addition, the script sends `0.06ETH` to the paymaster contract so it can pay the transaction fees we send later on.

1. In the `deploy` folder, create the file `deploy-paymaster.ts` and copy/paste the following.
Make sure the private key of the account used to deploy the contracts is configured in the `.env` file of the project.:

<span id="create-deploy-file" data-test-action></span>

```sh
touch deploy/deploy-paymaster.ts
```

Make sure the private key of the account used to deploy the contracts is configured in the `.env` file of the project.

<span id="add-testing-private-key" data-test-action></span>
<span id="add-deploy-script" data-test-action></span>

```ts [deploy-paymaster.ts]
import { deployContract, getWallet, getProvider } from "./utils";
Expand Down Expand Up @@ -416,16 +451,41 @@ In addition, the script sends `0.06ETH` to the paymaster contract so it can pay

1. Compile and the contracts from the project root:

```sh
<span id="create-ts-config" data-test-action></span>
<span id="compile-contracts" data-test-action></span>

::code-group

```bash [npx]
npx hardhat compile
```

```bash [yarn]
yarn hardhat compile
```

::

1. Execute the deployment script:

```sh
<span id="use-local-network" data-test-action></span>
<span id="start-local-network" data-test-action></span>
<span id="wait-for-hh-node" data-test-action></span>
<span id="temp-fix-import" data-test-action></span>
<span id="deploy-contracts" data-test-action></span>

::code-group

```bash [npx]
npx hardhat deploy-zksync --script deploy-paymaster.ts
```

```bash [yarn]
yarn hardhat deploy-zksync --script deploy-paymaster.ts
```

::

The output should be roughly the following:

```sh
Expand Down Expand Up @@ -461,10 +521,18 @@ Make sure you delete the `artifacts-zk` and `cache-zk` folders before recompilin

1. Create the `use-paymaster.ts` script in the `deploy` folder, replacing the parameter placeholders with the details from the previous deploy step.

<span id="create-deploy-paymaster-file" data-test-action></span>

```sh
touch deploy/use-paymaster.ts
```

::callout{icon="i-heroicons-exclamation-triangle"}
Make sure you use the private key of the wallet created by the previous script as that wallet contains the ERC20 tokens.
::

<span id="add-use-paymaster" data-test-action></span>

```ts [deploy/use-paymaster.ts]
import { utils, Wallet } from "zksync-ethers";
import { getWallet, getProvider } from "./utils";
Expand Down Expand Up @@ -534,12 +602,25 @@ Make sure you delete the `artifacts-zk` and `cache-zk` folders before recompilin
}
```

<span id="paymaster-address" data-test-action></span>
<span id="token-address" data-test-action></span>

1. Run the script:

```sh
<span id="run-use-paymaster" data-test-action></span>

::code-group

```bash [npx]
npx hardhat deploy-zksync --script use-paymaster.ts
```

```bash [yarn]
yarn hardhat deploy-zksync --script use-paymaster.ts
```

::

The output should look something like this:

```txt
Expand Down
33 changes: 15 additions & 18 deletions content/tutorials/how-to-test-contracts/10.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ During the alpha phase, ZKsync Era Test Nodes are currently undergoing developme

First, initialize a new Hardhat TypeScript project:

<span id="initialize-hardhat-project" data-name="runCommand"></span>
<span id="initialize-hardhat-project" data-test-action></span>

```bash
npx hardhat init
Expand All @@ -36,7 +36,7 @@ Select the `Create a TypeScript project` option and install the sample project's

To install the `hardhat-zksync` plugin, execute the following command:

<span id="install-hh-zksync" data-name="runCommand" data-command-folder="tests-output/hardhat-project"></span>
<span id="install-hh-zksync" data-test-action></span>

::code-group

Expand All @@ -52,8 +52,7 @@ yarn add -D @matterlabs/hardhat-zksync

Once installed, add the plugin at the top of your `hardhat.config.ts` file.

<span id="import-zksync-config" data-name="modifyFile" data-filepath="tests-output/hardhat-project/hardhat.config.ts"
data-at-line="3"></span>
<span id="import-zksync-config" data-test-action></span>

```ts [hardhat.config.ts]
import "@matterlabs/hardhat-zksync";
Expand All @@ -63,7 +62,7 @@ import "@matterlabs/hardhat-zksync";

You can now safely run the **ZKsync Era Test Node** with the following command:

<span id="run-hh-node" data-name="runCommand" data-command-folder="tests-output/hardhat-project" data-pre-command="bun pm2 start '<COMMAND>' --name hh-zknode"></span>
<span id="run-hh-node" data-test-action></span>

::code-group

Expand All @@ -77,9 +76,9 @@ yarn hardhat node-zksync

::

<span id="wait-for-hh-node" data-name="wait" data-timeout="6000"></span>
<span id="wait-for-hh-node" data-test-action></span>

<span id="test-hh-node" data-name="checkIfBalanceIsZero" data-network-url="http://127.0.0.1:8011" data-address="0xe2b8Cb53a43a56d4d2AB6131C81Bd76B86D3AFe5"></span>
<span id="test-hh-node" data-test-action></span>

::callout{icon="i-heroicons-exclamation-circle"}
We'll want to verify the correctness of our installations and test if we can run a **ZKsync Era Test Node**,
Expand Down Expand Up @@ -107,8 +106,7 @@ To enable the usage of ZKsync Era Test Node in Hardhat,
add the `zksync:true` option to the hardhat network in the `hardhat.config.ts` file
and the `latest` version of `zksolc`:

<span id="zksync-hh-network" data-name="modifyFile" data-filepath="tests-output/hardhat-project/hardhat.config.ts"
data-at-line="7"></span>
<span id="zksync-hh-network" data-test-action></span>

```ts
zksolc: {
Expand All @@ -128,7 +126,7 @@ it's necessary to use the `hardhat-chai-matchers` plugin.

In the root directory of your project, execute this command:

<span id="install-chai-ethers" data-name="runCommand" data-command-folder="tests-output/hardhat-project" data-pre-command="<COMMAND>"></span>
<span id="install-chai-ethers" data-test-action></span>

::code-group

Expand All @@ -144,16 +142,15 @@ yarn add -D @nomicfoundation/hardhat-chai-matchers [email protected]

After installing it, add the plugin at the top of your `hardhat.config.ts` file:

<span id="import-chai-matchers" data-name="modifyFile" data-filepath="tests-output/hardhat-project/hardhat.config.ts"
data-at-line="4"></span>
<span id="import-chai-matchers" data-test-action></span>

```ts [hardhat.config.ts]
import "@nomicfoundation/hardhat-chai-matchers";
```

With the previous steps completed, your `hardhat.config.ts` file should now be properly configured to include settings for local testing.

<span id="compare-config" data-name="compareToFile" data-filepath="tests-output/hardhat-project/hardhat.config.ts"></span>
<span id="compare-config" data-test-action></span>

```ts [hardhat.config.ts]
import { HardhatUserConfig } from "hardhat/config";
Expand Down Expand Up @@ -182,15 +179,15 @@ To set up the environment for using chai matchers and writing tests, you'll need

Inside the **contracts** folder, rename the example contract file to **Greeter.sol**.

<span id="rename-greeter-file" data-name="runCommand" data-command-folder="tests-output/hardhat-project"></span>
<span id="rename-greeter-file" data-test-action></span>

```bash
mv contracts/Lock.sol contracts/Greeter.sol
```

Now replace the example contract in **Greeter.sol** with the new `Greeter` contract below:

<span id="create-greeter-contract" data-name="writeToFile" data-filepath="tests-output/hardhat-project/contracts/Greeter.sol"></span>
<span id="create-greeter-contract" data-test-action></span>

```solidity [Greeter.sol]
// SPDX-License-Identifier: MIT
Expand Down Expand Up @@ -223,15 +220,15 @@ Now you can create a test with the `hardhat-chai-matchers` plugin:

Inside the `/test` folder, rename the example test file to `test.ts`.

<span id="rename-test-file" data-name="runCommand" data-command-folder="tests-output/hardhat-project"></span>
<span id="rename-test-file" data-test-action></span>

```bash
mv test/Lock.ts test/test.ts
```

Replace the old test with this example showcasing the functionalities of the contract:

<span id="create-test" data-name="writeToFile" data-filepath="tests-output/hardhat-project/test/test.ts"></span>
<span id="create-test" data-test-action></span>

```typescript
import * as hre from "hardhat";
Expand Down Expand Up @@ -284,7 +281,7 @@ describe("Greeter", function () {

Execute the following command in your terminal to run the tests:

<span id="run-test" data-name="runCommand" data-command-folder="tests-output/hardhat-project"></span>
<span id="run-test" data-test-action></span>

::code-group

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"lint-staged": "^15.2.4",
"markdownlint": "^0.34.0",
"markdownlint-cli2": "^0.13.0",
"node-pty": "^1.0.0",
"pm2": "^5.4.2",
"prettier": "^3.2.5",
"prettier-eslint": "^16.3.0",
Expand Down
18 changes: 18 additions & 0 deletions tests/configs/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { steps as erc20PaymasterSteps } from './erc20-paymaster';
import { steps as howToTestContractsSteps } from './how-to-test-contracts';

export function getConfig(tutorialName: string) {
let steps;
switch (tutorialName) {
case 'erc20-paymaster':
steps = erc20PaymasterSteps;
break;
case 'how-to-test-contracts':
steps = howToTestContractsSteps;
break;
default:
break;
}

return steps;
}
Loading
Loading