# InitCore

The main entry point of INIT.

## View Functions

### POS\_MANAGER

PosManager contract address.

```solidity
function POS_MANAGER() external returns (address posManager);
```

### config

Config contract address

```solidity
function config() external returns (address config);
```

### oracle

InitOracle contract address

```solidity
function oracle() external returns (address initOracle);
```

### liqIncentiveCalculator

LiqIncentiveCalculator contract address

```solidity
function liqIncentiveCalculator() external returns (address liqIncentiveCalculator);
```

### riskManager

RiskManager contract address

```solidity
function riskManager() external returns (address riskManager);
```

## External Functions

### mintTo

Mint inToken from a lending pool using the balance difference (∆balance) between the current and last stored balances. Users should transfer in the pool's underlying token before calling this function.

{% hint style="info" %}
This is a low-level function call. Make sure to atomically send tokens and call mintTo.
{% endhint %}

```solidity
function mintTo(address _pool, address _to) external returns (uint256 shares);
```

Parameters:

| Name    | Type      | Description                  |
| ------- | --------- | ---------------------------- |
| `_pool` | `address` | lending pool to mint inToken |
| `_to`   | `address` | address to mint inTokens to  |

Returns:

| Name     | Type      | Description              |
| -------- | --------- | ------------------------ |
| `shares` | `uint256` | amount of inToken minted |

### burnTo

Burn inToken for the pool's underlying token. Lending pool must have enough idle liquidity to transfer out the converted amount of underlying token.

{% hint style="info" %}
This is a low-level function call. Make sure to atomically send inTokens and call burnTo.
{% endhint %}

```solidity
function burnTo(address _pool, address _to) external returns (uint256 amt);
```

Parameters:

| Name    | Type      | Description                          |
| ------- | --------- | ------------------------------------ |
| `_pool` | `address` | lending pool to burn inToken         |
| `_to`   | `address` | address to receive underlying tokens |

Returns:

| Name  | Type      | Description                     |
| ----- | --------- | ------------------------------- |
| `amt` | `uint256` | amount of underlying tokens out |

### borrow

Borrow pool's underlying tokens from InitCore. Lending pool must have enough idle liquidity to borrow. Debt ceiling and borrow cap must also not be reached for the borrowing to succeed. Position health must also be healthy after the borrowing.

{% hint style="info" %}
The position's health factor will increase after a successful borrow.
{% endhint %}

```solidity
function borrow(address _pool, uint256 _amt, uint256 _posId, address _to) external returns (uint256 shares);
```

Parameters:

| Name     | Type      | Description                            |
| -------- | --------- | -------------------------------------- |
| `_pool`  | `address` | lending pool to borrow from            |
| `_amt`   | `uint256` | token amount to borrow                 |
| `_posId` | `uint256` | position id to account the borrow from |
| `_to`    | `address` | address to receive underlying tokens   |

Returns:

| Name     | Type      | Description                |
| -------- | --------- | -------------------------- |
| `shares` | `uint256` | debt shares for the borrow |

### repay

Repay pool's underlying tokens to InitCore.&#x20;

{% hint style="info" %}
The position's health factor will decrease after a successful repay.
{% endhint %}

```solidity
function repay(address _pool, uint256 _shares, uint256 _posId) external returns (uint256 amt);
```

Parameters:

| Name      | Type      | Description              |
| --------- | --------- | ------------------------ |
| `_pool`   | `address` | lending pool to repay to |
| `_shares` | `uint256` | shares amount to repay   |
| `_posId`  | `uint256` | position id to repay to  |

Returns:

| Name  | Type      | Description                       |
| ----- | --------- | --------------------------------- |
| `amt` | `uint256` | corresponding token amount repaid |

### createPos

Create a new position under the specified mode.&#x20;

{% hint style="info" %}
`viewer` address does not have any effect on the on-chain logic. It is only used for tracking the actual position owner in case the integrating protocol interacts with InitCore on behalf of the user.
{% endhint %}

```solidity
function createPos(uint16 _mode, address _viewer) external returns (uint256 posId);
```

Parameters:

| Name      | Type      | Description                                              |
| --------- | --------- | -------------------------------------------------------- |
| `_mode`   | `uint16`  | mode for the position                                    |
| `_viewer` | `address` | viewer address that represents the actual position owner |

Returns:

| Name    | Type      | Description     |
| ------- | --------- | --------------- |
| `posId` | `uint256` | new position id |

### setPosMode

Set a new position mode to an existing position.&#x20;

{% hint style="info" %}
The mode change might be invalid due to several reasons, for example, the debt ceiling reached on the new mode or some tokens are not supported on the new mode.
{% endhint %}

```solidity
function setPosMode(uint _posId, uint16 _mode) external;
```

Parameters:

| Name     | Type      | Description                |
| -------- | --------- | -------------------------- |
| `_posId` | `uint256` | position id to change mode |
| `_mode`  | `uint16`  | new mode to change to      |

### collateralize

Collateralize inTokens to InitCore.&#x20;

{% hint style="info" %}
The position's health factor will increase after a successful collateralization.
{% endhint %}

