GET /v1/config.approve(settlement, amount) on the USDC token. Skip this if you use
depositWithPermit.deposit(amount) on the settlement contract, with amount in
micro-USDC. Or call depositWithPermit(amount, deadline, v, r, s) with an
EIP-2612 permit to approve and deposit in one transaction.safe
tag. Your first deposit opens your account. Watch for a funds event on your
WebSocket account: channel, or poll GET /v1/account/{addr}
until known is true and freeCash includes it.12345678import { parseAbi } from 'viem' const erc20 = parseAbi(['function approve(address spender, uint256 amount) returns (bool)']) const settlement = parseAbi(['function deposit(uint256 amount)']) const amount = 1_000_000_000n // $1,000 await wallet.writeContract({ address: config.usdc, abi: erc20, functionName: 'approve', args: [config.settlement, amount] }) await wallet.writeContract({ address: config.settlement, abi: settlement, functionName: 'deposit', args: [amount] })
WithdrawalRequest(user, amount, nonce) with the account's own key
and send it to POST /v1/withdraw. The exchange
debits your free cash at once.withdrawal and its signature.
If it says "pending": true, fetch it from GET /v1/withdrawals/{user} in a
moment.withdraw(withdrawal, signature) on the settlement contract from your
account's address. The contract pays you once it has executed your fills up
to the withdrawal's afterFill, before its deadline.123456789const settlement = parseAbi([ 'function withdraw((address user, address to, uint128 amount, uint64 nonce, uint64 deadline, uint64 afterFill) wd, bytes sig)', ]) const { withdrawal: w, signature } = answer await wallet.writeContract({ address: config.settlement, abi: settlement, functionName: 'withdraw', args: [{ ...w, amount: BigInt(w.amount), nonce: BigInt(w.nonce), deadline: BigInt(w.deadline), afterFill: BigInt(w.afterFill) }, signature], })
NotYetSettled, it has not yet executed every fill
of yours before the withdrawal. Try again shortly.requestForcedWithdrawal(amount) on the
contract. The exchange must cancel your orders and pay you; if it does not within
the contract's forceWindow, anyone can freeze your account and you withdraw
with escapeWithdraw(). Custody
has the details.