Skip to content
Verified contractExactMIT

ArgonautsRendererV13

Address0x7beef198e2f89dce21f71c21dabbdd794d932b90
Balance

0 ETH

 

Transactions

5

 

Compiler

solc 0.8.24

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.24+commit.e11b9ed9
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,246,014
Creation transaction
0x20c2265cb9…a73158cb9b

src/RendererV13.sol

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

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Ownable2Step} from "@openzeppelin/contracts/access/Ownable2Step.sol";
import {SSTORE2} from "./libraries/SSTORE2.sol";
import {TraitDefs} from "./libraries/TraitDefs.sol";
import {Ink, Pen} from "./Strokes.sol";

interface IArtV13 {
    function isDragonsBreath(uint256 tokenId) external view returns (bool);
    function blobs(uint256 i) external view returns (address);
    function layout() external view returns (bytes memory);
    function vapeMouthIndex() external view returns (uint8);
    function vapeSmokeBlob() external view returns (uint8);
    function vapeBlueberryBlob() external view returns (uint8);
    function vapeDragonsBlob() external view returns (uint8);
    function crownHeadIndex() external view returns (uint8);
    function crownClipBlob(uint8 body) external view returns (uint8);
}

interface IPainterV13 {
    function picture(uint256 tokenId, uint8[7] memory t) external view returns (bytes memory);
    function still(uint256 tokenId, uint8[7] memory t) external view returns (bytes memory);
}

interface IPlayerV13 {
    function base() external view returns (address);
    function painter() external view returns (address);
    function mode() external view returns (uint8);
    function screenEyes() external view returns (uint8);
    function part(uint8 which) external view returns (bytes memory);
    function partChunks(uint8 which) external view returns (uint256);
    function moves(uint256 tokenId) external view returns (bool);
    function traitsFor(uint256 tokenId, uint8[7] memory t) external view returns (uint8[7] memory);
}

interface IInnerV13 {
    function player() external view returns (address);
    function tokenURI(uint256 tokenId, uint8[7] memory traits, bool printed) external view returns (string memory);
}