```solidity
function collateralize(uint256 _posId, address _pool) external;
```

Parameters:

| Name     | Type      | Description                     |
| -------- | --------- | ------------------------------- |
| `_posId` | `uint256` | position id to collateralize to |
| `_pool`  | `address` | lending pool to collateralize   |

### decollateralize

Decollateralize inTokens from InitCore.

{% hint style="info" %}
The position's health factor will decrease after a successful decollateralization.
{% endhint %}

{% hint style="info" %}
Decollateralize will transfer inTokens to the specified receiver address. If the underlying token is desired, users can `burn` the received inTokens.
{% endhint %}

```solidity
function decollateralize(uint256 _posId, address _pool, uint256 _shares, address _to) external;
```

Parameters:

| Name      | Type      | Description                         |
| --------- | --------- | ----------------------------------- |
| `_posId`  | `uint256` | position id to decollateralize from |
| `_pool`   | `address` | lending pool to decollateralize     |
| `_shares` | `uint256` | shares amount to decollateralize    |
| `_to`     | `address` | address to receive the inTokens     |

### collateralizeWLp

Collateralize supported wrapped LPs to InitCore.&#x20;

{% hint style="info" %}
The position's health factor will increase after a successful collateralization.
{% endhint %}

```solidity
function collateralizeWLp(uint256 _posId, address _wLp, uint256 _tokenId) external;
```

Parameters:

