owned this note
owned this note
Published
Linked with GitHub
# Gains network fee
## Oracle fee
- Paid whenever the order is executed
- Paid in function `getPrice()` in `GNSPriceAggregatorV6_4.sol`
- Calculated by helper function `linkFee`, then distributed evenly amongst all participating oracle nodes

- Each pair has an oracle fee coefficient stored in `GNSPairsStorageV6.sol`, then multiplied of the leveraged position of the order, divided by the LINK price to get the total amount of LINK to pay.

- We cannot remove this fee, but maybe we can consider a fixed amount of LINK instead of the current dynamic one depending on the leveraged volume.
## Fixed spread
- Paid for open market/limit orders
- When the most recent price feed is submitted by the oracles through the function `fulfill()` in `GNSPriceAggregatorV6_4.sol`, the aggregator contract calls the function `openTradeMarketCallback` (for open market order) or the function `executeNftOpenOrderCallback` (for open limit order) in `GNSTradingCallbacksV6_4`.
- Calculated by the function `marketExecutionPrice` in `GNSTradingCallbacksV6_4`.

- The input `price` is the open price submitted by the oracles
- The input `spreadP` is stored in `GNSPairsStorageV6` and configured when the pair is added to the system by the governance.
- The input `spreadReductionP` is baded on NFT tier ownership.
- The price diff the then a percentage (`spreadP - spreadReductionP`) of the `price`, which is added to the `price` if the order is long or subtracted if the order is short. In both cases user's profit after closing the position is less by this amount.
## Dynamic spread
- Taken on top of the fixed spread so it appears in the same places.
- Calculated by the function `getTradePriceImpactPure()` in `GNSPairInfosV6_1`.

- The input `openPrice` is the price after considering fixed spread.
- The input `long` is true for long orders and false for short orders.
- The input `startOpenInterest` is the volume of all long (or short) trades for the pair. It is stored in `GFarmTradingStorage5`.
- The input `tradeOpenInterest` is the leveraged volume of the current order.
- The input `onePercentDepth` is from the trading activities from Binance and imported onchain to `GNSPairInfosV6_1` by the manager.
- The bigger the current direction volume, i.e. asset utilization, the higher the price impact. The lower the `onePercentDepth`, i.e. illiquid pair, the higher the price impact.
- Again the price impact is added to the `price` if the order is long or subtracted if the order is short. In both cases user's profit after closing the position is less by this amount.
## DevGovFee
- Paid for open market/limit orders
- Calculated in the function `registerTrade()` in `GNSTradingCallbacksV6_4`.

- Calculated by the function `handleDevGoveFees` in `GFarmTradingStorage5`.

- Taken as a percentage of the leveraged position. The percentage is pair specific and stored in `GNSPairsStorageV6`.
- The fees are claimed by the function `claimFees()` in `GFarmTradingStorage5`.
## Referral fee
- Paid for open market/limit orders
- Calculated in the function `registerTrade()` in `GNSTradingCallbacksV6_4`.

- The referral fee is a percentage of the open fee. This percentage is calculated by `getPercentOfOpenFeeP_calc()` in `GNSReferralsV6_2`.

- Where `openFeeP` and `startReferrerFeeP` are two fixed constant
- Note that this is taken from DevGovFee
## NFT fee
- Paid for limit orders with spread reduction NFT
- Calculated in the function `registerTrade(), executeNftCloseOrderCallback()` in `GNSTradingCallbacksV6_4`.

## Staking fee
- Paid for market orders or limit orders without spread reduction NFT
- Calculated in the function `registerTrade(), unregisterTrade()` in `GNSTradingCallbacksV6_4`.

## Borrowing fee
- Paid when closing a trade
- Called in `unregisterTrade()` in `GNSTradingCallbacksV6_4`.
- Calculated by `getTradeBorrowingFee` in `GNSBorrowingFeesV6_4`.

- We consider two percentages, pairBorrowingFee and groupBorrowingFee, then take the larger of the two.
## NFT Bot fee
- Paid for close limit order
- Called in `executeNftCloseOrderCallback` in `GNSTradingCallbacksV6_4`.

- If it's liquidation, then flat rate 5%. If it's closing the trade then the percentage is taken from `GNSPairsStorageV6`.
# Fees in Kwenta and adjustments for Gains system
## Gelato fees
- Can be replaced by NFT bot fee in the Gains system
## Taker and maker fees
- Seems to be taken when positions are created and based on the difference between the long and short open interest.
- If there is more short open interests then short traders will become takers and subjected to taker fee, long traders will become makers and subjected to maker fee.
- We can use this fee to replace fixed and dynamic spread in the gains system. The open interests are stored in variables "openInterestDai" in "GFarmTradingStorage5" and we will need to adjust the code in the function `fulfill()` in `GNSPriceAggregatorV6_4.sol`, i.e. when the oracle reports the current price used to open the position.
## Funding fee
- LV denotes the volume of long positions
- SV denotes the volume of short positions
- s denotes the skew and is defined as `s = LV - SV`
- f denotes the funding rate and is calculated as `df = dr/dt = c*skew`, normally we consider df as a daily rate (see example) where c is the constant factor, which in kwenta is set to 0.000003
- In practice, the effect of this change is that funding rates will continuously drift higher/lower in the presence of uncorrected position imbalances, creating a natural price discovery mechanism for funding rates while simultaneously smoothing out funding rate trajectories.
- Another notable change is that with this mechanism, LPs would no longer exclusively earn funding (e.g. short skew + positive funding, LPs pay funding).
- Instead, funding flows through LPs without gain or loss over time (i.e. can be mathematically shown that net funding earned by LPs over time is zero).
- In Gains network the volume of long positions and short positions are stored in the variables "openInterestDai" in "GFarmTradingStorage5" and are updated regularly.

- We will need to add variables to store the global skew, skew per short token and per long token.
- We will then need to use these variables and change the calculation of `getTradeBorrowingFee` in `GNSBorrowingFeesV6_4`.
- The fee will be charged when the position gets closed.
- We also need to adjust to liquidation logics to incorporate this fee to force liquidation when necessary.