Skip to content
Verified contractPartialMIT

FrontonautsAnimator

Address0xf19345181f3b6f66bf1982c3e526bfa42b4d00d0
Balance

0 ETH

 

Transactions

88

 

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,245,176
Creation transaction
0xf0dd69d7f4…ba30d91eea

src/FrontonautsAnimator.sol

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

interface IBaseRenderer {
    function traitsOf(uint256 seed, uint256 tokenId) external view returns (uint256[8] memory);
    function validateExtra(string calldata extra) external pure;
}

/// @title FrontonautsAnimator
/// @notice The animation layer. For each token it returns extra CSS / SVG that the (locked) renderer draws on top of the art.
///         It cannot change traits or rarity: the token contract takes those only from the renderer. No attribute is added for animation.
///
///         RULES ARE ADD-ONLY. A rule can be created and later switched off, never edited, so a trait rule never replaces
///         an animation a token already has. The one exception is the single-token rule (category 7): it REPLACES all
///         trait animations of that token (so you can customise one token), and a newer single-token rule replaces an older one. Rule = "tokens whose <category> trait is <trait> get an animation with chance
///         <chance>/10000; which one of the <variants> they get is random too". Categories: 0 palette, 1 bones, 2 crown,
///         3 sight, 4 artifact, 5 relic, 6 cloak, 8 mask, 7 = one single token (trait = the token id, always applies, replaces that token's other animations).
///         Trait = index in that category (see the renderer's traitName), 0xFFFF = any trait. A "None" trait has index = item count.
///         Rules are checked in order. A token takes at most ONE animation per category (and one per-token-id animation):
///         if an earlier rule already gave it one for that category, later rules skip it. So you can add many different
///         animations for the same trait over time (each new rule only reaches tokens that do not have one yet), and different
///         categories (Shades + Woodpipe + Cloak ...) stack on the same token. Rolls are fixed per token and rule.
///         A global piece applies to every token. Write CSS against the classes the renderer puts on layers,
///         e.g. `.artifact.t-woodpipe{animation:...}`.
contract FrontonautsAnimator {
    error NotOwner();
    error BadInput();

    struct Rule {
        bool active;
        uint8 cat;
        uint16 trait;
        uint16 chance; // out of 10000
        string[] variants;
    }

    IBaseRenderer public immutable base;
    address public owner;
    address public pendingOwner;
    string public globalExtra;
    Rule[] private _rules;

    uint256 public constant MAX_RULES = 200;
    uint256 public constant MAX_VARIANTS = 8;
    uint256 public constant MAX_PIECE = 2400; // bytes per CSS/SVG piece
    uint256 private constant MAX_TOTAL = 6000; // what the renderer accepts per token

    event RuleChanged(uint256 indexed id);
    event GlobalChanged();

    modifier onlyOwner() {
        if (msg.sender != owner) revert NotOwner();
        _;
    }

    constructor(address baseRenderer) {
        base = IBaseRenderer(baseRenderer);
        owner = msg.sender;
    }

    /// @notice Two-step ownership: the new owner must call acceptOwnership(), so a typo can never lock you out.
    function transferOwnership(address newOwner) external onlyOwner {
        pendingOwner = newOwner;
    }

    function acceptOwnership() external {
        if (msg.sender != pendingOwner || msg.sender == address(0)) revert NotOwner();
        owner = msg.sender;
        pendingOwner = address(0);
    }

    /// @notice Give up ownership for good: the rules can then never be added or switched off again.
    function renounceOwnership() external onlyOwner {
        owner = address(0);
        pendingOwner = address(0);
    }

    /// @notice Animation applied to every token (empty = none).
    function setGlobal(string calldata piece) external onlyOwner {
        _check(piece);
        globalExtra = piece;
        emit GlobalChanged();
    }

    /// @notice Add a rule (id must be ruleCount). `variants` = the different animations a matching token can get.
    ///         cat 7 = one token: trait is the token id (1..65535) and chance is ignored (always applies).
    function setRule(uint256 id, uint8 cat, uint16 trait, uint16 chance, string[] calldata variants) external onlyOwner {
        if (cat > 8 || chance > 10000 || variants.length == 0 || variants.length > MAX_VARIANTS) revert BadInput();
        if (id != _rules.length || id >= MAX_RULES) revert BadInput();
        if (cat == 7 && (trait == 0 || trait == 0xFFFF)) revert BadInput();
        _rules.push();
        Rule storage r = _rules[id];
        r.active = true;
        r.cat = cat;
        r.trait = trait;
        r.chance = cat == 7 ? 10000 : chance;
        for (uint256 i; i < variants.length; ++i) {
            _check(variants[i]);
            r.variants.push(variants[i]);
        }
        emit RuleChanged(id);
    }

    /// @notice Switch a rule off for good. Its tokens lose that animation (later rules may then reach them).
    function removeRule(uint256 id) external onlyOwner {
        if (id >= _rules.length) revert BadInput();
        _rules[id].active = false;
        emit RuleChanged(id);
    }

    function ruleCount() external view returns (uint256) {
        return _rules.length;
    }

    function getRule(uint256 id) external view returns (bool active, uint8 cat, uint16 trait, uint16 chance, string[] memory variants) {
        Rule storage r = _rules[id];
        return (r.active, r.cat, r.trait, r.chance, r.variants);
    }

    /// @dev Variant index a token gets from rule `id`, or 255 if the rule does not apply (inactive, other trait, already
    ///      claimed in this category by an earlier rule, or the chance roll fails).
    function _pick(uint256 id, uint256 tokenId, uint256 seed, uint256[8] memory t, uint256 claimed) private view returns (uint256) {
        Rule storage r = _rules[id];
        if (!r.active) return 255;
        uint256 c = r.cat;
        if ((claimed >> c) & 1 == 1) return 255;
        if (c == 7) {
            if (r.trait != tokenId) return 255;
        } else if (r.trait != 0xFFFF && t[c == 8 ? 7 : c] != r.trait) {
            return 255;
        }
        uint256 h = uint256(keccak256(abi.encode(seed, tokenId, id, "anim")));
        if (c != 7 && h % 10000 >= r.chance) return 255;
        return (h >> 128) % r.variants.length;
    }

    /// @dev Newest active single-token rule (cat 7) for this token, as id + 1; 0 if none.
    function _override(uint256 tokenId) private view returns (uint256) {
        for (uint256 i = _rules.length; i > 0; --i) {
            Rule storage r = _rules[i - 1];
            if (r.active && r.cat == 7 && r.trait == tokenId) return i;
        }
        return 0;
    }

    /// @notice Extra animation code for one token. Pure function of (seed, tokenId, current rules).
    function extra(uint256 tokenId, uint256 seed) external view returns (string memory) {
        bytes memory out = bytes(globalExtra);
        uint256 n = _rules.length;
        if (n == 0) return string(out);
        uint256 ov = _override(tokenId);
        if (ov != 0) { // a single-token rule replaces every trait animation of that token
            uint256 h0 = uint256(keccak256(abi.encode(seed, tokenId, ov - 1, "anim")));
            bytes memory piece0 = bytes(_rules[ov - 1].variants[(h0 >> 128) % _rules[ov - 1].variants.length]);
            if (out.length + piece0.length <= MAX_TOTAL) out = bytes.concat(out, piece0);
            return string(out);
        }
        uint256[8] memory t = base.traitsOf(seed, tokenId);
        uint256 claimed;
        for (uint256 id; id < n; ++id) {
            uint256 v = _pick(id, tokenId, seed, t, claimed);
            if (v == 255) continue;
            bytes memory piece = bytes(_rules[id].variants[v]);
            if (out.length + piece.length > MAX_TOTAL) continue; // never exceed what the renderer accepts
            out = bytes.concat(out, piece);
            claimed |= uint256(1) << _rules[id].cat;
        }
        return string(out);
    }

    /// @notice Which variant (if any) of each rule a token gets; for the explore tool. 255 = rule does not apply.
    function rollsOf(uint256 tokenId, uint256 seed) external view returns (uint8[] memory res) {
        uint256 n = _rules.length;
        res = new uint8[](n);
        uint256[8] memory t = base.traitsOf(seed, tokenId);
        uint256 claimed;
        uint256 ov = _override(tokenId);
        for (uint256 id; id < n; ++id) {
            if (ov != 0 && id != ov - 1) { res[id] = 255; continue; }
            uint256 v = _pick(id, tokenId, seed, t, claimed);
            res[id] = uint8(v);
            if (v != 255) claimed |= uint256(1) << _rules[id].cat;
        }
    }

    function _check(string calldata piece) private view {
        if (bytes(piece).length > MAX_PIECE) revert BadInput();
        base.validateExtra(piece); // same safety filter the renderer applies when drawing
    }
}
Compiler settingsShow
{
  "metadata": {
    "bytecodeHash": "none"
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "london",
  "remappings": [
    "forge-std/=lib/forge-std/src/",
    "seadrop/=lib/seadrop/src/",
    "ERC721A/=lib/ERC721A/contracts/",
    "solmate/=lib/solmate/src/",
    "utility-contracts/=lib/utility-contracts/src/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/contracts/",
    "operator-filter-registry/=lib/operator-filter-registry/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.