# WIchiFarm

This contract wraps the ICHI MasterChef farming contract by holding ICHI LP tokens in the BlueberryBank, while the underlying LP tokens are deposited into the ICHI farming pool to generate additional yields The LP tokens are identified by token IDs encoded from the LP token address and accPerShare of the deposited time.

### Variables

The `WIchiFarm` contract has the following state variables:

* `ICHIv1`: An `IERC20Upgradeable` variable that represents the address of the legacy ICHI token.
* `ICHI`: An `IIchiV2` variable that represents the address of the ICHI v2 token.
* `ichiFarm`: An `IIchiFarm` variable that represents the address of the ICHI farming contract.

### Functions

#### initialize

{% code overflow="wrap" %}

```solidity
function initialize(address _ichi, address _ichiv1, address _ichiFarm) external initializer
```

{% endcode %}

This function initializes the contract's state variables with the provided ICHI v1, ICHI v2, and ICHI farming contract addresses.

**Parameters:**

<table><thead><tr><th width="199">Name</th><th width="126.33333333333331">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>_ichi</code></td><td>address</td><td>Address of the ICHI v2 token contract</td></tr><tr><td><code>_ichiv1</code></td><td>address</td><td>Address of the ICHI v1 token address</td></tr><tr><td><code>_ichiFarm</code></td><td>address</td><td>Address of the ICHI farming contract</td></tr></tbody></table>

#### encodeId

{% code overflow="wrap" %}

```solidity
function encodeId(uint256 pid, uint256 ichiPerShare) public pure returns (uint256 id)
```

{% endcode %}

This function encodes the pool ID and ICHI per share into an ERC-1155 token ID.

**Parameters:**

| Name           | Type    | Description                               |
| -------------- | ------- | ----------------------------------------- |
| `pid`          | uint256 | Pool ID                                   |
| `ichiPerShare` | uint256 | ICHI reward per share multiplied by 10e18 |

#### decodeId

{% code overflow="wrap" %}

```solidity
function decodeId(uint256 id) public pure returns (uint256 pid, uint256 ichiPerShare)
```

{% endcode %}

This function decodes the ERC-1155 token ID into the pool ID and ICHI per share.The pid is the first 16 bits of the token id, and ichiPerShare is the last 240 bits of the token id.

**Parameters:**

* `id` (uint256): ERC-1155 token ID

**Returns:**

<table><thead><tr><th width="172.33333333333331">Name</th><th width="132">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>pid</code></td><td>uint256</td><td>The pool ID extracted from the first 16 bits fo the <code>id</code></td></tr><tr><td><code>ichiPerShare</code></td><td>uint256</td><td>The value of ichiPerShare extracted from the last 240 bits of the <code>id</code></td></tr></tbody></table>

#### getUnderlyingToken

{% code overflow="wrap" %}

```solidity
function getUnderlyingToken(uint256 id) external view override returns (address)
```

{% endcode %}

This function returns the underlying ERC-20 token for a given ERC1155 token id. It calls `decodeId` to extract the pid from the token id and then looks up the corresponding LP token address from the ICHI farming pool.

**Parameters:**

* `id`(uint256): The ERC1155 token ID to decode

**Returns:**

* `address`: The address of the underlying ERC-20 token

#### pendingRewards

{% code overflow="wrap" %}

```solidity
function pendingRewards(uint256 tokenId, uint amount) public view override returns (address[] memory tokens, uint256[] memory rewards)
```

{% endcode %}

This function returns the pending ICHI rewards for a given ERC1155 token id and amount of LP tokens. It calls `decodeId` to extract the pid and stIchiPerShare values from the token id, looks up the current enIchiPerShare value from the ICHI farming pool, and calculates the difference in ICHI rewards based on the change in ichiPerShare values.

**Parameters:**

<table><thead><tr><th width="167.33333333333331">Name</th><th width="169">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>tokenId</code></td><td>uint256</td><td>The ERC1155 token ID to encode</td></tr><tr><td><code>amount</code></td><td>uint</td><td>The amount of share</td></tr></tbody></table>

**Returns:**

<table><thead><tr><th width="166.33333333333331">Name</th><th width="171">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>tokens</code></td><td>array</td><td>An array containing the address of the reward token ( specifically ICHI)</td></tr><tr><td><code>rewards</code></td><td>uint256</td><td>An array containing the amount of ICHI rewards</td></tr></tbody></table>

#### mint

{% code overflow="wrap" %}

```solidity
function mint(uint256 pid, uint256 amount) external nonReentrant returns (uint256)
```

{% endcode %}

This function mints new ERC1155 tokens by wrapping a user's LP tokens in the ICHI farming pool. It first transfers the LP tokens from the user to the ERC1155 contract, approves the ICHI farming pool to spend the LP tokens, deposits the LP tokens into the ICHI farming pool, and then mints new ERC1155 tokens with the pid and current ichiPerShare values.

**Parameters:**

<table><thead><tr><th width="203.33333333333331">Name</th><th width="153">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>pid</code></td><td>uint256</td><td>The ID of the pool to deposit the LP tokens</td></tr><tr><td><code>amount</code></td><td>uint256</td><td>The amount of LP tokens to deposit</td></tr></tbody></table>

**Returns:**

* `uint256`: The token ID that was minted.

#### burn

{% code overflow="wrap" %}

```solidity
function burn(uint256 id, uint256 amount) external nonReentrant returns (uint256)
```

{% endcode %}

This function burns existing ERC1155 tokens to redeem a user's LP tokens from the ICHI farming pool and claim any pending ICHI rewards. It first decodes the token id to extract the pid, then burns the ERC1155 tokens from the user's account. It then calls the `harvest` and `withdraw` functions of the ICHI farming pool to claim the user's LP tokens and ICHI rewards, respectively. If the user has any pending ICHI rewards in the legacy ICHI contract, this function converts them to ICHI v2 tokens. Finally, the user's LP tokens and ICHI rewards are transferred to the user's account.

**Parameters:**

<table><thead><tr><th width="203.33333333333331">Name</th><th width="248">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>id</code></td><td>uint256</td><td>The ERC1155 token ID to burn</td></tr><tr><td><code>amount</code></td><td>uint256</td><td>The amount of tokens to burn</td></tr></tbody></table>

**Returns:**

* `uint256`: The pool ID that you will receive LP tokens back.

#### \_ensureApprove

{% code overflow="wrap" %}

```solidity
function _ensureApprove(address token, address spender, uint256 amount) internal
```

{% endcode %}

This internal function ensures that a user has approved the spender to spend at least the given amount of the given token. If the current allowance is less than the required amount, it approves the spender to spend the token up to the required amount.

**Parameters:**

<table><thead><tr><th width="201.33333333333331">Name</th><th width="171">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>token</code></td><td>address</td><td>The address of the token to check</td></tr><tr><td><code>spender</code></td><td>address</td><td>The address of the spender to check</td></tr><tr><td><code>amount</code></td><td>uint256</td><td>The amount of allowance to ensure</td></tr></tbody></table>
