Gains Network
  • 📚Home
  • Gains Network
    • 🔎Overview
    • 🍏GNS Token
    • 📔Contract Addresses
      • Apechain Mainnet
      • Arbitrum Mainnet
      • Base Mainnet
      • Polygon Mainnet
  • gTrade (Leveraged Trading)
    • 🔎Overview
    • 🎛️Setting up to trade
      • 🟣Polygon Network Setup
      • 🔵Arbitrum Network Setup
    • 🏦Opening / Closing trades
    • 💲Trade Collaterals
      • Spotlight: WETH
    • 📖Asset classes
      • 🪙Cryptocurrencies
      • 💵Forex
      • 🛢️Commodities
    • 🎫Fees & Spread
    • 📰Pair List
    • 🧒How to use Practice Mode
    • ⚡One-Click Trading (1CT)
    • 🌌gTrade Solana
    • 📺Video tutorials
  • Loyalty Programs
    • Prime Trader
    • gTrade Credits
  • Community
    • gTrade Guardians
    • Governance forum
  • Reward Pools
    • 💚GNS Staking
    • 👛gToken Vaults
      • 👷Staker Functions
      • ❔Staker FAQ
      • ⏳Upgrades, updates, and timelocks
    • 🔼Active Pools & Incentives
  • Help
    • ❓FAQ
    • ⏳Chain congestion
  • Bridges
    • 🪐Arbitrum <-> Polygon Bridge
    • 🪐ERC20 Bridge (Polygon)
    • 🪐NFT Bridge (Polygon)
  • Developer
    • Integrators
      • Trading Contracts
      • Backend
      • Price Feed
      • Guides
        • v9 migration
        • Calculating borrowing fees
        • Calculating liquidation price
    • Technical Reference
      • Contracts
        • Changelogs
          • v9.2.1 Update
          • v9.1 Update
          • v9.1.1 Update
          • v9 Update
        • Core
          • Abstract
            • GNSAddressStore
            • GNSDiamondCut
            • GNSDiamondLoupe
            • GNSDiamondStorage
          • Facets
            • GNSBorrowingFees
            • GNSOtc
            • GNSPriceAggregator
            • GNSTradingInteractions
            • GNSTriggerRewards
            • GNSFeeTiers
            • GNSPairsStorage
            • GNSPriceImpact
            • GNSReferrals
            • GNSTradingCallbacks
            • GNSTradingStorage
          • GNSMultiCollatDiamond
          • GNSStaking
          • GToken
          • GTokenOpenPnlFeed
        • Interfaces
          • Libraries
            • IBorrowingFeesUtils
            • IOtcUtils
            • IPriceAggregatorUtils
            • ITriggerRewardsUtils
            • IFeeTiersUtils
            • IPairsStorageUtils
            • IPriceImpactUtils
            • IReferralsUtils
            • ITradingCallbacksUtils
            • ITradingCommonUtils
            • ITradingInteractionsUtils
            • ITradingStorageUtils
            • IUpdateLeverageUtils
            • IUpdatePositionSizeUtils
          • Types
            • IAddressStore
            • IBorrowingFees
            • IDiamondStorage
            • IOtc
            • IPriceAggregator
            • ITradingStorage
            • ITradingInteractions
            • ITriggerRewards
            • ITypes
            • IUpdateLeverage
            • IPairsStorage
            • IFeeTiers
            • IPriceImpact
            • IReferrals
            • ITradingCallbacks
            • IUpdatePositionSize
          • IChainlinkFeed
          • IArbSys
          • IChainlinkOracle
          • IERC20
          • IERC721
          • IERC721Design
          • IGeneralErrors
          • IGNSAddressStore
          • IGNSDiamond
          • IGNSDiamondCut
          • IGNSMultiCollatDiamond
          • IGNSDiamondLoupe
          • IGNSStaking
          • IGToken
          • IGTokenLockedDepositNft
          • IGTokenOpenPnlFeed
          • IGTokenLockedDepositNftDesign
          • ILiquidityPool
          • IRateProvider
          • IOwnable
        • Libraries
          • updateLeverage
            • UpdateLeverageLifecycles
          • updatePositionSize
            • IncreasePositionSizeUtils
            • DecreasePositionSizeUtils
            • UpdatePositionSizeLifecycles
          • AddressStoreUtils
          • ChainlinkClientUtils
          • ChainUtils
          • ConstantsUtils
          • CollateralUtils
          • DiamondUtils
          • LiquidityPoolUtils
          • OtcUtils
          • PackingUtils
          • StorageUtils
          • TriggerRewardsUtils
          • ArrayGetters
          • BorrowingFeesUtils
          • FeeTiersUtils
          • PriceAggregatorUtils
          • PriceImpactUtils
          • ReferralsUtils
          • TradingCallbacksUtils
          • PairsStorageUtils
          • TradingCommonUtils
          • TradingStorageUtils
          • TradingInteractionsUtils
        • Misc
          • EpochBasedTokenClaim
          • GNSCompensationHandler
          • GTokenRateProvider
          • GTokenLockedDepositNftDesign
          • ManagerTimelock
          • OwnerTimelock
          • VotingDelegator
        • Tokens
          • GainsNetworkToken
          • GTokenLockedDepositNft
      • Backend
        • Backend Types
      • SDK
        • Client Types
  • Archived
    • 🖼️gFARM2 NFTs
