# IBorrowingFeesUtils

*Interface for GNSBorrowingFees facet (inherits types and also contains functions, events, and custom errors)*

## setBorrowingPairParams

```solidity
function setBorrowingPairParams(uint8 _collateralIndex, uint16 _pairIndex, struct IBorrowingFees.BorrowingPairParams _value) external
```

*Updates borrowing pair params of a pair*

### 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.md#borrowingpairparams) | new value               |

## setBorrowingPairParamsArray

```solidity
function setBorrowingPairParamsArray(uint8 _collateralIndex, uint16[] _indices, struct IBorrowingFees.BorrowingPairParams[] _values) external
```

*Updates borrowing pair params of multiple pairs*

### Parameters

| Name              | Type                                                                                                                                      | Description             |
| ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | ----------------------- |
| \_collateralIndex | uint8                                                                                                                                     | index of the collateral |
| \_indices         | uint16\[]                                                                                                                                 | indices of the pairs    |
| \_values          | [IBorrowingFees.BorrowingPairParams\[\]](/developer/technical-reference/contracts/interfaces/types/iborrowingfees.md#borrowingpairparams) | new values              |

## setBorrowingGroupParams

```solidity
function setBorrowingGroupParams(uint8 _collateralIndex, uint16 _groupIndex, struct IBorrowingFees.BorrowingGroupParams _value) external
```

*Updates borrowing group params of a group*

### 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.md#borrowinggroupparams) | new value                    |

## setBorrowingGroupParamsArray

```solidity
function setBorrowingGroupParamsArray(uint8 _collateralIndex, uint16[] _indices, struct IBorrowingFees.BorrowingGroupParams[] _values) external
```

*Updates borrowing group params of multiple groups*

### Parameters

| Name              | Type                                                                                                                                        | Description             |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------- |
| \_collateralIndex | uint8                                                                                                                                       | index of the collateral |
| \_indices         | uint16\[]                                                                                                                                   | indices of the groups   |
| \_values          | [IBorrowingFees.BorrowingGroupParams\[\]](/developer/technical-reference/contracts/interfaces/types/iborrowingfees.md#borrowinggroupparams) | new values              |

## handleTradeBorrowingCallback

```solidity
function handleTradeBorrowingCallback(uint8 _collateralIndex, address _trader, uint16 _pairIndex, uint32 _index, uint256 _positionSizeCollateral, bool _open, bool _long) external
```

*Callback after a trade is opened/closed to store pending borrowing fees and adjust open interests*

### Parameters

| Name                     | Type    | Description                                                   |
| ------------------------ | ------- | ------------------------------------------------------------- |
| \_collateralIndex        | uint8   | index of the collateral                                       |
| \_trader                 | address | address of the trader                                         |
| \_pairIndex              | uint16  | index of the pair                                             |
| \_index                  | uint32  | index of the trade                                            |
| \_positionSizeCollateral | uint256 | position size of the trade in collateral tokens               |
| \_open                   | bool    | true if trade has been opened, false if trade has been closed |
| \_long                   | bool    | true if trade is long, false if trade is short                |

## resetTradeBorrowingFees

```solidity
function resetTradeBorrowingFees(uint8 _collateralIndex, address _trader, uint16 _pairIndex, uint32 _index, bool _long) external
```

*Resets a trade borrowing fee to 0 (useful when new trade opened or when partial trade executed)*

### Parameters

| Name              | Type    | Description                                    |
| ----------------- | ------- | ---------------------------------------------- |
| \_collateralIndex | uint8   | index of the collateral                        |
| \_trader          | address | address of the trader                          |
| \_pairIndex       | uint16  | index of the pair                              |
| \_index           | uint32  | index of the trade                             |
| \_long            | bool    | true if trade is long, false if trade is short |

## getBorrowingPairPendingAccFees

```solidity
function getBorrowingPairPendingAccFees(uint8 _collateralIndex, uint16 _pairIndex, uint256 _currentBlock) external view returns (uint64 accFeeLong, uint64 accFeeShort, uint64 pairAccFeeDelta)
```

*Returns the pending acc borrowing fees for a pair on both sides*

### 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              |
| accFeeShort     | uint64 | new pair acc borrowing fee on short side             |
| pairAccFeeDelta | uint64 | pair acc borrowing fee delta (for side that changed) |

## getBorrowingGroupPendingAccFees

```solidity
function getBorrowingGroupPendingAccFees(uint8 _collateralIndex, uint16 _groupIndex, uint256 _currentBlock) external view returns (uint64 accFeeLong, uint64 accFeeShort, uint64 groupAccFeeDelta)
```

*Returns the pending acc borrowing fees for a borrowing group on both sides*

### 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              |
| accFeeShort      | uint64 | new group acc borrowing fee on short side             |
| groupAccFeeDelta | uint64 | group acc borrowing fee delta (for side that changed) |

## getTradeBorrowingFee

```solidity
function getTradeBorrowingFee(struct IBorrowingFees.BorrowingFeeInput _input) external view returns (uint256 feeAmountCollateral)
```

*Returns the borrowing fee for a trade*

### Parameters

| Name    | Type                                                                                                                              | Description                                                                        |
| ------- | --------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
| \_input | [IBorrowingFees.BorrowingFeeInput](/developer/technical-reference/contracts/interfaces/types/iborrowingfees.md#borrowingfeeinput) | input data (collateralIndex, trader, pairIndex, index, long, collateral, leverage) |

### Return Values

| Name                | Type    | Description                          |
| ------------------- | ------- | ------------------------------------ |
| feeAmountCollateral | uint256 | borrowing fee (collateral precision) |

## getTradeLiquidationPrice

```solidity
function getTradeLiquidationPrice(struct IBorrowingFees.LiqPriceInput _input) external view returns (uint256)
```

*Returns the liquidation price for a trade*

### Parameters

| Name    | Type                                                                                                                      | Description                                                                                   |
| ------- | ------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
| \_input | [IBorrowingFees.LiqPriceInput](/developer/technical-reference/contracts/interfaces/types/iborrowingfees.md#liqpriceinput) | input data (collateralIndex, trader, pairIndex, index, openPrice, long, collateral, leverage) |

## getPairOisCollateral

```solidity
function getPairOisCollateral(uint8 _collateralIndex, uint16 _pairIndex) external view returns (uint256 longOi, uint256 shortOi)
```

*Returns the open interests for a pair*

### Parameters

| Name              | Type   | Description             |
| ----------------- | ------ | ----------------------- |
| \_collateralIndex | uint8  | index of the collateral |
| \_pairIndex       | uint16 | index of the pair       |

### Return Values

| Name    | Type    | Description                 |
| ------- | ------- | --------------------------- |
| longOi  | uint256 | open interest on long side  |
| shortOi | uint256 | open interest on short side |

## getBorrowingPairGroupIndex

```solidity
function getBorrowingPairGroupIndex(uint8 _collateralIndex, uint16 _pairIndex) external view returns (uint16 groupIndex)
```

*Returns the borrowing group index for a pair*

### Parameters

| Name              | Type   | Description             |
| ----------------- | ------ | ----------------------- |
| \_collateralIndex | uint8  | index of the collateral |
| \_pairIndex       | uint16 | index of the pair       |

### Return Values

| Name       | Type   | Description           |
| ---------- | ------ | --------------------- |
| groupIndex | uint16 | borrowing group index |

## getPairOiCollateral

```solidity
function getPairOiCollateral(uint8 _collateralIndex, uint16 _pairIndex, bool _long) external view returns (uint256)
```

*Returns the open interest in collateral tokens for a pair on one side*

### Parameters

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

## withinMaxBorrowingGroupOi

```solidity
function withinMaxBorrowingGroupOi(uint8 _collateralIndex, uint16 _pairIndex, bool _long, uint256 _positionSizeCollateral) external view returns (bool)
```

*Returns whether a trade is within the max group borrowing open interest*

### Parameters

| Name                     | Type    | Description                                     |
| ------------------------ | ------- | ----------------------------------------------- |
| \_collateralIndex        | uint8   | index of the collateral                         |
| \_pairIndex              | uint16  | index of the pair                               |
| \_long                   | bool    | true if long side                               |
| \_positionSizeCollateral | uint256 | position size of the trade in collateral tokens |

## getBorrowingGroup

```solidity
function getBorrowingGroup(uint8 _collateralIndex, uint16 _groupIndex) external view returns (struct IBorrowingFees.BorrowingData group)
```

*Returns a borrowing group's data*

### Parameters

| Name              | Type   | Description                  |
| ----------------- | ------ | ---------------------------- |
| \_collateralIndex | uint8  | index of the collateral      |
| \_groupIndex      | uint16 | index of the borrowing group |

## getBorrowingGroupOi

```solidity
function getBorrowingGroupOi(uint8 _collateralIndex, uint16 _groupIndex) external view returns (struct IBorrowingFees.OpenInterest group)
```

*Returns a borrowing group's oi data*

### Parameters

| Name              | Type   | Description                  |
| ----------------- | ------ | ---------------------------- |
| \_collateralIndex | uint8  | index of the collateral      |
| \_groupIndex      | uint16 | index of the borrowing group |

## getBorrowingPair

```solidity
function getBorrowingPair(uint8 _collateralIndex, uint16 _pairIndex) external view returns (struct IBorrowingFees.BorrowingData)
```

*Returns a borrowing pair's data*

### Parameters

| Name              | Type   | Description             |
| ----------------- | ------ | ----------------------- |
| \_collateralIndex | uint8  | index of the collateral |
| \_pairIndex       | uint16 | index of the pair       |

## getBorrowingPairOi

```solidity
function getBorrowingPairOi(uint8 _collateralIndex, uint16 _pairIndex) external view returns (struct IBorrowingFees.OpenInterest)
```

*Returns a borrowing pair's oi data*

### Parameters

| Name              | Type   | Description             |
| ----------------- | ------ | ----------------------- |
| \_collateralIndex | uint8  | index of the collateral |
| \_pairIndex       | uint16 | index of the pair       |

## getBorrowingPairGroups

```solidity
function getBorrowingPairGroups(uint8 _collateralIndex, uint16 _pairIndex) external view returns (struct IBorrowingFees.BorrowingPairGroup[])
```

*Returns a borrowing pair's oi data*

### Parameters

| Name              | Type   | Description             |
| ----------------- | ------ | ----------------------- |
| \_collateralIndex | uint8  | index of the collateral |
| \_pairIndex       | uint16 | index of the pair       |

## getAllBorrowingPairs

```solidity
function getAllBorrowingPairs(uint8 _collateralIndex) external view returns (struct IBorrowingFees.BorrowingData[], struct IBorrowingFees.OpenInterest[], struct IBorrowingFees.BorrowingPairGroup[][])
```

*Returns all borrowing pairs' borrowing data, oi data, and pair groups data*

### Parameters

| Name              | Type  | Description             |
| ----------------- | ----- | ----------------------- |
| \_collateralIndex | uint8 | index of the collateral |

## getBorrowingGroups

```solidity
function getBorrowingGroups(uint8 _collateralIndex, uint16[] _indices) external view returns (struct IBorrowingFees.BorrowingData[], struct IBorrowingFees.OpenInterest[])
```

*Returns borrowing groups' data and oi data*

### Parameters

| Name              | Type      | Description             |
| ----------------- | --------- | ----------------------- |
| \_collateralIndex | uint8     | index of the collateral |
| \_indices         | uint16\[] | indices of the groups   |

## getBorrowingInitialAccFees

```solidity
function getBorrowingInitialAccFees(uint8 _collateralIndex, address _trader, uint32 _index) external view returns (struct IBorrowingFees.BorrowingInitialAccFees)
```

*Returns borrowing groups' data*

### Parameters

| Name              | Type    | Description             |
| ----------------- | ------- | ----------------------- |
| \_collateralIndex | uint8   | index of the collateral |
| \_trader          | address | address of trader       |
| \_index           | uint32  | index of trade          |

## getPairMaxOi

```solidity
function getPairMaxOi(uint8 _collateralIndex, uint16 _pairIndex) external view returns (uint256)
```

*Returns the max open interest for a pair*

### Parameters

| Name              | Type   | Description             |
| ----------------- | ------ | ----------------------- |
| \_collateralIndex | uint8  | index of the collateral |
| \_pairIndex       | uint16 | index of the pair       |

## getPairMaxOiCollateral

```solidity
function getPairMaxOiCollateral(uint8 _collateralIndex, uint16 _pairIndex) external view returns (uint256)
```

*Returns the max open interest in collateral tokens for a pair*

### Parameters

| Name              | Type   | Description             |
| ----------------- | ------ | ----------------------- |
| \_collateralIndex | uint8  | index of the collateral |
| \_pairIndex       | uint16 | index of the pair       |

## BorrowingPairParamsUpdated

```solidity
event BorrowingPairParamsUpdated(uint8 collateralIndex, uint16 pairIndex, uint16 groupIndex, uint32 feePerBlock, uint48 feeExponent, uint72 maxOi)
```

*Emitted when a pair's borrowing params is updated*

### Parameters

| Name            | Type   | Description            |
| --------------- | ------ | ---------------------- |
| collateralIndex | uint8  |                        |
| pairIndex       | uint16 | index of the pair      |
| groupIndex      | uint16 | index of its new group |
| feePerBlock     | uint32 | new fee per block      |
| feeExponent     | uint48 | new fee exponent       |
| maxOi           | uint72 | new max open interest  |

## BorrowingPairGroupUpdated

```solidity
event BorrowingPairGroupUpdated(uint8 collateralIndex, uint16 pairIndex, uint16 prevGroupIndex, uint16 newGroupIndex)
```

*Emitted when a pair's borrowing group has been updated*

### Parameters

| Name            | Type   | Description                    |
| --------------- | ------ | ------------------------------ |
| collateralIndex | uint8  |                                |
| pairIndex       | uint16 | index of the pair              |
| prevGroupIndex  | uint16 | previous borrowing group index |
| newGroupIndex   | uint16 | new borrowing group index      |

## BorrowingGroupUpdated

```solidity
event BorrowingGroupUpdated(uint8 collateralIndex, uint16 groupIndex, uint32 feePerBlock, uint72 maxOi, uint48 feeExponent)
```

*Emitted when a group's borrowing params is updated*

### Parameters

| Name            | Type   | Description           |
| --------------- | ------ | --------------------- |
| collateralIndex | uint8  |                       |
| groupIndex      | uint16 | index of the group    |
| feePerBlock     | uint32 | new fee per block     |
| maxOi           | uint72 | new max open interest |
| feeExponent     | uint48 | new fee exponent      |

## BorrowingInitialAccFeesStored

```solidity
event BorrowingInitialAccFeesStored(uint8 collateralIndex, address trader, uint16 pairIndex, uint32 index, bool long, uint64 initialPairAccFee, uint64 initialGroupAccFee)
```

*Emitted when a trade's initial acc borrowing fees are stored*

### Parameters

| Name               | Type    | Description                                       |
| ------------------ | ------- | ------------------------------------------------- |
| collateralIndex    | uint8   |                                                   |
| trader             | address | address of the trader                             |
| pairIndex          | uint16  | index of the pair                                 |
| index              | uint32  | index of the trade                                |
| long               | bool    |                                                   |
| initialPairAccFee  | uint64  | initial pair acc fee (for the side of the trade)  |
| initialGroupAccFee | uint64  | initial group acc fee (for the side of the trade) |

## TradeBorrowingCallbackHandled

```solidity
event TradeBorrowingCallbackHandled(uint8 collateralIndex, address trader, uint16 pairIndex, uint32 index, bool open, bool long, uint256 positionSizeCollateral)
```

*Emitted when a trade is executed and borrowing callback is handled*

### Parameters

| Name                   | Type    | Description                                                   |
| ---------------------- | ------- | ------------------------------------------------------------- |
| collateralIndex        | uint8   |                                                               |
| trader                 | address | address of the trader                                         |
| pairIndex              | uint16  | index of the pair                                             |
| index                  | uint32  | index of the trade                                            |
| open                   | bool    | true if trade has been opened, false if trade has been closed |
| long                   | bool    | true if trade is long, false if trade is short                |
| positionSizeCollateral | uint256 | position size of the trade in collateral tokens               |

## BorrowingPairAccFeesUpdated

```solidity
event BorrowingPairAccFeesUpdated(uint8 collateralIndex, uint16 pairIndex, uint256 currentBlock, uint64 accFeeLong, uint64 accFeeShort)
```

*Emitted when a pair's borrowing acc fees are updated*

### Parameters

| Name            | Type    | Description                              |
| --------------- | ------- | ---------------------------------------- |
| collateralIndex | uint8   |                                          |
| pairIndex       | uint16  | index of the pair                        |
| currentBlock    | uint256 | current block number                     |
| accFeeLong      | uint64  | new pair acc borrowing fee on long side  |
| accFeeShort     | uint64  | new pair acc borrowing fee on short side |

## BorrowingGroupAccFeesUpdated

```solidity
event BorrowingGroupAccFeesUpdated(uint8 collateralIndex, uint16 groupIndex, uint256 currentBlock, uint64 accFeeLong, uint64 accFeeShort)
```

*Emitted when a group's borrowing acc fees are updated*

### Parameters

| Name            | Type    | Description                               |
| --------------- | ------- | ----------------------------------------- |
| collateralIndex | uint8   |                                           |
| groupIndex      | uint16  | index of the borrowing group              |
| currentBlock    | uint256 | current block number                      |
| accFeeLong      | uint64  | new group acc borrowing fee on long side  |
| accFeeShort     | uint64  | new group acc borrowing fee on short side |

## BorrowingPairOiUpdated

```solidity
event BorrowingPairOiUpdated(uint8 collateralIndex, uint16 pairIndex, bool long, bool increase, uint72 delta, uint72 newOiLong, uint72 newOiShort)
```

*Emitted when a borrowing pair's open interests are updated*

### Parameters

| Name            | Type   | Description                                                   |
| --------------- | ------ | ------------------------------------------------------------- |
| collateralIndex | uint8  |                                                               |
| pairIndex       | uint16 | index of the pair                                             |
| long            | bool   | true if long side                                             |
| increase        | bool   | true if open interest is increased, false if decreased        |
| delta           | uint72 | change in open interest in collateral tokens (1e10 precision) |
| newOiLong       | uint72 | new open interest on long side                                |
| newOiShort      | uint72 | new open interest on short side                               |

## BorrowingGroupOiUpdated

```solidity
event BorrowingGroupOiUpdated(uint8 collateralIndex, uint16 groupIndex, bool long, bool increase, uint72 delta, uint72 newOiLong, uint72 newOiShort)
```

*Emitted when a borrowing group's open interests are updated*

### Parameters

| Name            | Type   | Description                                                   |
| --------------- | ------ | ------------------------------------------------------------- |
| collateralIndex | uint8  |                                                               |
| groupIndex      | uint16 | index of the borrowing group                                  |
| long            | bool   | true if long side                                             |
| increase        | bool   | true if open interest is increased, false if decreased        |
| delta           | uint72 | change in open interest in collateral tokens (1e10 precision) |
| newOiLong       | uint72 | new open interest on long side                                |
| newOiShort      | uint72 | new open interest on short side                               |

## BorrowingZeroGroup

```solidity
error BorrowingZeroGroup()
```

## BorrowingWrongExponent

```solidity
error BorrowingWrongExponent()
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gains.trade/developer/technical-reference/contracts/interfaces/libraries/iborrowingfeesutils.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
