Deploy your first contract
Deploy a smart contract to a ZKsync chain in under 5 minutes
Choose between using testnet or a 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/contractsso 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
mintfunction
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.
If you don't already have forge installed,
you can install it via foundryup.
- Create a new foundry project:
forge init QuickstartTokencd QuickstartToken - Install OpenZeppelin Contracts.
forge install OpenZeppelin/openzeppelin-contractsOnce installed, add a
remappings.txtfile and add this line:@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/ - Create a new file in the
srcfolder calledQuickstartToken.sol. - Copy and paste the
QuickstartTokencontract above into theQuickstartToken.solfile. - Create a new file in the
scriptfolder calledQuickstartToken.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.
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.