# 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>
