Skip to content
Verified contractPartialMIT

BiomorphicTraits

Address0xdbe52d55771d3336801a15cbf1867224dee9e6c8
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.17

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.17+commit.8df45f5f
Language
solidity
Optimization
Yes, 200 runs
EVM version
london
License
MIT
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,235,433
Creation transaction
0x2a41f11a2e…10e5c8ffc3

src/BiomorphicTraits.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

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

/**
 * @title  BiomorphicTraits
 * @notice Pure token hash → traits function, spec `biomorphic-tokenhash-v1`
 *         (project doc `claude/tokenhash-spec-v1.md`). The JS generator uses
 *         the same rules, so on-chain attributes always match the artwork.
 *
 *         slot(i)   = (hash[2i] << 8) | hash[2i+1], i = 0..15
 *         pick(i,T) = first k with (slot(i) * sum(T)) >> 16 < w[0] + .. + w[k]
 *
 *         Every slot is read whether or not it is used, so one decision never
 *         shifts another. slot * sum < 2^32 for every table.
 *
 * @dev    Weight tables are packed big-endian uint16; names are joined by
 *         "|". The order of entries is part of the spec: do not reorder.
 */
contract BiomorphicTraits is IBiomorphicTraits {
    string public constant SPEC = "biomorphic-tokenhash-v1";

    /// @dev Unreachable with the constant tables below.
    error InvalidTable();

    /*//////////////////////////////////////////////////////////////
                                 TABLES
    //////////////////////////////////////////////////////////////*/

    // Slot 0 — Fusion level: 0:850, 2:85, 3:40, 4:20, 5:5
    bytes internal constant W_FUSION = hex"0352_0055_0028_0014_0005";
    // Fusion value per entry of W_FUSION.
    bytes internal constant V_FUSION = hex"00_02_03_04_05";

    // Slot 1 (Fusion = 0) — field:37, plain:10, panel:12, moon:12, wave:12, lines:12, topography:5
    bytes internal constant W_COMP = hex"0025_000a_000c_000c_000c_000c_0005";
    // Composition ids: 0 field, 1 plain, 2 panel, 3 moon, 4 wave, 5 lines, 6 topography
    bytes internal constant V_COMP = hex"00_01_02_03_04_05_06";

    // Slot 1 (Fusion > 0) — field:60, plain:25, topography:15
    bytes internal constant W_FBASE = hex"003c_0019_000f";
    bytes internal constant V_FBASE = hex"00_01_06";

    // Slot 3 — Palette, 28 entries, sum 887.
    bytes internal constant W_PALETTE =
        hex"000a_000a_000a"
        hex"0014_0014_0014_0014_0014_0014"
        hex"0023_0023_0023_0023_0023_0023_0023_0023_0023_0023_0023"
        hex"002c_002c_002c_002c_002c_002c_002c_002c";
    bytes internal constant N_PALETTE =
        "Gold|Lava|Glitch|"
        "Alien|Water|Homage|De Stijl|Emerald|LeWitt|"
        "Water Lilies|Cut-outs|Temple|Scarlet|Pastel|Haring|Topo|Soil|Iron|Wind|Metal|"
        "Cobalt|Pop|Riso|Zombie|Starry|Great Wave|Forest|Sunflowers";

    // Slot 4 — Form: full:45, alternate:20, arcs:20, rails:15
    bytes internal constant W_FORM = hex"002d_0014_0014_000f";
    bytes internal constant N_FORM = "Full|Alternate|Arcs|Rails";

    // Slot 5 — Form colour: ring:25, gradient:25, duotone:12, mono:12, accent:12, lewitt:10, blend:4
    bytes internal constant W_FCOL = hex"0019_0019_000c_000c_000c_000a_0004";
    bytes internal constant N_FCOL = "Ring|Tint|Duotone|Mono|Accent|LeWitt|Gradient";

    // Slot 6 — Form scale: regular:62, bold:28, fine:10
    bytes internal constant W_FSCALE = hex"003e_001c_000a";
    bytes internal constant N_SCALE_F = "Regular|Bold|Fine";

    // Slot 7 — Ribbons requested: 1:15, 2:30, 3:25, 4:15, 5:10, 6:5
    bytes internal constant W_RIB = hex"000f_001e_0019_000f_000a_0005";

    // Slot 9 — Ribbon colour: gradient:41, duotone:15, mono:15, accent:15, lewitt:10, blend:4
    bytes internal constant W_RCOL = hex"0029_000f_000f_000f_000a_0004";
    bytes internal constant N_RCOL = "Tint|Duotone|Mono|Accent|LeWitt|Gradient";

    // Slot 10 — Ribbon scale: fine:1, regular:1, bold:1
    bytes internal constant W_RSCALE = hex"0001_0001_0001";
    bytes internal constant N_SCALE_R = "Fine|Regular|Bold";

    // Slot 11 — Rhythm: even:40, beat:20, taper:20, pulse:20
    bytes internal constant W_RHYTHM = hex"0028_0014_0014_0014";
    bytes internal constant N_RHYTHM = "Even|Beat|Taper|Pulse";

    // Slot 12 — Speed: default:50, medium:30, fast:20
    bytes internal constant W_SPEED = hex"0032_001e_0014";
    bytes internal constant N_SPEED = "Default|Medium|Fast";

    // Composition display names by id (see V_COMP).
    bytes internal constant N_COMP = "Field|Faded ground|Panel|Moon|Wave|Lines|Topography";

    // Fusion parts: combinations of (panel, lines, wave, moon) taken
    // (Fusion - 1) at a time, lexicographic. Bit 3 = panel, 2 = lines,
    // 1 = wave, 0 = moon.
    bytes internal constant PARTS_F2 = hex"08_04_02_01";
    bytes internal constant PARTS_F3 = hex"0c_0a_09_06_05_03";
    bytes internal constant PARTS_F4 = hex"0e_0d_0b_07";
    bytes internal constant PARTS_F5 = hex"0f";

    /*//////////////////////////////////////////////////////////////
                                 OUTPUT
    //////////////////////////////////////////////////////////////*/

    struct Core {
        uint256 fusion; // 0, 2..5
        uint256 composition; // composition id when fusion == 0
        uint256 base; // composition id of the base
        uint256 partsMask; // bit 3 panel, 2 lines, 1 wave, 0 moon
        uint256 palette;
        uint256 form;
        uint256 formColour;
        uint256 formScale;
        uint256 ribbons; // 0..6 after the drop rule
        uint256 ribbonColour;
        uint256 ribbonScale;
        uint256 rhythm;
        uint256 speed;
    }

    /// @notice The 16 uint16 slots of a hash.
    function slots(bytes32 h) public pure returns (uint256[16] memory s) {
        uint256 v = uint256(h);
        for (uint256 i; i < 16; ++i) {
            s[i] = (v >> (240 - 16 * i)) & 0xffff;
        }
    }

    /// @notice Decisions for a hash (indices into the tables above).
    function core(bytes32 h) public pure returns (Core memory c) {
        uint256[16] memory s = slots(h);

        c.fusion = uint8(V_FUSION[_pick(s[0], W_FUSION)]);
        if (c.fusion == 0) {
            c.composition = uint8(V_COMP[_pick(s[1], W_COMP)]);
            if (c.composition >= 2 && c.composition <= 5) {
                c.base = 0; // field
                c.partsMask = _partBit(c.composition);
            } else {
                c.base = c.composition;
            }
        } else {
            c.base = uint8(V_FBASE[_pick(s[1], W_FBASE)]);
            bytes memory list = c.fusion == 2
                ? PARTS_F2
                : c.fusion == 3
                    ? PARTS_F3
                    : c.fusion == 4
                        ? PARTS_F4
                        : PARTS_F5;
            c.partsMask = uint8(list[(s[2] * list.length) >> 16]);
        }

        c.palette = _pick(s[3], W_PALETTE);
        c.form = _pick(s[4], W_FORM);
        c.formColour = _pick(s[5], W_FCOL);
        c.formScale = _pick(s[6], W_FSCALE);

        c.ribbons = _pick(s[7], W_RIB) + 1;
        uint256 pct;
        if (c.partsMask == 0 && c.base == 0) pct = 15; // lone Field
        else if (c.partsMask == 0 && c.base == 1) pct = 0; // lone Faded ground
        else pct = 35;
        if ((s[8] * 100) >> 16 < pct) c.ribbons = 0;

        c.ribbonColour = _pick(s[9], W_RCOL);
        c.ribbonScale = _pick(s[10], W_RSCALE);
        c.rhythm = _pick(s[11], W_RHYTHM);
        c.speed = _pick(s[12], W_SPEED);
    }

    /// @inheritdoc IBiomorphicTraits
    function attributesJSON(bytes32 h) external pure returns (string memory) {
        Core memory c = core(h);

        string memory comps;
        if (c.fusion == 0) {
            comps = _attr("Composition", _name(N_COMP, c.composition));
        } else {
            comps = _attr("Composition", _name(N_COMP, c.base));
            // Stacking order: panel, lines, wave, moon.
            uint8[4] memory ids = [2, 5, 4, 3];
            for (uint256 b; b < 4; ++b) {
                if (c.partsMask & (8 >> b) != 0) {
                    comps = string.concat(comps, ",", _attr("Composition", _name(N_COMP, ids[b])));
                }
            }
        }

        bool hasForm = c.base <= 1; // field or plain
        bool hasRibbons = c.ribbons != 0;

        string memory a = string.concat(
            comps,
            ",",
            _attr("Fusion", c.fusion == 0 ? "Off" : _digit(c.fusion)),
            ",",
            _attr("Palette", _name(N_PALETTE, c.palette)),
            ",",
            _attr("Form", hasForm ? _name(N_FORM, c.form) : "None"),
            ",",
            _attr("Form Colour", _name(N_FCOL, c.formColour)),
            ",",
            _attr("Form Scale", _name(N_SCALE_F, c.formScale))
        );
        a = string.concat(
            a,
            ",",
            _attr("Ribbon Count", hasRibbons ? _digit(c.ribbons) : "None"),
            ",",
            _attr("Ribbon Colour", hasRibbons ? _name(N_RCOL, c.ribbonColour) : "None"),
            ",",
            _attr("Ribbon Scale", hasRibbons ? _name(N_SCALE_R, c.ribbonScale) : "None"),
            ",",
            _attr("Rhythm", _name(N_RHYTHM, c.rhythm)),
            ",",
            _attr("Speed", _name(N_SPEED, c.speed))
        );
        return string.concat("[", a, "]");
    }

    /*//////////////////////////////////////////////////////////////
                                HELPERS
    //////////////////////////////////////////////////////////////*/

    /// @dev Cumulative weighted pick. `w` is packed big-endian uint16.
    function _pick(uint256 slot, bytes memory w) internal pure returns (uint256) {
        uint256 n = w.length / 2;
        uint256 sum = 0;
        for (uint256 k; k < n; ++k) sum += _w(w, k);
        uint256 x = (slot * sum) >> 16;
        uint256 acc = 0;
        for (uint256 k; k < n; ++k) {
            acc += _w(w, k);
            if (x < acc) return k;
        }
        revert InvalidTable(); // unreachable: x < sum
    }

    function _w(bytes memory w, uint256 k) private pure returns (uint256) {
        return (uint256(uint8(w[2 * k])) << 8) | uint8(w[2 * k + 1]);
    }

    /// @dev The k-th "|"-separated name.
    function _name(bytes memory names, uint256 k) internal pure returns (string memory) {
        uint256 start = 0;
        uint256 idx = 0;
        uint256 len = names.length;
        for (uint256 i; i <= len; ++i) {
            if (i == len || names[i] == "|") {
                if (idx == k) {
                    bytes memory out = new bytes(i - start);
                    for (uint256 j; j < out.length; ++j) out[j] = names[start + j];
                    return string(out);
                }
                ++idx;
                start = i + 1;
            }
        }
        revert InvalidTable(); // unreachable for valid tables
    }

    function _partBit(uint256 compId) private pure returns (uint256) {
        // panel(2) → 8, lines(5) → 4, wave(4) → 2, moon(3) → 1
        if (compId == 2) return 8;
        if (compId == 5) return 4;
        if (compId == 4) return 2;
        return 1;
    }

    function _digit(uint256 d) private pure returns (string memory) {
        return string(abi.encodePacked(bytes1(uint8(48 + d))));
    }

    function _attr(string memory t, string memory v) private pure returns (string memory) {
        return string.concat('{"trait_type":"', t, '","value":"', v, '"}');
    }
}

