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 builder —
approve_calldata/2produces the raw function-call bytes forapprove(spender, amount). Use this when you want the bytes for inspection, off-line signing, or submission through your own Polygon RPC client. - Submit wrapper —
approve/2builds, signs, and broadcasts the transaction in one call. Composesapprove_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
@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; usePolymarket.RPC.nonce/2,gas_price/1, andestimate_gas/2first if you need that.:private_key— 32-byte private key (raw binary or0x-prefixed hex) for the sender.
Returns {:error, {:missing_option, key}} when a required option is
absent, honoring the {:ok, _} | {:error, _} contract instead of
raising.
@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"