ZKsync 백서

ZKsync

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 12쪽 중 2쪽

ble security and finality without revealing sensitive data. Transaction filtering Learn how to filter transactions on the ZKsync chain, including L1→L2 and L2 transactions. Features Dive into Prividium™'s key features and capabilities. ## /zksync-network/quick-start Quick Start ZKsync Setup Get setup with ZKsync testnet or a local node Welcome to the Quickstart! This section is designed for developers new to ZKsync and gives you the options of deploying to either a local node or a testnet using the most popular smart contract development frameworks. You will learn how to: Get setup with either the testnet or a local node. Build an ERC-20 token contract. Deploy the smart contract. Interact with the contract using a script. Interact with the contract in a simple frontend. Setup Choose between using testnet or a local node. Testnet Local Node Prerequisites Before you move onto the next section, make sure that you: Have a browser wallet extension installed. If you're not sure what wallet to use, try Metamask . Have configured the ZKsync OS Developer Preview testnet in your wallet. Have bridged at least 0.05 testnet ETH to your wallet on the ZKsync OS Developer Preview network. Add ZKsync OS Developer Preview to your MetaMask wallet You can add ZKsync OS Developer Preview to your MetaMask wallet using the button below: Add ZKsync OS Developer Preview If you are using a different in-browser wallet, the button above may also work for them. Manually add ZKsync OS Developer Preview To manually add ZKsync OS Developer Preview as a custom network in your wallet, follow these steps: Find the “Add Network” option in your wallet (in MetaMask, you can find this in the networks dropdown). Click on “Add Network" and "Add network manually". Fill in the following details for the ZKsync OS Developer Preview testnet: Network details Property Value Network Name ZKsync OS Developer Preview RPC URL https://zksync-os-testnet-alpha.zksync.dev/ WebSocket URL wss://zksync-os-testnet-alpha.zksync.dev/ws Chain ID 8022833 Currency Symbol ETH Block Explorer URL https://zksync-os-testnet-alpha.staging-scan-v2.zksync.dev/ Explorer Verification API https://block-explorer-api.zksync-os-testnet-alpha.zksync.dev/api Get funds for your wallet Once you have your wallet connected to the ZKsync OS Developer Preview environment, you can bridge testnet ETH from Sepolia Testnet. You can get testnet funds on Sepolia from one of many testnet faucets . Bridging testnet ETH The easiest way to bridge funds to the testnet is by using the ZKsync OS Portal to bridge testnet ETH over. You can also bridge funds in your terminal with cast or in a script using zksync-js as shown in the examples below. Bridging with cast export BRIDGEHUB_ADDRESS = 0xc4fd2580c3487bba18d63f50301020132342fdbd export CHAIN_ID = 8022833 export SEPOLIA_RPC =< YOUR_SEPOLIA_RPC_ENDPOINT > export ADDRESS =< YOUR_WALLET_ADDRESS > export VALUE_TO_BRIDGE =< AMOUNT_TO_BRIDGE_IN_WEI > cast send -r $SEPOLIA_RPC $BRIDGEHUB_ADDRESS "requestL2TransactionDirect((uint256,uint256,address,uint256,bytes,uint256,uint256,bytes[],address))" "( $CHAIN_ID , $VALUE_TO_BRIDGE , $ADDRESS ,50,0x,300000,800,[], $ADDRESS )" --value $VALUE_TO_BRIDGE --private-key= $PRIVATE_KEY Bridging with zksync-js You can find examples for bridging with ethers or viem in the zksync-js docs . Starting a local node To start a local node, you will need to have anvil installed on your machine. You can install anvil via foundryup by following the installation instructions in the foundry docs. If you've used ZKsync in the past, you may remember having to use special tooling to run a local node. Since the Atlas upgrade , however, you can now use any standard EVM local node for testing. The only time you may need a ZKsync-specific node is to test bridging or crosschain transactions. For this tutorial, we won't need one. Once installed, open a new terminal and run the command below to start a local node: anvil You should see some information logged about the local network running on your machine, including the port where the node is running ( 8545 ), the chain ID ( 31337 ), and 10 different test wallet addresses and their private keys. These wallets are all pre-funded with ETH so you don't need to worry about adding funds. Keep the node running until you are done with this tutorial. Note that this is an in-memory node, meaning that if you stop it at any time, any contracts you deployed or transactions you sent will be lost. Introduction Welcome to the ZKsync Docs. Deploy your first contract Deploy a smart contract to a ZKsync chain in under 5 minutes ## /zksync-network/environment ZKsync Docs Elastic Network Chains Detailed list of all chains on the Elastic Network. The ZKsync ecosystem consists of multiple chains, each serving different purposes and markets. Below is a comprehensive list of all chains in the Elastic network: Chain IDs and availability may change. Always verify the latest information in the respective chain documentation before integrating. Showing 12 chains (12 mainnet) Mainnets only Include testnets Chain Name Chain ID Base Token Links Data Availability Abstract Mainnet 2741 ETH Ethereum ADI Network 36900 ADI Ethereum Cronos zkEVM Mainnet 388 zkCRO NoDA GRVT Mainnet 325 ETH NoDA Lens Chain 232 GHO Avail Memento ZK Chain 51888 ETH NoDA OpenZK Mainnet 1345 ozETH Ethereum Sophon Mainnet 50104 SOPH Avail Zero Network 543210 ETH Ethereum ZKcandy 320 ETH Avail ZKsync Era Mainnet 324 ETH Ethereum zkXPLA 375 ETH Ethereum Build a Frontend Build a simple frontend for your deployed contract with Vite and Wagmi L1 contracts L1 contract addresses ## /zksync-network/quick-start/deploy-your-first-contract Quick Start ZKsync Deploy your first contract Deploy a smart contract to a ZKsync chain in under 5 minutes Choose between using testnet or a local node. Testnet Local Node Review the smart contract code The quickstart contract is a basic ERC-20 token built with OpenZeppelin. The entire code is as follows: // SPDX-License-Identifier: MIT pragma solidity ^0.8.0 ; import "@openzeppelin/contracts/token/ERC20/ERC20.sol" ; import "@openzeppelin/contracts/access/Ownable.sol" ; import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol" ; contract QuickstartToken is ERC20 , Ownable , ERC20Burnable { constructor ( string memory name , string memory symbol ) ERC20 (name, symbol) Ownable (msg.sender) { _mint ( msg.sender , 100 * 10 ** decimals ()); } function mint ( address to , uint256 amount ) public onlyOwner { _mint (to, amount); } } The contract: imports helper contracts from @openzeppelin/contracts so that our contract is a standard ERC-20 contract, has an owner, and allows to tokens to be burned sets the token name to the symbol using the constructor arguments sets the deployer wallet as the owner mints an initial supply of 100 tokens to the deployer only allows the owner to mint additional tokens using the mint function The ERC20 token code is provided “as is” without any express or implied warranties. Regulations governing digital assets are still unclear in many jurisdictions. ERC20 tokens may possess unique legal, tax, and market risks, so it is up to you to determine which, if any, laws apply to your deployment of ERC20 tokens. The developers and publishers of this software disclaim any liability for any legal issues that may arise from its use. Project setup Choose between using Foundry or Hardhat . Foundry Hardhat If you don't already have forge installed, you can install it via foundryup . Create a new foundry project: forge init QuickstartToken cd QuickstartToken Install OpenZeppelin Contracts. forge install OpenZeppelin/openzeppelin-contracts Once installed, add a remappings.txt file and add this line: @openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/ Create a new file in the src folder called QuickstartToken.sol . Copy and paste the QuickstartToken contract above into the QuickstartToken.sol file. Create a new file in the script folder called QuickstartToken.s.sol . Copy and paste the script below into QuickstartToken.s.sol . This script will be used to deploy the contract. // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13 ; import { Script } from "forge-std/Script.sol" ; import { QuickstartToken } from "../src/QuickstartToken.sol" ; contract QuickstartTokenScript is Script { QuickstartToken public quickstartToken; function setUp () public {} function run () public { vm. startBroadcast (); quickstartToken = new QuickstartToken ( "Quickstart Token" , "QKT" ); vm. stopBroadcast (); } } Build the project. forge build Set your private key for deploying. Get this from your browser wallet for the same account where you bridged testnet ETH. export TESTNET_PRIVATE_KEY = "0x..." Deploy the contract using the command below. Your contract address will be logged in the output. forge script script/QuickstartToken.s.sol --rpc-url https://zksync-os-testnet-alpha.zksync.dev --broadcast --skip-simulation --private-key $TESTNET_PRIVATE_KEY (Optional) Verify the contract. This will allow you to see the contract code in the block explorer. Replace 0x<YOUR_CONTRACT_ADDRESS> with your deployed contract address from the previous step. forge verify-contract \ --chain-id 8022833 \ --verifier custom \ --verifier-url https://block-explorer-api.zksync-os-testnet-alpha.zksync.dev/api \ 0x<YOUR_CONTRACT_ADDRESS> \ src/QuickstartToken.sol:QuickstartToken Verify if the contract was successfully verified by searching for your contract address on the testnet block explorer and clicking on the "Contract" tab. Create a new project folder mkdir hardhat-example cd hardhat-example Initialize a new Hardhat 3 project. You can choose to either use Mocha with Ethers.js or Node Test Runner with viem . Select y to install the dependencies. npx hardhat --init Install OpenZeppelin Contracts. npm yarn pnpm bun npm install -D @openzeppelin/contracts Add ZKsync OS to the hardhat.config.ts file and configure the ignition required confirmations. hardhat.config.ts ignition : { requiredConfirmations : 1 , }, networks : { zksyncOS : { type : 'http' , chainType : 'generic' , url : 'https://zksync-os-testnet-alpha.zksync.dev' , accounts : [ configVariable ( 'TESTNET_PRIVATE_KEY' )], }, }, Add your private key to the Hardhat keystore as TESTNET_PRIVATE_KEY . If you've never used hardhat keystore before, you will be asked to set up a password. Get the private key from your browser wallet for the same account where you bridged testnet ETH. npm yarn pnpm bun npx hardhat keystore set TESTNET_PRIVATE_KEY Create a new file in the contracts folder called QuickstartToken.sol . Copy and paste the QuickstartToken contract above into the QuickstartToken.sol file. Create a new file in the ignition/modules folder called QuickstartToken.ts . Copy and paste the ignition module below into QuickstartToken.ts . This will be used to deploy the contract. import { buildModule } from '@nomicfoundation/hardhat-ignition/modules' ; export default buildModule ( 'QuickstartToken' , (m) => { const quickstartToken = m. contract ( 'QuickstartToken' , [ 'Quickstart Token' , 'QKT' ]); return { quickstartToken }; }); Compile and deploy the contract. Your contract address will be logged in the output. npm yarn pnpm bun npx hardhat compile npm yarn pnpm bun npx hardhat ignition deploy ignition/modules/QuickstartToken.ts --network zksyncOS Verify the contract You can optionally verify the contract so the code shows on the block explorer. Install the Hardhat verify SDK: npm yarn pnpm bun npm install --save-dev @nomicfoundation/hardhat-verify Add hardhatVerify to the hardhat plugins and configure the verification endpoint: hardhat.config.ts import hardhatVerify from "@nomicfoundation/hardhat-verify" ; const config : HardhatUserConfig = { plugins: [ hardhatVerify, // ...other plugins... ], // ...other config... chainDescriptors: { 8022833 : { name: 'zksyncOS' , blockExplorers: { blockscout: { name: 'Testnet Explorer' , url: 'https://zksync-os-testnet-alpha.staging-scan-v2.zksync.dev' , apiUrl: 'https://block-explorer-api.zksync-os-testnet-alpha.zksync.dev/api' , }, }, }, }, }; Use your deployed contract address to verify using hardhat-verify : npm yarn pnpm bun npx hardhat clean npx hardhat compile --build-profile production npx hardhat verify --build-profile production --network zksyncOS 0x < YOUR_CONTRACT_ADDRES S > "Quickstart Token" "QKT" Verify if the contract was successfully verified by searching for your contract address on the block explorer and clicking on the "Contract" tab. Review the smart contract code The quickstart contract is a basic ERC-20 token built with OpenZeppelin. The entire code is as follows: // SPDX-License-Identifier: MIT pragma solidity ^0.8.0 ; import "@openzeppelin/contracts/token/ERC20/ERC20.sol" ; import "@openzeppelin/contracts/access/Ownable.sol" ; import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol" ; contract QuickstartToken is ERC20 , Ownable , ERC20Burnable { constructor ( string memory name , string memory symbol ) ERC20 (name, symbol) Ownable (msg.sender) { _mint ( msg.sender , 100 * 10 ** decimals ()); } function mint ( address to , uint256 amount ) public onlyOwner { _mint (to, amount); } } The contract: imports helper contracts from @openzeppelin/contracts so that our contract is a standard ERC-20 contract, has an owner, and allows to tokens to be burned sets the token name to the symbol using the constructor arguments sets the deployer wallet as the owner mints an initial supply of 100 tokens to the deployer only allows the owner to mint additional tokens using the mint function The ERC20 token code is provided “as is” without any express or implied warranties. Regulations governing digital assets are still unclear in many jurisdictions. ERC20 tokens may possess unique legal, tax, and market risks, so it is up to you to determine which, if any, laws apply to your deployment of ERC20 tokens. The developers and publishers of this software disclaim any liability for any legal issues that may arise from its use. Project setup Choose between using Foundry or Hardhat . Foundry Hardhat Create a new foundry project. You should already have forge installed after installing foundryup in the previous setup. forge init QuickstartToken cd QuickstartToken Install OpenZeppelin Contracts. forge install OpenZeppelin/openzeppelin-contracts Once installed, add a remappings.txt file and add this line: @openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/ Create a new file in the src folder called QuickstartToken.sol . Copy and paste the QuickstartToken contract above into the QuickstartToken.sol file. Create a new file in the script folder called QuickstartToken.s.sol . Copy and paste the script below into QuickstartToken.s.sol . This script will be used to deploy the contract. // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13 ; import { Script } from "forge-std/Script.sol" ; import { QuickstartToken } from "../src/QuickstartToken.sol" ; contract QuickstartTokenScript is Script { QuickstartToken public quickstartToken; function setUp () public {} function run () public { vm. startBroadcast (); quickstartToken = new QuickstartToken ( "Quickstart Token" , "QKT" ); vm. stopBroadcast (); } } Build the project. forge build Deploy the contract using one of the test wallets provided by anvil . Your contract address will be logged in the output. forge script script/QuickstartToken.s.sol --rpc-url http://localhost:8545 --broadcast --skip-simulation --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 Create a new project folder mkdir hardhat-example cd hardhat-example Initialize a new Hardhat 3 project. You can choose to either use Mocha with Ethers.js or Node Test Runner with viem . Select y to install the dependencies. npx hardhat --init Install OpenZeppelin Contracts. npm yarn pnpm bun npm install -D @openzeppelin/contracts Add the local node to the hardhat.config.ts file and configure the ignition required confirmations. hardhat.config.ts ignition : { requiredConfirmations : 1 , }, networks : { anvil : { type : 'http' , chainType : 'generic' , url : 'http://localhost:8545' , accounts : [ '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80' ], }, }, Create a new file in the contracts folder called QuickstartToken.sol . Copy and paste the QuickstartToken contract above into the QuickstartToken.sol file. Create a new file in the ignition/modules folder called QuickstartToken.ts . Copy and paste the ignition module below into QuickstartToken.ts . This will be used to deploy the contract. import { buildModule } from '@nomicfoundation/hardhat-ignition/modules' ; export default buildModule ( 'QuickstartToken' , (m) => { const quickstartToken = m. contract ( 'QuickstartToken' , [ 'Quickstart Token' , 'QKT' ]); return { quickstartToken }; }); Compile and deploy the contract. Your contract address will be logged in the output. npm yarn pnpm bun npx hardhat compile npm yarn pnpm bun npx hardhat ignition deploy ignition/modules/QuickstartToken.ts --network anvil Now your first contract is fully deployed! In the next section we'll interact with it using a script to mint and transfer some tokens. Setup Get setup with ZKsync testnet or a local node Interact with your contract Interact with your deployed contract using a script ## /zksync-network/quick-start/interact-with-your-contract Quick Start ZKsync Interact with your contract Interact with your deployed contract using a script Choose between using testnet or a local node. Testnet Local Node Script setup Choose between using Foundry , Hardhat with viem , or Hardhat with ethers . Foundry Hardhat with viem Hardhat with ethers Create a new file in the script folder called InteractQuickstartToken.s.sol . Copy and paste the script below into InteractQuickstartToken.s.sol . This script will transfer 10 tokens to the recipient address and log some metadata for the token. // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13 ; import { console2 } from "forge-std/console2.sol" ; import { Script } from "forge-std/Script.sol" ; import { QuickstartToken } from "../src/QuickstartToken.sol" ; contract InteractQuickstartTokenScript is Script { uint256 internal constant TRANSFER_AMOUNT = 10 * 10 ** 18 ; address internal constant DEFAULT_RECIPIENT = 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 ; function run () public { address contractAddress = vm. envAddress ( "CONTRACT_ADDRESS" ); address recipientAddress = vm. envOr ( "RECIPIENT_ADDRESS" , DEFAULT_RECIPIENT); QuickstartToken quickstartToken = QuickstartToken (contractAddress); uint256 recipientBalanceBefore = quickstartToken. balanceOf (recipientAddress); vm. startBroadcast (); quickstartToken. transfer (recipientAddress, TRANSFER_AMOUNT); vm. stopBroadcast (); console2. log ( "Token name:" , quickstartToken. name ()); console2. log ( "Token symbol:" , quickstartToken. symbol ()); console2. log ( "Total supply:" , quickstartToken. totalSupply () / 10 ** 18 ); console2. log ( "Transferred amount:" , TRANSFER_AMOUNT / 10 ** 18 ); console2. log ( "Recipient balance increase:" , (quickstartToken. balanceOf (recipientAddress) - recipientBalanceBefore) / 10 ** 18 ); console2. log ( "Recipient balance:"
프로젝트로 돌아가기