Create NFT Market Place without any libraries like openZeppelin (part 1)

Amir Diafi
4 min readDec 20, 2021

Create smart contracts.

Created with canva.com

To develop on the blockchain we need a blockchain environment like Truffle
Truffle is the most popular development framework for Ethereum with a mission to make your life a whole lot easier.

To Install it (I consider you already have npm in your machine) just run the following command:

npm install truffle -g

after the truffle is installed successfully, we still need a personal blockchain for Ethereum development that provides us with a development blockchain environment with fake accounts with a 100.00 ETH balance so we can develop contracts and deploy them and run tests.
you can download it easily from here:
https://trufflesuite.com/ganache/

After that install it on your system and open it, then click on QUICKSTART
to start a blockchain (development); you will get

Now after we installed the requirements, we need to init our project.
Open the terminal and run:

truffle int -y

Now, let's take a look at the project structure:
we’ve truffle-config.js
and that's the configuration of our project, let’s make some changes to it, go to the networks and add the network development to work with Ganache,
Ganache works with the following network info:


develop: {
host: ‘127.0.0.1’, // Localhost port: 7545, // Standard Ethereum port for Ganache network_id: ‘5777’, // Ganache network Id},

we named the network develop.

now we have the test folder to test our contracts and the migrations folder for the contract deployment. and lastly, in the contracts folder that includes the contract files, we’ll find the default file named Integration.sol and that's for the truffle integration, so do not delete it.

now create a file in the contract folder, name it CreateNFT.sol,
we’ll work with the solidity compiler with the version more than or equal to 0.4.22 and less 0.9.0, just put the following lines:

// SPDX-License-Identifier: MITpragma solidity >=0.4.22 <0.9.0;

now create a contract, name it CreateNFT :

Let’s discuss this file:

  • Here we create tokens Ids uint256 array to store them in the blockchain,
    and the same thing about the URIs, but here we created it with the mapping function because of the string type.
  • Then we three functions two to create the URI Token and the second one is to fetch the token URI by the token id.
    the createTokenURI function is a function that we'll create the token for the passed URI, in this function we just decrease the number of the ids and then pass the current id to the next function setTokenURI bypassing the current id and the URI, and it’ll refer the id to the URI, and that’s it.
  • We can now call to get our URI by passing the id to the last function getTokenURI(id).

Now let’s call these functions:
first, we need to deploy our contract:
1. Go to The migration folder and create a folder, name it 1_nft_creator.js
and put the following code:

const TicketNFT = artifacts.require('TicketNFT')module.exports = function (deployer) {    deployer.deploy(TicketNFT)}

2. now run the following command at the root of our project:

truffle migrate compile-all --reset --network develop

we’ll migrate then deploy our contract at the network develop (that works with ganache).
if everything goes fine you’ll notice that a new folder created named build;
now our contract has been built, we need now to start development on the blockchain, to do that just run:

truffle develop

now in we need to get an instance for our contract, in the terminal run

CreateNFT.deployed().then(instance => app = instance)
get an instance from our contract

to create a token for a URI just call the createTokenURI and pass the URI like so:

app.createTokenURI('https://amirdiafi.com')
the result

after we call the function we created the token and we spent some Gas 🔌
(also when we deployed our contract), as you can see below and created a new block.

and get the TX hash code and the transactionHash, gasUsed ..etc.
now let’s retrieve our URI by passing the token Id:
we know we created just one token so logically we’ve can call it with the _tokenURIs[0]

now let’s call the function getTokenURI

app.getTokenURI(0)
our URI

and Voila we Got it again.
now we can fetch our data from the IPFS.

In the next part, we’ll create an NFT market item and pass its data like the pricing, the owner, the seller...etc.

STAY TUNED.

Follow me on Twitter: Amir Diafi
Find me on Linkedin: Amir Diafi

--

--