/// @title ArgonautsRendererV13
contract ArgonautsRendererV13 is Ownable2Step {
    using Pen for Ink;

    uint256 public constant MAX_ID = 9999;
    uint8 internal constant PLAYER = 1;
    uint8 internal constant IMAGE = 2;
    uint8 internal constant HEAD = 0;
    uint8 internal constant LIB = 1;
    uint8 internal constant SCREEN = 2;
    uint8 internal constant MAIN = 3;

    bytes32 internal constant A1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
    bytes32 internal constant A2 = "ghijklmnopqrstuvwxyz0123456789+/";

    struct Enc {
        uint256 n;
        uint256 acc;
    }

    IInnerV13 public immutable inner;
    IPlayerV13 public immutable player;
    IArtV13 public immutable base;
    uint8 public immutable ground;
    uint8 public immutable groundB;
    uint8 public immutable groundC;
    address internal immutable pairs;
    address[] internal tail;
    mapping(uint8 => address) public swap;

    event BatchMetadataUpdate(uint256 _fromTokenId, uint256 _toTokenId);
    event TailSet(uint256 chunks, uint256 length);
    event SwapSet(uint8 id, uint256 length);

    error NoInner();
    error Misaligned();

    constructor(address inner_, uint8[3] memory grounds) Ownable(msg.sender) {
        if (inner_.code.length == 0) revert NoInner();
        inner = IInnerV13(inner_);
        player = IPlayerV13(IInnerV13(inner_).player());
        base = IArtV13(player.base());
        ground = grounds[0];
        groundB = grounds[1];
        groundC = grounds[2];
        bytes memory tb = new bytes(16384);
        bytes memory al = new bytes(64);
        bytes32 a1 = A1;
        bytes32 a2 = A2;
        assembly ("memory-safe") {
            let a := add(al, 32)
            mstore(a, a1)
            mstore(add(a, 32), a2)
            let t := add(tb, 32)
            for { let x := 0 } lt(x, 64) { x := add(x, 1) } {
                let cx := byte(0, mload(add(a, x)))
                for { let y := 0 } lt(y, 64) { y := add(y, 1) } {
                    let cy := byte(0, mload(add(a, y)))
                    let i := add(t, shl(1, or(shl(6, x), y)))
                    mstore8(i, byte(0, mload(add(a, shr(2, cx)))))
                    mstore8(add(i, 1), byte(0, mload(add(a, or(shl(4, and(cx, 3)), shr(4, cy))))))
                    mstore8(add(i, 8192), byte(0, mload(add(a, or(shl(2, and(cx, 15)), shr(6, cy))))))
                    mstore8(add(i, 8193), byte(0, mload(add(a, and(cy, 63)))))
                }
            }
        }
        pairs = SSTORE2.write(tb);
    }

    function setTail(bytes[] calldata chunks) external onlyOwner {
        delete tail;
        _add(chunks);
    }

    function addTail(bytes[] calldata chunks) external onlyOwner {
        _add(chunks);
    }

    function _add(bytes[] calldata chunks) internal {
        for (uint256 i = 0; i < chunks.length; i++) {
            tail.push(SSTORE2.write(chunks[i]));
        }
        emit TailSet(tail.length, _size(tail));
        emit BatchMetadataUpdate(1, MAX_ID);
    }

    function setSwap(uint8 id, bytes calldata data) external onlyOwner {
        swap[id] = data.length == 0 ? address(0) : SSTORE2.write(data);
        emit SwapSet(id, data.length);
        emit BatchMetadataUpdate(1, MAX_ID);
    }

    function blobOf(uint8 id) public view returns (address p) {
        p = swap[id];
        if (p == address(0)) p = base.blobs(id);
    }

    function tailChunks() external view returns (uint256) {
        return tail.length;
    }

    function tailPart() external view returns (bytes memory out) {
        for (uint256 i = 0; i < tail.length; i++) {
            out = bytes.concat(out, SSTORE2.read(tail[i]));
        }
    }

    function covers(uint256 tokenId, uint8[7] memory t) public view returns (bool) {
        if (tail.length == 0 || player.mode() & PLAYER == 0 || player.partChunks(MAIN) == 0) return false;
        uint8 g = player.traitsFor(tokenId, t)[0];
        if (g != ground && g != groundB && g != groundC) return false;
        return player.moves(tokenId);
    }

    function tokenURI(uint256 tokenId, uint8[7] memory t, bool printed) external view returns (string memory) {
        if (!covers(tokenId, t)) return inner.tokenURI(tokenId, t, printed);
        t = player.traitsFor(tokenId, t);
        IPainterV13 painter = IPainterV13(player.painter());
        bytes memory svg = player.mode() & IMAGE != 0 ? painter.picture(tokenId, t) : painter.still(tokenId, t);
        bytes memory open = _open(tokenId, t, printed);
        bytes memory head = player.part(HEAD);
        bytes memory lib = player.part(LIB);
        bytes memory screen = t[4] == player.screenEyes() ? player.part(SCREEN) : bytes("");
        uint256 len = open.length + (svg.length + 2) / 3 * 4 + 41 + head.length;
        bytes memory d = _data(tokenId, t, len % 3);
        len += d.length / 3 * 4;
        uint256 rest = lib.length + screen.length + _size(tail);

        bytes memory table = SSTORE2.read(pairs);
        Ink memory f = Pen.open(64 + (len + 2) / 3 * 4 + rest);
        Enc memory e;
        f.put("data:application/json;base64,");
        _feed(f, e, open);
        _twice(f, e, svg, table);
        _feed(f, e, '","animation_url":"data:text/html;base64,');
        _feed(f, e, head);
        _twice(f, e, d, table);
        if (e.n != 0) revert Misaligned();
        _put(f, lib);
        _put(f, screen);
        _pour(f, tail);
        uint256 from = f.from;
        uint256 used = f.at - from;
        assembly {
            mstore(add(from, used), 0)
            mstore(sub(from, 64), 32)
            mstore(sub(from, 32), used)
            return(sub(from, 64), add(64, and(add(used, 31), not(31))))
        }
    }

    function _open(uint256 tokenId, uint8[7] memory t, bool printed) internal view returns (bytes memory) {
        Ink memory a = Pen.open(2_000);
        a.put('{"name":"Argonaut #');
        if (tokenId < 1000) a.ch(48);
        if (tokenId < 100) a.ch(48);
        if (tokenId < 10) a.ch(48);
        a.num(tokenId);
        a.put(
            '","description":"One of 9,999 signed, numbered textured prints on museum board. The digital lives here, inside the contract, forever. A Muse Facktory production.","attributes":['
        );
        for (uint8 layer = 0; layer < 7; layer++) {
            string memory v = TraitDefs.itemName(layer, t[layer]);
            if (layer == TraitDefs.LAYER_MOUTH && t[layer] == base.vapeMouthIndex()) {
                v = base.isDragonsBreath(tokenId) ? "Vape (Dragon's Breath)" : "Vape (Blueberry Kush)";
            }
            if (bytes(v).length == 0) continue;
            a.put('{"trait_type":"');
            a.put(bytes(TraitDefs.layerLabel(layer)));
            a.put('","value":"');
            a.put(bytes(v));
            a.put('"},');
        }
        a.put('{"trait_type":"Print","value":"');
        a.put(printed ? bytes("Claimed") : bytes("Unclaimed"));
        a.put('"}],"image":"data:image/svg+xml;base64,');
        return a.close();
    }

    function _data(uint256 tokenId, uint8[7] memory t, uint256 pending) internal view returns (bytes memory) {
        bytes memory lay = base.layout();
        bool vape = t[5] == base.vapeMouthIndex();
        bool alt = vape && base.isDragonsBreath(tokenId);
        Ink memory d = Pen.open(16_000);
        d.put("{t:[");
        d.num(tokenId);
        for (uint256 i = 0; i < 7; i++) {
            d.ch(0x2c);
            d.num(t[i]);
        }
        d.put("],g:");
        d.ch(alt ? 49 : 48);
        d.put(',n:"",s:[');
        for (uint8 layer = 0; layer < 7; layer++) {
            uint8 id = _blobId(lay, layer, t[layer]);
            if (layer == 5 && vape) id = alt ? base.vapeDragonsBlob() : base.vapeBlueberryBlob();
            if (layer == 1 && t[6] == base.crownHeadIndex()) {
                uint8 clip = base.crownClipBlob(t[1]);
                if (clip != 0) id = clip;
            }
            if (layer != 0) d.ch(0x2c);
            d.ch(0x22);
            if (id != 0xFF) _once(d, SSTORE2.read(blobOf(id)));
            d.ch(0x22);
        }
        d.put(',"');
        if (vape) _once(d, SSTORE2.read(base.blobs(base.vapeSmokeBlob())));
        d.put('"]}');
        while ((d.at - d.from) % 3 != 0) {
            d.ch(0x20);
        }
        while ((pending + (d.at - d.from) / 3 * 4) % 3 != 0) {
            d.put("   ");
        }
        return d.close();
    }

    function _blobId(bytes memory lay, uint8 layer, uint8 idx) internal pure returns (uint8) {
        uint256 o;
        for (uint8 l = 0; l < layer; l++) {
            o += 1 + uint8(lay[o]);
        }
        if (idx >= uint8(lay[o])) return 0xFF;
        return uint8(lay[o + 1 + idx]);
    }

    function _put(Ink memory k, bytes memory b) internal pure {
        uint256 n = b.length;
        assembly ("memory-safe") {
            let at := mload(k)
            mcopy(at, add(b, 32), n)
            mstore(k, add(at, n))
        }
    }

    function _size(address[] storage p) internal view returns (uint256 n) {
        for (uint256 i = 0; i < p.length; i++) {
            n += SSTORE2.codeSize(p[i]);
        }
    }

    function _pour(Ink memory k, address[] storage p) internal view {
        for (uint256 i = 0; i < p.length; i++) {
            address ptr = p[i];
            uint256 n = SSTORE2.codeSize(ptr);
            assembly ("memory-safe") {
                let at := mload(k)
                extcodecopy(ptr, at, 1, n)
                mstore(k, add(at, n))
            }
        }
    }

    function _once(Ink memory k, bytes memory data) internal pure {
        Enc memory e;
        _feed(k, e, data);
        _finish(k, e);
    }

    function _feed(Ink memory k, Enc memory e, bytes memory data) internal pure {
        bytes32 a1 = A1;
        bytes32 a2 = A2;
        assembly ("memory-safe") {
            let m := mload(0x40)
            mstore(0x1f, a1)
            mstore(0x3f, a2)
            let at := mload(k)
            let src := add(data, 32)
            let end := add(src, mload(data))
            let n := mload(e)
            let acc := mload(add(e, 32))
            for {} and(gt(n, 0), lt(src, end)) {} {
                acc := or(shl(8, acc), byte(0, mload(src)))
                src := add(src, 1)
                n := add(n, 1)
                if eq(n, 3) {
                    mstore8(at, mload(and(shr(18, acc), 0x3F)))
                    mstore8(add(at, 1), mload(and(shr(12, acc), 0x3F)))
                    mstore8(add(at, 2), mload(and(shr(6, acc), 0x3F)))
                    mstore8(add(at, 3), mload(and(acc, 0x3F)))
                    at := add(at, 4)
                    n := 0
                    acc := 0
                }
            }
            for {} lt(add(src, 2), end) {} {
                let w := shr(232, mload(src))
                src := add(src, 3)
                mstore8(at, mload(and(shr(18, w), 0x3F)))
                mstore8(add(at, 1), mload(and(shr(12, w), 0x3F)))
                mstore8(add(at, 2), mload(and(shr(6, w), 0x3F)))
                mstore8(add(at, 3), mload(and(w, 0x3F)))
                at := add(at, 4)
            }
            for {} lt(src, end) {} {
                acc := or(shl(8, acc), byte(0, mload(src)))
                src := add(src, 1)
                n := add(n, 1)
            }
            mstore(e, n)
            mstore(add(e, 32), acc)
            mstore(k, at)
            mstore(0x40, m)
        }
    }

    function _finish(Ink memory k, Enc memory e) internal pure {
        bytes32 a1 = A1;
        bytes32 a2 = A2;
        assembly ("memory-safe") {
            let n := mload(e)
            if n {
                let m := mload(0x40)
                mstore(0x1f, a1)
                mstore(0x3f, a2)
                let at := mload(k)
                let w := shl(mul(8, sub(3, n)), mload(add(e, 32)))
                mstore8(at, mload(and(shr(18, w), 0x3F)))
                mstore8(add(at, 1), mload(and(shr(12, w), 0x3F)))
                mstore8(add(at, 2), mload(and(shr(6, w), 0x3F)))
                mstore8(add(at, 3), 0x3d)
                if eq(n, 1) { mstore8(add(at, 2), 0x3d) }
                mstore(k, add(at, 4))
                mstore(e, 0)
                mstore(add(e, 32), 0)
                mstore(0x40, m)
            }
        }
    }

    function _inner(bytes memory data, uint256 from, uint256 to) internal pure returns (bytes memory) {
        bytes memory piece = new bytes(to - from);
        assembly ("memory-safe") {
            mcopy(add(piece, 32), add(add(data, 32), from), sub(to, from))
        }
        Ink memory k = Pen.open(16);
        _once(k, piece);
        return k.close();
    }

    function _twice(Ink memory k, Enc memory e, bytes memory data, bytes memory table) internal pure {
        uint256 n = data.length;
        uint256 off = ((3 - e.n) % 3) * 3;
        if (off > n) off = n;
        if (off != 0) _feed(k, e, _inner(data, 0, off));
        if (e.n == 0) {
            uint256 groups = (n - off) / 9;
            assembly ("memory-safe") {
                let ta := add(table, 32)
                let tb := add(ta, 8192)
                let at := mload(k)
                let src := add(add(data, 32), off)
                let end := add(src, mul(groups, 9))
                for {} lt(src, end) { src := add(src, 9) } {
                    let w := shr(184, mload(src))
                    mstore(at, mload(add(ta, shl(1, shr(60, w)))))
                    mstore(add(at, 2), mload(add(tb, shl(1, and(shr(54, w), 0xFFF)))))
                    mstore(add(at, 4), mload(add(ta, shl(1, and(shr(42, w), 0xFFF)))))
                    mstore(add(at, 6), mload(add(tb, shl(1, and(shr(36, w), 0xFFF)))))
                    mstore(add(at, 8), mload(add(ta, shl(1, and(shr(24, w), 0xFFF)))))
                    mstore(add(at, 10), mload(add(tb, shl(1, and(shr(18, w), 0xFFF)))))
                    mstore(add(at, 12), mload(add(ta, shl(1, and(shr(6, w), 0xFFF)))))
                    mstore(add(at, 14), mload(add(tb, shl(1, and(w, 0xFFF)))))
                    at := add(at, 16)
                }
                mstore(k, at)
            }
            off += groups * 9;
        }
        if (off < n) _feed(k, e, _inner(data, off, n));
    }
}

