Marketplace
Build a NFT marketplace to buy and sell NFTs
The marketplace smart contract allows users to buy and sell NFTs. A seller list an NFT for sales by specifying a certain price, and anyone can buy it by paying the demanded price.
There are 4 actions (or endpoints) available to interact with this smart contract:
- list asset
- buy asset
- updating listing
- cancel listing
Install package
First you can to install the @meshsdk/contracts
package:
Initialize the Marketplace
Utilizing the Marketplace contract requires a blockchain provider and a connected browser wallet. Here is an example how we can initialize the Marketplace.
To initialize the Marketplace, we import the MeshMarketplaceContract
. The first JSON object is the inputs
for the MeshTxInitiatorInput
, this requires a MeshTxBuilder
, a Provider
, a Wallet
, and define the network ID.
Second and third parameters are the ownerAddress
and feePercentageBasisPoint
. The ownerAddress
is the address of the marketplace owner which will receive the marketplace fee. The feePercentageBasisPoint
is the percentage of the sale price that the marketplace owner
will take. The fee numerator is in the order of hundreds, for example 200
implies a fee of 2%
.
Both on-chain and off-chain codes are open-source and available on Mesh Github Repository.
Mint a Mesh Token to try demos
List Asset
List an asset on the marketplace. This will allow other users to buy the asset. The seller will receive the listing price in ADA. The seller can cancel the listing at any time. The seller can also update the listing price at any time.
listAsset()
list an asset for sale. The function accepts the following parameters:
- asset (string) - the asset's unit to be listed
- price (number) - the listing price in Lovelace
List an asset for sale
Connect wallet to run this demo
Buy Asset
Purchase a listed asset from the marketplace. The seller will receive the listed price in ADA and the buyer will receive the asset. The marketplace owner will receive a fee if it is specified.
purchaseAsset()
purchase a listed asset. The function accepts the following parameters:
- utxo (UTxO) - unspent transaction output in the script
We have provided a very handle function, getUtxoByTxHash
, which will return the UTxO object for a given transaction hash.
A successful purchase will send the asset to the wallet that signed the transaction to purchase the asset.
Purchase a listed asset from the marketplace
Connect wallet to run this demo
Update Listing
Update a listing on the marketplace. For the contract, the seller can update the listing price.
relistAsset()
update a listing on the marketplace. The function accepts the following parameters:
- utxo (UTxO) - unspent transaction output in the script
- newListPrice (number) - the new listing price in Lovelace
We have provided a very handle function, getUtxoByTxHash
, which will return the UTxO object for a given transaction hash.
Update the listing price of an asset on the marketplace
Connect wallet to run this demo
Cancel Listing
Cancel a listing on the marketplace. The seller can cancel the listing at any time. The seller will receive the listed asset back.
delistAsset()
cancel a listing on the marketplace. The function accepts the following parameters:
- utxo (UTxO) - unspent transaction output in the script
We have provided a very handle function, getUtxoByTxHash
, which will return the UTxO object for a given transaction hash.
Cancel a listing on the marketplace
Connect wallet to run this demo