说明:

1.zksync era部署合约和ETH等网络不同,不能直接使用remix进行部署,官方出的解决方案是使用hardhat插件。

2.合约中的constructor需要传参进去,不能直接写入

3.官方的python sdk也是不能使用的状态。

系统环境:ubuntu 22.04

1.升级apt

sudo apt update

2.安装并激活NVM

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bashsource ~/.bashrc

3.安装NodeJS LTS版本

nvm install --lts

4.确认已成功安装 Node.js 和 npm

node --versionnpm --version

能正常显示版本就安装成功了

5.创建文件夹并初始化环境

mkdir greeter-examplecd greeter-examplenpm init -ynpm i -D typescript ts-node ethers@^5.7.2 zksync-web3@^0.14.3 hardhat @matterlabs/hardhat-zksync-solc @matterlabs/hardhat-zksync-deploy

ethers 版本当前只兼容v5.7.x

zksync-web3 版本需要对应本地版本

6.设置对应的文件

文件结构如图

hardhat.config.ts

import "@matterlabs/hardhat-zksync-deploy";import "@matterlabs/hardhat-zksync-solc";module.exports = {zksolc: {version: "1.3.5",compilerSource: "binary",settings: {},},defaultNetwork: "zkSyncTestnet",networks: {zkSyncTestnet: {url: "https://zksync2-testnet.zksync.dev",ethNetwork: "goerli", // Can also be the RPC URL of the network (e.g. `https://goerli.infura.io/v3/`)zksync: true,},},solidity: {version: "0.8.17",},};

这是在测试网发布的例子,如果想在Era主网发布

url: "https://mainnet.era.zksync.io",ethNetwork: "mainnet"//或者改为你自己的infura ETH主网地址

修改这两项即可。

deploy文件夹下放部署脚本deploy.ts

import { Wallet, utils } from "zksync-web3";import * as ethers from "ethers";import { HardhatRuntimeEnvironment } from "hardhat/types";import { Deployer } from "@matterlabs/hardhat-zksync-deploy";export default async function (hre: HardhatRuntimeEnvironment) {console.log(`Running deploy script for the Greeter contract`);// 初始化钱包 填入私钥const wallet = new Wallet("your private key");// 创建deployerconst deployer = new Deployer(hre, wallet);// 设置部署的合约名const artifact = await deployer.loadArtifact("Greeter");// 计算gas fee// 参数为合约中construct的参数const greeting = "Hi there!";const deploymentFee = await deployer.estimateDeployFee(artifact, [greeting]);//部署合约const parsedFee = ethers.utils.formatEther(deploymentFee.toString());console.log(`The deployment is estimated to cost ${parsedFee} ETH`);const greeterContract = await deployer.deploy(artifact, [greeting]);console.log("constructor args:" + greeterContract.interface.encodeDeploy([greeting]));const contractAddress = greeterContract.address;console.log(`${artifact.contractName} was deployed to ${contractAddress}`);}

contracts文件夹下放需要部署的合约sol文件

举例:Greeter.sol

//SPDX-License-Identifier: Unlicensepragma solidity ^0.8.0;contract Greeter {string private greeting;constructor(string memory _greeting) {greeting = _greeting;}function greet() public view returns (string memory) {return greeting;}function setGreeting(string memory _greeting) public {greeting = _greeting;}}

7.编译合约

按规则放置完成之后,可以进行合约编译和部署

npx hardhat compile
npx hardhat deploy-zksync

部署完成之后会提示部署合约的地址和手续费用。