DEV Community

Mack Schneider
Mack Schneider

Posted on

How to Get the Required Token Before an Onchain Invoice

Before paying an onchain invoice, match its chain, token contract and exact amount, then swap only the shortfall into that token while keeping enough native currency for gas. The invoice is a payment specification: a token symbol alone is not enough, because the same symbol can refer to different contracts or exist on multiple chains.

Read the invoice as a contract call

Identify the chain ID, recipient or invoice contract, token contract address, amount in base units, and any expiry or invoice reference. Check these against the protocol’s own published details; “120 USDT” does not establish which USDT contract or network the recipient will accept.

Determine how payment is submitted. A direct ERC-20 payment calls the token’s transfer function, while an invoice contract commonly pulls the funds with transferFrom after an allowance or permit. That difference determines whether you need a separate approval transaction, and which contract should receive it.

Convert the displayed amount using the token’s decimals. For example, an invoice for 120 USDT on a 6-decimal token requires 120,000,000 base units. Treat the invoice amount as exact: sending less can leave it unpaid, while sending extra may not increase the credited amount or be recoverable.

Swap the shortfall and preserve execution funds

Calculate the amount to acquire from your current balance, then include any payment-side amount the protocol specifies, such as a separate service charge. Keep ETH aside for the swap, approval if needed, and invoice call; an ERC-20 balance cannot pay Ethereum gas. The final gas charge depends on gas used and the block’s base fee plus priority fee, as Ethereum.org explains.

For example, if an invoice requires 120 USDT and your wallet has 35 USDT, the shortfall is 85 USDT before any separately stated charge. If your source asset is WBTC, the swap’s quoted output must cover that shortfall with a modest buffer for quote movement; swapping exactly 85 USDT worth can leave you underfunded if execution returns less. Fermi swap is a way to exchange tokens from your wallet, with trades filled from its own token inventory. Check that the quote’s output token is the invoice’s actual token, and compare expected output with the required amount before committing.

  1. Verify the invoice. Confirm chain ID, token contract, recipient, amount, expiry and invoice identifier from the protocol’s trusted source. This avoids paying a valid address on the wrong network or using a same-symbol token the invoice will not recognize.
  2. Check balances and estimate costs. Compare your token balance with the exact required amount, and reserve native ETH for all likely transactions. An approval plus a swap and payment can require multiple gas-paying calls; a permit-capable flow may combine authorization with a later call, but the token and invoice contract must support it.
  3. Swap only what is needed. Set the output token to the invoice’s verified contract and size the swap to cover the shortfall plus a small execution cushion. Choose slippage tolerance based on liquidity and volatility: a tighter setting limits price movement but can revert a changing quote, while a wider one can accept worse execution. Fermi swap may suit this task when its quoted inventory-backed output meets the required amount at acceptable execution cost.
  4. Authorize the payment contract if required. ERC-20’s approve sets an allowance for a spender; verify the spender is the invoice contract or the documented payment mechanism, and grant only the amount needed where practical. ERC-2612 defines signed permits with a nonce and deadline, but support is token-specific. The standards are described in the Ethereum Improvement Proposals ERC-20 and ERC-2612.
  5. Pay and verify settlement. Submit the invoice call before expiry, then confirm the transaction succeeded and the protocol marks that invoice identifier paid. A successful token transfer alone may not satisfy an invoice contract if the payment reference or required call was omitted.

A common mistake is to swap the displayed invoice amount without accounting for the balance already held, slippage, or gas; fix it by calculating the shortfall first and reserving ETH separately. Before acting, ask yourself: does this wallet hold enough of the exact required token on the exact chain to complete the invoice call and still pay its gas?

Top comments (0)