lib/openzeppelin-contracts/contracts/access/Ownable.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)

pragma solidity ^0.8.20;

import {Context} from "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * The initial owner is set to the address provided by the deployer. This can
 * later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    /**
     * @dev The caller account is not authorized to perform an operation.
     */
    error OwnableUnauthorizedAccount(address account);

    /**
     * @dev The owner is not a valid owner account. (eg. `address(0)`)
     */
    error OwnableInvalidOwner(address owner);

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the address provided by the deployer as the initial owner.
     */
    constructor(address initialOwner) {
        if (initialOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(initialOwner);
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        if (owner() != _msgSender()) {
            revert OwnableUnauthorizedAccount(_msgSender());
        }
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        if (newOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

lib/openzeppelin-contracts/contracts/access/Ownable2Step.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (access/Ownable2Step.sol)

pragma solidity ^0.8.20;

import {Ownable} from "./Ownable.sol";

/**
 * @dev Contract module which provides access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * This extension of the {Ownable} contract includes a two-step mechanism to transfer
 * ownership, where the new owner must call {acceptOwnership} in order to replace the
 * old one. This can help prevent common mistakes, such as transfers of ownership to
 * incorrect accounts, or to contracts that are unable to interact with the
 * permission system.
 *
 * The initial owner is specified at deployment time in the constructor for `Ownable`. This
 * can later be changed with {transferOwnership} and {acceptOwnership}.
 *
 * This module is used through inheritance. It will make available all functions
 * from parent (Ownable).
 */
abstract contract Ownable2Step is Ownable {
    address private _pendingOwner;

    event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Returns the address of the pending owner.
     */
    function pendingOwner() public view virtual returns (address) {
        return _pendingOwner;
    }

    /**
     * @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
     * Can only be called by the current owner.
     *
     * Setting `newOwner` to the zero address is allowed; this can be used to cancel an initiated ownership transfer.
     */
    function transferOwnership(address newOwner) public virtual override onlyOwner {
        _pendingOwner = newOwner;
        emit OwnershipTransferStarted(owner(), newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual override {
        delete _pendingOwner;
        super._transferOwnership(newOwner);
    }

    /**
     * @dev The new owner accepts the ownership transfer.
     */
    function acceptOwnership() public virtual {
        address sender = _msgSender();
        if (pendingOwner() != sender) {
            revert OwnableUnauthorizedAccount(sender);
        }
        _transferOwnership(sender);
    }
}

lib/openzeppelin-contracts/contracts/utils/Context.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)

pragma solidity ^0.8.20;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}

src/Strokes.sol

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

struct Ink {
    uint256 at;
    uint256 from;
    uint256 cap;
}

library Pen {
    error TooLong();

    function open(uint256 cap) internal pure returns (Ink memory k) {
        bytes memory b = new bytes(cap + 32);
        uint256 at;
        assembly ("memory-safe") {
            at := add(b, 32)
        }
        k.at = at;
        k.from = at;
        k.cap = cap;
    }

    function put(Ink memory k, bytes memory s) internal pure {
        assembly ("memory-safe") {
            let n := mload(s)
            let at := mload(k)
            mcopy(at, add(s, 32), n)
            mstore(k, add(at, n))
        }
    }

    function w(Ink memory k, bytes32 s, uint256 n) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            mstore(at, s)
            mstore(k, add(at, n))
        }
    }

    function ch(Ink memory k, uint256 c) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            mstore8(at, c)
            mstore(k, add(at, 1))
        }
    }

    function num(Ink memory k, uint256 v) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            switch lt(v, 10)
            case 1 {
                mstore8(at, add(48, v))
                mstore(k, add(at, 1))
            }
            default {
                switch lt(v, 100)
                case 1 {
                    mstore8(at, add(48, div(v, 10)))
                    mstore8(add(at, 1), add(48, mod(v, 10)))
                    mstore(k, add(at, 2))
                }
                default {
                    let n := 1
                    for { let t := v } gt(t, 9) {} {
                        t := div(t, 10)
                        n := add(n, 1)
                    }
                    let p := add(at, n)
                    mstore(k, p)
                    for {} 1 {} {
                        p := sub(p, 1)
                        mstore8(p, add(48, mod(v, 10)))
                        v := div(v, 10)
                        if iszero(v) { break }
                    }
                }
            }
        }
    }

    function sint(Ink memory k, int256 v) internal pure {
        if (v < 0) {
            ch(k, 0x2d);
            // forge-lint: disable-next-line(unsafe-typecast)
            num(k, uint256(-v));
        } else {
            // forge-lint: disable-next-line(unsafe-typecast)
            num(k, uint256(v));
        }
    }

    function hex6(Ink memory k, uint256 rgb) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            mstore8(at, 0x23)
            let n := and(shr(20, rgb), 15)
            mstore8(add(at, 1), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(shr(16, rgb), 15)
            mstore8(add(at, 2), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(shr(12, rgb), 15)
            mstore8(add(at, 3), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(shr(8, rgb), 15)
            mstore8(add(at, 4), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(shr(4, rgb), 15)
            mstore8(add(at, 5), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(rgb, 15)
            mstore8(add(at, 6), add(add(n, 48), mul(39, gt(n, 9))))
            mstore(k, add(at, 7))
        }
    }

    function op(Ink memory k, uint256 v) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            switch iszero(v)
            case 1 {
                mstore8(at, 48)
                at := add(at, 1)
            }
            default {
                switch lt(v, 1000)
                case 0 {
                    mstore8(at, 49)
                    at := add(at, 1)
                }
                default {
                    mstore8(at, 46)
                    mstore8(add(at, 1), add(48, div(v, 100)))
                    at := add(at, 2)
                    let r := mod(v, 100)
                    if r {
                        mstore8(at, add(48, div(r, 10)))
                        at := add(at, 1)
                        r := mod(r, 10)
                        if r {
                            mstore8(at, add(48, r))
                            at := add(at, 1)
                        }
                    }
                }
            }
            mstore(k, at)
        }
    }

    function secs(Ink memory k, uint256 steps) internal pure {
        num(k, steps / 10);
        uint256 r = steps % 10;
        if (r != 0) {
            ch(k, 0x2e);
            ch(k, 48 + r);
        }
        ch(k, 0x73);
    }

    function close(Ink memory k) internal pure returns (bytes memory b) {
        uint256 used = k.at - k.from;
        if (used > k.cap) revert TooLong();
        uint256 from = k.from;
        assembly ("memory-safe") {
            b := sub(from, 32)
            mstore(b, used)
        }
    }
}

