> For the complete documentation index, see [llms.txt](https://naviprotocol.gitbook.io/navi-protocol-developer-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://naviprotocol.gitbook.io/navi-protocol-developer-docs/smart-contract-overview/flash-loan.md).

# Flash Loan

The following functions provide users the ability to take out and repay flash loans from a specified pool. A flash loan allows users to borrow assets with the condition that the loan must be repaid within the same Sui transaction. If the loan is not fully repaid, the transaction reverts, ensuring that no loss occurs for the pool.

### **flash\_loan\_with\_ctx\_v2**

{% hint style="info" %}
This function allows a user to take out a flash loan of a specified amount of a certain coin type from a pool. The loan must be repaid within the same transaction. A `FlashLoanReceipt` is generated to keep track of the loan details.
{% endhint %}

```rust
public fun flash_loan_with_ctx_v2<CoinType>(
    config: &FlashLoanConfig, 
    pool: &mut Pool<CoinType>, 
    amount: u64,
    system_state: &mut SuiSystemState, 
    ctx: &mut TxContext
): (Balance<CoinType>, FlashLoanReceipt<CoinType>)
```

### flash\_repay\_with\_ctx

{% hint style="info" %}
This function handles the repayment of a flash loan. It takes in the loan receipt, the repayment balance object, and updates the pool's balance accordingly. If the loan is not repaid in full, the transaction will revert. The rest will be returned as a balance object.
{% endhint %}

```rust
public fun flash_repay_with_ctx<CoinType>(
    clock: &Clock,
    storage: &mut Storage,
    pool: &mut Pool<CoinType>,
    receipt: FlashLoanReceipt<CoinType>,
    repay_balance: Balance<CoinType>,
    ctx: &mut TxContext
): Balance<CoinType>
```

### flash\_repay\_with\_account\_cap

{% hint style="info" %}
This function allows a user to take out a flash loan while utilizing an `AccountCap`. The `AccountCap` serves as an agent layer during the loan process. The function returns both the loan balance and the receipt for tracking the loan.
{% endhint %}

```rust
public fun flash_repay_with_account_cap<CoinType>(
    clock: &Clock,
    storage: &mut Storage,
    pool: &mut Pool<CoinType>,
    receipt: FlashLoanReceipt<CoinType>,
    repay_balance: Balance<CoinType>,
    account_cap: &AccountCap
): Balance<CoinType> {}
```

<br>

### &#x20;<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://naviprotocol.gitbook.io/navi-protocol-developer-docs/smart-contract-overview/flash-loan.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