| Name       | Type\`    | Description                         |
| ---------- | --------- | ----------------------------------- |
| `_posId`   | `uint256` | position id to collateralize wLp to |
| `_wLp`     | `address` | wrapped LP contract address         |
| `_tokenId` | `uint256` | wrapped LP token id                 |

### decollateralizeWLp

Decollateralize supported wrapped LPs from InitCore.

{% hint style="info" %}
The position's health factor will decrease after a successful decollateralization.
{% endhint %}

```solidity
function decollateralizeWLp(
    uint256 _posId, 
    address _wLp, 
    uint256 _tokenId, 
    uint256 _amt, 
    address _to
) external;
```

Parameters:

| Name       | Type      | Description                             |
| ---------- | --------- | --------------------------------------- |
| `_posId`   | `uint256` | position id to decollateralize wLp from |
| `_wLp`     | `address` | wrapped LP contract address             |
| `_tokenId` | `uint256` | wrapped LP token id to decollateralize  |
| `_amt`     | `uint256` | wrapped LP amount to decollateralize    |
| `_to`      | `address` | address to receive the underlying LP.   |

### liquidate

Liquidate unhealthy position by repaying partial debt and receiving a portion of the collateral, with a small liquidation premium.&#x20;

{% hint style="info" %}
A position can only be liquidated if the position is unhealthy (health factor < 1).
{% endhint %}

{% hint style="info" %}
A liquidator can only liquidate a position until the position's health factor does not exceed the `Config.maxHealthAfterLiq_e18` value.
{% endhint %}

```solidity
function liquidate(
    uint256 _posId, 
    address _poolToRepay, 
    uint256 _repayShares, 
    address _poolOut, 
    uint256 _minShares
) external returns (uint256 shares);
```

Parameters:

| Name           | Type      | Description                                                         |
| -------------- | --------- | ------------------------------------------------------------------- |
| `_posId`       | `uint256` | position id to liquidate                                            |
| `_poolToRepay` | `address` | lending pool address to repay  for liquidation                      |
| `_repayShares` | `uint256` | shares amount for repay                                             |
| `_poolOut`     | `address` | lending pool address to receive inToken collateral from liquidation |
| `_minShares`   | `uint256` | min shares for liquidation (slippage control)                       |

Returns:

| Name     | Type      | Description                   |
| -------- | --------- | ----------------------------- |
| `shares` | `uint256` | shares of `_poolOut` received |

### liquidateWLp

Liquidate unhealthy position by repaying partial debt and receiving a portion of the WLp collateral, with a small liquidation premium.

{% hint style="info" %}
A position can only be liquidated if the position is unhealthy (health factor < 1).
{% endhint %}

{% hint style="info" %}
&#x20;A liquidator can only liquidate a position until the position's health factor does not exceed the `Config.maxHealthAfterLiq_e18` value.
{% endhint %}

```solidity
function liquidateWLp(
    uint256 _posId,
    address _poolToRepay,
    uint256 _repayShares,
    address _wLp,
    uint256 _tokenId,
    uint256 _minlpOut
) external returns (uint256 lpAmtOut);
```

Parameters:

| Name           | Type      | Description                                                |
| -------------- | --------- | ---------------------------------------------------------- |
| `_posId`       | `uint256` | position id to liquidate                                   |
| `_poolToRepay` | `address` | lending pool address to repay for liquidation              |
| `_repayShares` | `uint256` | shares amount for repay                                    |
| `_wLp`         | `address` | wrapped LP address to receive collateral from liquidation  |
| `_tokenId`     | `uint256` | wrapped LP token id to receive collateral from liquidation |
| `_minlpOut`    | `uint256` | min LP amount for liquidation (slippage control)           |

Returns:

| Name       | Type      | Description        |
| ---------- | --------- | ------------------ |
| `lpAmtOut` | `uint256` | LP amount received |

### flash

Flashloan tokens from InitCore and return them in the same transaction.

{% hint style="info" %}
`flash` will invoke `flashCallback` to the `msg.sender` to execute arbitrary data.
{% endhint %}

{% hint style="info" %}
The caller must implement `flashCallback` function. It is also recommended that the `flashCallback` function validates that the caller is InitCore. (`require(msg.ender == INIT_CORE);)`
{% endhint %}

```solidity
function flash(address[] calldata _pools, uint256[] calldata _amts, bytes calldata _data) external;
```

Parameters:

| Name     | Type        | Description                                 |
| -------- | ----------- | ------------------------------------------- |
| `_pools` | `address`   | array of pools to borrow tokens from        |
| `_amts`  | `uint256[]` | array of token amounts to borrow            |
| `_data`  | `bytes`     | custom data to be passed to `flashCallback` |

### multicall

Multicall to allow batched transactions from the caller. Any positions interacted in the multicall will delay the health check to the end of the multicall. This allows users to be able to borrow tokens out before providing collaterals to InitCore.

```solidity
function multicall(bytes[] calldata data) external payable returns (bytes[] memory results);
```

Parameters:&#x20;

| Name    | Type      | Description                         |
| ------- | --------- | ----------------------------------- |
| `_data` | `bytes[]` | array of bytes to perform multicall |

Returns:

| Name      | Type      | Description                                                |
| --------- | --------- | ---------------------------------------------------------- |
| `results` | `bytes[]` | array of bytes containing the return data of each sub-call |

### getCollateralCreditCurrent\_e36

Get a position's collateral credit (with borrow interest accrual) with `10^36` precision.

{% hint style="warning" %}
This is not a view function.&#x20;
{% endhint %}

```solidity
function getCollateralCreditCurrent_e36(uint256 _posId) external returns (uint256 collCredit_e36);
```

Parameters:

| Name     | Type      | Description                          |
| -------- | --------- | ------------------------------------ |
| `_posId` | `uint256` | position id to get collateral credit |

Returns:

| Name             | Type      | Description                                                  |
| ---------------- | --------- | ------------------------------------------------------------ |
| `collCredit_e36` | `uint256` | collateral credit in `10^36` precision with interest accrual |

### getBorrowCreditCurrent\_e36

Get a position's borrow credit (with borrow interest accrual) with `10^36` precision.

{% hint style="warning" %}
This is not a view function.
{% endhint %}

```solidity
function getBorrowCreditCurrent_e36(uint256 _posId) external returns (uint256 borrowCredit_e36);
```

Parameters:

| Name     | Type      | Description                      |
| -------- | --------- | -------------------------------- |
| `_posId` | `uint256` | position id to get borrow credit |

Returns:

| Name               | Type      | Description                                              |
| ------------------ | --------- | -------------------------------------------------------- |
| `borrowCredit_e36` | `uint256` | borrow credit in `10^36` precision with interest accrual |

### getPosHealthCurrent\_e18

Get a position's health factor (with borrow interest accrual) with `10^18` precision.

{% hint style="warning" %}
This is not a view function.
{% endhint %}

```solidity
function getPosHealthCurrent_e18(uint256 _posId) external returns (uint256 health_e18);
```

Parameters:

| Name     | Type      | Description                      |
| -------- | --------- | -------------------------------- |
| `_posId` | `uint256` | position id to get health factor |

Returns:

| Name         | Type      | Description                                              |
| ------------ | --------- | -------------------------------------------------------- |
| `health_e18` | `uint256` | health factor in `10^18` precision with interest accrual |

### callback

Execute a callback function from InitCore to the target address with custom data and `msg.value`. This should be used in conjunction with `multicall`.

{% hint style="info" %}
The target address must implement `coreCallback` function. It is also recommended that `coreCallback` function validates that the caller is InitCore. (`require(msg.ender == INIT_CORE);`&#x20;
{% endhint %}

```solidity
function callback(address _to, uint256 _value, bytes memory _data) external returns (bytes memory result);
```

Parameters:

| Name     | Type      | Description                                                                        |
| -------- | --------- | ---------------------------------------------------------------------------------- |
| `_to`    | `address` | call target address                                                                |
| `_value` | `uint256` | `msg.value` to pass to the call                                                    |
| `_data`  | `bytes`   | bytes data of the low-level function call (should also include function signature) |

Returns:

| Name     | Type    | Description                           |
| -------- | ------- | ------------------------------------- |
| `result` | `bytes` | bytes-encoded return data of the call |

### transferToken

Transfer tokens from the caller to the specified address. This should be used in conjunction with `multicall` to facilitate token transfers for depositing to lending pools.

{% hint style="info" %}
The caller must pre-approves the InitCore before the function call.
{% endhint %}

```solidity
function transferToken(address _token, address _to, uint _amt) external;
```

Parameters:

| Name     | Type      | Description                  |
| -------- | --------- | ---------------------------- |
| `_token` | `address` | token address to transfer    |
| `_to`    | `address` | address to receive the token |
| `_amt`   | `uint256` | token amount to transfer     |
