Skip to main content

Writing egg.sol

In this section, we will focus on writing egg.sol and implementing its functionality.

Preparations

Firstly, delete the initial code generated by Hardhat. Delete Lock.sol in the contracts folder and deploy.js in the scripts folder.

warning

Please do not delete any folders during this process!

Then, create new files egg.sol and icat.sol in the contracts folder and a new file test.js in the scripts folder.

Writing Smart Contract

Open egg.sol and paste the following code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract iCatEgg is ERC721 {

constructor() ERC721("iCat Egg", "EGG") {}
}

Let's go through it line by line.

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

Our pet egg is designed as an NFT. Therefore, on line 4, we reference the OpenZeppelin ERC-721 library to facilitate deploying the contract as an NFT.

contract iCatEgg is ERC721 {

This line of code, through contract inheritance, uses the OpenZeppelin library mentioned above to set the iCatEgg contract as an NFT contract.

constructor() ERC721("iCat Egg", "EGG") {}

This line of code sets the name of the NFT to iCat Egg and the symbol to EGG by passing two strings as parameters to the constructor.

From the above description, we can see that Solidity smart contracts are somewhat similar to class in other languages, having a constructor, etc. Understanding this might be helpful for the upcoming learning.

note

OpenZeppelin is an open-source smart contract development framework that provides a set of secure and reliable smart contract components and tools for decentralized applications (DApps) on Ethereum and other blockchain platforms. The framework aims to help developers build secure blockchain applications, preventing potential smart contract vulnerabilities and attacks. For more information, please visit the official website .

info

When writing Solidity code in VSCode, referencing the OpenZeppelin library might show errors like Expected string literal (path), "*" or alias list. or Source "@openzeppelin/contracts/token/ERC721/ERC721.sol" not found: File import callback not supported. The solution can be found in this article . If you have a better solution, feel free to discuss it in the GitHub discussion .

Testing the Smart Contract

Writing Test Cases

Open test.js and paste the following code:

const { ethers } = require("hardhat");

const main = async () => {
// Deploy the contract for the egg
const eggFactory = await ethers.getContractFactory("iCatEgg");
const eggContract = await eggFactory.deploy();
await eggContract.deployed();
console.log('NFT contract deployed to:', eggContract.address);
}

const runMain = async () => {
try {
await main();
process.exit(0);
}
catch(e) {
console.log(e);
process.exit(1);
}
}

runMain();

Explanation of the code execution logic line by line:

const eggFactory = await ethers.getContractFactory("iCatEgg");

This line actually compiles the smart contract and generates the necessary files in the artifacts folder.

const eggContract = await eggFactory.deploy();

During the execution of the Hardhat script, it creates a local Ethereum network on the computer, and after the script execution is complete, the network is immediately destroyed. Therefore, this line of code deploys the iCatEgg contract on this local Ethereum network.

await eggContract.deployed();

This line of code waits for the contract deployment to complete, avoiding asynchronous execution issues where later variables are used before they have values, similar to await.

console.log('NFT contract deployed to:', eggContract.address);

This line prints the contract address deployed on the local blockchain.

Running Test Cases

Before running, switch to hardhat.config.js and set the Solidity version to 0.8.17.

Now, open the terminal and enter the following:

npx hardhat run .\scripts\test.js

When you see the following output, it indicates that the smart contract has been successfully compiled and executed:

Compiled 13 Solidity files successfully
NFT contract deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3

In the next section, we will further enhance the functionality of the pet egg.