> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gains.trade/llms.txt
> Use this file to discover all available pages before exploring further.

# BorrowingFeesUtils

*GNSBorrowingFees facet internal library*

## setBorrowingPairParams

```solidity theme={null}
function setBorrowingPairParams(uint8 _collateralIndex, uint16 _pairIndex, struct IBorrowingFees.BorrowingPairParams _value) internal
```

*Check IBorrowingFeesUtils interface for documentation*

## setBorrowingPairParamsArray

```solidity theme={null}
function setBorrowingPairParamsArray(uint8 _collateralIndex, uint16[] _indices, struct IBorrowingFees.BorrowingPairParams[] _values) internal
```

*Check IBorrowingFeesUtils interface for documentation*

## setBorrowingGroupParams

```solidity theme={null}
function setBorrowingGroupParams(uint8 _collateralIndex, uint16 _groupIndex, struct IBorrowingFees.BorrowingGroupParams _value) internal
```

*Check IBorrowingFeesUtils interface for documentation*

## setBorrowingGroupParamsArray

```solidity theme={null}
function setBorrowingGroupParamsArray(uint8 _collateralIndex, uint16[] _indices, struct IBorrowingFees.BorrowingGroupParams[] _values) internal
```

*Check IBorrowingFeesUtils interface for documentation*

## handleTradeBorrowingCallback

```solidity theme={null}
function handleTradeBorrowingCallback(uint8 _collateralIndex, address _trader, uint16 _pairIndex, uint32 _index, uint256 _positionSizeCollateral, bool _open, bool _long) internal
```

*Check IBorrowingFeesUtils interface for documentation*

## resetTradeBorrowingFees

```solidity theme={null}
function resetTradeBorrowingFees(uint8 _collateralIndex, address _trader, uint16 _pairIndex, uint32 _index, bool _long) internal
```

*Check IBorrowingFeesUtils interface for documentation*

## getBorrowingPairPendingAccFees

```solidity theme={null}
function getBorrowingPairPendingAccFees(uint8 _collateralIndex, uint16 _pairIndex, uint256 _currentBlock) internal view returns (uint64 accFeeLong, uint64 accFeeShort, uint64 pairAccFeeDelta)
```

*Check IBorrowingFeesUtils interface for documentation*

## getBorrowingGroupPendingAccFees

```solidity theme={null}
function getBorrowingGroupPendingAccFees(uint8 _collateralIndex, uint16 _groupIndex, uint256 _currentBlock) internal view returns (uint64 accFeeLong, uint64 accFeeShort, uint64 groupAccFeeDelta)
```

*Check IBorrowingFeesUtils interface for documentation*

## getTradeBorrowingFee

```solidity theme={null}
function getTradeBorrowingFee(struct IBorrowingFees.BorrowingFeeInput _input) internal view returns (uint256 feeAmountCollateral)
```

*Check IBorrowingFeesUtils interface for documentation*

## getTradeLiquidationPrice

```solidity theme={null}
function getTradeLiquidationPrice(struct IBorrowingFees.LiqPriceInput _input) internal view returns (uint256)
```

*Check IBorrowingFeesUtils interface for documentation*

## getPairOisCollateral

```solidity theme={null}
function getPairOisCollateral(uint8 _collateralIndex, uint16 _pairIndex) internal view returns (uint256 longOiCollateral, uint256 shortOiCollateral)
```

*Check IBorrowingFeesUtils interface for documentation*

## getBorrowingPairGroupIndex

```solidity theme={null}
function getBorrowingPairGroupIndex(uint8 _collateralIndex, uint16 _pairIndex) internal view returns (uint16 groupIndex)
```

*Check IBorrowingFeesUtils interface for documentation*

## getPairOiCollateral

```solidity theme={null}
function getPairOiCollateral(uint8 _collateralIndex, uint16 _pairIndex, bool _long) internal view returns (uint256)
```

*Check IBorrowingFeesUtils interface for documentation*

## withinMaxBorrowingGroupOi

