Building an NFT offer system smart contract in Solidity

Verdotte Aututu
3 min readJan 24, 2023

--

NFTs, or non-fungible tokens, are created using blockchain technology, allowing unique digital assets to be minted, bought, sold, and traded on a decentralized platform. NFTs are not controlled by any central authority and are resistant to censorship.

In this article, we are going to implement a smart contract that will allow the user to mint, update the listing, make, accept and cancel an offer using the ER-721 standard and Openzeppelin library which has become the status quo library for smart contract development.

Contract Implementation

In the above code snippet, we extended the OpenZeppelin library and uses the ERC721URIStorage and ReentrancyGuard contracts from the library. the first contract helps us to leverage methods such as _safeMint(), _setTokenUri(), _exist(), ownerOf(), _transfer and so on. The ReentrancyGuard contract prevent the reentrancy attack by making the nonReentrant modifier available, which ensure there are no nested (reentrant) calls to the concerned functions.

The contract has a public variable called tokenCounter which keeps track of the total number of tokens minted. There are also three public constant variables which represent the different states an offer can be in.

The contract uses several mappings to store data such as the price of each token, whether a token is currently listed or not, and offers made on a token. It also has several events that are emitted when specific actions are performed.

The contract’s constructor initializes the ERC-721 token with the name AmazonNFT and the symbol ANFT. The mint() function allows a user to create a new token and assigns it a price and a URI. The updateListingStatus()function allows the owner of a token to update the token’s listing status. The makeOffer() function allows a user to make an offer on a token that is currently listed for sale, the acceptOffer() function allows owner of a token to accept an offer made on their token and the cancelOffer allows offorers to withdraw their offers. The internal function _transferFund can be used only inside the contract to transfer ethers.

Test Implementation

Create the testing deployment script file and paste the following codes:

Add another file to contain helper functions as follow:

Let’s create a file to contain our test cases, you can name it as you want and paste the following code snippet:

In the above test implementation, we are checking that the smart contract is properly handling the minting, making, cancelling, and accepting offers of NFTs.

Conclusion

In this tutorial, we managed to implement a basic NFT offer system smart contract where a user can mint, update listing status, make, accept and cancel offers. However, this contract can be extended to support more functions such as updating offers, rejecting offers or even adding an auction system support.

To further explore, you can also build a full stack NFT minting dApp with the help of this beginner tutorial or Ethereum Foundation’s NFT tutorial.

--

--

Verdotte Aututu
Verdotte Aututu

Written by Verdotte Aututu

Software Engineer | Backend | Blockchain | DAPP | Web3 | NodeJS | AWS

Responses (1)