Powered by GitBook
On this page
  • Overview
  • Borrowing fee per block
  • Example:

Was this helpful?

  1. Developer
  2. Integrators
  3. Guides

Calculating borrowing fees

Learn how to calculate trader's borrowing fees

Previousv9 migrationNextCalculating liquidation price

Last updated 10 months ago

Was this helpful?

Overview

  • Borrowing fees are paid only by one side (either longs or shorts) which is decided by current open interest (higher OI pays the fee)

  • Each pair is part of a group, meaning that borrowing fee should be calculated for pair and pair's group and the higher of two should be charged (never both)

  • Borrowing fee is charged on trader's position size (collateral * leverage)

Borrowing fee per block

The formula for borrowing fee per block is:

feePerBlock * (abs(longOi - shortOi) / maxOi) ** feeExponent

Example:

Let's calculate the borrowing fee for ENA/USD pair (pairIndex is 219) against USDC collateral (collateralIndex is 3)

1) Fetching Open Interest and Fees

Access current open interests and fees for given collateral and pair index via endpoint described .

2) Calculating Pair Borrowing Fees

To calculate ENA/USD borrowing fees, we use the following object retrieved from Step 1:

tradingVariables.collaterals[2].borrowingFees.pairs[219] // collaterals[2] because its 0 indexed
{
    "oi": {
        "long": "228761980790000", // 1e10
        "short": "59904000000000", // 1e10
        "max": "8806660000000000" // 1e10
    },
    "feePerBlock": "100236", // 1e10
    "feeExponent": "1",
    "groups": [
        {
            "groupIndex": "2",
            ...
        }
    ],
    ...
}

With these values, we can calculate the pair's borrowing fee (per block):

feePerBlock = 0.0000100236
longOi = 22876.198079
shortOi = 5990.4
maxOi = 880666

pairFeePerBlock = 0.0000100236 * Math.abs(22876.198079 - 5990.4) / 880666 ** 1 = 1.9219146149012726e-7

3) Calculating Group Borrowing Fees

Now we need to do the same for group fee, reading the groupIndex from the same trading-variables response (for this example, groupIndex is 2). The process is the same as for calculating pair fees, using groups values of borrowingFees:

tradingVariables.collaterals[2].borrowingFees.groups[2]
{
    "oi": {
        "long": "7704464978990000", // 1e10
        "short": "1841270854980000", // 1e10
        "max": "50906510000000000" // 1e10
    },
    "feePerBlock": "16871", // 1e10
    "feeExponent": "1"
    ...
}

Therefore, the group's borrowing fee (per block) will be:

feePerBlock = 0.0000016871
longOi = 770446.497899
shortOi = 184127.085498
maxOi = 5090651

groupFeePerBlock = 0.0000016871 * Math.abs(770446.497899 - 184127.085498) / 5090651 ** 1 = 1.9431296324610092e-7

In this example, groupFeePairBlockis greater than pairFeePerBlok (1.9431296324610092e-7 > 1.9219146149012726e-7) so groupFeePairBlock is the effective feePerBlock.

4) Estimating Borrowing fee per hour

To estimate the hourly paid fee, we need to multiply the number of blocks per hour feePerBlock.

feePerHour = blocksPerHour * Math.max(pairFeePerBlock, groupFeePerBlock)

For this example, on Arbitrum (12000 blocks per hour), the estimated borrowing fee per hour is:

feePerHour = 12000 * Math.max(1.9219146149012726e-7, 1.9431296324610092e-7) // 0.002331755558953211% Borrowing (L) 

Which corresponds to gTrade's UI:

trading-variables
here