```solidity theme={null}
function withinMaxBorrowingGroupOi(uint8 _collateralIndex, uint16 _pairIndex, bool _long, uint256 _positionSizeCollateral) internal view returns (bool)
```

*Check IBorrowingFeesUtils interface for documentation*

## getBorrowingGroup

```solidity theme={null}
function getBorrowingGroup(uint8 _collateralIndex, uint16 _groupIndex) internal view returns (struct IBorrowingFees.BorrowingData)
```

*Check IBorrowingFeesUtils interface for documentation*

## getBorrowingGroupOi

```solidity theme={null}
function getBorrowingGroupOi(uint8 _collateralIndex, uint16 _groupIndex) internal view returns (struct IBorrowingFees.OpenInterest)
```

*Check IBorrowingFeesUtils interface for documentation*

## getBorrowingPair

```solidity theme={null}
function getBorrowingPair(uint8 _collateralIndex, uint16 _pairIndex) internal view returns (struct IBorrowingFees.BorrowingData)
```

*Check IBorrowingFeesUtils interface for documentation*

## getBorrowingPairOi

```solidity theme={null}
function getBorrowingPairOi(uint8 _collateralIndex, uint16 _pairIndex) internal view returns (struct IBorrowingFees.OpenInterest)
```

*Check IBorrowingFeesUtils interface for documentation*

## getBorrowingPairGroups

```solidity theme={null}
function getBorrowingPairGroups(uint8 _collateralIndex, uint16 _pairIndex) internal view returns (struct IBorrowingFees.BorrowingPairGroup[])
```

*Check IBorrowingFeesUtils interface for documentation*

## getAllBorrowingPairs

```solidity theme={null}
function getAllBorrowingPairs(uint8 _collateralIndex) internal view returns (struct IBorrowingFees.BorrowingData[], struct IBorrowingFees.OpenInterest[], struct IBorrowingFees.BorrowingPairGroup[][])
```

*Check IBorrowingFeesUtils interface for documentation*

## getBorrowingGroups

```solidity theme={null}
function getBorrowingGroups(uint8 _collateralIndex, uint16[] _indices) internal view returns (struct IBorrowingFees.BorrowingData[], struct IBorrowingFees.OpenInterest[])
```

*Check IBorrowingFeesUtils interface for documentation*

## getBorrowingInitialAccFees

```solidity theme={null}
function getBorrowingInitialAccFees(uint8 _collateralIndex, address _trader, uint32 _index) internal view returns (struct IBorrowingFees.BorrowingInitialAccFees)
```

*Check IBorrowingFeesUtils interface for documentation*

## getPairMaxOi

```solidity theme={null}
function getPairMaxOi(uint8 _collateralIndex, uint16 _pairIndex) internal view returns (uint256)
```

*Check IBorrowingFeesUtils interface for documentation*

## getPairMaxOiCollateral

```solidity theme={null}
function getPairMaxOiCollateral(uint8 _collateralIndex, uint16 _pairIndex) internal view returns (uint256)
```

*Check IBorrowingFeesUtils interface for documentation*

## \_getSlot

```solidity theme={null}
function _getSlot() internal pure returns (uint256)
```

*Returns storage slot to use when fetching storage relevant to library*

## \_getStorage

```solidity theme={null}
function _getStorage() internal pure returns (struct IBorrowingFees.BorrowingFeesStorage s)
```

*Returns storage pointer for storage struct in diamond contract, at defined slot*

## \_getMultiCollatDiamond

```solidity theme={null}
function _getMultiCollatDiamond() internal view returns (contract IGNSMultiCollatDiamond)
```

*Returns current address as multi-collateral diamond interface to call other facets functions.*

## validCollateralIndex

```solidity theme={null}
modifier validCollateralIndex(uint8 _collateralIndex)
```

*Reverts if collateral index is not valid*

## \_getBorrowingPairPendingAccFee

