S Price: $0.454044 (+0.09%)

Contract Diff Checker

Contract Name:
GenCodeTEST3

Contract Source Code:

File 1 of 1 : GenCodeTEST3

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

// Library: Base64
library Base64 {
    string internal constant _TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    function encode(bytes memory data) internal pure returns (string memory) {
        if (data.length == 0) return "";

        string memory table = _TABLE;
        string memory result = new string(4 * ((data.length + 2) / 3));

        assembly {
            let tablePtr := add(table, 1)
            let resultPtr := add(result, 32)
            for {
                let i := 0
            } lt(i, mload(data)) {
            } {
                i := add(i, 3)
                let input := and(mload(add(data, i)), 0xffffff)
                let out := mload(add(tablePtr, and(shr(18, input), 0x3F)))
                out := shl(8, out)
                out := add(out, and(mload(add(tablePtr, and(shr(12, input), 0x3F))), 0xFF))
                out := shl(8, out)
                out := add(out, and(mload(add(tablePtr, and(shr(6, input), 0x3F))), 0xFF))
                out := shl(8, out)
                out := add(out, and(mload(add(tablePtr, and(input, 0x3F))), 0xFF))
                mstore(resultPtr, shl(224, out))
                resultPtr := add(resultPtr, 4)
            }
            switch mod(mload(data), 3)
            case 1 {
                mstore(sub(resultPtr, 2), shl(240, 0x3d3d))
            }
            case 2 {
                mstore(sub(resultPtr, 1), shl(248, 0x3d))
            }
        }

        return result;
    }
}

// Library: Strings
library Strings {
    bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";

    function toString(uint256 value) internal pure returns (string memory) {
        if (value == 0) {
            return "0";
        }
        uint256 temp = value;
        uint256 digits;
        while (temp != 0) {
            digits++;
            temp /= 10;
        }
        bytes memory buffer = new bytes(digits);
        while (value != 0) {
            digits -= 1;
            buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
            value /= 10;
        }
        return string(buffer);
    }
}

// Abstract Contract: ERC721
abstract contract ERC721 {
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
    function _safeMint(address to, uint256 tokenId) internal virtual {}
}

// Abstract Contract: ERC721URIStorage
abstract contract ERC721URIStorage is ERC721 {
    mapping(uint256 => string) private _tokenURIs;

    function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
        _tokenURIs[tokenId] = _tokenURI;
    }
}

/**
 * @title GenCodeTEST-3
 * @notice Optimized contract for minting NFTs with generative art.
 * @dev Each wallet can mint only once. Gas optimizations applied.
 */
contract GenCodeTEST3 is ERC721URIStorage {
    using Strings for uint256;

    uint256 private constant _MAX_SUPPLY = 2;
    uint256 private _totalSupply;
    mapping(address => bool) private _hasMinted;

    event Minted(address indexed user, uint256 tokenId);

    constructor() {}

    function mint() external {
        require(!_hasMinted[msg.sender], "You have already minted your NFT");
        require(_totalSupply < _MAX_SUPPLY, "All NFTs have been minted");

        _hasMinted[msg.sender] = true;
        uint256 tokenId = ++_totalSupply;

        string memory svg = _generateSVG(tokenId, msg.sender);
        string memory tokenURI = _generateTokenURI(tokenId, svg);

        _safeMint(msg.sender, tokenId);
        _setTokenURI(tokenId, tokenURI);

        emit Minted(msg.sender, tokenId);
    }

    function _generateTokenURI(uint256 tokenId, string memory svg)
        private
        pure
        returns (string memory)
    {
        return string(
            abi.encodePacked(
                "data:application/json;base64,",
                Base64.encode(
                    bytes(
                        abi.encodePacked(
                            '{"name": "GenCodeTEST-3 #',
                            tokenId.toString(),
                            '", "description": "A unique generative art NFT.", "image": "data:image/svg+xml;base64,',
                            Base64.encode(bytes(svg)),
                            '"}'
                        )
                    )
                )
            )
        );
    }

    function _generateSVG(uint256 tokenId, address sender)
        private
        pure
        returns (string memory)
    {
        string memory colorCream = "#FFF8DC";
        string[5] memory colors = ["#FFD700", "#FFA500", "#FF4500", "#FF0000", "#8B0000"];
        uint256 seed = uint256(keccak256(abi.encodePacked(sender, tokenId)));
        string memory color2 = colors[seed % colors.length];

        return string(
            abi.encodePacked(
                '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 500">',
                '<defs><linearGradient id="grad1" x1="10%" y1="20%" x2="60%" y2="100%">',
                '<stop offset="10%" style="stop-color:', colorCream, ';stop-opacity:1;" />',
                '<stop offset="100%" style="stop-color:', color2, ';stop-opacity:1;" />',
                '</linearGradient></defs>',
                '<rect width="500" height="500" fill="url(#grad1)" />',
                '</svg>'
            )
        );
    }
}

Please enter a contract address above to load the contract details and source code.

Context size (optional):