Code Snippets

Minting Functions

// Function to mint a standard Lordz NFT
function mintToken(address user, string memory catNum, string memory lrdId) payable external returns (uint256) {
    require (msg.value >= bnbMintCost, "Not enough BNB");
    require((user == _msgSender() || _msgSender() == owner()), "Reserved function");
    require(IERC20(tokenAddr).balanceOf(user) >= lordzMintCost * 10 ** 9, "Not enough $LORDZ");
    IERC20(tokenAddr).transferFrom(user, royalAddr, lordzMintCost * 10 ** 9);
    uint256 id = totalSupply() + 1;
    catalog[id] = catNum;
    lordzID[id] = lrdId;
    _safeMint(user, id);
    _setTokenURI(id, string(abi.encodePacked(catNum, ".json")));
    return id;
}

// Function to mint a rare OP Lordz NFT using VRF for randomness
function mintVRFToken(address user, string memory catNum, string memory lrdId) payable external returns (uint256) {
    require (msg.value >= bnbVRFCost, "Not enough BNB");
    require((user == _msgSender() || _msgSender() == owner()), "Reserved function");
    require (!mintLocked[user], "Minting is locked");
    require(IERC20(tokenAddr).balanceOf(user) >= lordzVRFCost * 10 ** 9, "Not enough $LORDZ");
    IERC20(tokenAddr).transferFrom(user, royalAddr, lordzVRFCost * 10 ** 9);
    mintLocked[user] = true;
    uint256 id = totalSupply() + 1;
    catalog[id] = catNum;
    lordzID[id] = lrdId;
    bytes32 requestId = getRandomNumber();
    rarity[id] = resultVRF;
    reqId = requestId;
    _safeMint(user, id);
    _setTokenURI(id, string(abi.encodePacked(catNum, "a.json")));
    return id;
}

Burning Function

// Function to burn a Lordz NFT
function burnToken(uint256 tokenId) external onlyTokenOwner(tokenId) {
    _burn(tokenId);
}

Transfer Functions

// Function to safely transfer a Lordz NFT
function safeTransferFrom(address from, address to, uint256 tokenId) public override {
    safeTransferFrom(from, to, tokenId, "");
}

// Function to safely transfer a Lordz NFT with data
function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public override {
    require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
    _safeTransfer(from, to, tokenId, _data);
}

// Function to transfer a Lordz NFT
function transferFrom(address from, address to, uint256 tokenId) public override {
    require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
    _transfer(from, to, tokenId);
}

Approval Functions

// Function to approve another address to transfer a specific Lordz NFT
function approve(address to, uint256 tokenId) public override {
    address owner = ERC721.ownerOf(tokenId);
    require(to != owner, "ERC721: approval to current owner");
    require(_msgSender() == owner || isApprovedForAll(owner, _msgSender()), "ERC721: approve caller is not owner nor approved for all");
    _approve(to, tokenId);
}

// Function to set or unset the approval of a given operator
function setApprovalForAll(address operator, bool approved) public override {
    _setApprovalForAll(_msgSender(), operator, approved);
}

Randomness Functions

// Function to request randomness from the VRF coordinator
function getRandomNumber() internal returns (bytes32 requestId) {
    require(LINK.balanceOf(address(this)) >= linkFee, "Not enough LINK");
    return requestRandomness(keyHash, linkFee);
}

// Callback function used by VRF Coordinator to fulfill randomness
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
    randomResult = (randomness % 1000);
    if (randomResult < aFreq) {
        resultVRF = "a";
    } else if (randomResult >= aFreq && randomResult < (aFreq + bFreq)) {
        resultVRF = "b";
    } else if (randomResult >= (aFreq + bFreq) && randomResult < (aFreq + bFreq + cFreq)) {
        resultVRF = "c";
    } else if (randomResult >= (aFreq + bFreq + cFreq) && randomResult < (aFreq + bFreq + cFreq + dFreq)) {
        resultVRF = "d";
    } else {
        resultVRF = "e";
    }
    reqId = requestId;
}

Withdrawal Functions

// Function to withdraw all LINK tokens from the contract
function withdrawLink() external onlyOwner {
    IERC20 linkContract = IERC20(linkAddr);
    linkContract.transfer(_msgSender(), linkContract.balanceOf(address(this)));
}

// Function to withdraw all BNB from the contract
function withdrawBNB() external onlyOwner {
    payable(msg.sender).transfer(address(this).balance);
}

// Function to withdraw all BNB from the contract to a specific address
function withdrawBNBto(address addr) external onlyOwner {
    payable(addr).transfer(address(this).balance);
}

Ownership Functions

// Function to renounce ownership of the contract
function renounceOwnership() public override onlyOwner {
    _transferOwnership(address(0));
}

// Function to transfer ownership of the contract to a new address
function transferOwnership(address newOwner) public override onlyOwner {
    require(newOwner != address(0), "Ownable: new owner is the zero address");
    _transferOwnership(newOwner);
}

Helper Functions

// Function to set the base URI for token metadata
function setBaseURI(string memory newBaseURI) external onlyOwner {
    baseURI = newBaseURI;
}

// Function to set token metadata URI
function setTokenMetadataURI(uint256 tokenId) external onlyTokenOwner(tokenId) {
    _setTokenURI(tokenId, string(abi.encodePacked(catalog[tokenId], rarity[tokenId], ".json")));
    mintLocked[ownerOf(tokenId)] = false;
}

// Function to set frequencies for random minting
function setFreqs(uint256 a, uint256 b, uint256 c, uint256 d, uint256 e) external onlyOwner {
    require((a + b + c + d + e) == 1000, "All values must add up to 1000");
    aFreq = a;
    bFreq = b;
    cFreq = c;
    dFreq = d;
    eFreq = e;
}

// Function to set the LINK fee for VRF
function setLinkFee(uint256 fee) external onlyOwner {
    linkFee = fee;
}

// Function to set the mint cost in LORDZ tokens
function setLordzMintCost(uint256 cost) external onlyOwner {
    lordzMintCost = cost;
}

// Function to set the VRF mint cost in LORDZ tokens
function setLordzVRFCost(uint256 cost) external onlyOwner {
    lordzVRFCost = cost;
}

// Function to set the mint cost in BNB
function setBnbMintCost(uint256 cost) external onlyOwner {
    bnbMintCost = cost;
}

// Function to set the VRF mint cost in BNB
function setBnbVrfCost(uint256 cost) external onlyOwner {
    bnbVRFCost = cost;
}

// Function to set the token address for LORDZ
function setTokenAddr(address token) external onlyOwner {
    tokenAddr = token;
}

// Function to set the royalty address
function setRoyalAddr(address addr) external onlyOwner {
    royalAddr = addr;
}

These snippets highlight the core functionality of the MemeLordz NFT smart contract, ensuring secure and efficient interactions with the NFTs within the ecosystem.

Last updated