# Message Processing

{% hint style="info" %}
Please note, bounced messages are omitted for a better understanding of basic processing.
{% endhint %}

### 1. Deposit

#### &#x20;1.1 Optimistic deposit (instant)

The user sends `deposit` message to Pool. Pool asks `mint` and sends Pool Jettons to Staker.

<figure><img src="https://1416290228-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuzSBGDc0tvURRLaZsYuk%2Fuploads%2FkAS9pDgEJFKXJGnD8mQd%2Fpool-3-deposit-optimistic.drawio%20(1).svg?alt=media&#x26;token=6fe67d89-70ca-4f5a-96d4-71579960fee0" alt=""><figcaption></figcaption></figure>

####

#### 1.2 Pessimistic deposit (delayed)&#x20;



{% hint style="info" %}
&#x20;Payouts are NFTs currently but could be implemented as Jettons. NFT Payouts are represented by two collections Payout NFT deposit and Payout NFT withdrawal.
{% endhint %}

The user sends a `deposit` message to Pool. Pool requests Payout Collection NFT mint for the user a Payout NFT Item (deposit).&#x20;

<figure><img src="https://1416290228-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuzSBGDc0tvURRLaZsYuk%2Fuploads%2FnIkR4TRSiIoAAUZ0Qk0U%2Fmacschemes%20(2)-deposit_pessimistic_mint.drawio%20(2).svg?alt=media&#x26;token=79a681d5-d131-4e3d-802e-ee80bdf9f268" alt=""><figcaption></figcaption></figure>



The user is an owner of NFT, Pool of Payout NFT Collection.&#x20;

At the end of the round, Pool inited `start distribution`, all Payouts Items burn and the user gets Pool Jettons in exchange.

<figure><img src="https://1416290228-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuzSBGDc0tvURRLaZsYuk%2Fuploads%2FHyz77gci63r3GjSt1GYJ%2Fmacschemes%20(2)-deposit_pessimistic_payout_burn.drawio%20(2).svg?alt=media&#x26;token=0bf5b513-a257-4c33-9406-84b5556cc3b1" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Optimistic/Pessimistic flag - global Pool config parameter&#x20;
{% endhint %}

### 2 - Withdrawals

#### 2.1 - Optimistic withdrawal (Instant)

At any moment Staker can ask to withdraw their funds from the Pool. The Staker asks to burn his Pool Jettons and get back TON amount including the reward.

If the Pool can not pay the requested sum currently Pool Jettons will be minted and returned to the Staker's Pool Jetton Wallet.&#x20;

<figure><img src="https://1416290228-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuzSBGDc0tvURRLaZsYuk%2Fuploads%2F8wqyCZJiGlLKGSOiJ4aF%2Fmacschemes%20(2)-withdrawal_optimistic_kof.drawio%20(3).svg?alt=media&#x26;token=e3ff6b36-d44e-42dd-91d7-525efcca1bfb" alt=""><figcaption></figcaption></figure>

#### 2.2 Pessimistic withdrawal (Delayed)&#x20;

The user sends a "withdraw" message and gets Payout NFT Item (withdrawal).&#x20;

<figure><img src="https://1416290228-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuzSBGDc0tvURRLaZsYuk%2Fuploads%2F8e6HKlpSraPGEX2AZTtj%2Fmacschemes%20(2)-withdrawal_pessimistic_payout_mint.drawio%20(3).svg?alt=media&#x26;token=9904dfc4-0304-4f16-bf62-c7a6d1837195" alt=""><figcaption></figcaption></figure>



