> For the complete documentation index, see [llms.txt](https://docs.inverter.network/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.inverter.network/contracts/technical-reference/modules/funding-manager/bonding-curve/abstracts/redeemingbondingcurvebase_v1.sol.md).

# RedeemingBondingCurveBase\_v1.sol

[Git Source](https://github.com/InverterNetwork/inverter-contracts/blob/649b450f02fc8b735c128ff0821467e71966c666/src/modules/fundingManager/bondingCurve/abstracts/RedeemingBondingCurveBase_v1.sol)

**Inherits:** IRedeemingBondingCurveBase\_v1, BondingCurveBase\_v1

**Author:** Inverter Network

Manages the redemption of issuance for collateral along a bonding curve in the Inverter Network, including fee handling and sell functionality control.

*Inherits from {BondingCurveBase\_v1}. Extends by providing core functionalities for redeem operations, fee adjustments, and redemption calculations. Fee calculations utilize BPS for precision. Redeem-specific calculations should be implemented in derived contracts.*

### State Variables

#### sellIsOpen

*Indicates whether the sell functionality is open or not. Enabled = true || disabled = false.*

```solidity
bool public sellIsOpen;
```

#### sellFee

*Sell fee expressed in base points, i.e. 0% = 0; 1% = 100; 10% = 1000.*

```solidity
uint public sellFee;
```

#### \_\_gap

*Storage gap for future upgrades.*

```solidity
uint[50] private __gap;
```

### Functions

#### supportsInterface

*See {IERC165-supportsInterface}.*

```solidity
function supportsInterface(bytes4 interfaceId)
    public
    view
    virtual
    override(BondingCurveBase_v1)
    returns (bool);
```

#### sellingIsEnabled

*Modifier to guarantee the selling functionality is enabled.*

```solidity
modifier sellingIsEnabled();
```

#### sellTo

Redeem tokens and directs the proceeds to a specified receiver address.

*Executes a sell order, with the proceeds being sent directly to the \_receiver's address. This function wraps the `_sellOrder` internal function with specified parameters to handle the transaction and direct the proceeds.*

```solidity
function sellTo(address _receiver, uint _depositAmount, uint _minAmountOut)
    public
    virtual
    sellingIsEnabled
    validReceiver(_receiver);
```

**Parameters**

| Name             | Type      | Description                                                                               |
| ---------------- | --------- | ----------------------------------------------------------------------------------------- |
| `_receiver`      | `address` | The address that will receive the redeemed tokens.                                        |
| `_depositAmount` | `uint256` | The amount of tokens to be sold.                                                          |
| `_minAmountOut`  | `uint256` | The minimum acceptable amount of proceeds that the receiver should receive from the sale. |

#### sell

Redeem collateral for the sender's address.

*Redirects to the internal function `_sellOrder` by passing the sender's address and deposit amount.*

```solidity
function sell(uint _depositAmount, uint _minAmountOut) public virtual;
```

**Parameters**

| Name             | Type      | Description                                                                     |
| ---------------- | --------- | ------------------------------------------------------------------------------- |
| `_depositAmount` | `uint256` | The amount of issued token deposited.                                           |
| `_minAmountOut`  | `uint256` | The minimum acceptable amount the user expects to receive from the transaction. |

#### openSell

Opens the selling functionality for the collateral.

*Only callable by the {Orchestrator\_v1} admin. Reverts if selling is already open.*

```solidity
function openSell() external virtual onlyOrchestratorAdmin;
```

#### closeSell

Closes the selling functionality for the collateral.

*Only callable by the {Orchestrator\_v1} admin. Reverts if selling is already closed.*

```solidity
function closeSell() external virtual onlyOrchestratorAdmin;
```

#### setSellFee

Sets the fee percentage for selling collateral, payed in collateral.

*Only callable by the {Orchestrator\_v1} admin. The fee cannot exceed 10000 basis points. Reverts if an invalid fee is provided.*

```solidity
function setSellFee(uint _fee) external virtual onlyOrchestratorAdmin;
```

**Parameters**

| Name   | Type      | Description              |
| ------ | --------- | ------------------------ |
| `_fee` | `uint256` | The fee in basis points. |

#### calculateSaleReturn

Calculates the amount of tokens to be redeemed based on a given deposit amount.

*This function takes into account any applicable sell fees before computing the collateral amount to be redeemed. Revert when `_depositAmount` is zero.*

```solidity
function calculateSaleReturn(uint _depositAmount)
    public
    view
    virtual
    returns (uint redeemAmount);
```

**Parameters**

| Name             | Type      | Description                                 |
| ---------------- | --------- | ------------------------------------------- |
| `_depositAmount` | `uint256` | The amount of tokens deposited by the user. |

**Returns**

| Name           | Type      | Description                                                                |
| -------------- | --------- | -------------------------------------------------------------------------- |
| `redeemAmount` | `uint256` | The amount of collateral that will be redeemed as a result of the deposit. |

#### getStaticPriceForSelling

Calculates and returns the static price for selling the issuance token.

```solidity
function getStaticPriceForSelling() external view virtual returns (uint);
```

**Returns**

| Name     | Type      | Description                                           |
| -------- | --------- | ----------------------------------------------------- |
| `<none>` | `uint256` | uint The static price for selling the issuance token. |

#### \_redeemTokensFormulaWrapper

*Function used for wrapping the call to the external contract responsible for calculating the redeeming amount. This function is an abstract function and must be implemented in the downstream contract.*

```solidity
function _redeemTokensFormulaWrapper(uint _depositAmount)
    internal
    view
    virtual
    returns (uint);
```

**Parameters**

| Name             | Type      | Description                                    |
| ---------------- | --------- | ---------------------------------------------- |
| `_depositAmount` | `uint256` | The amount of issuing token that is deposited. |

**Returns**

| Name     | Type      | Description                                          |
| -------- | --------- | ---------------------------------------------------- |
| `<none>` | `uint256` | uint Return the amount of collateral to be redeemed. |

#### \_sellOrder

*Executes a sell order by transferring tokens from the receiver to the contract,. calculating the redeem amount, and finally transferring the redeem amount back to the receiver. This function is internal and not intended for end-user interaction. PLEASE NOTE: The current implementation only requires that enough collateral token is held for redeeming to be possible. No further functionality is implemented which would manages the outflow of collateral, e.g., restricting max redeemable amount per user, or a redeemable amount which differes from the actual balance. Throws an exception if `_depositAmount` is zero or if there's insufficient collateral in the contract for redemption.*

```solidity
function _sellOrder(address _receiver, uint _depositAmount, uint _minAmountOut)
    internal
    returns (uint totalCollateralTokenMovedOut, uint issuanceFeeAmount);
```

**Parameters**

| Name             | Type      | Description                                                                     |
| ---------------- | --------- | ------------------------------------------------------------------------------- |
| `_receiver`      | `address` | The address receiving the redeem amount.                                        |
| `_depositAmount` | `uint256` | The amount of tokens being sold by the receiver.                                |
| `_minAmountOut`  | `uint256` | The minimum acceptable amount the user expects to receive from the transaction. |

**Returns**

| Name                           | Type      | Description                                                                                                       |
| ------------------------------ | --------- | ----------------------------------------------------------------------------------------------------------------- |
| `totalCollateralTokenMovedOut` | `uint256` | The total amount of collateral tokens that are transfered away from the collateral token amount of this contract. |
| `issuanceFeeAmount`            | `uint256` | The amount of issuance token subtracted as fee.                                                                   |

#### \_handleCollateralTokensAfterSell

Virtual function to handle collateral tokens after a successful sell.

*The downstream contract is responsible for implementing checks to ensure the contract has the right balance.*

```solidity
function _handleCollateralTokensAfterSell(
    address _receiver,
    uint _collateralTokenAmount
) internal virtual;
```

**Parameters**

| Name                     | Type      | Description                                                  |
| ------------------------ | --------- | ------------------------------------------------------------ |
| `_receiver`              | `address` | The address for which the collateral tokens will be handled. |
| `_collateralTokenAmount` | `uint256` | The amount of collateral tokens to handle.                   |

#### \_sellingIsEnabledModifier

*Checks if the sell functionality is enabled.*

```solidity
function _sellingIsEnabledModifier() internal view;
```

#### \_setSellFee

*Sets the sell transaction fee, expressed in BPS.*

```solidity
function _setSellFee(uint _fee) internal virtual;
```

**Parameters**

| Name   | Type      | Description                                      |
| ------ | --------- | ------------------------------------------------ |
| `_fee` | `uint256` | The fee percentage to set for sell transactions. |
