Categories
Ref: medium

Building NFT Marketplace On Klaytn Part-1 | by Chanderprakash

What is an NFT marketplace?

An NFT marketplace is a platform where users can buy, sell, and trade non-fungible tokens (NFTs). NFTs are unique digital assets that are stored on a blockchain, typically Ethereum, and represent ownership or proof of authenticity of a particular item or piece of content, such as digital art, collectibles, music, videos, virtual real estate, and more.

In an NFT marketplace, creators can mint NFTs, which involves creating a digital token and attaching it to a specific piece of content. Once minted, these NFTs can be listed for sale on the marketplace, where buyers can purchase them using cryptocurrency. Each NFT has a unique digital signature, making it distinguishable from other tokens, and its ownership and transaction history are recorded on the blockchain, providing transparency and authenticity.

Popular NFT marketplaces include platforms like OpenSea, Rarible, and Foundation, where artists, collectors, and enthusiasts converge to buy, sell, and explore the burgeoning world of digital ownership and creativity.

Let us understand the example contracts needed to build an NFT marketplace, don’t limit yourself to this obviously you can build more.

Now let’s come to our part
1.BiddingContract

2.NFTMarketplace
3.TransferFunds

Now let us understand this contracts one by one,
1. Bidding Contract —
This Solidity contract implements a simple NFT auction system. Sellers can create auctions with a starting price and a discount rate over time. Buyers can bid on the NFT within the auction duration, with the price decreasing gradually based on the discount rate. Once a buyer fulfills the price requirement, they receive the NFT, and any excess payment is refunded.

Now let us understand the solidity smart contract,

This part of the code defines an interface called `IKIP17`, which outlines a function `transferFrom`. This function allows transferring a non-fungible token (NFT) with a given `_tokenId` from one address `_from` to another address `_to`. Essentially, it specifies the structure that any contract implementing the KIP-17 standard for NFTs needs to adhere to, enabling interoperability among different NFT contracts and applications.

Now let us understand the contract

Let us understand this code,

1. `uint256 private constant DURATION = 7 days;`: Defines the duration of the auction as 7 days.

2. `IKIP17 public nft;`: Declares a public variable to store the KIP-17 compliant NFT contract address.

3. `uint256 public nftId;`: Declares a public variable to store the ID of the auctioned NFT.

4. `address payable public seller;`: Declares a public variable to store the seller’s address, allowing it to receive Ether.

5. `uint256 public startingPrice;`: Declares a public variable to store the initial price of the NFT.

6. `uint256 public discountRate;`: Declares a public variable to store the rate at which the NFT’s price decreases over time.

7. `uint256 public startAt;`: Declares a public variable to store the timestamp indicating the start of the auction.

8. `uint256 public expiresAt;`: Declares a public variable to store the timestamp indicating the end of the auction.

This is the constructor function of the contract. It takes four parameters:

1. `uint256 _startingPrice`: Represents the starting price of the NFT auction.

2. `uint256 _discountRate`: Denotes the rate at which the price of the NFT decreases over time during the auction.

3. `address _nft`: Indicates the address of the KIP-17 compliant NFT contract being auctioned.

4. `uint256 _nftId`: Stores the unique identifier (ID) of the NFT being auctioned.

When the contract is deployed, this constructor is called with these parameters, initializing the state variables `startingPrice`, `discountRate`, `nft`, and `nftId` with the provided values. Additionally, it sets the `seller` variable to the address of the sender (contract deployer), and records the current timestamp as the starting time of the auction (`startAt`). Finally, it calculates the expiration time of the auction (`expiresAt`) by adding the predefined duration of 7 days to the starting time. If the starting price provided is not higher than the sum of the discount rate and the duration, the deployment will fail with an error message stating “Starting price is too low”.

This section within the constructor function initializes the contract’s state variables and checks for validity:

1. `seller = payable(msg.sender);`: Sets the `seller` variable to the address of the sender (the deployer of the contract) and makes it payable, indicating that it can receive Ether.

