Onchain.ERC7730.Formatter (onchain v0.7.0)

Copy Markdown View Source

Display-rule engine for ERC-7730 descriptors.

Applies a single field's format rule to the value resolved at its path, producing a %{label, value, formatted_value, raw} map:

  • raw — the canonical decoded value (address as a 20-byte binary, numbers as integers, booleans, byte strings).
  • value — a JSON-friendly normalization of raw (address as an EIP-55 checksummed hex string, bytes as 0x-hex; numbers/booleans/strings as-is).
  • formatted_value — the human-readable string per the field's format rule.

Path resolution

PrefixRoot
#.name (or bare name)decoded message / calldata params
@.to / @.value / @.fromtransaction envelope
$.a.b.cthe descriptor document (constants, metadata, enums)

Supported formats (first pass)

raw, amount (native currency), tokenAmount, addressName, date, duration, unit, enum. calldata (embedded calls) and nftName (needs an on-chain name lookup) are deferred and fall back to raw rendering.

Token metadata resolution

tokenAmount needs a token's decimals/symbol. Resolution order:

  1. opts[:tokens] — caller map %{lowercase_hex => %{decimals:, symbol:}}.
  2. metadata.token in the descriptor (for single-token EIP-712 descriptors).
  3. opts[:rpc_url] — live Onchain.ERC20.decimals/2 + symbol/2 lookup.
  4. Otherwise the raw integer is shown unscaled.

API Functions

FunctionArityDescriptionParam Kinds
format_field4Apply a descriptor field's format rule to its resolved value.field: value, resolution: value, descriptor: value, opts: value

Summary

Functions

Apply a descriptor field's format rule to its resolved value.

Types

result()

@type result() :: %{
  label: String.t() | nil,
  value: term(),
  formatted_value: String.t(),
  raw: term()
}

Functions

format_field(field, resolution, descriptor, opts \\ [])

@spec format_field(
  Onchain.ERC7730.Descriptor.field(),
  map(),
  Onchain.ERC7730.Descriptor.t(),
  keyword()
) ::
  {:ok, result()} | {:error, {atom(), term()}}

Apply a descriptor field's format rule to its resolved value.

Parameters

  • field - A parsed display field (see Onchain.ERC7730.Descriptor) (value)
  • resolution - Binding resolution (%{message, types, envelope}) from Onchain.ERC7730.Binding (value)
  • descriptor - Parsed %Onchain.ERC7730.Descriptor{} (for $. paths) (value)
  • opts - Resolution opts: :tokens, :rpc_url, :native_symbol, :native_decimals, :names (default: [], value)

Returns

Rendered field, or {:error, {:unresolved_path, path}} for a missing visible field ({:ok, %{label, value, formatted_value, raw}} | {:error, {tag, reason}})

# descripex:contract
%{
  params: %{
    opts: %{
      default: [],
      description: "Resolution opts: :tokens, :rpc_url, :native_symbol, :native_decimals, :names",
      kind: :value
    },
    field: %{
      description: "A parsed display field (see Onchain.ERC7730.Descriptor)",
      kind: :value
    },
    descriptor: %{
      description: "Parsed %Onchain.ERC7730.Descriptor{} (for $. paths)",
      kind: :value
    },
    resolution: %{
      description: "Binding resolution (%{message, types, envelope}) from Onchain.ERC7730.Binding",
      kind: :value
    }
  },
  returns: %{
    type: "{:ok, %{label, value, formatted_value, raw}} | {:error, {tag, reason}}",
    description: "Rendered field, or {:error, {:unresolved_path, path}} for a missing visible field"
  }
}