how to create functionality to make NFT burn automatically when its expiry date is reached ? while minting i am storing 2 parameters minted date and number of months to expire , so i want it to burn automatically after expiry date is reached
1 Answer
To check the time use Chainlink Keepers. Your contract will be able to check if the current date has passed the expiration date. After the condition is met you could call OpenZeppelin's _burn function:
// 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);
}