6. Buying an NFT
Buying an NFT on a marketplace (often referred to as placing a "bid," where you offer ADA in exchange for assets) involves the use of smart contracts. These contracts facilitate secure agreements between parties who may not necessarily trust each other.
For this exercise, we'll assume you're already familiar with the concept of smart contracts and understand how they operate. If not, consider reviewing resources such as the Cardano Developer Portal (opens in a new tab) and the Aiken EUTxO crash course (opens in a new tab) to name but a few.
Steps to Buy an NFT
The first step in buying an NFT is to select the one you want to make an offer on. To do this, refer to the “View Assets for Sale” query to see all assets listed on the marketplace.
After choosing the NFT, a similar process occurs as when you are selling an NFT (refer to the previous “Selling an NFT" example for details).
On the Marketplace application, when a user clicks on "Buy", the application backend will build a transaction that matches the requirements of the smart contract. This includes:
-
Setting the datum with details like amount of ADA charged by the asset, royalties for the artist, and marketplace service fees. This step is necessary to unlock the NFT in the transaction, ensuring all required payouts are fulfilled.
-
Including the redeemer code, in our example “accept” (accept the offer/listing requirements).
-
Providing the necessary transaction UTXOs and address, inputs and outputs.
To successfully build the transaction, you'll need the following data, typically stored in the Ledger Sync database:
-
Protocol Parameters: Define blockchain settings necessary for calculating transaction fees and other critical parameters.
-
UTXOs: The Unspent Transaction Outputs associated with the wallet that will fund the transaction.
-
Slot/Block Tip: The current slot or block tip to ensure the transaction is valid within the blockchain context.
Once the required data is gathered, the transaction can be built, signed through the user's wallet, and submitted on-chain. After submission, the validator will confirm that all conditions are met, and if so, and the NFT will be transferred to the buyer's wallet and if any, a change.
To make sure that the asset has been transferred from the smart contract address to your wallet, you can use the following query: Breakdown list of all tokens or NFTs held in the wallet.
All of what just described is returned in the various wallet and NFT queries we discussed previously.