2. `startingPrice = _startingPrice;`: Assigns the provided `_startingPrice` parameter to the `startingPrice` variable, representing the initial price of the NFT in the auction.

3. `discountRate = _discountRate;`: Assigns the provided `_discountRate` parameter to the `discountRate` variable, indicating the rate at which the NFT’s price decreases over time during the auction.

4. `startAt = block.timestamp;`: Records the current block timestamp as the start time of the auction, storing it in the `startAt` variable.

5. `expiresAt = block.timestamp + DURATION;`: Calculates the expiration time of the auction (`expiresAt`) by adding the predefined duration (`DURATION`) of 7 days to the current block timestamp.

6. `require(_startingPrice >= _discountRate + DURATION, “Starting price is too low”);`: Checks if the provided starting price is higher than or equal to the sum of the discount rate and the duration of the auction. If it’s not, the contract deployment fails with the error message “Starting price is too low”.

7. `nft = IKIP17(_nft);`: Initializes the `nft` variable with the address of the KIP-17 compliant NFT contract provided as the `_nft` parameter.

8. `nftId = _nftId;`: Assigns the provided `_nftId` parameter to the `nftId` variable, storing the unique identifier (ID) of the NFT being auctioned.

This function, `getPrice()`, calculates and returns the current price of the NFT being auctioned. Here’s how it works:

1. `uint256 timeElapsed = block.timestamp — startAt;`: Calculates the time elapsed since the start of the auction by subtracting the stored start time (`startAt`) from the current block timestamp (`block.timestamp`).

2. `uint256 discount = discountRate * timeElapsed;`: Calculates the discount on the starting price based on the elapsed time. It multiplies the discount rate (`discountRate`) by the time elapsed since the auction started (`timeElapsed`).

3. `return startingPrice — discount;`: Computes the current price of the NFT by subtracting the discount from the starting price. This adjusted price reflects any decrease due to the passage of time according to the specified discount rate. The function then returns this calculated price.

Certainly, let’s break down the `buy()` function step by step:

1. `require(block.timestamp < expiresAt, “This NFT bidding has ended”);`: This line checks if the current block timestamp is before the expiration timestamp (`expiresAt`) of the auction. If the current time has surpassed the expiration time, the function aborts with the error message “This NFT bidding has ended”, indicating that the auction has concluded and no further purchases can be made.

2. `uint256 price = getPrice();`: This line calculates the current price of the NFT using the `getPrice()` function. The `getPrice()` function computes the current price based on the elapsed time since the start of the auction and the discount rate.

3. `require(msg.value >= price, “The amount of KLAY sent is less than the price of the token”);`: This line ensures that the amount of Klay sent with the transaction (`msg.value`) is greater than or equal to the calculated price of the NFT (`price`). If the sent amount is less than the price, the function aborts with the error message “The amount of KLAY sent is less than the price of the token”.

4. `nft.transferFrom(seller, msg.sender, nftId);`: This line transfers ownership of the NFT from the seller to the buyer (`msg.sender`) using the `transferFrom()` function of the KIP-17 compliant NFT contract (`nft`). It transfers the NFT with the specified ID (`nftId`).

5. `uint256 refund = msg.value — price;`: This line calculates the amount of excess Klay sent by subtracting the price of the NFT (`price`) from the amount of Klay sent with the transaction (`msg.value`).

6. `if(refund > 0){ msg.sender.transfer(refund); }`: If there’s any excess Klay sent (`refund > 0`), this block refunds the excess amount back to the buyer (`msg.sender`) using the `transfer()` function.

7. `selfdestruct(seller);`: Finally, this line self-destructs the contract and transfers any remaining funds to the seller (`seller`). This ensures that after the purchase, the contract is terminated and any remaining funds are returned to the seller.

You can explore other smart contracts needed to build NFT Marketplace; note that these are one example.

https://github.com/chanderprakash20/How-to-NFT-Marketplace-on-Klaytn

Source link

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *