How can I calculate the gas fee of a swap on Uniswap?

According to gas tracker on Etherscan the average gas price is 212 gwei and the average Uniswap swap estimate is \$70.26 which implies that the gas fee is around 201101.
To my knowledge swap gas fee is 21000 or so. What other operations are associated with a Uniswap swap that burn the extra gas?

To my knowledge swap gas fee is 21000 or so

A transfer between two EOAs (i.e. normal wallet accounts) is 21,000 gas.

As soon as you include smart contracts in the equation the gas cost increases: each opcode associated with each step in the execution of a smart contract incurs a cost. (As outlined in Appendix G of the Yellow Paper.)

Without actually digging into the opcodes associated with a Uniswap (v2) swap operation, just looking at an example transaction gives an idea that there's a lot going on when swapping from token A -> token B.

Generally it's a 3-part process (this is for the `swapExactTokensForTokens()` path):

1. User transfers token A to Uniswap where it's converted to wrapped ETH (WETH)
2. The WETH is swapped for token B
3. Token B is transferred to the user

However, if you look a bit deeper at the example above, this 3-part process actually involves 13 internal transactions (i.e. messages between contracts) - there's a lot going on.

(If you want to dig deeper, then the v2 contracts are here: https://github.com/Uniswap/uniswap-v2-core)

Gas fee is the product of the following:

• Number of gas units required
• Price per gas unit, in gwei
• Price of gwei in ETH (always a billionth of an Ether = 10e-9 ETH)
• Price of ETH in, say, USD

Let's take an example. We are moving ETH between two wallets in a standard way. As you point out, this is (always) 21,000 gas. Suppose the gas fee (number of gwei to buy one unit of gas) is 100 and the price of ETH is 4000.

``````USD price = 21,000 * 100 * 10e-9 * 4000 = \$8.4
``````

Now let's look at a more realistic example. From DeFi Dashboard I see the average gas price for UniSwap v2 is 159,709. From Ethereum Gas Charts the average price at this time of day for this day of the week is 134 gwei. The price of Ether is 4701.

``````USD price = 159,709 * 134 * 10e-9 * 4701 = \$100
``````