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

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 { MeshMarketplaceContract } from '@meshsdk/contracts';
import { BlockfrostProvider, MeshTxBuilder } from '@meshsdk/core';

const blockchainProvider = new BlockfrostProvider(APIKEY);

const meshTxBuilder = new MeshTxBuilder({
  fetcher: blockchainProvider,
  submitter: blockchainProvider,
});

const contract = new MeshMarketplaceContract(
  {
    mesh: meshTxBuilder,
    fetcher: blockchainProvider,
    wallet: wallet,
    networkId: 0,
  },
  'addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv06fwlvuacpyv59g3a3w2fhk7daa8aepvacnpamyhyyxrgnscrfpsa',
  200
);

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 token to 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.

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
const tx = await contract.listAsset('d9312da562da182b02322fd8acb536f37eb9d29fba7c49dc172555274d657368546f6b656e', 10000000);
const signedTx = await wallet.signTx(tx);
const txHash = await wallet.submitTx(signedTx);

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
const utxo = await contract.getUtxoByTxHash(txHashToSearchFor);
const tx = await contract.purchaseAsset(utxo);
const signedTx = await wallet.signTx(tx, true);
const txHash = await wallet.submitTx(signedTx);

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
const utxo = await contract.getUtxoByTxHash(txHashToSearchFor);
const tx = await contract.relistAsset(utxo, 20000000);
const signedTx = await wallet.signTx(tx, true);
const txHash = await wallet.submitTx(signedTx);

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
const utxo = await contract.getUtxoByTxHash(txHashToSearchFor);
const tx = await contract.delistAsset(utxo);
const signedTx = await wallet.signTx(tx, true);
const txHash = await wallet.submitTx(signedTx);