```solidity theme={null}
function _getBorrowingPairPendingAccFee(uint8 _collateralIndex, uint16 _pairIndex, uint256 _currentBlock, bool _long) internal view returns (uint64 accFee)
```

*Returns pending acc borrowing fee for a pair on one side only*

### Parameters

| Name              | Type    | Description             |
| ----------------- | ------- | ----------------------- |
| \_collateralIndex | uint8   | index of the collateral |
| \_pairIndex       | uint16  | index of the pair       |
| \_currentBlock    | uint256 | current block number    |
| \_long            | bool    | true if long side       |

### Return Values

| Name   | Type   | Description                |
| ------ | ------ | -------------------------- |
| accFee | uint64 | new pair acc borrowing fee |

## \_getBorrowingGroupPendingAccFee

```solidity theme={null}
function _getBorrowingGroupPendingAccFee(uint8 _collateralIndex, uint16 _groupIndex, uint256 _currentBlock, bool _long) internal view returns (uint64 accFee)
```

*Returns pending acc borrowing fee for a borrowing group on one side only*

### Parameters

| Name              | Type    | Description                  |
| ----------------- | ------- | ---------------------------- |
| \_collateralIndex | uint8   | index of the collateral      |
| \_groupIndex      | uint16  | index of the borrowing group |
| \_currentBlock    | uint256 | current block number         |
| \_long            | bool    | true if long side            |

### Return Values

| Name   | Type   | Description                 |
| ------ | ------ | --------------------------- |
| accFee | uint64 | new group acc borrowing fee |

## \_getBorrowingPendingAccFees

```solidity theme={null}
function _getBorrowingPendingAccFees(struct IBorrowingFees.PendingBorrowingAccFeesInput _input) internal pure returns (uint64 newAccFeeLong, uint64 newAccFeeShort, uint64 delta)
```

*Pure function that returns the new acc borrowing fees and delta between two blocks (for pairs and groups)*

### Parameters