library Fixed {
    int256 internal constant WAD = 1e18;
    int256 internal constant HALF = 5e17;
    int256 internal constant PI = 3141592653589793238;
    int256 internal constant TWO_PI = 6283185307179586476;
    int256 internal constant HALF_PI = 1570796326794896619;

    function sin(int256 x) internal pure returns (int256) {
        unchecked {
            x = x % TWO_PI;
            if (x < 0) x += TWO_PI;
            bool neg;
            if (x >= PI) {
                x -= PI;
                neg = true;
            }
            if (x > HALF_PI) x = PI - x;
            int256 x2 = x * x / WAD;
            int256 term = x;
            int256 sum = x;
            term = term * x2 / WAD / 6;
            sum -= term;
            term = term * x2 / WAD / 20;
            sum += term;
            term = term * x2 / WAD / 42;
            sum -= term;
            term = term * x2 / WAD / 72;
            sum += term;
            term = term * x2 / WAD / 110;
            sum -= term;
            term = term * x2 / WAD / 156;
            sum += term;
            return neg ? -sum : sum;
        }
    }

    function cos(int256 x) internal pure returns (int256) {
        return sin(x + HALF_PI);
    }

    function round(int256 x) internal pure returns (int256 q) {
        unchecked {
            int256 v = x + HALF;
            q = v / WAD;
            if (v < 0 && v % WAD != 0) q -= 1;
        }
    }

    function op(int256 x) internal pure returns (uint256 k) {
        unchecked {
            if (x <= 0) return 0;
            // forge-lint: disable-next-line(unsafe-typecast)
            k = uint256((x * 1000 + HALF) / WAD);
            if (k > 1000) k = 1000;
        }
    }
}

