# 加密狗整编空投第221篇:用VPS在 zkSync 网络上部署合约 - 加密狗 - Medium **Published by:** [no1space](https://paragraph.com/@no1space/) **Published on:** 2023-06-08 **URL:** https://paragraph.com/@no1space/221-vps-zksync-medium ## Content 之前写了篇不用VPS部署智能合约的教程,很多人会遇到各种各样的问题,那今天我们就来用VPS部署,一般情况下不会出现跑不通的问题了。一、往期教程二、教程前1、安装前需要使用VPS服务器,最低配置如下:CPU:4vCore 内存:6GB 硬盘:100GB 操作系统:Ubuntu 20.042、云主机+SSH工具需要你使用VPS或者你的机场做为云主机,云主机请切换成Ubuntu系统; SSH工具:小白用户建议使用Xshell或Finalshell。三、如何连接到 VPSMacOs:如果你用的是这个操作系统,你只需要用自己的设备与 SSH 连接; Windows:如果你是Win,有很多工具可以用,比如putty 、MobaXterm、Xshell 1、下载一个应用(本教程以putty为例,小白用户建议使用Xshell或Finalshell) 2、复制云服务器IP链接3、点击“接受”,输入云服务器的用户名和密码 注意:要粘贴你已经复制的任何命令,只需右键单击要粘贴命令即可4、再次输入密码,并输入新密码。 以上就是云服务器+SSH组合的步骤,下文所有步骤都在SSH工具中进行(小白用户建议使用Xshell或Finalshell) 5、使用Ubuntu,因此将目录更改为 /home/ubuntucd /home/ubuntu 6、root 每个VPS都需要root,如果你用的是PuTTY,就按如下方式root,其他工具基础都差不了多少。三、服务器设置1、输入如下指令sudo apt-get update && sudo apt-get install -y 2、安装CURLapt install curl -y curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh sudo bash /tmp/nodesource_setup.sh 3、安装NODEJSsudo apt-get install -y nodejs 4、安装VIM :sudo apt-get install vim 5、部署TIMEnpm init -y npm install --save-dev hardhat 6、继续**:**npm install -g npm@9.6.2 点击箭头标记的地方,按ENTER按三下回车键7、创建目录并进入目录mkdir greeter cd greeter npm init -y npm add -D typescript ts-node @types/node ethers@^5.7.2 zksync-web3@^0.14.3 @ethersproject/hash @ethersproject/web hardhat @matterlabs/hardhat-zksync-solc @matterlabs/hardhat-zksync-deploy 8、编辑配置:vim hardhat.config.ts 插入次代码(这是官方代码,如果怕被女巫,可以用ai生成一个) :import "@matterlabs/hardhat-zksync-deploy"; import "@matterlabs/hardhat-zksync-solc"; module.exports = { zksolc: { version: "1.3.6", compilerSource: "binary", settings: {}, }, defaultNetwork: "zkSyncMainnet", networks: { zkSyncMainnet: { url: "https://zksync2-mainnet.zksync.io", ethNetwork: "mainnet", zksync: true, }, }, solidity: { version: "0.8.17", }, }; 按Esc输入:wq回车9、创建两个目录:mkdir contracts mkdir deploy vim contracts/Greeter.sol 10、继续插入代码://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; } } 按 ESC输入 :wq按 ENTERnpx hardhat compile 11、继续输入vim deploy/deploy.ts 12、输入下面的代码,将代码中的*WALLET-PRIVATE-KEY*改为你自己的私钥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`); // Initialize the wallet. const wallet = new Wallet(*<WALLET-PRIVATE-KEY*"); // 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 there!"; 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}`); } // Edit the greeting of the contract const newGreeting = "Hey guys"; const setNewGreetingHandle = await greeterContract.setGreeting(newGreeting); await setNewGreetingHandle.wait(); const newGreetingFromContract = await greeterContract.greet(); if (newGreetingFromContract == newGreeting) { console.log(`Contract greets us with ${newGreeting}!`); } else { console.error(`Contract said something unexpected: ${newGreetingFromContract}`); } } 按 ESC输入 :wq按 ENTER四、部署npx hardhat deploy-zksync 这是你的合约地址 复制合约地址,粘贴到EXPLORER查看:五、验证合约第1步、在刚打开的区块链浏览器上,选择下方contract,点击Verify Smart Contract;第2步、填写智能合约,下图打箭头的部分都是要填写的:Contract Name 填Greeter在Enter the Solidy Contract Code栏中,粘贴://SPDX-License-Identifier: Unlicense 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; } } 在 Constructor Arguments 栏中,插入部署合约后出现的数据:第3步、填写所有栏后,点击“Verify Smart Contract”;出现如下界面,说明部署成功;如果报错,请降低版本。六、运行合约验证成功后,可以去检查智能合约的运行情况。 第1步、点击区块链浏览器中的Back to contract,并转到contract部分。 第2步、选择read部分,点击Query按钮,应出现以下消息:Hey guys!第3步、转到Write,打开,写下任何短语,例如Hi ZkSync,然后点击Connect Write to write 确认交易(要确认2次)。第4步、交易完成后,返回read部分,并按下Query按钮,我们应该会看到我们在上次操作中写入的短语。 如果像下图这样,恭喜你,你的智能合约已打开并成功运行。如果没有出现,请返回Write部分,看看你是不是没有出现交易hash,如果没有出现,请在此点击Write。 ## Publication Information - [no1space](https://paragraph.com/@no1space/): Publication homepage - [All Posts](https://paragraph.com/@no1space/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@no1space): Subscribe to updates