Polymarket.ERC20 (Polymarket v0.2.0)

Copy Markdown View Source

ERC-20 helpers for the SDK's on-chain surface.

The SDK currently needs only one ERC-20 function — approve — to authorize the CollateralOnramp / CollateralOfframp and the CTF contract to spend tokens on the caller's behalf.

Two surfaces

  • Calldata builderapprove_calldata/2 produces the raw function-call bytes for approve(spender, amount). Use this when you want the bytes for inspection, off-line signing, or submission through your own Polygon RPC client.
  • Submit wrapperapprove/2 builds, signs, and broadcasts the transaction in one call. Composes approve_calldata/2 + Polymarket.Tx + Polymarket.RPC. Caller provides every signing field (:nonce, :gas_price, :gas_limit, :chain_id, :private_key); the wrapper does not auto-fetch from RPC.

Calldata bytes are validated against the canonical ethers.js v6 encoder in test/fixtures/calldata.json. The submit wrapper's composition (calldata → tx → signed bytes) is verified against a manually-composed reference in test/polymarket/submit_test.exs.

No transaction-submission policy is baked in. Confirmation polling, retry on nonce too low, and EIP-1559 type-2 fees are out of scope here.

Summary

Functions

Builds, signs, and broadcasts an approve(spender, amount) transaction to the ERC-20 token at :contract. Returns the on-chain transaction hash on success.

Builds calldata for approve(address spender, uint256 amount).

Functions

approve(rpc, opts)

@spec approve(
  Polymarket.RPC.t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Builds, signs, and broadcasts an approve(spender, amount) transaction to the ERC-20 token at :contract. Returns the on-chain transaction hash on success.

Required opts

  • :contract — the ERC-20 token address being approved (e.g. pUSD).
  • :spender — the address being authorized (e.g. CTF or onramp).
  • :amount — non-negative integer, in the token's smallest unit.
  • :nonce, :gas_price, :gas_limit, :chain_id — caller-provided transaction fields. The wrapper does not auto-fetch from RPC; use Polymarket.RPC.nonce/2, gas_price/1, and estimate_gas/2 first if you need that.
  • :private_key — 32-byte private key (raw binary or 0x-prefixed hex) for the sender.

Returns {:error, {:missing_option, key}} when a required option is absent, honoring the {:ok, _} | {:error, _} contract instead of raising.

approve_calldata(spender, amount)

@spec approve_calldata(String.t(), non_neg_integer()) :: binary()

Builds calldata for approve(address spender, uint256 amount).

Examples

iex> Polymarket.ERC20.approve_calldata(
...>   "0x0000000000000000000000000000000000000001",
...>   1_000
...> )
...> |> Polymarket.ABI.to_hex()
"0x095ea7b3000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000003e8"