Jump to content
Onchain Report

Crypto news from every chain

How Do You Calculate Net DEX Swap Output Tokens?

To calculate the net amount received from a DEX swap, total output-token transfers into your wallet and subtract same-token transfers out; record gas separately.

Onchain Report Newsroom#2b1e0b3 min read

How Do You Calculate Net DEX Swap Output Tokens?

To calculate the net amount received from a DEX swap, add the output-token transfers into the recipient wallet and subtract transfers of that token out during the same transaction. This gives the wallet’s actual net receipt; explorer rows can also include movements between pools and routers.

Count only output-token transfers across the recipient wallet boundary

Start with the confirmed swap transaction in the relevant network explorer. You need the transaction hash, the recipient wallet address and the output token’s contract—the address that identifies the token. Confirm the token contract and recipient carefully, especially when the transaction includes several tokens.

SyncSwap swap transactions can show pool and router movements beside the final wallet credit. For context on why those rows appear, read SyncSwap’s multiple rows and net trade, which explains how those movements relate to the trade.

In the transaction’s transfer rows, find entries for the output token. Add amounts whose destination is the recipient wallet. Then subtract amounts whose source is that wallet. ERC-20 Transfer events record token amounts moving between addresses, so use the event’s token contract and the from and to addresses to identify which rows count.

Say the recipient wallet gets 9.8 units of the output token and sends 0.1 of that same token during the transaction. Its net receipt is 9.7 units. Hold the transaction, recipient and output token constant, but change one detail: if the wallet sends none of the output token back out, its net receipt is 9.8 units. The outgoing transfer changes the result because it crosses the same wallet boundary in the opposite direction.

That boundary is the key to reading busy transaction details. A transfer from a pool to a router, or from one router address to another, is not an extra receipt for your wallet. Count it only if the recipient wallet is one end of the transfer. This avoids adding intermediate movements to the amount actually credited to the wallet.

Use the calculation: net output-token receipt = output-token transfers into recipient − output-token transfers out during the transaction. Report the result in the output token’s units. You can then compare that net amount with the swap quote, using the same token and units.

Keep gas separate from the output-token transfer calculation. Transaction gas is paid in the network’s gas token, so it reduces that balance rather than an ERC-20 output amount. If the gas token is also the swap’s output token, record the swap receipt and gas cost separately to make clear which amount you are reporting.

Related

How to read token transfers for a DEX swap

Read the token contract and sender and recipient addresses on each transfer row. Keep rows for the output token where the recipient wallet is the sender or destination, then add incoming amounts and subtract outgoing ones.

Does swap gas reduce the amount of tokens received?

Gas does not change the ERC-20 output amount shown by its transfer events. It is paid in the network’s gas token and should be noted separately; when that token is also the output token, the wallet’s overall balance change also reflects the gas cost.