caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
Working tree is now the canonical repo. Off-chain play-money + trustless on-chain
(FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution,
mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 23:29:50 +03:00

14 KiB

ERC721

Simple ERC721 implementation with storage hitchhiking.

Note:

  • The ERC721 standard allows for self-approvals. For performance, this implementation WILL NOT revert for such actions. Please add any checks with overrides if desired.
  • For performance, methods are made payable where permitted by the ERC721 standard.
  • The safeTransfer functions use the identity precompile (0x4) to copy memory internally.

If you are overriding:

  • NEVER violate the ERC721 invariant: the balance of an owner MUST always be equal to their number of ownership slots. The transfer functions do not have an underflow guard for user token balances.
  • Make sure all variables written to storage are properly cleaned (e.g. the bool value for isApprovedForAll MUST be either 1 or 0 under the hood).
  • Check that the overridden function is actually used in the function you want to change the behavior of. Much of the code has been manually inlined for performance.

Constants

_MAX_ACCOUNT_BALANCE

uint256 internal constant _MAX_ACCOUNT_BALANCE = 0xffffffff

An account can hold up to 4294967295 tokens.

Custom Errors

NotOwnerNorApproved()

error NotOwnerNorApproved()

Only the token owner or an approved account can manage the token.

TokenDoesNotExist()

error TokenDoesNotExist()

The token does not exist.

TokenAlreadyExists()

error TokenAlreadyExists()

The token already exists.

BalanceQueryForZeroAddress()

error BalanceQueryForZeroAddress()

Cannot query the balance for the zero address.

TransferToZeroAddress()

error TransferToZeroAddress()

Cannot mint or transfer to the zero address.

TransferFromIncorrectOwner()

error TransferFromIncorrectOwner()

The token must be owned by from.

AccountBalanceOverflow()

error AccountBalanceOverflow()

The recipient's balance has overflowed.

TransferToNonERC721ReceiverImplementer()

error TransferToNonERC721ReceiverImplementer()

Cannot safely transfer to a contract that does not implement
the ERC721Receiver interface.

Events

Transfer(address,address,uint256)

event Transfer(
    address indexed from, address indexed to, uint256 indexed id
)

Emitted when token id is transferred from from to to.

Approval(address,address,uint256)

event Approval(
    address indexed owner, address indexed account, uint256 indexed id
)

Emitted when owner enables account to manage the id token.

ApprovalForAll(address,address,bool)

event ApprovalForAll(
    address indexed owner, address indexed operator, bool isApproved
)

Emitted when owner enables or disables operator to manage all of their tokens.

ERC721

ownerOf(uint256)

function ownerOf(uint256 id) public view virtual returns (address result)

Returns the owner of token id.

Requirements:

  • Token id must exist.

balanceOf(address)

function balanceOf(address owner)
    public
    view
    virtual
    returns (uint256 result)

Returns the number of tokens owned by owner.

Requirements:

  • owner must not be the zero address.

getApproved(uint256)

function getApproved(uint256 id)
    public
    view
    virtual
    returns (address result)

Returns the account approved to manage token id.

Requirements:

  • Token id must exist.

approve(address,uint256)

function approve(address account, uint256 id) public payable virtual

Sets account as the approved account to manage token id.

Requirements:

  • Token id must exist.
  • The caller must be the owner of the token,
    or an approved operator for the token owner.

Emits an Approval event.

isApprovedForAll(address,address)

function isApprovedForAll(address owner, address operator)
    public
    view
    virtual
    returns (bool result)

Returns whether operator is approved to manage the tokens of owner.

setApprovalForAll(address,bool)

function setApprovalForAll(address operator, bool isApproved)
    public
    virtual

Sets whether operator is approved to manage the tokens of the caller.

Emits an ApprovalForAll event.

transferFrom(address,address,uint256)

function transferFrom(address from, address to, uint256 id)
    public
    payable
    virtual

Transfers token id from from to to.

Requirements:

  • Token id must exist.
  • from must be the owner of the token.
  • to cannot be the zero address.
  • The caller must be the owner of the token, or be approved to manage the token.

Emits a Transfer event.

safeTransferFrom(address,address,uint256)

function safeTransferFrom(address from, address to, uint256 id)
    public
    payable
    virtual

Equivalent to safeTransferFrom(from, to, id, "").

safeTransferFrom(address,address,uint256,bytes)

function safeTransferFrom(
    address from,
    address to,
    uint256 id,
    bytes calldata data
) public payable virtual

Transfers token id from from to to.

Requirements:

  • Token id must exist.
  • from must be the owner of the token.
  • to cannot be the zero address.
  • The caller must be the owner of the token, or be approved to manage the token.
  • If to refers to a smart contract, it must implement
    IERC721Receiver-onERC721Received, which is called upon a safe transfer.

Emits a Transfer event.

supportsInterface(bytes4)

function supportsInterface(bytes4 interfaceId)
    public
    view
    virtual
    returns (bool result)

Returns true if this contract implements the interface defined by interfaceId.
See: https://eips.ethereum.org/EIPS/eip-165
This function call must use less than 30000 gas.

Internal Query Functions

_exists(uint256)

function _exists(uint256 id) internal view virtual returns (bool result)

Returns if token id exists.

_ownerOf(uint256)

function _ownerOf(uint256 id)
    internal
    view
    virtual
    returns (address result)

Returns the owner of token id.
Returns the zero address instead of reverting if the token does not exist.

Internal Data Hitchhiking Functions

For performance, no events are emitted for the hitchhiking setters.
Please emit your own events if required.

_getAux(address)

function _getAux(address owner)
    internal
    view
    virtual
    returns (uint224 result)

Returns the auxiliary data for owner.
Minting, transferring, burning the tokens of owner will not change the auxiliary data.
Auxiliary data can be set for any address, even if it does not have any tokens.

_setAux(address,uint224)

function _setAux(address owner, uint224 value) internal virtual

Set the auxiliary data for owner to value.
Minting, transferring, burning the tokens of owner will not change the auxiliary data.
Auxiliary data can be set for any address, even if it does not have any tokens.

_getExtraData(uint256)

function _getExtraData(uint256 id)
    internal
    view
    virtual
    returns (uint96 result)

Returns the extra data for token id.
Minting, transferring, burning a token will not change the extra data.
The extra data can be set on a non-existent token.

_setExtraData(uint256,uint96)

function _setExtraData(uint256 id, uint96 value) internal virtual

Sets the extra data for token id to value.
Minting, transferring, burning a token will not change the extra data.
The extra data can be set on a non-existent token.

Internal Mint Functions

_mint(address,uint256)

function _mint(address to, uint256 id) internal virtual

Mints token id to to.

Requirements:

  • Token id must not exist.
  • to cannot be the zero address.

Emits a Transfer event.

_mintAndSetExtraDataUnchecked(address,uint256,uint96)

function _mintAndSetExtraDataUnchecked(address to, uint256 id, uint96 value)
    internal
    virtual

Mints token id to to, and updates the extra data for token id to value.
Does NOT check if token id already exists (assumes id is auto-incrementing).

Requirements:

  • to cannot be the zero address.

Emits a Transfer event.

_safeMint(address,uint256)

function _safeMint(address to, uint256 id) internal virtual

Equivalent to _safeMint(to, id, "").

_safeMint(address,uint256,bytes)

function _safeMint(address to, uint256 id, bytes memory data)
    internal
    virtual

Mints token id to to.

Requirements:

  • Token id must not exist.
  • to cannot be the zero address.
  • If to refers to a smart contract, it must implement
    IERC721Receiver-onERC721Received, which is called upon a safe transfer.

Emits a Transfer event.

Internal Burn Functions

_burn(uint256)

function _burn(uint256 id) internal virtual

Equivalent to _burn(address(0), id).

_burn(address,uint256)

function _burn(address by, uint256 id) internal virtual

Destroys token id, using by.

Requirements:

  • Token id must exist.
  • If by is not the zero address,
    it must be the owner of the token, or be approved to manage the token.

Emits a Transfer event.

Internal Approval Functions

_isApprovedOrOwner(address,uint256)

function _isApprovedOrOwner(address account, uint256 id)
    internal
    view
    virtual
    returns (bool result)

Returns whether account is the owner of token id, or is approved to manage it.

Requirements:

  • Token id must exist.

_getApproved(uint256)

function _getApproved(uint256 id)
    internal
    view
    virtual
    returns (address result)

Returns the account approved to manage token id.
Returns the zero address instead of reverting if the token does not exist.

_approve(address,uint256)

function _approve(address account, uint256 id) internal virtual

Equivalent to _approve(address(0), account, id).

_approve(address,address,uint256)

function _approve(address by, address account, uint256 id)
    internal
    virtual

Sets account as the approved account to manage token id, using by.

Requirements:

  • Token id must exist.
  • If by is not the zero address, by must be the owner
    or an approved operator for the token owner.

Emits a Approval event.

_setApprovalForAll(address,address,bool)

function _setApprovalForAll(address by, address operator, bool isApproved)
    internal
    virtual

Approve or remove the operator as an operator for by,
without authorization checks.

Emits an ApprovalForAll event.

Internal Transfer Functions

_transfer(address,address,uint256)

function _transfer(address from, address to, uint256 id) internal virtual

Equivalent to _transfer(address(0), from, to, id).

_transfer(address,address,address,uint256)

function _transfer(address by, address from, address to, uint256 id)
    internal
    virtual

Transfers token id from from to to.

Requirements:

  • Token id must exist.
  • from must be the owner of the token.
  • to cannot be the zero address.
  • If by is not the zero address,
    it must be the owner of the token, or be approved to manage the token.

Emits a Transfer event.

_safeTransfer(address,address,uint256)

function _safeTransfer(address from, address to, uint256 id)
    internal
    virtual

Equivalent to _safeTransfer(from, to, id, "").

_safeTransfer(address,address,uint256,bytes)

function _safeTransfer(
    address from,
    address to,
    uint256 id,
    bytes memory data
) internal virtual

Transfers token id from from to to.

Requirements:

  • Token id must exist.
  • from must be the owner of the token.
  • to cannot be the zero address.
  • The caller must be the owner of the token, or be approved to manage the token.
  • If to refers to a smart contract, it must implement
    IERC721Receiver-onERC721Received, which is called upon a safe transfer.

Emits a Transfer event.

_safeTransfer(address,address,address,uint256)

function _safeTransfer(address by, address from, address to, uint256 id)
    internal
    virtual

Equivalent to _safeTransfer(by, from, to, id, "").

_safeTransfer(address,address,address,uint256,bytes)

function _safeTransfer(
    address by,
    address from,
    address to,
    uint256 id,
    bytes memory data
) internal virtual

Transfers token id from from to to.

Requirements:

  • Token id must exist.
  • from must be the owner of the token.
  • to cannot be the zero address.
  • If by is not the zero address,
    it must be the owner of the token, or be approved to manage the token.
  • If to refers to a smart contract, it must implement
    IERC721Receiver-onERC721Received, which is called upon a safe transfer.

Emits a Transfer event.

Hooks For Overriding

_beforeTokenTransfer(address,address,uint256)

function _beforeTokenTransfer(address from, address to, uint256 id)
    internal
    virtual

Hook that is called before any token transfers, including minting and burning.

_afterTokenTransfer(address,address,uint256)

function _afterTokenTransfer(address from, address to, uint256 id)
    internal
    virtual

Hook that is called after any token transfers, including minting and burning.