How a NFT marketplace work?

Almost everyone know what NFT is now, and they have tried to mint all kinds of NFT to sell them to others in this bull market year of crypto asset.After one mint some NFTs,they would find a NFT marketplace and list their NFT on it for sale.Then,the decentralized marketplace get some fees from the sellers for offering some quick,efficient ways to sell their NFT.As the NFT market gets hotter, the marketplace get more reward.It seems to be a big business with a lot of profit,believe it or not, it’s just a piece of cake.

Now there are many Marketplaces make their code open-source , I have researched two of them,the first and second place of the NFT marketplace now -Opensea and Rarible.Opensea is known as a very early established marketplace,with the oldest smart contract with solidity 0.4 version,so it may not be quite useful now.In contrast, Rarible offer a smart contract with solidity over 0.5 version, which may be more friendly for us to deploy and try it,so I ‘ll choose Rarible’s contract to introduce the process of NFT exchange transaction.

There is the source code of Rarible:

https://etherscan.io/address/0xcd4ec7b66fbc029c116ba9ffb3e59351c20b5b06#code

There are 5 supplement contracts(4 useful with 1 deprecated) and 1 main contract.

As you deploy the main contract,you are asked to input the address of contract mentioned above,so it’s vital to deploy the 5 supplement contracts first.They are:

1.TransferProxy

2.TransferProxyForDeprecated(optional,not that useful)

3.ERC20TransferProxy

4.ExchangeStateV3

5.ExchangeOrderHolderV1

After these contracts are all deployed,it’s time to deploy the main contract(ExchangeV3) now.

The constructor needs 7 addresses (TransferProxyAddress,TransferProxyForDeprecatedAddress,ERC20TransferProxy,ExchangeStateV3,ExchangeOrderHolderV1,Beneficiary,BuyerfeeSigner),if some are not necessary,we can input as 0x00000000000000000000000000000000000000(Null address).

After deployment, we should do some approval and addOperator.

1.Approve the NFT we want to sell to the TransferProxy contract.

2.Add ExchangeV1 as the operator of TransferProxy

3.Add ExchangeV1 as the operator of ExchangeState

4.Add ExchangeV1 as the operator of ERC20TransferProxy

After that,the marketplace is about to finish,we can test the NFT/ETH exchange or NFT/ERC20 exchange now.We can do “Buy Now to “pay for NFT with ether,or “Make Offer” to pay for NFT with ERC20 token.

For “Buy Now”, seller input the order(just fill in the order tuple following the struct describe in contract)and call prepareMessage to generate a hashMessage.Buyer signed the message and get a signature ,then the signature will be separate into an array in [v,r,s] type.Buyer would get the order,sig,buyerFeeSig.Buyer just need to input the price that match the order that the seller set in the order,and sign the transaction,the transaction of buying NFT with ether would be fininshed soon.

For “Make Offer”,buyer should first approve the contract to use his/her ERC20 first.Then,buyer input the order including how much they want to pay for the nft and call prepareMessage to generate a message,and then sign it to get the signature,and give it to seller.Seller would receive order from buyer and decide to accept offer or not.If seller accept and sign the transaction,contract will do exchange NFT for ERC20 between seller and buyer,the transaction just finished.

In the above mentioned procedure ,I just skip the prepareBuyerFeeMessage and sign,it’s a procedure that Rarible prepare the revenue share message and give it to the final transaction signer to finish the transaction and get some revenue from buyer.

The whole transaction seems to be complicated ,but it’s quite simple if we ignore the buyerFee and BuyerFeeSigner.Buyer/seller just input their order and sign it and give it to the other side,then someone signed the transaction.In brief ,just one “sign message” and the other “sign transaction” ,the transaction will be succeed.

I have followed Rarible’s contract and write functions to make offer using ether,anyone interested about it can follow this:

https://github.com/timmychien/NFT/blob/main/NFTMarket.sol

For more question about how Rarible contract work,you can leave some message below,I’ll reply your question soon.

Thanks a lot for finishing reading.