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

hardhat-zksync-node - extend hardhat network type with URL, fix node example #426

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
22 changes: 7 additions & 15 deletions examples/node-example/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# zkSync 2.0 deploy environment example
# zkSync 2.0 node environment example

This project demonstrates how to compile and deploy your contracts in zkSync 2.0 using the Hardhat plugins.
This project demonstrates how to run [era-test-node](https://era.zksync.io/docs/tools/testing/era-test-node.html) locally using the zksync's `hardhat-zksync-node` Hardhat plugin for testing purposes.

## Prerequisites

Expand All @@ -10,21 +10,16 @@ This project demonstrates how to compile and deploy your contracts in zkSync 2.0
## Configuration

Plugin configuration is located in [`hardhat.config.ts`](./hardhat.config.ts).
You should only change the zkSync network configuration.

`hardhat.config.ts` example with zkSync network configured with the name `zkTestnet` and `goerli` used as the underlying layer 1 network:
`hardhat.config.ts` example with the hardhat network's zksync set to true::
```ts

import "@matterlabs/hardhat-zksync-deploy";
import { HardhatUserConfig } from 'hardhat/types';

const config: HardhatUserConfig = {
networks: {
goerli: {
url: 'https://goerli.infura.io/v3/<API_KEY>' // you can use either the URL of the Ethereum Web3 RPC, or the identifier of the network (e.g. `mainnet` or `rinkeby`)
},
zkTestnet: {
url: 'https://zksync2-testnet.zksync.dev', // you should use the URL of the zkSync network RPC
ethNetwork: 'goerli',
hardhat: {
zksync: true
},
}
Expand All @@ -50,12 +45,9 @@ After that you should be able to run plugins:
yarn
yarn hardhat compile
yarn hardhat deploy-zksync
yarn hardhat test
```

- `yarn hardhat compile`: compiles all the contracts in the `contracts` folder.
- `yarn hardhat deploy-zksync`: runs all the deploy scripts in the `deploy` folder.
- To run a specific script, add the `--script` argument, e.g. `--script 001_deploy.ts`.
- To run on a specific zkSync network, use standard hardhat `--network` argument, e.g. `--network zkTestnet`
(with `zkTestnet` network specified in the `hardhat.config` networks section, with the `zksync` flag set to `true` and `ethNetwork` specified).

If you don't specify zkSync network (`--network`), `local-setup` with <http://localhost:8545> (Ethereum RPC URL) and <http://localhost:3050> (zkSync RPC URL) will be used.
- `yarn hardhat test`: runs all the tests against the era-test-node instance instantiated in a separate process.
10 changes: 0 additions & 10 deletions examples/node-example/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,7 @@ const config: HardhatUserConfig = {
hardhat: {
zksync: true,
},
ethNetwork: {
url: 'http://0.0.0.0:8545',
},
zkSyncNetwork: {
url: 'http://0.0.0.0:3050',
ethNetwork: 'ethNetwork',
zksync: true,
},
},
// Docker image only works for solidity ^0.8.0.
// For earlier versions you need to use binary releases of zksolc.
solidity: {
version: '0.8.17',
},
Expand Down
10 changes: 7 additions & 3 deletions examples/node-example/test/tests.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { expect } from "chai";
import { Contract, Wallet } from "zksync-web3";
import { Contract, Wallet, Provider } from "zksync-web3";
import { Deployer } from "@matterlabs/hardhat-zksync-deploy";
import { ZkSyncArtifact } from '@matterlabs/hardhat-zksync-deploy/src/types';
import { ZkSyncProviderAdapter } from "@matterlabs/hardhat-zksync-node";
import "@matterlabs/hardhat-zksync-node/dist/type-extensions";
import * as hre from "hardhat";

const RICH_PRIVATE_KEY = '0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110';

describe("Greeter", function () {
let provider: Provider;
let deployer: Deployer;
let artifact: ZkSyncArtifact;
let contract: Contract;

beforeEach(async function () {
// Deploy the contract before each test
deployer = new Deployer(hre, new Wallet('0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110', (hre.network.provider as ZkSyncProviderAdapter)._zkSyncProvider));
provider = new Provider(hre.network.config.url);
deployer = new Deployer(hre, new Wallet(RICH_PRIVATE_KEY, provider));
artifact = await deployer.loadArtifact('Greeter');
contract = await deployer.deploy(artifact, ['Hello, world!']);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-zksync-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@matterlabs/hardhat-zksync-node",
"version": "0.0.1-beta.3",
"version": "0.0.1-beta.4",
"description": "Hardhat plugin to run zkSync era-test-node locally",
"repository": "github:matter-labs/hardhat-zksync",
"homepage": "https://github.com/matter-labs/hardhat-zksync/tree/main/packages/hardhat-zksync-node",
Expand Down
1 change: 1 addition & 0 deletions packages/hardhat-zksync-node/src/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare module 'hardhat/types/config' {

interface HardhatNetworkConfig {
zksync: boolean;
url: string;
}

interface HttpNetworkConfig {
Expand Down
3 changes: 2 additions & 1 deletion packages/hardhat-zksync-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ export async function getLatestRelease(owner: string, repo: string, userAgent: s
if (error.response) {
// The request was made and the server responded with a status code outside of the range of 2xx
throw new ZkSyncNodePluginError(
`Failed to get latest release for ${owner}/${repo}. Status: ${error.response.status
`Failed to get latest release for ${owner}/${repo}. Status: ${
error.response.status
}, Data: ${JSON.stringify(error.response.data)}`
);
} else if (error.request) {
Expand Down
12 changes: 3 additions & 9 deletions packages/hardhat-zksync-node/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ import { spawn, ChildProcess } from 'child_process';
import * as utils from '../src/utils';
import { constructCommandArgs, getLatestRelease, getAssetToDownload, download } from '../src/utils';
import { RPCServerDownloader } from '../src/downloader';
import {
TASK_NODE_ZKSYNC,
PROCESS_TERMINATION_SIGNALS,
ZKSYNC_ERA_TEST_NODE_NETWORK_NAME,
MAX_PORT_ATTEMPTS,
} from '../src/constants';
import { Network } from 'hardhat/types';
import { TASK_NODE_ZKSYNC, PROCESS_TERMINATION_SIGNALS } from '../src/constants';

chai.use(sinonChai);

Expand Down Expand Up @@ -274,15 +268,15 @@ describe('node-zksync plugin', async function () {
const port = 12345; // any port for testing purposes
await utils.waitForNodeToBeReady(port);

expect(axios.post).to.have.been.calledWith(`http://localhost:${port}`);
expect(axios.post).to.have.been.calledWith(`http://127.0.0.1:${port}`);
});

it("should throw an error if the node isn't ready after maxAttempts", async () => {
// Make the stub reject all the time to simulate the node never being ready
sinon.stub(axios, 'post').rejects(new Error('Node not ready'));

try {
await utils.waitForNodeToBeReady(8080, 10);
await utils.waitForNodeToBeReady(8080, 1);
throw new Error('Expected waitForNodeToBeReady to throw but it did not');
} catch (err: any) {
expect(err.message).to.equal("Server didn't respond after multiple attempts");
Expand Down
Loading