struct Card {
    uint256 tok;
    uint8[7] t;
    uint256[576] px;
    uint256[128] x;
    uint256[] eyes;
    uint256[] lens;
    uint256[] red;
    uint256[] blue;
    uint256[] screen;
    uint256[] body;
    uint256[] head;
    uint256[] halo;
    uint256[] led;
    uint256[] smoke;
    uint256[] petals;
    uint256[] polyps;
    uint256[] glints;
    uint256[] crown;
    uint256 smokeRGB;
    bool dragon;
    int256 tempo;
    int256 tb0;
    int256 pv;
}

struct Wisps {
    uint256 nw;
    uint256[] full;
    uint256[] half;
    int256[] dx;
    uint256[] dy;
    uint256 any1;
    uint256 any2;
}

struct Light {
    uint256[] mask;
    uint256 count;
    uint256 cycle;
    bool own;
    uint256 loop;
    uint256 colour;
    int256 amount;
    int256 rise;
    int256 fall;
    uint256 mulC;
    uint256 mulS;
    uint256 add;
    uint256 mod;
}

/// @title Strokes
abstract contract Strokes {
    using Pen for Ink;

    int256 internal constant WAD = 1e18;

    uint256 internal constant RGB = 0xFFFFFF;
    uint256 internal constant SMK = 1 << 27;
    uint256 internal constant RED = 1 << 28;
    uint256 internal constant BLUE = 1 << 29;

    uint256 internal constant GROUND = 0;
    uint256 internal constant BODY = 1;
    uint256 internal constant CLOAK = 2;
    uint256 internal constant EYES = 4;
    uint256 internal constant MOUTH = 5;
    uint256 internal constant HEAD = 6;
    uint256 internal constant SMOKE = 7;

    function _mark() internal pure returns (uint256 m) {
        assembly ("memory-safe") {
            m := mload(0x40)
        }
    }

    function _free(uint256 m) internal pure {
        assembly {
            mstore(0x40, m)
        }
    }

    function _own(uint256 v) internal pure returns (uint256) {
        return (v >> 24) & 7;
    }

    function _chroma(uint256 c) internal pure returns (uint256) {
        unchecked {
            uint256 r = (c >> 16) & 255;
            uint256 g = (c >> 8) & 255;
            uint256 b = c & 255;
            uint256 mx = r > g ? r : g;
            if (b > mx) mx = b;
            uint256 mn = r < g ? r : g;
            if (b < mn) mn = b;
            return mx - mn;
        }
    }

    function _top(uint256 c) internal pure returns (uint256 mx) {
        uint256 r = (c >> 16) & 255;
        uint256 g = (c >> 8) & 255;
        uint256 b = c & 255;
        mx = r > g ? r : g;
        if (b > mx) mx = b;
    }

    function _lum(uint256 c) internal pure returns (uint256) {
        unchecked {
            return 299 * ((c >> 16) & 255) + 587 * ((c >> 8) & 255) + 114 * (c & 255);
        }
    }

    function _trim(uint256[] memory a, uint256 n) internal pure {
        assembly ("memory-safe") {
            mstore(a, n)
        }
    }

    function _sw(Card memory c, uint256 i) internal pure returns (int256) {
        // forge-lint: disable-next-line(unsafe-typecast)
        return int256((c.x[i] * 1e18) >> 32);
    }

    function _steps(Card memory c, uint256 p10) internal pure returns (uint256 n) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            n = uint256((int256(p10) * WAD * 2 + c.tempo) / (2 * c.tempo));
            if (n < 2) n = 2;
        }
    }

    function _from(Card memory c, uint256 p10) internal pure returns (int256) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            return (c.tb0 * 10 / int256(p10)) % WAD;
        }
    }

    function _at(int256 from, uint256 i, uint256 n) internal pure returns (int256) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            return (from + WAD * int256(i) / int256(n)) % WAD;
        }
    }

    function _ch(uint256 v, uint256 a, uint256 b) internal pure returns (uint256 o) {
        unchecked {
            o = (v * a + b * 100 + 50) / 100;
            if (o > 255) o = 255;
        }
    }

    function _scaled(uint256 rgb, uint256 a, uint256 b) internal pure returns (uint256) {
        return (_ch(rgb >> 16, a, b) << 16) | (_ch((rgb >> 8) & 255, a, b) << 8) | _ch(rgb & 255, a, b);
    }

    function _flat(uint256 n, uint256 colour) internal pure returns (uint256[] memory out) {
        unchecked {
            out = new uint256[](n);
            for (uint256 i = 0; i < n; i++) {
                out[i] = colour;
            }
        }
    }

    function _tint(Card memory c, uint256[] memory pxs, uint256 a, uint256 b)
        internal
        pure
        returns (uint256[] memory out)
    {
        unchecked {
            out = new uint256[](pxs.length);
            for (uint256 i = 0; i < pxs.length; i++) {
                out[i] = _scaled(c.px[pxs[i]] & RGB, a, b);
            }
        }
    }

    function _any(uint256[] memory v) internal pure returns (bool) {
        unchecked {
            for (uint256 i = 0; i < v.length; i++) {
                if (v[i] != 0) return true;
            }
            return false;
        }
    }

    function _anim(
        Ink memory k,
        bytes32 attr,
        uint256 attrLen,
        uint256[] memory v,
        uint256 steps,
        uint256 shift,
        bool raw
    ) internal pure {
        unchecked {
            k.w('<animate attributeName="', 24);
            k.w(attr, attrLen);
            k.w('" values="', 10);
            _vals(k, v, raw);
            k.w('" dur="', 7);
            k.secs(steps);
            k.ch(0x22);
            if (shift != 0) {
                k.w(' begin="-', 9);
                k.secs(shift);
                k.ch(0x22);
            }
            k.w(' calcMode="discrete" repeatCount', 32);
            k.w('="indefinite"/>', 15);
        }
    }

    function _vals(Ink memory k, uint256[] memory v, bool raw) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            let n := mload(v)
            let p := add(v, 32)
            for { let i := 0 } lt(i, n) { i := add(i, 1) } {
                if i {
                    mstore8(at, 0x3b)
                    at := add(at, 1)
                }
                let x := mload(add(p, shl(5, i)))
                switch raw
                case 0 {
                    switch iszero(x)
                    case 1 {
                        mstore8(at, 48)
                        at := add(at, 1)
                    }
                    default {
                        switch lt(x, 1000)
                        case 0 {
                            mstore8(at, 49)
                            at := add(at, 1)
                        }
                        default {
                            mstore8(at, 46)
                            mstore8(add(at, 1), add(48, div(x, 100)))
                            at := add(at, 2)
                            let r := mod(x, 100)
                            if r {
                                mstore8(at, add(48, div(r, 10)))
                                at := add(at, 1)
                                r := mod(r, 10)
                                if r {
                                    mstore8(at, add(48, r))
                                    at := add(at, 1)
                                }
                            }
                        }
                    }
                }
                default {
                    if gt(x, 99) {
                        mstore8(at, add(48, div(x, 100)))
                        at := add(at, 1)
                        x := mod(x, 100)
                        mstore8(at, add(48, div(x, 10)))
                        at := add(at, 1)
                        x := mod(x, 10)
                    }
                    if gt(x, 9) {
                        mstore8(at, add(48, div(x, 10)))
                        at := add(at, 1)
                        x := mod(x, 10)
                    }
                    mstore8(at, add(48, x))
                    at := add(at, 1)
                }
            }
            mstore(k, at)
        }
    }

    function _run(Ink memory k, uint256 x, uint256 y, uint256 n) internal pure {
        assembly ("memory-safe") {
            function d(at, v) -> to {
                to := at
                if gt(v, 9) {
                    mstore8(to, add(48, div(v, 10)))
                    to := add(to, 1)
                }
                mstore8(to, add(48, mod(v, 10)))
                to := add(to, 1)
            }
            let at := mload(k)
            mstore8(at, 0x4d)
            at := d(add(at, 1), x)
            mstore8(at, 0x20)
            at := d(add(at, 1), y)
            mstore8(at, 0x68)
            at := d(add(at, 1), n)
            mstore(at, "v1h-")
            at := d(add(at, 4), n)
            mstore8(at, 0x7a)
            mstore(k, add(at, 1))
        }
    }

    function _px(uint256[576] memory px, uint256 p) internal pure returns (uint256 v) {
        assembly ("memory-safe") {
            v := mload(add(px, shl(5, p)))
        }
    }

    function _paths(Ink memory k, uint256[] memory pxs, uint256[] memory cols) internal pure {
        unchecked {
            uint256 len = pxs.length;
            uint256[] memory runs = new uint256[](len);
            uint256 nr;
            uint256 i;
            while (i < len) {
                uint256 p = pxs[i];
                uint256 col = cols[i];
                uint256 n = 1;
                while (i + n < len && pxs[i + n] == p + n && (p + n) / 24 == p / 24 && cols[i + n] == col) n++;
                runs[nr++] = p | (n << 16) | (col << 32);
                i += n;
            }
            for (i = 0; i < nr; i++) {
                uint256 r = runs[i];
                if (r >> 255 != 0) continue;
                uint256 col = (r >> 32) & RGB;
                k.w('<path fill="', 12);
                k.hex6(col);
                k.w('" d="', 5);
                for (uint256 j = i; j < nr; j++) {
                    uint256 q = runs[j];
                    if (q >> 255 != 0 || ((q >> 32) & RGB) != col) continue;
                    _run(k, (q & 0xFFFF) % 24, (q & 0xFFFF) / 24, (q >> 16) & 0xFFFF);
                    runs[j] = q | (1 << 255);
                }
                k.w('"/>', 3);
            }
        }
    }

    function _shape(Ink memory k, uint256[] memory pxs, uint256 parity) internal pure {
        unchecked {
            uint256 len = pxs.length;
            uint256 i;
            while (i < len) {
                uint256 p = pxs[i];
                if (parity < 2 && p % 2 != parity) {
                    i++;
                    continue;
                }
                uint256 n = 1;
                if (parity == 2) {
                    while (i + n < len && pxs[i + n] == p + n && (p + n) / 24 == p / 24) n++;
                }
                _run(k, p % 24, p / 24, n);
                i += n;
            }
        }
    }

    function _fade(Ink memory k, uint256[] memory pxs, uint256[] memory cols, uint256[] memory v, uint256 steps)
        internal
        pure
    {
        if (pxs.length == 0 || !_any(v)) return;
        k.w('<g opacity="0">', 15);
        _anim(k, "opacity", 7, v, steps, 0, false);
        _paths(k, pxs, cols);
        k.w("</g>", 4);
    }

    function _lights(Ink memory k, Card memory c, Light memory o) internal pure {
        unchecked {
            uint256 nc = o.own ? _steps(c, o.cycle) : o.cycle;
            uint256[] memory xs = new uint256[](o.loop);
            uint256[] memory ys = new uint256[](o.loop);
            uint256[] memory env = new uint256[](nc);
            for (uint256 s = 0; s < o.count; s++) {
                // forge-lint: disable-next-line(unsafe-typecast)
                int256 c0 = (o.own ? c.tb0 * 10 / int256(o.cycle) : int256(0)) + WAD * int256(s) / int256(o.count);
                // forge-lint: disable-next-line(unsafe-typecast)
                uint256 first = uint256(c0 / WAD);
                int256 fr = c0 % WAD;
                // forge-lint: disable-next-line(unsafe-typecast)
                uint256 sh = uint256(fr * int256(nc) / WAD);
                for (uint256 j = 0; j < o.loop; j++) {
                    uint256 p = o.mask[(c.x[((first + j) * o.mulC + s * o.mulS + o.add) % o.mod] * o.mask.length) >> 32];
                    xs[j] = p % 24;
                    ys[j] = p / 24;
                }
                for (uint256 m = 0; m < nc; m++) {
                    int256 f = _at(fr, m, nc);
                    int256 e = f < o.rise ? f * WAD / o.rise : WAD - (f - o.rise) * WAD / o.fall;
                    env[m] = Fixed.op(e * o.amount / 1000);
                }
                k.w('<rect width="1" height="1" fill=', 32);
                k.w('"', 1);
                k.hex6(o.colour);
                k.w('" opacity="0">', 14);
                _anim(k, "x", 1, xs, nc * o.loop, sh, true);
                _anim(k, "y", 1, ys, nc * o.loop, sh, true);
                _anim(k, "opacity", 7, env, nc, 0, false);
                k.w("</rect>", 7);
            }
        }
    }
}

