# FlashLoan

The concept of the NFT Flash Loan is very similar to the concept of ERC20 asset Flash Loan → it is an uncollateralised loan that allow the borrowing of an NFT, as long as the borrowed NFT is returned before the end of the transaction(together with the loan cost paid to the pool).

Flash-loaned NFTs would behave in many cases exactly like you were the owner of them (you can flash-loan them every single time when you would normally use the NFT).

### Overview

#### Here is how you can get started :

To execute an NFT Flash Loan you will have to deploy your own contract that will interact with Nafta smart contract:

1. Your contract calls `Nafta` contract requesting a Flash Loan using <mark style="color:red;">`flashLoan()`</mark>. You will indicate a certain <mark style="color:red;">`nftID`</mark> , <mark style="color:red;">`nftAddress`</mark> , <mark style="color:red;">`maxLoanPrice`</mark> - *Price the user is willing to pay for the flashloan &* <mark style="color:red;">`receiverAddress`</mark>.
2. After some sanity checks, `Nafta` transfers the requested <mark style="color:red;">`nftID`</mark> from <mark style="color:red;">`_poolNFTs`</mark> to your contract, then calls <mark style="color:red;">`IFlashNFTReceiver`</mark>on your contract (or another contract that you specify as the <mark style="color:red;">`receiverAddress`</mark>).
3. Your contract, now holding the flash loaned <mark style="color:red;">`nftId`</mark>, executes any arbitrary operation in its code.
4. Once your code is executed, you transfer the flashLoaned<mark style="color:red;">`nftID`</mark> back to <mark style="color:red;">`Nafta`</mark>.

* If the original NFT is not returned then the transaction is reverted.

{% hint style="warning" %}
All of the above happens in 1 transaction (hence in a single ethereum block)
{% endhint %}

### Flash Loan Fee

<mark style="color:red;">`flashFee`</mark> - The fee user has to pay for a single rent (in WETH9) *\[Range: 0-4k ETH]*

### Step by Step

#### 1. Set Up

Your contract that receives the flash loaned NFT has to integrate <mark style="color:red;">`IFlashNFTReceiver`</mark> interface by implementing the relevant <mark style="color:red;">`executeOperation()`</mark> function.

```jsx
interface IFlashNFTReceiver is IERC721Receiver {
    function executeOperation(
     address nftAddress, 
     uint256 nftId, 
     uint256 feeInWeth, 
     address msgSender, 
     bytes calldata data) external returns (bool);
```

#### 2. Calling `flashLoan()`

To call <mark style="color:red;">`flashloan()`</mark> on `Nafta`, we need to pass in the relevant parameters.

```jsx
function flashloan(
address nftAddress, 
uint256 nftId, 
uint256 maxLoanPrice, 
address receiverAddress, 
bytes calldata data) external;
```

#### 3. Completing the NFT flash loan

Once you have performed your logic with the flash loaned NFT (in your <mark style="color:red;">`executeOperation()`</mark> function), you will need to pay back the flash loaned NFTs with <mark style="color:red;">`flashFee`</mark>