| Name    | Type                                                                                                                                                 | Description                                                         |
| ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------- |
| \_input | [IBorrowingFees.PendingBorrowingAccFeesInput](/developer/technical-reference/contracts/interfaces/types/iborrowingfees#pendingborrowingaccfeesinput) | input data (last acc fees, OIs, fee per block, current block, etc.) |

### Return Values

| Name           | Type   | Description                                                  |
| -------------- | ------ | ------------------------------------------------------------ |
| newAccFeeLong  | uint64 | new acc borrowing fee on long side                           |
| newAccFeeShort | uint64 | new acc borrowing fee on short side                          |
| delta          | uint64 | delta with current acc borrowing fee (for side that changed) |

## \_getTradeLiquidationPrice

```solidity theme={null}
function _getTradeLiquidationPrice(uint256 _openPrice, bool _long, uint256 _collateral, uint256 _leverage, uint256 _feesCollateral, uint256 _collateralPrecisionDelta, struct IPairsStorage.GroupLiquidationParams _liquidationParams, enum ITradingStorage.ContractsVersion _contractsVersion, uint256 _pairSpreadP) internal pure returns (uint256)
```

*Pure function that returns the liquidation price for a trade (1e10 precision)*

### Parameters

| Name                       | Type                                                                                                                                   | Description                                                 |
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- |
| \_openPrice                | uint256                                                                                                                                | trade open price (1e10 precision)                           |
| \_long                     | bool                                                                                                                                   | true if long, false if short                                |
| \_collateral               | uint256                                                                                                                                | trade collateral (collateral precision)                     |
| \_leverage                 | uint256                                                                                                                                | trade leverage (1e3 precision)                              |
| \_feesCollateral           | uint256                                                                                                                                | closing fees + borrowing fees amount (collateral precision) |
| \_collateralPrecisionDelta | uint256                                                                                                                                | collateral precision delta (10^18/10^decimals)              |
| \_liquidationParams        | [IPairsStorage.GroupLiquidationParams](/developer/technical-reference/contracts/interfaces/types/ipairsstorage#groupliquidationparams) | liquidation parameters for the trade                        |
| \_contractsVersion         | enum ITradingStorage.ContractsVersion                                                                                                  | contracts version of the trade                              |
| \_pairSpreadP              | uint256                                                                                                                                | pair spread percentage (1e10)                               |

## \_setBorrowingPairParams

```solidity theme={null}
function _setBorrowingPairParams(uint8 _collateralIndex, uint16 _pairIndex, struct IBorrowingFees.BorrowingPairParams _value) internal
```

*Function to set borrowing pair params*

### Parameters

| Name              | Type                                                                                                                               | Description             |
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------------- | ----------------------- |
| \_collateralIndex | uint8                                                                                                                              | index of the collateral |
| \_pairIndex       | uint16                                                                                                                             | index of the pair       |
| \_value           | [IBorrowingFees.BorrowingPairParams](/developer/technical-reference/contracts/interfaces/types/iborrowingfees#borrowingpairparams) | new pair params         |

## \_setBorrowingGroupParams

```solidity theme={null}
function _setBorrowingGroupParams(uint8 _collateralIndex, uint16 _groupIndex, struct IBorrowingFees.BorrowingGroupParams _value) internal
```

*Function to set borrowing group params*

### Parameters

| Name              | Type                                                                                                                                 | Description                  |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------- |
| \_collateralIndex | uint8                                                                                                                                | index of the collateral      |
| \_groupIndex      | uint16                                                                                                                               | index of the borrowing group |
| \_value           | [IBorrowingFees.BorrowingGroupParams](/developer/technical-reference/contracts/interfaces/types/iborrowingfees#borrowinggroupparams) | new group params             |

## \_updateOi

```solidity theme={null}
function _updateOi(struct IBorrowingFees.OpenInterest _oiStorage, bool _long, bool _increase, uint256 _amountCollateral, uint128 _collateralPrecision) internal returns (uint72 newOiLong, uint72 newOiShort, uint72 delta)
```

*Function to update a borrowing pair/group open interest*

### Parameters

| Name                  | Type                                                                                                                 | Description                                                      |
| --------------------- | -------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------- |
| \_oiStorage           | [IBorrowingFees.OpenInterest](/developer/technical-reference/contracts/interfaces/types/iborrowingfees#openinterest) | open interest storage reference                                  |
| \_long                | bool                                                                                                                 | true if long, false if short                                     |
| \_increase            | bool                                                                                                                 | true if increase, false if decrease                              |
| \_amountCollateral    | uint256                                                                                                              | amount of collateral to increase/decrease (collateral precision) |
| \_collateralPrecision | uint128                                                                                                              | collateral precision (10^decimals)                               |

### Return Values

| Name       | Type   | Description                                             |
| ---------- | ------ | ------------------------------------------------------- |
| newOiLong  | uint72 | new long open interest (1e10)                           |
| newOiShort | uint72 | new short open interest (1e10)                          |
| delta      | uint72 | difference between new and current open interest (1e10) |

## \_updatePairOi

```solidity theme={null}
function _updatePairOi(uint8 _collateralIndex, uint16 _pairIndex, bool _long, bool _increase, uint256 _amountCollateral) internal
```

*Function to update a borrowing group's open interest*

### Parameters

| Name               | Type    | Description                                                      |
| ------------------ | ------- | ---------------------------------------------------------------- |
| \_collateralIndex  | uint8   | index of the collateral                                          |
| \_pairIndex        | uint16  | index of the borrowing group                                     |
| \_long             | bool    | true if long, false if short                                     |
| \_increase         | bool    | true if increase, false if decrease                              |
| \_amountCollateral | uint256 | amount of collateral to increase/decrease (collateral precision) |

## \_updateGroupOi

```solidity theme={null}
function _updateGroupOi(uint8 _collateralIndex, uint16 _groupIndex, bool _long, bool _increase, uint256 _amountCollateral) internal
```

*Function to update a borrowing group's open interest*

### Parameters

| Name               | Type    | Description                                                      |
| ------------------ | ------- | ---------------------------------------------------------------- |
| \_collateralIndex  | uint8   | index of the collateral                                          |
| \_groupIndex       | uint16  | index of the borrowing group                                     |
| \_long             | bool    | true if long, false if short                                     |
| \_increase         | bool    | true if increase, false if decrease                              |
| \_amountCollateral | uint256 | amount of collateral to increase/decrease (collateral precision) |

## \_getBorrowingPairGroupAccFeesDeltas

```solidity theme={null}
function _getBorrowingPairGroupAccFeesDeltas(uint8 _collateralIndex, uint256 _i, struct IBorrowingFees.BorrowingPairGroup[] _pairGroups, struct IBorrowingFees.BorrowingInitialAccFees _initialFees, uint16 _pairIndex, bool _long, uint256 _currentBlock) internal view returns (uint64 deltaGroup, uint64 deltaPair, bool beforeTradeOpen)
```

\_Calculates the borrowing group and pair acc fees deltas for a trade between pair group at index *i and next one*

### Parameters

| Name              | Type                                                                                                                                       | Description                            |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------- |
| \_collateralIndex | uint8                                                                                                                                      | index of the collateral                |
| \_i               | uint256                                                                                                                                    | index of the borrowing pair group      |
| \_pairGroups      | [IBorrowingFees.BorrowingPairGroup\[\]](/developer/technical-reference/contracts/interfaces/types/iborrowingfees#borrowingpairgroup)       | all pair's historical borrowing groups |
| \_initialFees     | [IBorrowingFees.BorrowingInitialAccFees](/developer/technical-reference/contracts/interfaces/types/iborrowingfees#borrowinginitialaccfees) | trade initial borrowing fees           |
| \_pairIndex       | uint16                                                                                                                                     | index of the pair                      |
| \_long            | bool                                                                                                                                       | true if long, false if short           |
| \_currentBlock    | uint256                                                                                                                                    | current block number                   |

### Return Values

| Name            | Type   | Description                                                |
| --------------- | ------ | ---------------------------------------------------------- |
| deltaGroup      | uint64 | difference between new and current group acc borrowing fee |
| deltaPair       | uint64 | difference between new and current pair acc borrowing fee  |
| beforeTradeOpen | bool   | true if pair group was set before trade was opened         |

## \_setPairPendingAccFees

```solidity theme={null}
function _setPairPendingAccFees(uint8 _collateralIndex, uint16 _pairIndex, uint256 _currentBlock) internal returns (uint64 accFeeLong, uint64 accFeeShort)
```

### Parameters

| Name              | Type    | Description             |
| ----------------- | ------- | ----------------------- |
| \_collateralIndex | uint8   | index of the collateral |
| \_pairIndex       | uint16  | index of the pair       |
| \_currentBlock    | uint256 | current block number    |

### Return Values

| Name        | Type   | Description                                               |
| ----------- | ------ | --------------------------------------------------------- |
| accFeeLong  | uint64 | new pair acc borrowing fee on long side (1e10 precision)  |
| accFeeShort | uint64 | new pair acc borrowing fee on short side (1e10 precision) |

## \_setGroupPendingAccFees

```solidity theme={null}
function _setGroupPendingAccFees(uint8 _collateralIndex, uint16 _groupIndex, uint256 _currentBlock) internal returns (uint64 accFeeLong, uint64 accFeeShort)
```

### Parameters

| Name              | Type    | Description                  |
| ----------------- | ------- | ---------------------------- |
| \_collateralIndex | uint8   | index of the collateral      |
| \_groupIndex      | uint16  | index of the borrowing group |
| \_currentBlock    | uint256 | current block number         |

### Return Values

| Name        | Type   | Description                                                |
| ----------- | ------ | ---------------------------------------------------------- |
| accFeeLong  | uint64 | new group acc borrowing fee on long side (1e10 precision)  |
| accFeeShort | uint64 | new group acc borrowing fee on short side (1e10 precision) |
