Upgrade function
The upgrade function appears to be designed to allow the owner of a specific non-fungible token (NFT) to upgrade its associated plan. Let's break down its functionality:
Function Explanation:
upgrade(uint256 tokenId, uint256 newPlanIndex) external payable nonReentrant
external: This function can only be called from outside the contract.payable: This function can receive BNB (or another native cryptocurrency, depending on the 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 upgraded.newPlanIndex: The index of the new plan to which the NFT should be upgraded.
Function Body:
Ownership Check:
Ensures that the caller (
msg.sender) is the owner of the NFT with the giventokenId.
Plan Validation:
Checks if the provided
newPlanIndexis valid. It should be less than 5 (indicating there are 5 plans) and greater than 0.
Current Plan Determination:
Retrieves the current plan associated with the NFT using its
tokenId.Iterates through the
plansarray to determine the index of the NFT's current plan. This is done by comparing the name of each plan in theplansarray with the name of the NFT's current plan using thekeccak256hash function. Once a match is found, the loop breaks, and thecurrentPlanIndexis set.
JavaScript Code:
const Web3 = require('web3');
const web3 = new Web3('https://bsc-dataseed.binance.org/');
const contractABI = [...]; // Smart contract ABI
const contractAddress = '0x511B52B473aB081B08E98F679501492eBBaB509f';
const contract = new web3.eth.Contract(contractABI, contractAddress);
async function upgradeNFT(tokenId, newPlanIndex) {
const fromAddress = 'YOUR_WALLET_ADDRESS'; // The address calling the function (NFT owner)
const gasLimit = 200000; // This is just an estimate. You might need to adjust based on your contract's requirements.
try {
const tx = await contract.methods.upgrade(tokenId, newPlanIndex)
.send({ from: fromAddress, gas: gasLimit });
console.log("Transaction successful with hash:", tx.transactionHash);
} catch (error) {
console.error("Error executing the upgrade:", error);
}
}
// Example usage:
upgradeNFT(123, 2); // Upgrading NFT with tokenId 123 to plan index 2Explanation:
Initialization:
We start by initializing
web3with an BSC node URL.We define the ABI and address of the smart contract, then create a contract instance using
web3.
upgradeNFTFunction:This function is designed to interact with the
upgradefunction of the smart contract.It takes two parameters:
tokenId(the ID of the NFT you want to upgrade) andnewPlanIndex(the index of the new plan to which you want to upgrade the NFT).The
fromAddressis the BSC address that will be calling the function. This should be the owner of the NFT.We estimate a
gasLimitfor the transaction. This is a rough estimate, and you might need to adjust it based on the actual gas requirements of your contract.Inside the function, we call the
upgrademethod of the contract and send the transaction. If successful, the transaction hash is logged to the console.
Error Handling:
If there's an issue with the transaction (e.g., not enough gas, the NFT isn't owned by the caller, the plan index is invalid), the error will be caught and logged to the console.
Example Usage:
We provide an example of how to use the
upgradeNFTfunction. In this example, we're attempting to upgrade an NFT with atokenIdof 123 to a plan with an index of 2.
This JavaScript function provides a way to interact with the BSC smart contract's upgrade function from an external application or script.
Last updated