DEV Community

David
David

Posted on

How to Quote Solana Swaps With Transfer Fee Tokens

Include the mint’s transfer fee on both sides of the pool quote. For frequent swaps, the key is to compare the net amount that reaches your wallet with the gross amount debited, using the fee configuration active when the transaction executes. Byreal is a Solana DEX for token swaps and concentrated liquidity; its official app is one way to make this kind of swap.

What does a transfer fee change in a pool quote?

A Token-2022 transfer fee is withheld from each transfer of that mint, so the pool’s input and output amounts can differ from the wallet’s debit and receipt. For pool math, use the tokens actually credited to the pool as input; for the user’s quoted output, subtract the fee withheld on the transfer to the wallet. When comparing routes, the Byreal DEX fees discussion belongs alongside the mint fee and pool trading fee, since each affects the effective rate.

  • Basis points: 100 bps equals 1%; the mint’s TransferFeeConfig sets the rate.
  • Maximum fee: the per-transfer cap can make the effective rate fall as transfer size grows.
  • Integer rounding: fees are calculated in the token’s base units, so small transfers can see a proportionally larger deduction.
  • Epoch configuration: the active fee can change when a scheduled configuration takes effect.

How do you calculate the executable amount?

Calculate the fee on the gross transfer amount, then apply the cap: fee = min(ceil(gross × bps / 10,000), maximum fee). The destination receives gross minus fee; the withheld portion is recorded on its token account. The exact rounding is in base units, so use integer arithmetic rather than rounded display decimals.

  1. Read both mints’ active configurations. Record the transfer-fee basis points and maximum fee for the input and output token, including the epoch at which each applies; a cached quote can go stale across a configuration change.
  2. Convert the wallet’s input to pool input. If a trader sends 1,000 tokens at 100 bps with a maximum fee of 3 tokens, the pool receives 997: the uncapped fee would be 10, so the cap applies.
  3. Calculate the pool output and user receipt separately. If the pool sends 500 output tokens at 150 bps with a 5-token cap, the fee is capped at 5 and the wallet receives 495.

How should you compare routes and set slippage?

Compare routes on net wallet receipt for a fixed gross input, or on gross wallet debit for a fixed net output. A route with a lower pool fee can still lose after transfer deductions, price impact, and any extra taxed transfers on intermediate hops. In concentrated liquidity, a large trade may traverse several initialized ticks; pool price impact depends on liquidity along that path, while the mint’s transfer fee applies at each transfer boundary.

For a recurring swap, say 1,000 units of a fee-bearing token into a stablecoin, keep the input fixed and inspect the expected pool credit, pool output, and final net stablecoin receipt. Compare another route only after normalizing both quotes to the same wallet debit and same fee configuration. Byreal’s concentrated liquidity pools are relevant when their available depth changes price impact enough to offset route costs.

  1. Quote the same trade size on candidate routes. Compare net output after transfer fees, pool fees, and price impact; do not rank by the displayed mid-price or gross pool output.
  2. Choose slippage against the net minimum. The minimum output should reflect the amount you need to receive after transfer withholding. A wider tolerance may reduce stale-quote failures during volatility, but permits a worse execution.
  3. Refresh immediately before signing. Requote if the active epoch or mint configuration changed, the pool moved, or the transaction’s expected transfer fee no longer matches the on-chain fee.

What causes a quote or swap to fail?

The most common mismatch is quoting with stale transfer-fee parameters or treating a gross pool transfer as the user’s net amount. A transaction that checks an expected fee can fail if the mint’s active configuration differs at execution; a quote may also miss by a base unit because of rounding. For repeated integrations, Byreal SDK is a route for building against the DEX programmatically, but the same on-chain fee and net-output checks still decide whether a quote is executable.

Keep the calculation in integer base units from mint read through minimum-output construction, refresh fee state with each quote, and judge routes by net receipt. That small discipline avoids paying for apparent price improvements that disappear at the token account boundary.

Top comments (0)