src/libraries/SSTORE2.sol

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

/// @title SSTORE2 — data as contract bytecode
/// @notice After 0xsequence/sstore2 and solady. Runtime is 0x00 (STOP) ++ data.
library SSTORE2 {
    // ──────────────────────── errors ────────────────────────
    error DeploymentFailed();
    error DataTooLarge();
    error ReadOutOfBounds();

    /// @dev EIP-170 (24,576) less the STOP byte
    uint256 internal constant MAX_DATA = 24_575;

    // ──────────── write (deploy data as contract) ──────────
    function write(bytes memory data) internal returns (address pointer) {
        if (data.length > MAX_DATA) revert DataTooLarge();
        // Prepend 0x00 (STOP opcode) so the contract can't be called.
        // Constructor (10 bytes at offsets 0x00–0x09):
        //   PUSH2 codeSize   (61 XXXX)  — codeSize = data.length + 1
        //   DUP1             (80)
        //   PUSH1 0x0a       (60 0a)    — offset where runtime starts
        //   RETURNDATASIZE   (3d)       — pushes 0 (cheaper than PUSH1 0)
        //   CODECOPY          (39)       — copy runtime to memory
        //   RETURNDATASIZE   (3d)       — pushes 0
        //   RETURN            (f3)       — return runtime from memory
        // Runtime (starts at 0x0a):
        //   0x00 (STOP) followed by the raw data
        bytes memory code = abi.encodePacked(
            hex"61",
            uint16(data.length + 1),
            hex"80600a3d393df3",
            hex"00",
            data
        );

        assembly {
            pointer := create(0, add(code, 0x20), mload(code))
        }
        if (pointer == address(0)) revert DeploymentFailed();
    }

    // ──────────── read (extcodecopy) ──────────
    function read(address pointer) internal view returns (bytes memory data) {
        return read(pointer, 0, codeSize(pointer));
    }

    function read(address pointer, uint256 start, uint256 end) internal view returns (bytes memory data) {
        // +1 to skip the STOP byte prefix
        start += 1;
        end += 1;

        uint256 size = end - start;
        if (end > pointer.code.length) revert ReadOutOfBounds();

        data = new bytes(size);
        assembly {
            extcodecopy(pointer, add(data, 0x20), start, size)
        }
    }

    function codeSize(address pointer) internal view returns (uint256 size) {
        size = pointer.code.length - 1;
    }
}

