ZkSynk deploy* the Greeter contract | Деплой Гритер смарт контракта зк-синк
Требования к серверу:я взяла СPХ31 хетцнере
Также нам понадобится:
- кошелек метамаск с ETH Goerli
- Тестовая сеть zksynk era testnet (можно подключить
тут)
- Тестовые токенамы ETH в сети zksynk (кран
тут)
- Приватный ключ от метамаска (не используйте кошельки с ральными деньгами!)
- RPC ETH1 goerli (можно взять на
инфуре)
Подготавливаем сервер:
Код:
sudo apt-get update -y && sudo apt upgrade -y && sudo apt-get install make build-essential unzip lz4 gcc git jq chrony -y
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
nvm -v
nvm install v16.16.0
node -v
#вывод - v16.16.0
Код:
mkdir greeter-example
cd greeter-example
yarn init -y
yarn add -D typescript ts-node ethers@^5.7.2 zksync-web3 hardhat @matterlabs/hardhat-zksync-solc @matterlabs/hardhat-zksync-deploy @ethersproject/hash @ethersproject/web
Код:
nano hardhat.config.ts
#создаем файл hardhat.config.ts и вставляем в него следующий текст:
require("@matterlabs/hardhat-zksync-deploy");
require("@matterlabs/hardhat-zksync-solc");
module.exports = {
zksolc: {
version: "1.3.10",
compilerSource: "binary",
settings: {
optimizer: {
enabled: true,
},
experimental: {
},
},
},
defaultNetwork: "zkTestnet",
networks: {
zkTestnet: {
url: "https://zksync2-testnet.zksync.dev", // URL of the zkSync network RPC
ethNetwork: "<RPC>",
zksync: true
}
},
solidity: {
version: "0.8.16",
},
};
Замените <RPC> на ссылку из инфуры (https), после чего сохраняем и выходим из нано
Код:
mkdir contracts deploy
nano contracts/Greeter.sol
#создаем файл contracts/Greeter.sol и вставляем в него следующий текст:
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.8;
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
Код:
cd deploy
nano deploy.ts
#создаем файл 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";
// 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 you want to deploy.
const deployer = new Deployer(hre, wallet);
const artifact = await deployer.loadArtifact("Greeter");
// Estimate contract deployment fee
const greeting = "Hi there!";
const deploymentFee = await deployer.estimateDeployFee(artifact, [greeting]);
// OPTIONAL: Deposit funds to L2
// Comment this block if you already have funds on zkSync.
const depositHandle = await deployer.zkWallet.deposit({
to: deployer.zkWallet.address,
token: utils.ETH_ADDRESS,
amount: deploymentFee.mul(2),
});
// Wait until the deposit is processed on zkSync
await depositHandle.wait();
// 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 parsedFee = ethers.utils.formatEther(deploymentFee.toString());
console.log(`The deployment is estimated to cost ${parsedFee} ETH`);
const greeterContract = await deployer.deploy(artifact, [greeting]);
//obtain the Constructor Arguments
console.log("constructor args:" + greeterContract.interface.encodeDeploy([greeting]));
// Show the contract info.
const contractAddress = greeterContract.address;
console.log(`${artifact.contractName} was deployed to ${contractAddress}`);
}
Заменить <WALLET-PRIVATE-KEY> на приватный ключ вашего кошелька метамаск
Код:
cd ..
yarn hardhat deploy-zksync
#вывод должен быть примерно как на скрине ниже
В следующих статьях развернем еще несколько видов смарт контрактов в зк синк, так что подписывайтесь!
Congratulations! You have deployed a smart contract to zkSync Era Testnet