Every round `pool_update()` is launched, which leads to burning Payout NFT Items (users' Payout NFT withdrawals) and converting them to TON or Pool Jettons. TON and Pool Jettons distributed to the appropriate owner of the original NFT Payout.&#x20;



<figure><img src="https://1416290228-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuzSBGDc0tvURRLaZsYuk%2Fuploads%2Fq1eDMw5fMeFKZ3hISNFW%2Fmacschemes%20(2)-withdrawal_pessimistic_payout_burn.drawio%20(2).svg?alt=media&#x26;token=96429e22-b797-45df-8cc3-ba99630f09e4" alt=""><figcaption></figcaption></figure>

If it is Pool Jetton user can ask to immediately withdraw TON in exchange for Pool Jettons he got for previous finalized validation rounds.&#x20;



### 3 Loan

A Controller asks Pool for a loan, with a message `request_loan` that contains desired interest and prepays amount for the loan.  The Controller asks the Pool for a loan. If this request is approved the answer msg `credit` is displayed and the controller marks the borrowing time and updates the borrowed\_amount on-chain.

<figure><img src="https://1416290228-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuzSBGDc0tvURRLaZsYuk%2Fuploads%2FBKwH4ohSMP8QNs6kpURK%2Fpool-3-request%20loan.drawio%20(1).svg?alt=media&#x26;token=f8295f92-b535-4273-9f6d-b7a02958de01" alt=""><figcaption></figcaption></figure>

### 4 Using borrowed amounts for staking&#x20;

A Validator sends a `new_stake` request to a Controller, the Controller sends a `new_stake` request to an Elector with a borrowed amount (where both opcodes are equal).

<figure><img src="https://1416290228-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuzSBGDc0tvURRLaZsYuk%2Fuploads%2FfNzz1N7tAL8wdWf3XM2c%2F4-Using%20loan.drawio.svg?alt=media&#x26;token=a3668bd1-446e-44ee-bfa3-cd6c1eb83be3" alt=""><figcaption><p>Using borrowed sum for stake</p></figcaption></figure>

### 5 Loan Repayment&#x20;

#### 5.1 Regular repayment

When a regular loan repayment is requested, the Elector sends the `recover_stake_ok` request to the Controller. The Controller then sends a `pool::loan_repayment` message with a value equal to the `MIN_TONS_FOR_STORAGE` added with the borrowed\_amount.

<figure><img src="https://1416290228-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuzSBGDc0tvURRLaZsYuk%2Fuploads%2F2jdGqq8FaZUxY9B9uU27%2Fpool-3-loan%20repayment%20elector.drawio%20(1).svg?alt=media&#x26;token=fe618036-e8f5-427e-96d4-66fbbdcacffa" alt=""><figcaption></figcaption></figure>



#### 5.2 - Request by a Validator or an external participant

The sender (any contract) can request the Controller return borrowed sum with `controller::return_unused_loan` op. If it happens after `grace_period` time after the end of the round and the Sender is not a Validator of the corresponding Controller, the Controller will pay a fine to the sender.

<figure><img src="https://1416290228-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuzSBGDc0tvURRLaZsYuk%2Fuploads%2Fz2rY0WBo6eAb1habiETt%2Fpool-3-loan%20repayment%20validator%20and%20other.drawio%20(1).svg?alt=media&#x26;token=7945782f-7e63-428f-936e-2caa882234d1" alt=""><figcaption></figcaption></figure>

#### &#x20;5.3 Forced repayment

When a forced loan repayment is requested, the Governor sends the `return_available_funds` message to the Controller. The Controller then sends the `loan_repayment` a message with the correct `value` equals to the `min(borrowed, available_funds)`.&#x20;

If the Pool is able to find the correct borrower and value in its borrowers list, the loan is closed otherwise an error is thrown (so far the protocol has not implemented the logic for cases when the loan is not closed).

<figure><img src="https://1416290228-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuzSBGDc0tvURRLaZsYuk%2Fuploads%2FaZkjlculRUCGU7KwKXpf%2Fpool-3-loan%20repayment%20governor.drawio%20(1).svg?alt=media&#x26;token=7f6f7255-1c82-4ba5-934d-706bd1c6c55f" alt=""><figcaption></figcaption></figure>