src/libraries/TraitDefs.sol

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

/// @title TraitDefs — trait names for the frozen table
/// @notice Index order matches the chain manifest. Retired values keep their
///         slots so indices never shift.
library TraitDefs {
    uint8 internal constant LAYER_BACKGROUND = 0;
    uint8 internal constant LAYER_BODY = 1;
    uint8 internal constant LAYER_HOODIE = 2;
    uint8 internal constant LAYER_NECK = 3;
    uint8 internal constant LAYER_EYES = 4;
    uint8 internal constant LAYER_MOUTH = 5;
    uint8 internal constant LAYER_HEAD = 6;

    function layerLabel(uint8 layer) internal pure returns (string memory) {
        if (layer == LAYER_BACKGROUND) return "Palette";
        if (layer == LAYER_BODY) return "Bones";
        if (layer == LAYER_HOODIE) return "Cloak";
        if (layer == LAYER_NECK) return "Relic";
        if (layer == LAYER_EYES) return "Sight";
        if (layer == LAYER_MOUTH) return "Artifact";
        if (layer == LAYER_HEAD) return "Crown";
        revert("bad layer");
    }

    function itemName(uint8 layer, uint8 item) internal pure returns (string memory) {
        if (layer == LAYER_BACKGROUND) return _bg(item);
        if (layer == LAYER_BODY) return _body(item);
        if (layer == LAYER_HOODIE) return _hoodie(item);
        if (layer == LAYER_NECK) return _neck(item);
        if (layer == LAYER_EYES) return _eyes(item);
        if (layer == LAYER_MOUTH) return _mouth(item);
        if (layer == LAYER_HEAD) return _head(item);
        revert("bad layer");
    }

    function _bg(uint8 i) private pure returns (string memory) {
        string[34] memory n = [
            "Bubblegum", "Yellow", "Violet", "Wine", "Sky", "Void", "MuseGreen", "Ancient",
            "Punkblue", "Blush", "Offwhite",
            "Hot Rose", "Emerald", "Bright Lilac", "Neon Mint", "Paper White",
            "Radioactive Void Charcoal", "Radioactive Deep Raspberry", "Radioactive Seafoam", "Radioactive Lavender", "Radioactive Paper White",
            "Ice Prism Pink", "Violet Pink", "Violet Cyan", "Navy Pink", "Void Pink",
            "Void Blue", "Void Cyan", "Navy Blue Vignette", "Void Teal Vignette",
            "Siren", "Seafoam", "Lavender", "Storm"
        ];
        return n[i];
    }

    function _body(uint8 i) private pure returns (string memory) {
        string[10] memory n = [
            "Alien", "Radioactive", "Gold", "Petrified", "Floral",
            "Coral", "Silver", "Prehistoric", "Bone", "Floral"
        ];
        return n[i];
    }

    function _hoodie(uint8 i) private pure returns (string memory) {
        string[6] memory n = ["", "Servant", "Death", "Royalty", "Ivory", "Clergy"];
        return n[i];
    }

    function _neck(uint8 i) private pure returns (string memory) {
        string[2] memory n = ["", "Gold"];
        return n[i];
    }

    function _eyes(uint8 i) private pure returns (string memory) {
        string[15] memory n = [
            "", "Shades", "Glasses", "Digital", "Eye Patch", "3D Glasses", "Designer",
            "Gucci", "Louis Vuitton", "Prada", "Versace", "Dior", "Balenciaga", "Chanel", "Eye Patch"
        ];
        return n[i];
    }

    function _mouth(uint8 i) private pure returns (string memory) {
        string[3] memory n = ["", "Woodpipe", "THC Vape"];
        return n[i];
    }

    function _head(uint8 i) private pure returns (string memory) {
        string[8] memory n = ["", "Oarsman's Band", "Bandana", "Dawn Pink Beanie", "Aegean Blue Beanie", "Purphat", "Golden Fleece", "Corsair"];
        return n[i];
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "forge-std/=lib/forge-std/src/",
    "ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.