# NFT Lending Integration

## Requirements

1. A new smart contract that allows Wasabi to interact with your lending protocol,
2. API (or an SDK) that allows fetching for loan offers and any related variables & metadata  (signature, nonce, etc.) required to borrow.

## Part 1: Implementing the INFTLending Interface

We created an NFT lending interface that can make borrow and repay loans. Implement this interface which will call your protocol's smart contracts.

```solidity
/// @notice NFTLending Interface
interface INFTLending {
    /// @notice Loan Details struct
    /// @param borrowAmount Borrow amount
    /// @param repayAmount Repay amount
    /// @param loanExpiration Loan expiration
    /// @param nftAddress The collateral NFT address
    /// @param tokenId The collateral NFT ID
    struct LoanDetails {
        uint256 borrowAmount;
        uint256 repayAmount;
        uint256 loanExpiration;
        address nftAddress;
        uint256 tokenId;
    }

    /// @notice Get loan details for given loan id
    /// @param _loanId The loan id
    function getLoanDetails(
        uint256 _loanId
    ) external view returns (LoanDetails memory);

    /// @notice Borrow WETH from the protocol
    /// @param _inputData Encoded input parameters
    /// @return _loanId The loan id
    function borrow(
        bytes calldata _inputData
    ) external payable returns (uint256 _loanId);

    /// @notice Repay the loan
    /// @param _loanId The if of loan to repay
    /// @param _receiver The address that should receive the collateral NFT
    function repay(uint256 _loanId, address _receiver) external payable;
}
```

Create a Solidity contract This contract should serve as the core component representing your lending protocol and must include the following functions:

* `getLoanDetails(uint256 _loanId)`: Implement this function to retrieve comprehensive loan details based on a specified loan ID. It should return a `LoanDetails` object.
  * The `repayAmount` should be the `principal + max interest`. So if the lending protocol accrues interest, `repayAmount` should be returned as the fully accrued interest amount.
* `borrow(bytes calldata _inputData)`: Develop this function to enable Wasabi to request and receive ETH loans from your protocol.
  * Related borrow request data that will be passed to the lending protocol will be appropriately encoded off-chain and should be decoded here. The decoded parameters should be used to call the lending protocol functions. The following snippet is from `ArcadeLending.sol`

```solidity
 // 1. Decode `inputData` into appropriate variables
(
    LoanLibrary.LoanTerms memory loanTerms,
    address borrower,
    address lender,
    LoanLibrary.Signature memory sig,
    uint160 nonce,
    LoanLibrary.Predicate[] memory itemPredicates
) = abi.decode(
    _inputData,
    (LoanLibrary.LoanTerms, address, address, LoanLibrary.Signature, uint160, LoanLibrary.Predicate[])
);
```

* `repay(uint256 _loanId, address _receiver)`: Implement this function to facilitate loan repayments initiated by Wasabi. Ensure the collateral NFTs are returned to the designated address.

### Resources

* [INFTLending Interface](https://github.com/DkodaLabs/wasabi/blob/main/contracts/lending/interfaces/INFTLending.sol)
* [Example implementation (Arcade.xyz)](https://github.com/DkodaLabs/wasabi/blob/main/contracts/lending/arcade/ArcadeLending.sol)

## Part 2: Loan Offers&#x20;

Create an API or SDK that the Wasabi team will be able to use to fetch collection loan offers from your protocol.

We require 2 endpoints,

* A collection offer list endpoint,
* A collection offer transaction data endpoint.

### Collection Loan Offer List Endpoint

Create an endpoint that should return a list of collection-wide loan offers. This endpoint should take in a NFT collection address and filter offers with that address. For each offer, we need the following variables:

* `id` A unique ID that points to the loan offer
* `loanAmount` The amount that the borrower will receive
* `duration` The duration for the loan in seconds
* `upfrontFee` Any fee that is paid to the protocol when a user borrows, 0 otherwise
* `totalAvailableSize` The total available size for this offer.&#x20;
  * For individual offers, it should match the `loanAmount`
  * If the offer can be taken up multiple times, then the total committed amount for the offer

Example:

```json
{
      "id": "a7ba77f9-2417-42de-acb2-ffa038891e4c",
      "loanAmount": "3925306000000000000",
      "paybackAmount": "4312459468493150700",
      "duration": 2592000,
      "upfrontFee": "0",
      "totalAvailableSize": "3925306000000000000"
}
```

### Offer Transaction Data Endpoint

Create an endpoint that will return the variables that should be used when borrowing from your protocol. These variables will be encoded and then sent for a transaction submission. Once the transaction is submitted, the data will be decoded and sent to your protocol. An example can be found here:

[#part-1-implementing-the-inftlending-interface](#part-1-implementing-the-inftlending-interface "mention")

Once these endpoints are created, you should send us documentation and examples so we can integrate them into our NFT Lending Indexer server.
