All pages
Powered by GitBook
1 of 4

Loading...

Loading...

Loading...

Loading...

Write a Contract

This document explains how to automatically write any smart contract using the OpenZeppelin Wizard. The resulting smart contract code can either be integrated with Remix by Clicking the Open in Remix button, or copied to clipboard and pasted in the user's intended IDE.

Getting Started

Navigate to the OpenZeppelin Wizard in your browser. First thing to notice is the Solidity Wizard and Cairo Wizard buttons.

One can choose any of the following tabs to begin creating an out-of-box smart contract code in either Solidity (for EVM chains) or Cairolang (useful for Starknet). These are:

  • ERC20: for writing an ERC-20 token smart contract

  • ERC721: for writing an NFT token smart contract

  • ERC1155: for writing an ERC-1155 token smart contract

  • Governor: for creating a DAO

  • Custom: for writing a customized smart contract

Writing An NFT Contract

For illustration purpose, we will be creating a NFT smart contract. Suppose you wanted to create a Mintable, Burnable ERC721 token and specify an appropriate license for it.

  1. Select the ERC721 tab.

  2. Give your NFT a name and a symbol by filling the Name and Symbol fields.

  3. Use the check-boxes on the left to select features of your token

  • Put a tick on the Mintable check-box

  • Put a tick on the Auto Increment Ids check-box, this ensures uniqueness of each minted NFT

  • Put a tick on the Burnable check-box

Notice that new lines of code are automatically written each time a feature is selected.

Voila! Contract Ready

With the resulting lines of code, you now have the NFT token contract written in Solidity. As mentioned above, this source code can now be ported to an IDE of your choice or opened directly in Remix.

The below figure depicts the auto-written NFT smart contract code.

Either leave the default MIT license or type the license of your choice

Verify Contracts

Verify Contracts

Once verified, a smart contract or token contract's source code becomes publicly available and verifiable, creating transparency and trust.

There are several ways to verify a contract, programmatically or manually on the UI.

Verify on the UI

  1. Go the the page

  2. Enter in the contract address you received during deployment. The dropdown will show you several available verification options. Select the one you would like to use and continue.

    1. Solidity (Flattended source code)

    2. Solidity (Standard JSON Input)

Solidity (Flattened Source Code)

  1. Contract Address: The 0x address supplied on contract creation (added above)

  2. Is Yul Contract: Select if the contract is coded in Yul for efficiency.

  3. Include Nightly Builds: Select if you want to show nightly builds.

  4. Compiler: derived from the first line in the contract pragma solidity X.X.X. Use the corresponding compiler version rather than the nightly build.

Solidity (Standard JSON Input)

  1. Include nightly builds. You can choose Yes or No depending on your compiler.

  2. Compiler. Choose the compiler version used to compile your smart contract. If you selected yes for nightly builds, use the compiler version rather than the build.

  3. Standard Input JSON. Upload your Standard Input JSON file. File should follows solidity format and all the sources must be in Literal Content format, not a URL.

Click the Verify & publish button and wait for the response.

Verify Programmatically

To verify contracts please follow the Verifying a Smart Contract guide to learn the different options.

In particular, to be able to verify the contracts programatically we will need following steps:

1- Install @nomiclabs/hardhat-etherscan package:

2- Import into hardhat.config.ts

3- Update hardhat.config.ts following:

4- Verify the contract Once the config is updated, you can verofy the contract with

Smart Contracts

