# Protocol interaction Theory

## Mint

\
`#Ok: the total balance in underlying (fToken converted to underlying amount) the user has in the market.`\
`#Err:  Insufficient Balance, if the user does not have enough underlying in the protocol.`

## Redeem

```
mint(market_id : Principal, amount : Nat) : async T.TxReceipt
```

Converts fTokens in the market of ID *market\_id* to the underlying asset, and stores it in the book. The amount depends on the current exchange rate and the *amount specified.* \
\
**Returns:**

\
`#Ok: The current amount owned by the user, still in the market, as the underlying asset.`\
`#Err:  Unauthorized, if a user tries to redeem while using the fTokens as collateral for a borrow.`

## Declare/remove as collateral

```
enter_market(market_id : Principal) : async ()
```

Locks the value the user has in the market of ID market\_id as collateral for a borrow. This allows us to offer users the ability to borrow against certain assets instead of risking their entire portfolio.\
\
**Returns:**\
\
`NULL type`

And to remove an asset:

```
exit_market(market_id : Principal) : async ()
```

Removes the user balance of market\_id to be used as collateral. Will fail if user has a borrow open.\
\
**Returns:**\
\
`NULL type`

## Borrow

Note: If you have any amount of lent balance open for this borrow, you will not be able to borrow any additional amount.

```
borrow(_token : Principal, _amount : Nat) : async T.TxReceipt
```

Opens a borrow position in market\_*id of the size* amount. \
\
**Returns:**

\
`#Ok: The total borrow balance the user has in market of ID '_token'`

## Repay

```
repay(_token : Principal, _underlying_token_amount : Nat) : async T.TxReceipt
```

Repays a borrow of the size underlying\_*token\_amount* in the market \_token. Can partially repay an open borrow.\
\
**Returns:**

\
`#Ok: Remaining borrow balance.`\
`#Err:  Insufficient Balance, if a user tries to repay with not enough book balance/borrow balance.`

## Liquidate

```
liquidate(_collateral_token:T.Token,_borrowed_token:T.Token,_borrower:Principal,_underlying_token_amount:Nat): async T.TxReceipt
```

Liquidates \_borrower and gives the collateral to the caller.

\
`#Ok: Remaining fToke balance.`\
`#Err:  Insufficient Balance - Token Not Found - Principal Not Found (wrong borrower ID)`


---

# 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://finterest.gitbook.io/product-docs/for-later/protocol-interaction-theory.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.
