5SKY
  • ✨Introduction
  • Products
    • 🌌5SKY NFT Project
      • 5SKY LTI (Core)
      • Lottery Smart Contract
    • 🪙5SKY Token Project
      • Token Specifications
      • Tokenomics Explained
      • Token Features
      • Functions Overview
  • Getting Started
    • Create a Wallet
    • Overview of the 5SKY NFT
      • Connecting MetaMask
      • Connecting Trust Wallet
  • 💻DEVELOPERS
    • Decentralization & Transparent Commitment
      • Plan Structure
      • NFTData Structure
      • TokenURI function
      • Purchase function
      • Upgrade function
  • 🎇FAQ
    • 5Sky Ecosystem
    • NFT Project
    • LTI Smart Contract
    • Lottery Smart Contract
    • Token Project
  • ☎️Contact Us
    • Customer Support
    • Social Accounts & Communities
Powered by GitBook
On this page
  1. DEVELOPERS
  2. Decentralization & Transparent Commitment

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:

  1. tokenId: The unique identifier of the NFT being purchased.

  2. inviteCode: A code that presumably relates to a referral or invitation system.

Function Body:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. Refunding Excess Amount:

    • If the buyer sent more BNB than required, the excess amount is refunded.

  6. 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:

const Web3 = require('web3');
const web3 = new Web3('https://bsc-dataseed.binance.org/');

const contractABI = [...]; // The ABI of your smart contract
const contractAddress = '0x511B52B473aB081B08E98F679501492eBBaB509f';
const contract = new web3.eth.Contract(contractABI, contractAddress);

const tokenId = 123; // Replace with the desired token ID
const inviteCode = "YOUR_INVITE_CODE"; // Replace with the desired invite code
const amountToSend = web3.utils.toWei('YOUR_AMOUNT_IN_ETHER', 'ether'); // Convert the desired amount to Wei

async function purchaseNFT() {
    try {
        const transaction = await contract.methods.purchase(tokenId, inviteCode).send({
            from: 'YOUR_WALLET_ADDRESS',
            value: amountToSend,
            gas: 200000 // Adjust the gas limit as needed
        });
        console.log("Transaction successful with hash:", transaction.transactionHash);
    } catch (error) {
        console.error("Error executing transaction:", error);
    }
}

purchaseNFT();

Explanation:

  1. 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.

  2. Setting Parameters:

    • Define the tokenId and inviteCode you want to use for the purchase.

    • Convert the amount you want to send (in BNB) to Wei using web3.utils.toWei.

  3. Purchase Function:

    • The purchaseNFT function is defined to call the purchase 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.

  4. Execution:

    • Finally, we call the purchaseNFT function to execute the transaction.

PreviousTokenURI functionNextUpgrade function

Last updated 1 year ago

💻