> For the complete documentation index, see [llms.txt](https://bitprotocol.gitbook.io/bitprotocol/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bitprotocol.gitbook.io/bitprotocol/resources/technical-documentation/bitflashloan.md).

# BitFlashLoan

## **Write Methods**

```
function flashLoan(
        IERC3156FlashBorrower receiver,
        address token,
        uint256 amount,
        bytes calldata data
  ) external returns (bool)
```

Executes a flash loan by borrowing tokens, calling the receiver's callback, and ensuring repayment with fees.

| **Parameter** | **Type**                | **Description**                                                              |
| ------------- | ----------------------- | ---------------------------------------------------------------------------- |
| `receiver`    | `IERC3156FlashBorrower` | Contract that receives and handles the flash loan (must implement callback). |
| `token`       | `address`               | Address of the ERC20 token to borrow.                                        |
| `amount`      | `uint256`               | Amount of tokens to borrow.                                                  |
| `data`        | `bytes`                 | Arbitrary data passed to the receiver's callback function.                   |

**Returns:**

| **Value** | **Type** | **Description**                                              |
| --------- | -------- | ------------------------------------------------------------ |
| `success` | `bool`   | `true` if the flash loan and callback executed successfully. |

## **View Methods**

```
function flashFee(address token, uint256 amount) public view returns (uint256)
```

Calculates the fee for a flash loan based on the token's fee rate.

| **Parameter** | **Type**  | **Description**                   |
| ------------- | --------- | --------------------------------- |
| `token`       | `address` | Token address to query.           |
| `amount`      | `uint256` | Loan amount to calculate fee for. |

**Returns:**

| **Value** | **Type**  | **Description**                          |
| --------- | --------- | ---------------------------------------- |
| `fee`     | `uint256` | Fee amount (`amount * feeRate / 10000`). |

```
function maxFlashLoan(address token) public view returns (uint256)
```

Returns the maximum borrowable amount for a token (based on collateral percentage).

| **Parameter** | **Type**  | **Description**         |
| ------------- | --------- | ----------------------- |
| `token`       | `address` | Token address to query. |

**Returns:**

| **Value**   | **Type**  | **Description**                                                        |
| ----------- | --------- | ---------------------------------------------------------------------- |
| `maxAmount` | `uint256` | Maximum borrowable amount (`totalCollateral * maxPercentage / 10000`). |

## Structs

## Events

```
event FlashLoan(
    address indexed initiator,
    address indexed token,
    uint256 amount,
    uint256 fee
  )
```

```
event FlashLoanFeesUpdated(address indexed token, uint256 newFeeRate)
```

```
event FlashLoanMaxPercentageUpdated(address indexed token, uint256 newMaxPercentage)
```

```
event FlashLoansPaused(bool paused)
```

```
event MinSystemCollateralizationRatioUpdated(uint256 newRatio)
```

```
event TokenSupported(address indexed token, address indexed troveManager)
```

```
event DefaultFeeRateUpdated(uint256 newRate)
```

```
event MaxFeeRateUpdated(uint256 newRate)
```
