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

Gopher
3 min readMar 25, 2022

The boring story began a year ago, when my boring artist friend came to me and offered to create an NFT collection on the Ethereum block-chain. To be honest, I was never interested in crypto and at the time I had no idea what NFT`s were and how they worked.

In this article, I will not explain what NFT`s are and what they are for, instead I will focus on the technical part, because when I started, there was very little material and I had to come up with some solutions myself.

After a little research, I was able to break the initial task from the abstract “create an NFT collection” into smaller and more specific ones:

  • generate 10,000 unique images
  • generate 10,000 metadata for each image
  • upload 10,000 images along with metadata to decentralized storage
  • create a smart contract for NFT tokens
  • upload the created smart contract to the Ethereum main-net
  • create a website that will interact with our smart contract using web-3, where users can exchange their ethers for our NFT tokens

It may seem that each task is simple and can be solved very quickly. Actually, at every stage, unforeseen moments awaited me, which we will now talk about.

How to generate 10,000 unique images?

Why exactly 10,000? The answer is quite simple, the most popular NFT projects offer collections of exactly 10,000 NFT tokens. Each creator is free to decide how many NFT tokens they want to release, but we decided not to deviate from the canon and also made 10,000 tokens.

So how do you generate 10,000 unique images anyway? Of course, with the help of automatic layering of layers on top of each other. After some thought, the artist and I came to the conclusion that for our project we need the following layers:

  • background — 20 items
  • body— 25 items
  • head — 15 items
  • emotions — 20 items
  • clothes — 30 items
  • shoes — 25 items
  • accessories — 40 items

In total, we ended up with approximately 175 unique item layers, which is more than enough to get 10,000 unique characters. Now it remains to write a program that will accept layers as input, and produce ready-made characters at the output.

I will use Golang to write the program. Let’s go.

First, we need to define 2 structures, one for the layers and one for the canvas.

Let’s take a closer look at both structures.

Structure ImageLayer:

  • Field “Image” — image layer
  • Field “Priority” — layer priority, because layers must be applied in a certain order, first the background, then the body, then the head, etc…
  • Fields “XPos” and “YPos” — layer position on the canvas

Structure BgProperty:

  • Field “Width” — canvas width
  • Field “Length” — canvas length

So, when the basic structures are defined, we can move on to writing a service that will actually combine our layers in a certain order.

The service code is quite simple, at the input the service takes a list of layers and canvas parameters, and at the output it returns bytes of the generated image. I would like to note that Go has a fairly good library for working with images:

Great, when the code for generating images is ready, we can move on to creating metadata.

The metadata stores all the properties and links to images for NFT tokens, and it is according to the metadata that most marketplaces look for NFT tokens. Therefore, it is very important to choose the right format for metadata.

What format should the metadata for NFT tokens be in?

Because NFT tokens are based on the ERC-721 standard, and the standard itself does not describe in any way what format the metadata should be in, we are free to use any format we want.

But if we want our NFT tokens to be fully tradable on marketplaces like OpenSea, we must follow the following json format:

Well, now that we have decided on the format, let’s describe the structure for storing metadata:

Before we start generating metadata, we need to upload images to a decentralized storage. In the next article, I will explain how to upload images to decentralized storage, and we will also continue to work with metadata.

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

--

--