Onchain.ERC1155 (onchain v0.5.4)

Copy Markdown View Source

ERC-1155 (Multi Token) read operations.

Thin wrappers around Onchain.Contract.call/5 for standard ERC-1155 queries. ERC-1155 is semi-fungible: the same token ID can have multiple owners with different balances.

Error Format

Errors pass through from underlying modules:

SourceError Shape
Onchain.Address.validate/1{:error, {:invalid_address, input}}
Onchain.Contract.call/5{:error, {:encode_error, ...}}, {:error, {:rpc_error, ...}}, {:error, {:decode_error, ...}}

Functions

FunctionPurpose
balance_of/4Balance of a specific token ID for an owner
balance_of!/4Same, raises on error
balance_of_batch/4Batch balance query for multiple owner/token pairs
balance_of_batch!/4Same, raises on error
uri/3Metadata URI for a token ID
uri!/3Same, raises on error
approved_for_all?/4Whether an operator is approved for all tokens
approved_for_all!/4Same, raises on error

API Functions

FunctionArityDescriptionParam Kinds
approved_for_all!4Check if an operator is approved for all tokens. Raises on error.contract: value, owner: value, operator: value, opts: value
approved_for_all?4Check if an operator is approved for all tokens of an owner.contract: value, owner: value, operator: value, opts: value
uri!3Get the metadata URI for a token ID. Raises on error.contract: value, token_id: value, opts: value
uri3Get the metadata URI for a token ID.contract: value, token_id: value, opts: value
balance_of_batch!4Batch query balances. Raises on error.contract: value, owners: value, token_ids: value, opts: value
balance_of_batch4Batch query balances for multiple owner/token ID pairs.contract: value, owners: value, token_ids: value, opts: value
balance_of!4Get the balance of a specific token ID for an owner. Raises on error.contract: value, owner: value, token_id: value, opts: value
balance_of4Get the balance of a specific token ID for an owner.contract: value, owner: value, token_id: value, opts: value

Summary

Functions

Check if an operator is approved for all tokens. Raises on error.

Check if an operator is approved for all tokens of an owner.

Get the balance of a specific token ID for an owner.

Get the balance of a specific token ID for an owner. Raises on error.

Batch query balances for multiple owner/token ID pairs.

Batch query balances. Raises on error.

Get the metadata URI for a token ID.

Get the metadata URI for a token ID. Raises on error.

Functions

approved_for_all!(contract, owner, operator, opts \\ [])

@spec approved_for_all!(
  String.t() | binary(),
  String.t() | binary(),
  String.t() | binary(),
  keyword()
) :: boolean()

Check if an operator is approved for all tokens. Raises on error.

Parameters

  • contract - ERC-1155 contract address (value)
  • owner - Token owner address (value)
  • operator - Operator address to check (value)
  • opts - Options: :rpc_url, :timeout, :block (default: [], value)

Returns

Whether the operator is approved for all tokens (boolean)

# descripex:contract
%{
  params: %{
    owner: %{description: "Token owner address", kind: :value},
    opts: %{
      default: [],
      description: "Options: :rpc_url, :timeout, :block",
      kind: :value
    },
    operator: %{description: "Operator address to check", kind: :value},
    contract: %{description: "ERC-1155 contract address", kind: :value}
  },
  returns: %{
    type: :boolean,
    description: "Whether the operator is approved for all tokens"
  }
}

approved_for_all?(contract, owner, operator, opts \\ [])

@spec approved_for_all?(
  String.t() | binary(),
  String.t() | binary(),
  String.t() | binary(),
  keyword()
) :: {:ok, boolean()} | {:error, term()}

Check if an operator is approved for all tokens of an owner.

Parameters

  • contract - ERC-1155 contract address (value)
  • owner - Token owner address (value)
  • operator - Operator address to check (value)
  • opts - Options: :rpc_url, :timeout, :block (default: [], value)

Returns

Whether the operator is approved for all tokens ({:ok, boolean()} | {:error, term()})

# descripex:contract
%{
  params: %{
    owner: %{description: "Token owner address", kind: :value},
    opts: %{
      default: [],
      description: "Options: :rpc_url, :timeout, :block",
      kind: :value
    },
    operator: %{description: "Operator address to check", kind: :value},
    contract: %{description: "ERC-1155 contract address", kind: :value}
  },
  returns: %{
    type: "{:ok, boolean()} | {:error, term()}",
    description: "Whether the operator is approved for all tokens",
    example: "false"
  }
}

balance_of(contract, owner, token_id, opts \\ [])

@spec balance_of(
  String.t() | binary(),
  String.t() | binary(),
  non_neg_integer(),
  keyword()
) ::
  {:ok, non_neg_integer()} | {:error, term()}

Get the balance of a specific token ID for an owner.

Parameters

  • contract - ERC-1155 contract address (value)
  • owner - Address to check balance for (value)
  • token_id - Token ID (non-negative integer) (value)
  • opts - Options: :rpc_url, :timeout, :block (default: [], value)

Returns

Balance of the token ID for the owner ({:ok, non_neg_integer()} | {:error, term()})

# descripex:contract
%{
  params: %{
    owner: %{description: "Address to check balance for", kind: :value},
    opts: %{
      default: [],
      description: "Options: :rpc_url, :timeout, :block",
      kind: :value
    },
    contract: %{description: "ERC-1155 contract address", kind: :value},
    token_id: %{description: "Token ID (non-negative integer)", kind: :value}
  },
  returns: %{
    type: "{:ok, non_neg_integer()} | {:error, term()}",
    description: "Balance of the token ID for the owner",
    example: "10"
  }
}

