# MidasPair721

### MidasPair721

Contract used to trade through the Liquidity Book and also acts as the manager of liquidity positions.

#### constructor

```Solidity
constructor(address _factory) {}
```

**Parameters**

| **Name**  | **Type** | **Description**                                   |
| --------- | -------- | ------------------------------------------------- |
| \_factory | address  | The address of the factory which creates the pair |

#### SellNFT

```Solidity
```

BuyNFT

<pre class="language-Solidity"><code class="lang-Solidity"><strong>event BuyNFT(uint256 indexed nftTokenId, address indexed from, uint24 tradeBin, uint128 indexed lpTokenId);
</strong></code></pre>

#### ERC721PositionMinted

```Solidity
event ERC721PositionMinted(uint256 lpTokenId, uint24 binLower, uint24 binStep, uint256 binAmount, uint256[] nftTokenIds);
```

#### ERC20PositionMinted

```Solidity
event ERC20PositionMinted(uint256 lpTokenId, uint24 binLower, uint24 binStep, uint256 binAmount);
```

#### PositionBurned

```Solidity
event PositionBurned(uint256 lpTokenId, address owner, uint256 nftAmount, uint256 ftAmount, uint256 feeCollected);
```

#### ClaimFee

```Solidity
event ClaimFee(uint128 indexed lpTokenId, address indexed owner, uint256 indexed feeCollected);
```

#### FlashLoan

```Solidity
    event FlashLoan(address indexed caller, IMidasFlashLoanCallback receiver, uint256[] NFTIDs)
```

#### NewRoyaltyFee

```Solidity
    event NewRoyaltyFee(uint128 indexed newFee)
```

#### initialize

```Solidity
function initialize() external;
```

Function to initialize the pair which is strict to MidasFactory.

#### getTokenX

```Solidity
function getTokenX() external pure override returns (IERC721)
```

Pure function to get the NFT address of this pair.

#### getTokenY

```Solidity
function getTokenY() external pure override returns (IERC20)
```

Pure function to get the FT address of this pair.

#### getLPToken

```Solidity
function getLPToken() external pure override returns (LPToken)
```

Pure function to get the LP Token address of this pair.

#### factory

```Solidity
function factory() external view override returns (IMidasFactory721)
```

View function to get the factory address of this pair.

#### getReserves

```Solidity
function getReserves() external view override returns (uint128 reserveX, uint128 reserveY)
```

View function to get the NFT and FT reserves of this pair.

**Return Values**

| **Name** | **Type** | **Description**     |
| -------- | -------- | ------------------- |
| reserveX | uint128  | The reserves of NFT |
| reserveY | uint128  | The reserves of FT  |

#### getIDs

```Solidity
function getIDs() external view override returns (uint24 bestOfferID, uint24 floorPriceID, uint128 currentPositionID)
```

View function to get current index price info of this pair.

**Return Values**

| **Name**          | **Type** | **Description**                 |
| ----------------- | -------- | ------------------------------- |
| bestOfferID       | uint24   | The best offer id of this pair  |
| floorPriceID      | uint24   | The floor price id of this pair |
| currentPositionID | uint128  | The latest position ID          |

#### getGlobalFees

```Solidity
function getGlobalFees() external view override returns (uint128 feesTotal, uint128 feesProtocol)
```

View function to get the global fees information, the total fees and those for protocol

**Return Values**

| **Name**     | **Type** | **Description**             |
| ------------ | -------- | --------------------------- |
| feesTotal    | uint128  | The unclaimed total fees    |
| feesProtocol | uint128  | The unclaimed protocol fees |

#### feeParameters

```Solidity
function feeParameters() external view override returns (uint128 rate, uint128 protocolRate, uint128 royaltyRate)
```

View function to get the fee parameters of this pair.

**Return Values**

| **Name**     | **Type** | **Description**                                          |
| ------------ | -------- | -------------------------------------------------------- |
| rate         | uint128  | The fee rate of the LP could collected from trades       |
| protocolRate | uint128  | The protocol fee rate which is collected through the fee |
| royaltyRate  | uint128  | The royalty rate of this pair                            |

#### getBin

```Solidity
function getBin(uint24 _id) external view override returns (uint128 reserveX, uint128 reserveY)
```

View function to get the reserves of bin at `id`.

**Parameters**

| **Name** | **Type** | **Description** |
| -------- | -------- | --------------- |
| \_id     | uint24   | The bin id      |

**Return Values**

