# 在zkSync Era上快速部署合约 **Published by:** [211168.eth](https://paragraph.com/@211168/) **Published on:** 2023-04-02 **URL:** https://paragraph.com/@211168/zksync-era ## Content 在zkSync Era上部署合约大致分为4歩: 1 安装环境 电脑上安装好nodejs,node版本不低于16.13.0, 安装包管理npm和yarn 2 初始化项目 创建项目主目录,在主目录下运行: yarn init -y 和 yarn add -D typescript ts-node @types/node ethers@^5.7.2 zksync-web3 @ethersproject/hash @ethersproject/web hardhat @matterlabs/hardhat-zksync-solc @matterlabs/hardhat-zksync-deploy 新建hardhat.config.ts文件,内容如下:import "@matterlabs/hardhat-zksync-deploy"; import "@matterlabs/hardhat-zksync-solc"; module.exports = { zksolc: { version: "1.3.5", compilerSource: "binary", settings: {}, }, defaultNetwork: "zkMainnet", networks: { zkMainnet: { url: "https://mainnet.era.zksync.io", ethNetwork: "mainnet", zksync: true, }, }, solidity: { version: "0.8.17", }, }; 3 创建并编译合约 在主目录下创建contracts子目录,新建极简合约contracts/Greater.sol: //SPDX-License-Identifier: Unlicensed pragma 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; } } 使用 yarn hardhat compile 命令编译合约。 4 部署合约 在主目录下创建deploy子目录,新建deploy/deploy.js文件:import { utils, Wallet } from "zksync-web3"; import * as ethers from "ethers"; import { HardhatRuntimeEnvironment } from "hardhat/types"; import { Deployer } from "@matterlabs/hardhat-zksync-deploy"; // An example of a deploy script that will deploy and call a simple contract. export default async function (hre: HardhatRuntimeEnvironment) { console.log(`Running deploy script for the Greeter contract`); let key = '<YOUR_PRIVATE_KEY>' // Initialize the wallet. const wallet = new Wallet(key); console.log(wallet.address) // Create deployer object and load the artifact of the contract we want to deploy. const deployer = new Deployer(hre, wallet); const artifact = await deployer.loadArtifact("Greeter"); // Deploy this contract. The returned object will be of a `Contract` type, similarly to ones in `ethers`. // `greeting` is an argument for contract constructor. const greeting = "Hi Frens!"; const greeterContract = await deployer.deploy(artifact, [greeting]); // Show the contract info. const contractAddress = greeterContract.address; console.log(`${artifact.contractName} was deployed to ${contractAddress}`); // Call the deployed contract. const greetingFromContract = await greeterContract.greet(); if (greetingFromContract == greeting) { console.log(`Contract greets us with ${greeting}!`); } else { console.error(`Contract said something unexpected: ${greetingFromContract}`); } } 注: <YOUR_PRIVATE_KEY>替换成你的钱包私钥 用命令 yarn hardhat deploy-zksync 部署合约。 开卷~ ## Publication Information - [211168.eth](https://paragraph.com/@211168/): Publication homepage - [All Posts](https://paragraph.com/@211168/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@211168): Subscribe to updates