Purchase function
The purchase
function seems to be designed to allow users to purchase a non-fungible token (NFT) that's up for sale. Let's break down its functionality:
Function Explanation:
purchase(uint256 tokenId, string memory inviteCode) external payable nonReentrant
external
: This function can only be called from outside the contract.payable
: This function can receive BNB (in the case of the BSC blockchain).nonReentrant
: A modifier that ensures the function cannot be re-entered during its execution, which is a common security measure to prevent reentrancy attacks.
Function Parameters:
tokenId
: The unique identifier of the NFT being purchased.inviteCode
: A code that presumably relates to a referral or invitation system.
Function Body:
Purchase Validation:
Checks if the NFT with the given
tokenId
is actually for sale.Calculates the required fee in BNB (Binance Coin) based on the current BNB to USDT (Tether) price.
Calculates the total BNB required to purchase the NFT.
Ensures the caller has sent enough BNB to cover the purchase.
Parent-Child Relationship:
Determines the parent NFT's ID using the provided
inviteCode
.Validates that the parent NFT hasn't reached its maximum referral limit.
NFT Transfer:
Transfers the NFT from the current owner to the buyer.
Removes the NFT from the list of NFTs that are up for sale.
Updates the parent-child relationship between NFTs.
Payment Handling:
Registers the payment status as pending.
Stores payment information, including the BNB amount and the payer's address.
Distributes rewards based on the NFT's plan and the amount of BNB sent.
Refunding Excess Amount:
If the buyer sent more BNB than required, the excess amount is refunded.
Event Emission:
Emits the
NFTPurchased
event to notify any listeners of the NFT's purchase.If there's a refund, emits the
RefundExcessAmount
event.
Practical Use:
This function facilitates the purchase of an NFT by a user. The purchase price is determined in USDT but is paid in BNB. The function also handles a referral or invitation system where NFTs can have parent-child relationships, and each NFT (parent) has a maximum number of referrals (children) it can have. After a successful purchase, rewards are distributed, and any excess BNB sent by the buyer is refunded.
JavaScript Code:
Explanation:
Initialization:
We start by initializing
web3
with the BSC node's URL.We then define the ABI and address of the smart contract and create a contract instance.
Setting Parameters:
Define the
tokenId
andinviteCode
you want to use for the purchase.Convert the amount you want to send (in BNB) to Wei using
web3.utils.toWei
.
Purchase Function:
The
purchaseNFT
function is defined to call thepurchase
method of the smart contract.We use the
.send
method to send the transaction. This method requires the sender's address (from
), the amount of BNB to send (value
), and the gas limit (gas
).If the transaction is successful, the transaction hash is logged. Otherwise, any errors are caught and logged.
Execution:
Finally, we call the
purchaseNFT
function to execute the transaction.
Last updated