Marketplace
This marketplace allows anyone to buy and sell native assets such as 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
Do check out the guide and the marketplace starter kit that might help you get started. This contract is written in plu-ts, you can view the contract on GitHub.
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.
import { BasicMarketplace } from '@meshsdk/contracts'; import { KoiosProvider } from '@meshsdk/core'; import { useWallet } from '@meshsdk/react'; const blockchainProvider = new KoiosProvider('preprod'); const { wallet } = useWallet(); const marketplace = new BasicMarketplace({ fetcher: blockchainProvider, initiator: wallet, network: 'preprod', signer: wallet, submitter: blockchainProvider, percentage: 25000, // 2.5% owner: 'addr_test1vpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0c7e4cxr', });
You can define the fetcher
and submitter
with one of our blockchain providers or use your own custom provider. We use these fetcher
and submitter
to query for locked UTxO and submit transactions. The connected wallet are defined in the initiator
and signer
. The network can defined in network
, it has to be one of the following values: "testnet" | "preview" | "preprod" | "mainnet"
The owner
is the address of the marketplace owner which will receive the marketplace fee. The percentage
is the percentage of the sale price that the marketplace owner
will take. Note that, the fee numerator is in the order of millions, for example 3000
implies a fee of 3000/1_000_000
(or 0.003
) implies a fee of 0.3%
.
Try the demo
You can test this martetplace smart contract on this page.
Firstly, switch your wallet network to one of the testnets, and connect wallet.
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.
address
is the seller's address, here we use the first Used Addresses of the connected wallet. asset
is the asset's unit
to be listed. price
is the listing price in Lovelace.
async marketplace.listAsset( address: string, asset: string, price: number )
It is important to save the listing infomation (asset, seller address and listing price) in a database. This is needed to update/cancel/purchase the listing.
const { wallet } = useWallet(); const sellerAddress = (await wallet.getUsedAddresses())[0]; const txHash = await marketplace.listAsset( sellerAddress, 'd9312da562da182b02322fd8acb536f37eb9d29fba7c49dc172555274d657368546f6b656e', 10000000 );
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.
async marketplace.purchaseAsset( address: string, asset: string, price: number )
address
is the seller's address. asset
is the listed asset's unit
. price
is the listed price in Lovelace.
const txHash = await marketplace.purchaseAsset( 'SELLER ADDRESS', 'd9312da562da182b02322fd8acb536f37eb9d29fba7c49dc172555274d657368546f6b656e', 10000000 );
Update Listing
Update a listing on the marketplace. For the contract, the seller can update the listing price.
address
is the seller's address. asset
is the listed asset's unit
. oldPrice
is the listed price in Lovelace. newPrice
is the updated listed price in Lovelace.
async marketplace.relistAsset( address: string, asset: string, oldPrice: number newPrice: number )
It is important to update the updated listing price in a database.
const txHash = await marketplace.relistAsset( 'SELLER ADDRESS', 'd9312da562da182b02322fd8acb536f37eb9d29fba7c49dc172555274d657368546f6b656e', 10000000, 20000000 );
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.
address
is the seller's address. asset
is the listed asset's unit
. price
is the listed price in Lovelace.
async marketplace.delistAsset( address: string, asset: string, price: number )
const txHash = await marketplace.delistAsset( 'SELLER ADDRESS', 'd9312da562da182b02322fd8acb536f37eb9d29fba7c49dc172555274d657368546f6b656e', 10000000 );