EVM Version: Select the correct EVM version if known, otherwise use default.

  • Enter the Solidity Contract Code: You may need to flatten your solidity code if it utilizes a library or inherits dependencies from another contract. We recommend hardhat or the POA solidity flattener. To flatten your contract using hardhat, see here

  • Add Contract Libraries: Enter the name and 0x address for any required libraries called in the .sol file. You can add multiple contracts with the "+" button.

  • Click the Verify and Publish button.

  • If all goes well, you will see a checkmark next to Code in the code tab, and an additional tab called Read Contract. The contract name will now appear in BlockScout with any transactions related to your contract.

  • verify contract
    yarn add --dev @nomiclabs/hardhat-etherscan
    import "@nomiclabs/hardhat-etherscan";
      etherscan: {
        apiKey: {
          "edu-chain-testnet": "XXXX",
          "edu-chain": "XXXX",
        },
        customChains: [
          {
            network: "edu-chain-testnet",
            chainId: 656476,
            urls: {
              apiURL: "https://edu-chain-testnet.blockscout.com/api",
              browserURL: "https://edu-chain-testnet.blockscout.com",
            },
          },
          {
            network: "edu-chain",
            chainId: 41923, // Replace with the correct mainnet chain ID if different
            urls: {
              apiURL: "https://educhain.blockscout.com/api",
              browserURL: "https://educhain.blockscout.com",
            },
          },
        ],
      },
    npx hardhat verify --network edu-chain YOUR-CONTRACT-ADDRESS YOUR-CONSTRUCTOR-ARGUMENTS

    Deploy Using Hardhat

    import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

    Hardhat is a popular smart contract development frameworks. In this tutorial, we will be using Hardhat to deploy a simple Counter smart contract to the Custom Rollup Testnet. We will explore the basics of creating a Hardhat project with a sample contract and a script to deploy it.

    For the full instruction on how to use Hardhat, please refer to the official Hardhat documentation.

    Create New Project

    Start with creating an npm project by going to an empty folder, running npm init, and following its instructions. You can use another package manager, like yarn, but Hardhat recommends you use npm 7 or later, as it makes installing Hardhat plugins simpler.

    Hardhat Smart Contract

    To create the sample project, run npx hardhat init in your project folder:

    • Press <ENTER> choose javascript, typescript or empty project

    • Press <ENTER> to set the project root

    • Press <ENTER> again to accept addition of .gitignore

    Create deployer account

    • Create the .env file in your project root folder and add the following line:

    • Populate the .env file with your private key. You can get your private key from Metamask. See the section below on how to get your private key from Metamask.

    How to get your Private Key in Metamask
    • Click the vertical 3 dots in the upper-right corner of Metamask window

    • Select Account details and then click Show private key

    :::warning Do not commit your private key to a public repository!

    Verify that your .gitignore file contains .env to prevent your private key from being committed to a public repository. :::

    Configure Hardhat

    • Open the hardhat.config.js file and paste the code below:

    Write Smart Contract

    :::info Note: The existing smart contract code that comes with the sample project is a Lock.sol contract. Feel free to delete it or leave it. :::

    • Create a new file, in the contracts folder, named Counter.sol:

    • Copy the below code and paste it in the Counter.sol contract code:

    Create Deploy Script

    • Delete the content of the scripts/deploy.js file and add the code below:

    Compile Contract

    • Install dotenv package: npm install dotenv

    • Compile your contract code (i.e., go back to the project root in the CLI),

    Deploy Contract

    • Run the deploy script:

    Press <ENTER> to install hardhat @nomicfoundation/hardhat-toolbox

    Enter your Metamask password to reveal the private key
  • Copy the private key and paste it into the .env file.

  • ACCOUNT_PRIVATE_KEY='my private key'
    require("dotenv").config();
    require("@nomicfoundation/hardhat-toolbox");
    
    module.exports = {
      solidity: "0.8.19",
      paths: {
        artifacts: "./src",
      },
      networks: {
        "edu-chain-testnet": {
          // Testnet configuration
          url: `https://rpc.open-campus-codex.gelato.digital`,
          accounts: [process.env.ACCOUNT_PRIVATE_KEY],
        },
        "edu-chain": {
          // Mainnet configuration
          url: `https://rpc.edu-chain.raas.gelato.cloud`,
          accounts: [process.env.ACCOUNT_PRIVATE_KEY],
        },
      },
      etherscan: {
        apiKey: {
          "edu-chain-testnet": "XXXX",
          "edu-chain": "XXXX",
        },
        customChains: [
          {
            network: "edu-chain-testnet",
            chainId: 656476,
            urls: {
              apiURL: "https://edu-chain-testnet.blockscout.com/api",
              browserURL: "https://edu-chain-testnet.blockscout.com",
            },
          },
          {
            network: "edu-chain",
            chainId: 41923, // Replace with the correct mainnet chain ID if different
            urls: {
              apiURL: "https://educhain.blockscout.com/api",
              browserURL: "https://educhain.blockscout.com",
            },
          },
        ],
      },
    };
    import { HardhatUserConfig } from "hardhat/config";
    import "@nomicfoundation/hardhat-toolbox";
    import * as dotenv from "dotenv";
    
    dotenv.config({ path: __dirname + "/.env" });
    const ACCOUNT_PRIVATE_KEY = process.env.ACCOUNT_PRIVATE_KEY || "";
    
    const config: HardhatUserConfig = {
      solidity: "0.8.19",
      paths: {
        artifacts: "./src",
      },
      networks: {
        "edu-chain-testnet": {
          // Testnet configuration
          url: `https://rpc.open-campus-codex.gelato.digital`,
          accounts: [ACCOUNT_PRIVATE_KEY],
        },
        "edu-chain": {
          // Mainnet configuration
          url: `https://rpc.edu-chain.raas.gelato.cloud`,
          accounts: [ACCOUNT_PRIVATE_KEY],
        },
      },
      etherscan: {
        apiKey: {
          "edu-chain-testnet": "XXXX",
          "edu-chain": "XXXX",
        },
        customChains: [
          {
            network: "edu-chain-testnet",
            chainId: 656476,
            urls: {
              apiURL: "https://edu-chain-testnet.blockscout.com/api",
              browserURL: "https://edu-chain-testnet.blockscout.com",
            },
          },
          {
            network: "edu-chain",
            chainId: 41923,
            urls: {
              apiURL: "https://educhain.blockscout.com/api",
              browserURL: "https://educhain.blockscout.com",
            },
          },
        ],
      },
    };
    
    export default config;
    touch contracts/Counter.sol
    //SPDX-License-Identifier: MIT
    pragma solidity ^0.8.19;
    
    contract Counter {
    uint256 currentCount = 0;
    
        function increment() public {
            currentCount = currentCount + 1;
        }
    
        function retrieve() public view returns (uint256){
            return currentCount;
        }
    }
    const hre = require("hardhat");
    
    async function main() {
      const deployedContract = await hre.ethers.deployContract("Counter");
      await deployedContract.waitForDeployment();
      console.log(`Counter contract deployed to ${deployedContract.target}`);
    }
    
    main().catch((error) => {
      console.error(error);
      process.exitCode = 1;
    });
    npx hardhat compile
    npx hardhat run scripts/deploy.js --network edu-chain //For testnet
    npx hardhat run scripts/deploy.js --network edu-chain-testnet //For mainnet