# Fees

Understanding how fees are calculated.

## Overview

There are 4 different fees the user should know about.
1. 1.
Inbound Fee (sourceChain: gasRate * txSize)
2. 2.
Affiliate Fee (affiliateFee * swapAmount)
3. 3.
Liquidity Fee (swapSlip * swapAmount)
4. 4.
Outbound Fee (destinationChain: gasRate * txSize)

### Terms

• SourceChain: the chain the user is swapping from
• DestinationChain: the chain the user is swapping to txSize: the size of the transaction in bytes (or units)
• gasRate: the current gas rate of the external network
• swapAmount: the amount the user is swapping swapSlip: the slip created by the
• swapAmount, as a function of poolDepth
• affiliateFee: optional fee set by interface in basis points

## Fees Detail

### Inbound Fee

This is the fee the user pays to make a transaction on the source chain, which the user pays directly themselves. The gas rate recommended to use is fast where the tx is guaranteed to be committed in the next block. Any longer and the user will be waiting a long time for their swap and their price will be invalid (thus they may get an unnecessary refund).
$inboundFee = txSize * gasRate$
THORChain calculates and posts fee rates at https://thornode.ninerealms.com/thorchain/inbound_addresses
Always use a "fast" or "fastest" fee, if the transaction is not confirmed in time, it could be abandoned by the network or failed due to old prices. You should allow your users to cancel or re-try with higher fees.

### Liquidity Fee

This is simply the slip created by the transaction multiplied by its amount. It is priced and deducted from the destination amount automatically by the protocol.
$slip = \frac{swapAmount}{swapAmount + poolDepth}$
$fee =slip * swapAmount$

### Affiliate Fee

In the swap transaction you build for your users you can include an affiliate fee for your exchange (accepted in $RUNE or a synthetic asset, so you will need a$RUNE address).
• The affiliate fee is in basis points (0-10,000) and will be deducted from the inbound swap amount from the user.
• If the inbound swap asset is a native THORChain asset ($RUNE or synth) the affiliate fee amount will be deducted directly from the transaction amount. • If the inbound swap asset is on any other chain the network will submit a swap to$RUNE with the destination address as your affiliate fee address.
• If the affiliate is added to an ADDLP tx, then the affiliate is included in the network as an LP.
SWAP:CHAIN.ASSET:DESTINATION:LIMIT:AFFILIATE:FEE
$affliateFee = \frac{feeInBasisPoints * swapAmount}{10000}$

### Outbound Fee

This is the fee the Network pays on behalf of the user to send the outbound transaction. To adequately pay for network resources (TSS, compute, state storage) the fee is marked up to be 3 times the actual on-chain fee the nodes pay.
$outboundFee = txSize * gasRate * 3$
The minimum Outbound Layer1 Fee the network will charge is on /thorchain/mimir and is priced in USD (based on THORChain's USD pool prices). This means really cheap chains still pay their fair share. It is currently set to 100000000 = $1.00 See Outbound Fee for more information. ## Fee Ordering for Swaps Fees are taken in the following order when conducting a swap. 1. 1. Inbound Fee (user wallet controlled, not THORChain controlled) 2. 2. Affiliate Fee (if any) - skimmed from the input. 3. 3. Swap Fee (denoted in output asset) 4. 4. Outbound Fee (taken from the swap output) To work out the total fees, fees should be converted to a common asset (e.g. RUNE or USD) then added up. Total fees should be less than the input else it is likely to result in a refund. ### Refunds and Minimum Swappable Amount If a transaction fails, it is refunded, thus it will pay the outboundFee for the SourceChain not the DestinationChain. Thus devs should always swap an amount that is a maximum of the following, plus a buffer to allow for sudden gas spikes: 1. 1. The Destination Chain outbound_fee 2. 2. The Source Chain outbound_fee 3. 3.$1.00 (the minimum)
The outbound_fee for each chain is returned on the Inbound Addresses endpoint, priced in the gas asset.
So, to calculate the minimum swappable amount for a swap from ChainA.AssetA -> ChainB.AssetB:
1. 1.
Get the outbound_fees of both ChainA and ChainB from the Inbound Addresses Endpoint
2. 2.
Convert both outbound_fees to the swap's input asset (ChainA.AssetA) using the flat exchange rate (without slippage)
3. 3.
Take the maximum between those two values, and multiply by a healthy buffer to allow room for sudden gas spikes (at least 1.5)
Remember, if the swap limit is not met or the swap is otherwise refunded the outbound_fee of the Source Chain will be deducted from the input amount, so give your users enough room.

### Understanding gas_rate

THORNode keeps track of current gas prices. Access these at the /inbound_addresses endpoint of the THORNode API. The response is an array of objects like this:
{
"chain": "ETH",
"router": "0x3624525075b88B24ecc29CE226b0CEc1fFcB6976",
"halted": false,
"gas_rate": "10"
"gas_rate_units": "satsperbyte",
"outbound_fee": "30000",
"outbound_tx_size": "1000",
}
The gas_rate property can be used to estimate network fees for each chain the swap interacts with. For example, if the swap is BTC -> ETH the swap will incur fees on the bitcoin network and Ethereum network. The gas_rate property works differently on each chain "type" (e.g. EVM, UTXO, BFT).
The gas_rate_units explain what the rate is for chain, as a prompt to the developer.
The outbound_tx_size is what THORChain internally budgets as a typical transaction size for each chain.
The outbound_fee is gas_rate * outbound_tx_size * 3 and developers can use this to budget for the fee to be charged to the user.
Keep in mind the outbound_fee is priced in the gas asset of each chain. For chains with tokens, be sure to convert the outbound_fee to the outbound token to determine how much will be taken from the outbound amount. To do this, use the getValueOfAsset1InAsset2 formula described in the Math section.

## Fee Calculation by Chain

### THORChain (Native Rune)

The THORChain blockchain has a set 0.02 RUNE fee. This is set within the THORChain Constants by NativeTransactionFee. As THORChain is 1e8, 2000000 TOR = 0.02 RUNE

### Binance Chain

THORChain uses the gas_rate as the flat Binance Chain transaction fee.
E.g. If the gas_rate = 11250 then fee is 0.0011250 BNB.

### UTXO Chains like Bitcoin

For UXTO chains link Bitcoin, gas_rateis denoted in Satoshis. The gas_rate is calculated by looking at the average previous block fee seen by the THORNodes.
All THORChain transactions use BECH32 so a standard tx size of 250 bytes can be used. The standard UTXO fee is then gas_rate* 250.

### EVM Chains like Ethereum

For EVM chains like Ethereum, gas_rateis denoted in GWEI. The gas_rate is calculated by looking at the average previous block fee seen by the THORNodes
An Ether Tx fee is: gasRate * 10^9 (GWEI) * 21000 (units).
An ERC20 Tx is larger: gasRate * 10^9 (GWEI) * 70000 (units)
THORChain calculates and posts gas fee rates at https://thornode.ninerealms.com/thorchain/inbound_addresses