DEV Community

Cover image for Create NFT Market Place without any libraries
sana
sana

Posted on

Create NFT Market Place without any libraries

Create NFT Market Place without any libraries like

openZeppelin (part 1)

Create smart contracts.

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
Enter fullscreen mode Exit fullscreen mode

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

Image description

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
},
Enter fullscreen mode Exit fullscreen mode

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: MIT
pragma solidity >=0.4.22 <0.9.0;

Enter fullscreen mode Exit fullscreen mode

now create a contract, name it CreateNFT :

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

contract CreateNFT {
    uint256[] private tokensIds;
    mapping(uint256 => string) private _tokenURIs;

    function createTokenURI(string memory _tokenURI)
        public
        returns (uint256, string memory)
    {
        uint256 currentTokenId = tokensIds.length;
        setTokenURI(currentTokenId, _tokenURI);
        tokensIds.push(currentTokenId++);
        return (currentTokenId, _tokenURI);
    }

    function setTokenURI(uint256 tokenId, string memory _tokenURI) public {
        _tokenURIs[tokenId] = _tokenURI;
    }

    function getTokenURI(uint256 tokenId) public view returns (string memory) {
        string memory _tokenURI = _tokenURIs[tokenId];
        return _tokenURI;
    }
}
Enter fullscreen mode Exit fullscreen mode

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)
}
Enter fullscreen mode Exit fullscreen mode
  1. now run the following command at the root of our project:
truffle migrate compile-all --reset --network develop
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

Image description

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')
Enter fullscreen mode Exit fullscreen mode

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.

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.
Image description

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)
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
sloan profile image
Sloan the DEV Moderator

This looks like a great article! 🙌

In fact, the topic of your post would also work really well in the Meta Punk Community too!

MetaPunk 🦙 🦙

We’re a community where blockchain builders and makers, web3 devs, and nft creators can connect, learn and share 🦙

favicon metapunk.to

Meta Punk is a really cool international NFT and web3 community where artists, web3 developers, and traders can connect, learn, and share exciting discoveries and ideas. 🦙

Would you consider posting this article there too? Because Meta Punk is built on the same platform as DEV (Forem) you can fairly easily copy the Markdown and post it there as well.

Really hope that you'll share this awesome post with the community there and consider browsing the other Forem communities out there!