Step-by-step instruction: How to create an NFT collection using Golang and Ethereum (Smart Contract)

Gopher
3 min readMar 26, 2022

In previous articles, we learned how to generate 10,000 images for our NFT collection using Golang and uploaded all generated images to IPFS decentralized storage.

In this article, I would like to share my knowledge and experience, as well as the pitfalls that I had to face when developing smart contracts for NFT collections on the Ethereum block-chain.

We will create a smart contract for our NFT collection, test and upload the created smart contract to the Ethereum test-net. But before we start coding, I would like to dwell on the ERC-721 standard, because this standard describes the specification of NFT tokens.

Let’s take a closer look at what methods our smart contract should have:

The full specification for the ERC-721 standard can be found here.

We do not have to implement the entire standard ourselves, a more optimal approach is to reuse ready-made libraries: OpenZeppelin is a library for developing secure smart contracts and this is what we will work with.

The advantages of this solution are obvious:

  • Code for a smart contract out of the box
  • Auditing our smart contract will take much less money and time

Creating a smart contract

Let’s create a generic smart contract for our NFT collection, let’s call it MonkeyNFT. Our smart contract inherits the standard OpenZeppelin libraries:

  • ERC721 and ERC721Enumerable — contract modules that provide basic functionality for our NFT token
  • Ownable is a contract module that provides a basic access control mechanism

To develop a smart contract, we will be using a tool like hardhat, a very cool tool for developing, testing and deploying, especially for those who are tired of Truffle and its endless number of bugs.

The utility has a function to create a basic template, we just need to run:

npx hardhat init

Let’s now edit the base template to suit our needs. In the contracts directory, we need to write the following solidity code:

Let’s now take a closer look at the code we wrote:

  • The saleIsActive flag indicates that our collection is either ready for sale or not. This function is very useful at the initial stage, when for some reason it is necessary to stop sales.
  • The maxPurchase variable stores the number of tokens that the user can buy at one time. This is protection against bots so that they are not able to buy the entire collection at once.
  • The mintMonkey function is the main function through which users can buy our NFT token. The payable modifier simply says that the call to this function is payable to the user.

To compile our smart contract, we need to run the following command:

npx hardhat compile

As a result, abi-artifacts should be created in the artifact directory.

Smart contract testing

Because the loaded smart contract cannot be modified, we definitely need to test it before deploying it to the Ethereum block-chain.

The test directory contains default tests that you can delete. We won’t need them anymore, we’ll write our own tests instead.

To run the tests we need to run the command:

npx hardhat test

If the tests pass successfully, we will see:

Compiling 1 file with 0.8.4 
Compilation finished successfully
Token contract
✓ Should initialize contract
✓ Should set the right owner
✓ Should mint (41ms)
3 passing (1s)

We have successfully tested:

  • Smart contract creation
  • Successfully assigned smart contract owner
  • Successfully called mintMonkeys paid function to sell NFT token

Smart contract deployment

We will deploy our smart contract on the Ethereum test-net. To do this, we need to go to the scripts directory and delete all default scripts (we don’t need them anymore).

Let’s write a new deployment script:

We also need to change the hardhat.config file:

Everything is ready for deployment, let’s run the final script:

npx hardhat run scripts/deploy.js --network rinkeby

P.S. All source code can be found on GitHub.

--

--