Solidity
一定時間後自動刻錄 NFT
如何創建功能以使 NFT 在到期時自動燃燒? 在鑄造時,我儲存了 2 個參數鑄造日期和到期月數,所以我希望它在到期後自動燃燒
要檢查時間,請使用 Chainlink Keepers。您的契約將能夠檢查目前日期是否已過期。滿足條件後,您可以呼叫 OpenZeppelin 的 _burn 函式:
// SPXD-License-Identifier: MIT pragma solidity ^0.8.8; import "@chainlink/contracts/src/v0.8/interfaces/KeeperCompatibleInterface.sol"; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract NFT is KeeperCompatibleInterface, ERC721 { //time is in unix //set mintDate and timeTillExpire uint256 public currentDate = block.timestamp; uint256 public mintDate; uint256 public timeTillExpire; uint256 public expireDate = mintDate + timeTillExpire; uint256 public tokenId = 0; //check if it has passed expiration function checkUpkeep(bytes memory) public override returns (bool needsUpkeep, bytes memory) { bool timePassed = (expireDate >= currentDate); needsUpkeep = (timePassed); } //burns token once condition is met function performUpkeep(bytes calldata) external override { (bool needsUpkeep, ) = checkUpkeep(""); require(needsUpkeep == true, "Upkeep not needed."); _burn(tokenId); }