Perpetual Protocol 🥨

Posted on Dec 07, 2022Read on Mirror.xyz

How Perp Calculates Fees

If you’re familiar with Uniswap, then you’ll know that fees can be calculated in both quote and base tokens. In this article, we’ll explain why trading fees on Perp must be denominated in the quote token and how the clearing house contract calculates these fees.

Perp builds on top of Uniswap v3, using this architecture to facilitate the trading of derivatives whereby virtual token balances represent your positions (which you can learn more about in the article below.

https://perpprotocol.mirror.xyz/8ygBlS_2sJclQanTMpWTlOpeBKpA2fAP1HttLESDWCI

The major difference between Perp and Uniswap in terms of trading fees is that the latter has a configurable fee for each pool, with the factory contract allowing pools to be created with a fee of either 0.05%, 0.30% or 1%. On the other hand, takers trading on Perp pay a fee equivalent to 0.10% of their position size, no matter which pool they are using. 

Another distinction is that makers on Uniswap v3 can earn fees in both base and quote assets. For example, makers in the DAI-ETH pool can earn ETH (which is the quote asset) and DAI (the base asset) whenever traders swap these tokens, as the pool charges fees in the input token, that is the token the trader sells. However, the base token (e.g., ETH for the ETH-USD market) represents a trader’s position on Perp, so this same fee dynamic is not possible for makers. 

Fees are always denominated in USDC on Perp, so this is the only asset makers can only earn when takers use the liquidity they provide. As a result, there needs to be a way for the clearing house contract to calculate the fees in terms of the base token and then convert it to an equivalent amount in the quote token. Some clever accounting is used to ensure that fees are only quoted in terms of the base asset (USD), which is explained in the following sections. 

What is Quote Only Fee?

For a trade on Uniswap, the trader pays a fee denominated in their input currency. For example, when selling ETH through the DAI-ETH pool, you’ll pay the trading fee in ETH. On the other hand, if selling DAI for ETH, you’ll pay fees using DAI instead. 

When a trader sells ETH, a transaction fee of 0.30% is incurred, so if they’re selling 1 ETH, they’ll pay a fee equal to 0.003 ETH. This fee is then deducted from the amount of ETH that is actually used to exchange into DAI, resulting in (1 - 0.003) = 0.997 ETH being swapped. So if the price of ETH is 1,500 DAI, then the trader will receive (1500 * 0.997) = 1495.5 DAI once the swap is complete. 

What about if someone wants to short 1 ETH on Perp? While Perp builds on top of Uniswap v3’s pools, this scenario is slightly different, as the fee denominated in the base asset is converted into a quote-only fee. 

Trader Balances Using Quote Only Fee

The purpose of the quote-only fee in Perp is to scale the amount of the base or quote tokens by multiplying by 1/(1 - t) (where t represents the Uniswap pool fee), effectively ignoring the fee in base tokens. Then the returned quote or base asset amount is scaled down by multiplying by (1 - t) to reflect the trading fees strictly in the quote token.

What the clearing house does is to effectively mint additional vETH or vUSD to cover the pool fees, but then adjusts the amount when the trader opens a position to ensure they only pay 0.10% of their position size. 

Example: Short ETH

Assuming a trader wants to short 1 ETH, then the quote only fee can be calculated as follows:

The base token (vETH) amount is divided by the (1 - t) to get the amount that is added to the virtual token pool. In this example, we’ll use t = 0.30%. This results in 1 / (1 - 0.003) = 1.003 ETH being added to the vETH-vUSD pool. This amount is then multiplied by t to get the pool fee, which is equal to 1.003 * 0.003 = 0.003 ETH. 

The actual amount used in the trade for the quote asset (vUSD) is 1 vETH. Since the trader is going short, the clearing house first mints vETH using their collateral, then swaps this for vUSD in the Uniswap pool. From the Uniswap callback, the returned quote from the Uniswap pool would be 1500 vUSD. The transaction fee charged by the clearing house contract is equal to 1 ETH * 0.10% = 0.001 ETH = $1.50, assuming there’s no slippage and the price of ETH is $1,500. 

Note that in practice, the position size for the trader who went short would be slightly less than the amount they’d get from swapping on Uniswap because of slippage on the Perp’s automated market maker. 

The real amount being exchanged for vUSD is 1 vETH to ensure a balance between longs and shorts, where the taker pays 1 ETH while 1 vETH is added to the pool for makers. A transaction fee of 1.50 vUSD is deducted from the callback amount and recorded by the clearinghouse to achieve the effect of a quote-only fee. The trader then has a short position equal to approximately 1 ETH, where openNotional = 1498.50 (1500 - 1.50). 

With the transformation of base fees into quote fees, the clearing house essentially mints extra base tokens to cover the pool fee, then scales this amount down the amount when updating the trader’s balance. This means we can effectively ignore the Uniswap pool fees. Therefore, all fee calculations are done in Perp’s clearing house and are completely independent of Uniswap’s fees, avoiding excessive interactions with these pools. 

Example: Long ETH

Now let’s look at the scenario where the trader wants to long 1 ETH, as shown by the diagram below. 

First, the trader uses their collateral stored in the clearing house contract to mint 1500 vUSD tokens. The vUSD amount is scaled up by dividing by (1 - t) to get 1504.51, which is the amount added to the Uniswap v3 pool. 

But the real amount that is used to exchange vUSD for vETH is then scaled down by multiplying 1504.41 by (1 - t) to return 1500 vUSD, which is then exchanged for base tokens (i.e., vETH).  

With the price of ETH at $1500 and assuming no slippage, the return from the Uniswap callback will be 1.000006 vETH. But once accounting for the trading fee of 0.10% calculated by the clearing house, the trader ends up with a long position of 0.999006 vETH, where openNotional = -$1500. 

If you’re familiar with trading directly with the smart contract, the examples above show the situations where the parameter exactInput = false. Programmatic traders can set the parameter exactInput = true to receive an exact amount in vUSD or vETH. 

*More details can be found in our developer docs. *

For instance, this option can be used if you want to open a long position of exactly 1 vETH, rather than a position of 0.999006 vETH, as in the previous example. The fees are still calculated in the same way, but will be slightly different as in the examples presented above, since the position sizes in terms of ETH are specified rather than the USD amount. 

A key benefit of calculating fees through the clearing house contract is that Perp can customize the fees charged for each market outside of the default tiers found in Uniswap. As a result, there’s flexibility in configuring custom fees for stable-to-stable markets (such as USDC-USDT or ETH-stETH), where lower trading fees are more practical.

In summary, when a trader opens a position, virtual token amounts are scaled up to account for the Uniswap pool fee. These amounts are then scaled down so that fees can be denominated solely in the quote asset (vUSD). By offsetting the pool fees, the calculations presented here ensure that there’s a balance of longs and shorts between takers and makers, without having to give up the use of Uniswap’s architecture to support the trading of perpetual swaps. 

Links