| **Name** | **Type** | **Description**                |
| -------- | -------- | ------------------------------ |
| reserveX | uint128  | The reserves of NFT in the bin |
| reserveY | uint128  | The reserves of FT in the bin  |

#### getLpInfos

```Solidity
function getLpInfos(uint128 _LPtokenID) external view override returns (uint24, uint24, uint128)
```

View function to get the LP info via LP tokenId.

**Parameters**

| **Name**    | **Type** | **Description**    |
| ----------- | -------- | ------------------ |
| \_LPtokenID | uint128  | The ID of LP Token |

**Return Values**

| **Name**  | **Type** | **Description**                     |
| --------- | -------- | ----------------------------------- |
| originBin | uint24   | The start bin of this position      |
| binStep   | uint24   | The bin step of this position       |
| fee       | uint128  | The unclaimed fee for this position |

#### getPriceFromBin

```Solidity
function getPriceFromBin(uint24 _id) external view override returns (uint128)
```

View function to get the price mapping to the specific bin id.

**Parameters**

| **Name** | **Type** | **Description** |
| -------- | -------- | --------------- |
| \_id     | uint24   | The id of bin   |

**Return Values**

| **Name** | **Type** | **Description**                          |
| -------- | -------- | ---------------------------------------- |
| price    | uint128  | The price mapping to the specific bin id |

#### getLPFromNFT

```Solidity
function getLPFromNFT(uint24 _id) external view override returns (uint128)
```

View function to get the LP tokenId of the specific NFT.

**Parameters**

| **Name** | **Type** | **Description**         |
| -------- | -------- | ----------------------- |
| \_id     | uint24   | The token id of the NFT |

**Return Values**

| **Name**  | **Type** | **Description**        |
| --------- | -------- | ---------------------- |
| \_LPtoken | uint128  | The ID of the LP token |

#### getBinParamFromLP

```Solidity
function getBinParamFromLP(uint128 _LPtokenID, uint256 _amount) external view override returns (uint128 _totalPrice)
```

View function to get the quote price of the specific position.

**Parameters**

| **Name**    | **Type** | **Description**                   |
| ----------- | -------- | --------------------------------- |
| \_lpTokenID | uint128  | The ID of the LP token            |
| \_amount    | uint256  | The amount of NFT is going to buy |

**Return Values**

| **Name**     | **Type** | **Description**         |
| ------------ | -------- | ----------------------- |
| \_totalPrice | uint128  | The quote price of NFTs |

#### getLpReserve

```Solidity
function getLpReserve(uint128 _lpTokenID) external view override returns (uint128, uint128)
```

View function to get the reserves of the specific position.

**Parameters**

| **Name**    | **Type** | **Description**    |
| ----------- | -------- | ------------------ |
| \_lpTokenID | uint128  | The id of LP Token |

**Return Values**

| **Name** | **Type** | **Description**                     |
| -------- | -------- | ----------------------------------- |
| amountX  | uint128  | The amount of NFTs in this position |
| amountY  | uint128  | The amount of FT in this position   |

#### sellNFT

```Solidity
function sellNFT(uint256 NFTID, address _to) external override nonReentrant returns (uint128 _amountOut)
```

Function to buy the NFT asset at the spot price.

**Parameters**

| **Name** | **Type** | **Description**                    |
| -------- | -------- | ---------------------------------- |
| NFTID    | uint256  | The token id of the NFT to be sold |
| \_to     | address  | The address of the recipient       |

**Return Values**

| **Name**    | **Type** | **Description**                       |
| ----------- | -------- | ------------------------------------- |
| \_amountOut | uint128  | The amount of FT the receiver can get |

#### buyNFT

```Solidity
function buyNFT(uint256 NFTID, address _to) external override nonReentrant
```

Function to buy the NFT asset at the spot price.

**Parameters**

| **Name** | **Type** | **Description**                      |
| -------- | -------- | ------------------------------------ |
| NFTID    | uint256  | The token id of the NFT to be bought |
| \_to     | address  | The address of the recipient         |

#### mintNFT

```Solidity
function mintNFT(uint24[] calldata _ids, uint256[] calldata _NFTIDs, address _to, bool isLimited) external override nonReentrant
```

Performs a low level add liquidity of NFT. This needs to be called from a contract which performs important safety checks.

**Parameters**

| **Name**  | **Type**            | **Description**                                           |
| --------- | ------------------- | --------------------------------------------------------- |
| \_ids     | uint24\[] calldata  | The list of ids to add liquidity                          |
| \_NFTIDs  | uint256\[] calldata | The token id of the NFT                                   |
| \_to      | address             | The address of the recipient                              |
| isLimited | bool                | Determine whether the liquidity position is a limit order |

