Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 10, 2022 12:28 pm GMT

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

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:
weve truffle-config.js
and that's the configuration of our project, lets 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, well 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,
well 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 :

// SPDX-License-Identifier: MITpragma 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;    }}

Lets 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 itll refer the id to the URI, and thats it.
We can now call to get our URI by passing the id to the last function getTokenURI(id).

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

well migrate then deploy our contract at the network develop (that works with ganache).
if everything goes fine youll 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)

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')

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 lets retrieve our URI by passing the token Id:
we know we created just one token so logically weve can call it with the _tokenURIs[0]

now lets call the function getTokenURI

app.getTokenURI(0)

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

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


Original Link: https://dev.to/sananayab/create-nft-market-place-without-any-libraries-3hj9

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To