src/IBiomorphicTraits.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

/// @title  IBiomorphicTraits
/// @notice Pure hash → traits function. The Lab defines the spec (fixed draw
///         order, integer weights, byte layout); the JS generator uses the
///         same function, so on-chain and in-browser traits always agree.
interface IBiomorphicTraits {
    /// @return attributes A JSON array of OpenSea attributes, e.g.
    ///         `[{"trait_type":"Palette","value":"Lava"}, ...]`.
    function attributesJSON(bytes32 tokenHash)
        external
        view
        returns (string memory attributes);
}
Compiler settingsShow
{
  "metadata": {
    "bytecodeHash": "none"
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "london",
  "remappings": [
    "ERC721A-Upgradeable/=lib/seadrop/lib/ERC721A-Upgradeable/contracts/",
    "ERC721A/=lib/seadrop/lib/ERC721A/contracts/",
    "create2-helpers/=lib/seadrop/lib/create2-helpers/",
    "create2-scripts/=lib/seadrop/lib/create2-helpers/script/",
    "ds-test/=lib/seadrop/lib/ds-test/src/",
    "forge-std/=lib/forge-std/src/",
    "murky/=lib/seadrop/lib/murky/src/",
    "openzeppelin-contracts-upgradeable/=lib/seadrop/lib/openzeppelin-contracts-upgradeable/",
    "openzeppelin-contracts/=lib/seadrop/lib/openzeppelin-contracts/contracts/",
    "operator-filter-registry/=lib/seadrop/lib/operator-filter-registry/src/",
    "seadrop/=lib/seadrop/src/",
    "solady/=lib/solady/src/",
    "solmate/=lib/seadrop/lib/solmate/src/",
    "utility-contracts/=lib/seadrop/lib/utility-contracts/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.