**Return Values**

| **Name**     | **Type** | **Description**          |
| ------------ | -------- | ------------------------ |
| \_ids.length | uint256  | The amount of NFT to add |
| \_LPtokenID  | uint128  | The ID of the LP token   |

#### mintFT

```Solidity
function mintFT(uint24[] calldata _ids, address _to) external override nonReentrant
```

Performs a low level add liquidity of FT. This needs to be called from a contract which performs important safety checks.

**Parameters**

| **Name** | **Type**           | **Description**                  |
| -------- | ------------------ | -------------------------------- |
| \_ids    | uint24\[] calldata | The list of ids to add liquidity |
| \_to     | address            | The address of the recipient     |

**Return Values**

| **Name**             | **Type** | **Description**         |
| -------------------- | -------- | ----------------------- |
| \_amountYAddedToPair | uint128  | The amount of FT to add |
| \_LPtokenID          | uint128  | The ID of the LP token  |

#### burn

```Solidity
function burn(uint128 _LPtokenID, address _nftReceiver, address _to) external override nonReentrant
```

Performs a low level remove liquidity. This needs to be called from a contract which performs important safety checks

**Parameters**

| **Name**      | **Type** | **Description**                         |
| ------------- | -------- | --------------------------------------- |
| \_LPtokenID   | uint128  | The ID of the LP token                  |
| \_nftReceiver | address  | The address of the NFT assets recipient |
| \_to          | address  | The address of the FT assets recipient  |

**Return Values**

| **Name** | **Type** | **Description**                |
| -------- | -------- | ------------------------------ |
| amountY  | uint128  | The amount of FT sent to `_to` |

#### collectProtocolFees

```Solidity
function collectProtocolFees() external override nonReentrant
```

Collect the protocol fees and send them to the fee recipient.

**Return Values**

| **Name** | **Type** | **Description**                             |
| -------- | -------- | ------------------------------------------- |
| amountY  | uint128  | The amount of FT sent to the fee recipient. |

#### collectLPFees

```Solidity
function collectLPFees(uint128 _LPtokenID , address _to) external override nonReentrant
```

Collect the LP fees and send them to the fee recipient.

**Return Values**

| **Name**  | **Type** | **Description**                             |
| --------- | -------- | ------------------------------------------- |
| amountFee | uint128  | The amount of FT sent to the fee recipient. |

#### collectRoyaltyFees

```Solidity
function collectRoyaltyFees() external override nonReentrant
```

Collect the royalty fees and send them to the royalty fee recipient.

**Return Values**

| **Name**      | **Type** | **Description**                             |
| ------------- | -------- | ------------------------------------------- |
| \_royaltyFees | uint128  | The amount of FT sent to the fee recipient. |

#### updateRoyalty

```Solidity
function updateRoyalty(uint128 _newRate, address payable[] calldata newrecipients, uint256[] calldata newshares) external override nonReentrant
```

Update the royalty info of the NFT according to EIP2981.

**Parameters**

| **Name**      | **Type**                    | **Description**                |
| ------------- | --------------------------- | ------------------------------ |
| \_newRate     | uint256                     | The ID of the LP token         |
| newrecipients | address payable\[] calldata | The addresses of the recipient |
| newshares     | uint256\[] calldata         | The shares of these recipients |

#### updateSafetyLock

```Solidity
function updateSafetyLock(bool newLock) external override nonReentrant
```

Update the safety locker status which is strict to factory.

**Parameters**

| **Name** | **Type** | **Description**                         |
| -------- | -------- | --------------------------------------- |
| newLock  | bool     | The status of this pair's safety locker |

#### flashLoan

```Solidity
function flashLoan(IMidasFlashLoanCallback receiver, uint256[] calldata _tokenIds, bytes calldata data) external override nonReentrant
```

**Parameters**

| **Name**   | **Type**                | **Description**                                         |
| ---------- | ----------------------- | ------------------------------------------------------- |
| receiver   | IMidasFlashLoanCallback | The implementation of receiver                          |
| \_tokenIds | uint256\[] calldata     | The list of NFT tokenIds to be borrowed                 |
| data       | bytes calldata          | The data to be transferred along with function revoking |


---

# Agent Instructions: 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:

```
GET https://midaswap.gitbook.io/docs/contracts/midaspair721.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
