Encode for External Wallets

Build raw transaction payloads for OptionBook fills without using the built-in ethers signer — for use with viem, wagmi, Account Abstraction wallets, Safe, and similar tools.

When to use encode methods

Use encodeFillOrder() and encodeSwapAndFillOrder() instead of fillOrder() / swapAndFillOrder() when:

  • You are using viem or wagmi and want to send the transaction through your own wallet client

  • Your wallet is an Account Abstraction wallet (Coinbase Smart Wallet, Safe, Biconomy, etc.)

  • You need custom gas estimation or nonce management

  • You are building a transaction batch and need the raw calldata

Both encode methods return { to: string; data: string } — the target contract address and encoded calldata. No transaction is sent.

encodeFillOrder()

import { ethers } from 'ethers';
import { ThetanutsClient } from '@thetanuts-finance/thetanuts-client';

const provider = new ethers.JsonRpcProvider('https://mainnet.base.org');
const client = new ThetanutsClient({ chainId: 8453, provider });

// Fetch the order (no signer needed for this part)
const orders = await client.api.fetchOrders();
const order = orders[0];

// Encode the fill (no signer needed)
const { to, data } = client.optionBook.encodeFillOrder(
  order,
  10_000000n,                              // 10 USDC (optional — omit for max fill)
  '0x92b8ac05b63472d1D84b32bDFBBf3e1887331567', // optional referrer
);

Parameters: same as fillOrder().

Returns: { to: string; data: string }

Sending with viem/wagmi

Sending with ethers.js signer directly

encodeSwapAndFillOrder()

Encoded variant of swapAndFillOrder(). Atomically swaps from a source token and fills the order — useful when the user holds WETH but the order requires USDC collateral.

Parameters: same as swapAndFillOrder().

Returns: { to: string; data: string }

You must approve the swap router to spend the source token before sending this transaction.

Approval encoding for external wallets

If you also need to encode the collateral approval for an external wallet:

Referrer in encode methods

Encode methods accept the same referrer parameter as their non-encode counterparts:


See Also

Last updated