balance_of!(contract, owner, token_id, opts \\ [])

@spec balance_of!(
  String.t() | binary(),
  String.t() | binary(),
  non_neg_integer(),
  keyword()
) ::
  non_neg_integer()

Get the balance of a specific token ID for an owner. Raises on error.

Parameters

  • contract - ERC-1155 contract address (value)
  • owner - Address to check balance for (value)
  • token_id - Token ID (non-negative integer) (value)
  • opts - Options: :rpc_url, :timeout, :block (default: [], value)

Returns

Balance of the token ID (non_neg_integer)

# descripex:contract
%{
  params: %{
    owner: %{description: "Address to check balance for", kind: :value},
    opts: %{
      default: [],
      description: "Options: :rpc_url, :timeout, :block",
      kind: :value
    },
    contract: %{description: "ERC-1155 contract address", kind: :value},
    token_id: %{description: "Token ID (non-negative integer)", kind: :value}
  },
  returns: %{type: :non_neg_integer, description: "Balance of the token ID"}
}

balance_of_batch(contract, owners, token_ids, opts \\ [])

@spec balance_of_batch(
  String.t() | binary(),
  [String.t() | binary()],
  [non_neg_integer()],
  keyword()
) ::
  {:ok, [non_neg_integer()]} | {:error, term()}

Batch query balances for multiple owner/token ID pairs.

Parameters

  • contract - ERC-1155 contract address (value)
  • owners - List of owner addresses (must match token_ids length) (value)
  • token_ids - List of token IDs (must match owners length) (value)
  • opts - Options: :rpc_url, :timeout, :block (default: [], value)

Returns

List of balances corresponding to each owner/token_id pair ({:ok, [non_neg_integer()]} | {:error, term()})

# descripex:contract
%{
  params: %{
    opts: %{
      default: [],
      description: "Options: :rpc_url, :timeout, :block",
      kind: :value
    },
    contract: %{description: "ERC-1155 contract address", kind: :value},
    owners: %{
      description: "List of owner addresses (must match token_ids length)",
      kind: :value
    },
    token_ids: %{
      description: "List of token IDs (must match owners length)",
      kind: :value
    }
  },
  returns: %{
    type: "{:ok, [non_neg_integer()]} | {:error, term()}",
    description: "List of balances corresponding to each owner/token_id pair",
    example: "[10, 0, 5]"
  }
}

balance_of_batch!(contract, owners, token_ids, opts \\ [])

@spec balance_of_batch!(
  String.t() | binary(),
  [String.t() | binary()],
  [non_neg_integer()],
  keyword()
) ::
  [non_neg_integer()]

Batch query balances. Raises on error.

Parameters

  • contract - ERC-1155 contract address (value)
  • owners - List of owner addresses (must match token_ids length) (value)
  • token_ids - List of token IDs (must match owners length) (value)
  • opts - Options: :rpc_url, :timeout, :block (default: [], value)

Returns

List of balances ([non_neg_integer()])

# descripex:contract
%{
  params: %{
    opts: %{
      default: [],
      description: "Options: :rpc_url, :timeout, :block",
      kind: :value
    },
    contract: %{description: "ERC-1155 contract address", kind: :value},
    owners: %{
      description: "List of owner addresses (must match token_ids length)",
      kind: :value
    },
    token_ids: %{
      description: "List of token IDs (must match owners length)",
      kind: :value
    }
  },
  returns: %{type: "[non_neg_integer()]", description: "List of balances"}
}

uri(contract, token_id, opts \\ [])

@spec uri(String.t() | binary(), non_neg_integer(), keyword()) ::
  {:ok, String.t()} | {:error, term()}

Get the metadata URI for a token ID.

Parameters

  • contract - ERC-1155 contract address (value)
  • token_id - Token ID (non-negative integer) (value)
  • opts - Options: :rpc_url, :timeout, :block (default: [], value)

Returns

Token metadata URI (may contain {id} placeholder per ERC-1155 spec) ({:ok, String.t()} | {:error, term()})

# descripex:contract
%{
  params: %{
    opts: %{
      default: [],
      description: "Options: :rpc_url, :timeout, :block",
      kind: :value
    },
    contract: %{description: "ERC-1155 contract address", kind: :value},
    token_id: %{description: "Token ID (non-negative integer)", kind: :value}
  },
  returns: %{
    type: "{:ok, String.t()} | {:error, term()}",
    description: "Token metadata URI (may contain {id} placeholder per ERC-1155 spec)",
    example: "\"https://token-cdn-domain/{id}.json\""
  }
}

uri!(contract, token_id, opts \\ [])

@spec uri!(String.t() | binary(), non_neg_integer(), keyword()) :: String.t()

Get the metadata URI for a token ID. Raises on error.

Parameters

  • contract - ERC-1155 contract address (value)
  • token_id - Token ID (non-negative integer) (value)
  • opts - Options: :rpc_url, :timeout, :block (default: [], value)

Returns

Token metadata URI (string)

# descripex:contract
%{
  params: %{
    opts: %{
      default: [],
      description: "Options: :rpc_url, :timeout, :block",
      kind: :value
    },
    contract: %{description: "ERC-1155 contract address", kind: :value},
    token_id: %{description: "Token ID (non-negative integer)", kind: :value}
  },
  returns: %{type: :string, description: "Token metadata URI"}
}