Skip to content
Verified contractExactMIT

TokenScreener

Address0xa5a31dece2a755a076771c8111906fc7d3fc1d00
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.26

 

Same code

6 more contracts

 

Details

Compiler
solc 0.8.26+commit.8a97fa7a
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
25,978,077
Creation transaction
0xd96ae3b942…2b4e443e3c

src/TokenScreener.sol

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

/// @notice Reads a deployed ERC20's bytecode and reports what powers it kept.
///
/// @dev This exists because a launch with a creator-supplied token cannot rely on the guarantee
///      LaunchToken gives for free: no owner, no mint, no pause, so the only way to hurt a holder
///      is to sell. A custom contract can do all of those, and a curve holding that token would be
///      the thing left holding the bag -- its inventory diluted by a mint, its trades reverted by a
///      blacklist, its graduation bricked by a pause.
///
///      What this contract does is read the runtime bytecode and answer two questions: does it
///      contain opcodes an immutable fixed-supply token has no use for, and does its dispatcher
///      route to functions that can change the rules after people have bought. It is deployed
///      separately from the factory so a frontend, an integrator, or a competing interface can ask
///      the same question and get the same answer the launch was gated on.
///
///      ## What it can and cannot tell you
///
///      The opcode half is sound. Bytecode either contains DELEGATECALL or it does not, and a walk
///      that respects PUSH immediates cannot be talked out of seeing it.
///
///      The selector half is a name-matching exercise and should be read as one. It catches the
///      shapes that ship in practice -- `setTaxes`, `blacklist`, `enableTrading` -- because those
///      names are copied between contracts along with the code. It does not catch a mint spelled
///      `harvest()`, and pretending otherwise would be the dangerous kind of false comfort.
///
///      That gap is why the flags are not the last line of defence. BondingCurve pins the token's
///      codehash and total supply at launch and measures the balance delta on every transfer, so a
///      power that is *used* halts the curve whatever it was named. Static screening is here to
///      tell a buyer what a contract could do before they buy; the curve is what stops it mattering
///      after they have.
contract TokenScreener {
    // ------------------------------------------------------------------ fatal flags

    /// @dev Nothing below is a matter of taste. Each one either breaks the curve's arithmetic or
    ///      puts the code that runs outside the code that was screened.

    /// @notice No code at the address, or no readable ERC20 surface on it.
    uint256 public constant NOT_AN_ERC20 = 1 << 0;

    /// @notice Runtime bytecode contains DELEGATECALL or CALLCODE.
    /// @dev Every upgradeable proxy delegatecalls -- transparent, UUPS, beacon and the EIP-1167
    ///      minimal clone alike -- so catching the opcode catches the whole family without needing
    ///      to read storage slots, which the EVM gives no way to do for a foreign contract anyway.
    ///      The screened bytecode would otherwise be a description of what runs today only.
    ///
    ///      A token linking an external Solidity library also lands here. Legitimate, and still
    ///      disqualifying: the linked code is not in what was screened.
    uint256 public constant DELEGATES = 1 << 1;

    /// @notice Runtime bytecode contains SELFDESTRUCT.
    /// @dev Post-Cancun this no longer removes code outside the deploying transaction, so it is
    ///      close to inert. It is fatal here anyway: a fixed-supply token has no reason to carry it,
    ///      and its presence says the contract was written with an escape hatch in mind.
    uint256 public constant SELF_DESTRUCTS = 1 << 2;

    /// @notice Dispatcher routes to a recognisable mint.
    /// @dev The curve sells a fixed slice of a fixed supply. Anything that can add supply after
    ///      launch dilutes the slice held back for the pool, which is the closest thing to a rug
    ///      this design has.
    uint256 public constant MINTABLE = 1 << 3;

    // ------------------------------------------------------------------ warning flags

    /// @dev These launch. They are real powers and they are shown to buyers, but a token can be
    ///      worth owning while holding them, and deciding otherwise is not the launchpad's call.

    /// @notice `owner()`, `getOwner()` or `admin()` returns an address that can still act.
    uint256 public constant OWNER_LIVE = 1 << 4;

    /// @notice Dispatcher routes to per-address allow/deny controls over transfers.
    uint256 public constant BLACKLIST = 1 << 5;

    /// @notice Dispatcher routes to a pause switch.
    uint256 public constant PAUSABLE = 1 << 6;

    /// @notice Dispatcher routes to something that rewrites a fee or tax rate after deployment.
    uint256 public constant MUTABLE_TAX = 1 << 7;

    /// @notice Dispatcher routes to per-transaction or per-wallet size limits.
    uint256 public constant TRANSFER_LIMITS = 1 << 8;

    /// @notice Dispatcher routes to a trading on/off switch.
    /// @dev The classic honeypot shape when paired with an owner: open for buys, closed for sells.
    uint256 public constant TRADING_TOGGLE = 1 << 9;

    /// @notice Dispatcher routes to per-address fee exemptions.
    uint256 public constant FEE_EXCLUSIONS = 1 << 10;

    /// @notice Dispatcher routes to an admin sweep of tokens or native held by the contract.
    uint256 public constant ADMIN_WITHDRAW = 1 << 11;

    /// @notice Dispatcher routes to `burnFrom`, which can destroy a balance the caller does not own.
    uint256 public constant BURNS_OTHERS = 1 << 12;

    /// @notice Dispatcher routes to a rebase, which moves balances without a Transfer event.
    uint256 public constant REBASES = 1 << 13;

    /// @notice `decimals()` is above 18.
    uint256 public constant ODD_DECIMALS = 1 << 14;

    /// @notice The solc metadata trailer could not be located, so the tail was scanned as code.
    /// @dev Informational, and it should never fire on a launch this factory deployed, since the
    ///      launchpad compiles the source itself and solc always appends the trailer. When it does
    ///      fire, opcode flags on that contract may be reading constant data as instructions.
    uint256 public constant NO_METADATA = 1 << 15;

    /// @notice The contract reads the clock: `block.number`, `block.timestamp` or the block hash.
    ///
    /// @dev Disclosed, never fatal, and the honest answer to the one thing a pre-launch simulation
    ///      structurally cannot catch. Every behavioural check in `LaunchProbe` happens inside a
    ///      single `eth_call`, which is one block and a handful of transfers. A contract that reads
    ///      the clock can therefore behave impeccably for the whole measurement and differently from
    ///      the next block onward -- a tax that arms at `block.number > deployedAt` passes the tax
    ///      check, the sellability check and the pool check, and then takes ninety percent of every
    ///      trade forever.
    ///
    ///      No amount of simulating fixes that; the general case is undecidable. What can be done is
    ///      to say so. Plenty of honest contracts read the clock -- a vesting schedule, a launch
    ///      window, a cooldown -- so this cannot block a launch. It tells a buyer that this token's
    ///      behaviour is allowed to change with time, and that what the screen measured was one
    ///      moment.
    uint256 public constant TIME_DEPENDENT = 1 << 16;

    /// @notice The flags that stop a launch. Everything else is disclosed and allowed.
    uint256 public constant FATAL_MASK = NOT_AN_ERC20 | DELEGATES | SELF_DESTRUCTS | MINTABLE;

    // ------------------------------------------------------------------ opcodes

    uint8 private constant OP_PUSH0 = 0x5f;
    uint8 private constant OP_PUSH1 = 0x60;
    uint8 private constant OP_PUSH4 = 0x63;
    uint8 private constant OP_PUSH32 = 0x7f;
    uint8 private constant OP_JUMPDEST = 0x5b;
    /// @dev The block-context family: BLOCKHASH, COINBASE, TIMESTAMP, NUMBER, PREVRANDAO. Read as a
    ///      range rather than three equalities -- it is one comparison cheaper per byte scanned, and
    ///      it is the tighter question anyway. Every opcode in it answers "when or where am I
    ///      running", which is exactly the capability that lets behaviour differ between the moment
    ///      this screen measures and every moment afterwards.
    uint8 private constant OP_BLOCK_CONTEXT_FIRST = 0x40;
    uint8 private constant OP_BLOCK_CONTEXT_LAST = 0x44;
    uint8 private constant OP_CALLCODE = 0xf2;
    uint8 private constant OP_DELEGATECALL = 0xf4;
    uint8 private constant OP_SELFDESTRUCT = 0xff;

    /// @dev Burn addresses an owner slot can hold while meaning "nobody". Checked alongside zero.
    address private constant DEAD = 0x000000000000000000000000000000000000dEaD;

    /// @notice One bit per leading byte that starts some selector in the table below.
    ///
    /// @dev The walk finds a PUSH4 every few instructions and almost none of them are interesting,
    ///      so asking the table about each one means running its whole comparison chain hundreds of
    ///      times per contract. This is the cheap way out: 33 selectors have 29 distinct leading
    ///      bytes, so testing one bit rejects about 89% of candidates for the cost of a shift, and
    ///      only a survivor pays for the table.
    ///
    ///      A mask that drifted out of step with the table would silently stop reporting a flag, so
    ///      test_BloomAdmitsEverySelectorInTheTable pins the two together.
    uint256 public constant SELECTOR_BLOOM =
        0x0020100010011244100000010000141002002240200000198000028000680802;

    // ------------------------------------------------------------------ screening

    /// @notice Screen a deployed token, treating no address as already-inert.
    function screen(address token) external view returns (uint256 flags) {
        return screen(token, address(0));
    }

    /// @notice Screen a deployed token.
    ///
    /// @param inertOwner An address that, if it holds the owner slot, does not count as a live
    ///        owner. The factory passes itself: a token it deployed runs `owner = msg.sender` in
    ///        its constructor and lands owned by a contract with no function that would ever call
    ///        it, which is renounced in every way that matters. Passing address(0) disables this.
    ///
    /// @return flags Bitmask of the constants above. Zero means nothing was found -- which is a
    ///         statement about this screen, not a certificate.
    function screen(address token, address inertOwner) public view returns (uint256 flags) {
        if (token.code.length == 0) return NOT_AN_ERC20;

        flags = _scanCode(token);
        flags |= _scanSurface(token, inertOwner);
    }

    /// @notice Whether a flag set stops a launch.
    function isFatal(uint256 flags) public pure returns (bool) {
        return flags & FATAL_MASK != 0;
    }

    /// @notice Screen and report in one call, for a frontend that wants both.
    function screenVerdict(address token, address inertOwner)
        external
        view
        returns (uint256 flags, bool fatal)
    {
        flags = screen(token, inertOwner);
        fatal = isFatal(flags);
    }

    // ------------------------------------------------------------------ bytecode walk

    /// @notice Walks the runtime bytecode instruction by instruction.
    ///
    /// @dev Walking rather than searching is the whole point. A naive scan for the DELEGATECALL
    ///      byte finds it inside any constant that happens to contain 0xf4 -- an address, a hash, a
    ///      packed struct -- and a scan for a selector finds it in unrelated data. Stepping over
    ///      each PUSH's immediate bytes means every byte examined is an opcode the EVM would
    ///      actually execute, and every selector found is one the dispatcher actually pushes.
    ///
    ///      Cost is linear in code size: a 6KB token walks in roughly 6,000 iterations. That is
    ///      paid once, inside the launch transaction, and never again.
    function _scanCode(address token) private view returns (uint256 flags) {
        bytes memory code = token.code;
        uint256 len = code.length;

        (uint256 end, bool foundMetadata) = _codeEnd(code, len);
        if (!foundMetadata) flags |= NO_METADATA;

        // One word is read per instruction and sliced, rather than indexing the array byte by byte:
        // the opcode and any selector following it both live in the same 32 bytes, and array
        // indexing would bounds-check every one of them. Reading past the final instruction is
        // harmless -- memory beyond the array is still memory -- and only bytes below `end` are
        // ever interpreted.
        uint256 dataPtr;
        assembly {
            dataPtr := add(code, 0x20)
        }

        uint256 i;
        while (i < end) {
            uint256 word;
            assembly {
                word := mload(add(dataPtr, i))
            }
            uint8 op = uint8(word >> 248);

            if (op >= OP_BLOCK_CONTEXT_FIRST && op <= OP_BLOCK_CONTEXT_LAST) {
                flags |= TIME_DEPENDENT;
            } else if (op == OP_DELEGATECALL || op == OP_CALLCODE) {
                flags |= DELEGATES;
            } else if (op == OP_SELFDESTRUCT) {
                flags |= SELF_DESTRUCTS;
            } else if (op == OP_PUSH4 && i + 5 <= len) {
                // Solidity's dispatcher compares calldata's first word against a PUSH4 of each
                // selector it serves, so a PUSH4 landing on an instruction boundary is as close to
                // a declaration of "this contract answers to X" as bytecode gets.
                uint32 selector = uint32(word >> 216);
                if ((SELECTOR_BLOOM >> uint8(selector >> 24)) & 1 == 1) {
                    flags |= selectorFlag(bytes4(selector));
                }
            }

            // PUSH0 carries no immediate; PUSH1..PUSH32 carry 1..32 bytes that are data, not code.
            if (op >= OP_PUSH1 && op <= OP_PUSH32) {
                unchecked {
                    i += uint256(op) - uint256(OP_PUSH1) + 2;
                }
            } else {
                unchecked {
                    ++i;
                }
            }
        }
    }

    /// @notice Where the executable region ends, excluding solc's metadata trailer.
    ///
    /// @dev solc appends a CBOR blob -- the source hash, the compiler version -- followed by two
    ///      bytes giving its length. Those bytes are never executed, and they are dense enough that
    ///      scanning them as code reliably invents a DELEGATECALL or a SELFDESTRUCT that is not
    ///      there. Stripping the trailer is the difference between a screen that works and one that
    ///      fails almost every real contract.
    ///
    ///      The length alone is not enough to trust: on a contract with no trailer the last two
    ///      bytes are ordinary code and would slice off a real region. So the byte the length points
    ///      at must also look like the CBOR map header solc opens with, 0xa0..0xaf.
    function _codeEnd(bytes memory code, uint256 len) private pure returns (uint256 end, bool found) {
        if (len < 4) return (len, false);

        uint256 declared = (uint256(uint8(code[len - 2])) << 8) | uint256(uint8(code[len - 1]));
        if (declared == 0 || declared + 2 > len) return (len, false);

        uint8 header = uint8(code[len - 2 - declared]);
        if (header & 0xf0 != 0xa0) return (len, false);

        uint256 boundary = len - 2 - declared;

        // Everything above is attacker-controlled, so on its own it decides nothing.
        //
        // Both the declared length and the byte it points at come from the contract being screened,
        // and a contract can end `<0xa?> <hidden code> <length>` to move this boundary earlier than
        // its real code ends. The walk would then stop before the hidden region and miss whatever
        // was put there -- a delegatecall, a selfdestruct, a mint -- while `found` stayed true, so
        // not even the NO_METADATA hint would fire. A forged trailer was enough to buy a clean
        // verdict on a token that could be upgraded or inflated afterwards.
        //
        // What makes hidden code dangerous is that it can run, and code in this region can only run
        // if something jumps to it. So the region is walked the same way the EVM's own jumpdest
        // analysis walks it -- stepping over PUSH immediates, so a 0x5b inside pushed data does not
        // count -- and if a real JUMPDEST turns up, the trailer is not treated as inert data and
        // the whole runtime is scanned instead.
        //
        // Genuine solc metadata is a CBOR blob whose bytes almost never decode to a walk-aligned
        // JUMPDEST, so this costs real contracts nothing; a forged trailer that hides reachable
        // code necessarily contains one.
        // Genuine solc metadata is a CBOR map, and it is worth checking that it actually is one
        // rather than only that it does not look like code.
        //
        // The jumpdest test alone was measured across 114 compiled contracts and misread six of
        // them -- one in nineteen -- because a real metadata hash happens to contain a walk-aligned
        // 0x5b about that often. Each of those had its metadata scanned as instructions, and a
        // 0xff anywhere in the hash then read as SELFDESTRUCT: a fatal flag, on a token that had
        // done nothing but be compiled. It is not a rare accident, it is a coin flip nobody can
        // influence, and it would have rejected legitimate launches at random.
        //
        // Parsing is the stronger test, and the cheaper one to be confident in. A blob that decodes
        // cleanly as CBOR and consumes exactly the declared bytes is metadata; instructions hidden
        // behind a forged trailer are not going to, because CBOR's own length prefixes have to
        // agree with the trailer's at every level.
        if (_isCborMap(code, boundary, len - 2)) return (boundary, true);

        if (_containsJumpdest(code, boundary, len)) return (len, false);

        return (boundary, true);
    }

    /// @dev Whether `code[start:end)` is one well-formed CBOR map consuming exactly that range.
    ///
    ///      Only the shapes solc emits are recognised -- maps, text strings, byte strings, and the
    ///      one-byte-length forms of each. Anything else is not metadata as far as this is
    ///      concerned, which is the conservative direction: an unrecognised trailer falls through
    ///      to being treated as possible code.
    function _isCborMap(bytes memory code, uint256 start, uint256 end) private pure returns (bool) {
        if (start >= end) return false;

        uint8 head = uint8(code[start]);
        // Major type 5, a map, with the pair count in the low bits. Solc emits two or three.
        if (head < 0xa1 || head > 0xb7) return false;

        uint256 pairs = head - 0xa0;
        uint256 i = start + 1;

        for (uint256 pair; pair < pairs; ++pair) {
            // Key, then value. Both are ordinary CBOR items and neither is nested in what solc
            // produces, so a flat walk is enough.
            for (uint256 half; half < 2; ++half) {
                if (i >= end) return false;
                uint8 b = uint8(code[i]);
                uint256 payload;

                if (b >= 0x40 && b <= 0x57) {
                    payload = b - 0x40; // byte string, length in the low bits
                } else if (b >= 0x60 && b <= 0x77) {
                    payload = b - 0x60; // text string, length in the low bits
                } else if (b == 0x58 || b == 0x78) {
                    // byte or text string with a following one-byte length
                    if (i + 1 >= end) return false;
                    payload = uint8(code[i + 1]);
                    i += 1;
                } else if (b <= 0x17) {
                    payload = 0; // small unsigned integer, value in the byte itself
                } else if (b == 0x18) {
                    if (i + 1 >= end) return false;
                    payload = 0;
                    i += 1;
                } else {
                    return false;
                }

                i += 1 + payload;
                if (i > end) return false;
            }
        }

        // Exactly, not merely within: bytes left over mean the trailer is longer than the CBOR it
        // claims to be, and what is in the gap is the whole question.
        return i == end;
    }

    /// @notice Whether a region contains a JUMPDEST the EVM would accept as a jump target.
    ///
    /// @dev Steps over PUSH immediates exactly as `_scanCode` does, because that is the rule the
    ///      EVM itself applies: a 0x5b byte inside pushed data is not a valid destination, and
    ///      counting it would reject honest contracts whose metadata happens to contain the byte.
    function _containsJumpdest(bytes memory code, uint256 from, uint256 to)
        private
        pure
        returns (bool)
    {
        uint256 i = from;
        while (i < to) {
            uint8 op = uint8(code[i]);
            if (op == OP_JUMPDEST) return true;

            if (op >= OP_PUSH1 && op <= OP_PUSH32) {
                i += uint256(op - OP_PUSH1) + 2;
            } else {
                i += 1;
            }
        }
        return false;
    }

    // ------------------------------------------------------------------ selector table

    /// @notice Maps a dispatcher selector to the power it implies, or zero.
    ///
    /// @dev Public so an interface can explain a flag by the function that raised it, and so the
    ///      table can be tested directly rather than only through its effect on a fixture.
    ///
    /// @dev Ordered by how much the answer matters, so the expensive-to-reach cases are the ones
    ///      nobody minds paying for. The signatures are the ones that actually recur in deployed
    ///      tokens; this table is meant to grow, and growing it is a redeploy of this contract plus
    ///      pointing the factory at the new one, which affects future launches only.
    function selectorFlag(bytes4 selector) public pure returns (uint256) {
        // -- supply inflation, fatal
        if (selector == 0x40c10f19) return MINTABLE; // mint(address,uint256)
        if (selector == 0xa0712d68) return MINTABLE; // mint(uint256)
        if (selector == 0x449a52f8) return MINTABLE; // mintTo(address,uint256)
        if (selector == 0xcc872b66) return MINTABLE; // issue(uint256)

        // -- transfer gating
        if (selector == 0x404e5129) return BLACKLIST; // blacklist(address,bool)
        if (selector == 0x153b0d1e) return BLACKLIST; // setBlacklist(address,bool)
        if (selector == 0x44337ea1) return BLACKLIST; // addToBlacklist(address)
        if (selector == 0xd01dd6d2) return BLACKLIST; // setBlacklisted(address,bool)
        if (selector == 0x13b2a23e) return BLACKLIST; // setBots(address,bool)

        if (selector == 0x8456cb59) return PAUSABLE; // pause()
        if (selector == 0x3f4ba83a) return PAUSABLE; // unpause()
        if (selector == 0x16c38b3c) return PAUSABLE; // setPaused(bool)

        if (selector == 0x8a8c523c) return TRADING_TOGGLE; // enableTrading()
        if (selector == 0xc2e5ec04) return TRADING_TOGGLE; // setTradingEnabled(bool)
        if (selector == 0xc9567bf9) return TRADING_TOGGLE; // openTrading()
        if (selector == 0x293230b8) return TRADING_TOGGLE; // startTrading()

        // -- economics that can be rewritten under a holder
        if (selector == 0xc647b20e) return MUTABLE_TAX; // setTaxes(uint256,uint256)
        if (selector == 0x0b78f9c0) return MUTABLE_TAX; // setFees(uint256,uint256)
        if (selector == 0x69fe0e2d) return MUTABLE_TAX; // setFee(uint256)
        if (selector == 0xdc1052e2) return MUTABLE_TAX; // setBuyTax(uint256)
        if (selector == 0x8cd09d50) return MUTABLE_TAX; // setSellTax(uint256)
        if (selector == 0x6db79437) return MUTABLE_TAX; // updateFees(uint256,uint256)
        if (selector == 0xc6d69a30) return MUTABLE_TAX; // setTaxRate(uint256)

        if (selector == 0xec28438a) return TRANSFER_LIMITS; // setMaxTxAmount(uint256)
        if (selector == 0x5d0044ca) return TRANSFER_LIMITS; // setMaxWallet(uint256)
        if (selector == 0x27a14fc2) return TRANSFER_LIMITS; // setMaxWalletAmount(uint256)

        if (selector == 0x437823ec) return FEE_EXCLUSIONS; // excludeFromFee(address)
        if (selector == 0x6612e66f) return FEE_EXCLUSIONS; // setExcludedFromFee(address,bool)

        // -- reaching into balances the caller does not hold
        if (selector == 0x79cc6790) return BURNS_OTHERS; // burnFrom(address,uint256)
        if (selector == 0xbc4f2d6d) return REBASES; // rebase(uint256)

        if (selector == 0x8cd4426d) return ADMIN_WITHDRAW; // rescueERC20(address,uint256)
        if (selector == 0xf5648a4f) return ADMIN_WITHDRAW; // withdrawStuckETH()
        if (selector == 0x01681a62) return ADMIN_WITHDRAW; // sweep(address)

        return 0;
    }

    // ------------------------------------------------------------------ live surface

    /// @dev Everything here is a staticcall, so a contract that reverts on these simply reads as
    ///      not-an-ERC20 rather than taking the screen down with it.
    function _scanSurface(address token, address inertOwner) private view returns (uint256 flags) {
        (bool okSupply, uint256 supply) = _tryUint(token, "totalSupply()");
        if (!okSupply || supply == 0) return NOT_AN_ERC20;

        (bool okDecimals, uint256 decimals) = _tryUint(token, "decimals()");
        if (!okDecimals || decimals > 36) return NOT_AN_ERC20;
        if (decimals > 18) flags |= ODD_DECIMALS;

        (bool okBalance,) = _tryUint(token, "balanceOf(address)", address(this));
        if (!okBalance) return NOT_AN_ERC20;

        address holder = ownerOf(token);
        if (holder != address(0) && holder != DEAD && holder != inertOwner) flags |= OWNER_LIVE;
    }

    /// @notice The address in the token's owner slot, or zero if it has none.
    /// @dev Tried in the order the ecosystem settled on them. Surfaced publicly because a frontend
    ///      wants to show *who* holds the token, not only that somebody does.
    function ownerOf(address token) public view returns (address) {
        (bool ok, address holder) = _tryAddress(token, "owner()");
        if (ok) return holder;
        (ok, holder) = _tryAddress(token, "getOwner()");
        if (ok) return holder;
        (ok, holder) = _tryAddress(token, "admin()");
        return ok ? holder : address(0);
    }

    function _tryUint(address token, string memory sig) private view returns (bool, uint256) {
        (bool ok, bytes memory ret) = token.staticcall(abi.encodeWithSignature(sig));
        if (!ok || ret.length < 32) return (false, 0);
        return (true, abi.decode(ret, (uint256)));
    }

    function _tryUint(address token, string memory sig, address arg)
        private
        view
        returns (bool, uint256)
    {
        (bool ok, bytes memory ret) = token.staticcall(abi.encodeWithSignature(sig, arg));
        if (!ok || ret.length < 32) return (false, 0);
        return (true, abi.decode(ret, (uint256)));
    }

    /// @dev A word that is not a clean left-padded address is treated as no answer, so a function
    ///      that happens to share the name but not the shape cannot be read as an owner.
    function _tryAddress(address token, string memory sig) private view returns (bool, address) {
        (bool ok, bytes memory ret) = token.staticcall(abi.encodeWithSignature(sig));
        if (!ok || ret.length < 32) return (false, address(0));
        uint256 word = abi.decode(ret, (uint256));
        if (word > type(uint160).max) return (false, address(0));
        return (true, address(uint160(word)));
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "forge-std/=lib/forge-std/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.