# Overview

We provide an illustration to the protocol logic in <https://excalidraw.com/#json=TcmwLn2W4K9H_UlCExFXa,J_yKjXNaowF0gYL8uvPruA>

The basic flow of opening a leverage position in LAMM contains 3 steps:

* Borrow Liquidity: input `tokenId` and `liquidity`, the protocol decreases `liquidity` from an LP position to obtain `amountFromBorrowed` and `amountToBorrowed` for a token pair.
* Swap: In one swap, the protocol swaps `amountFromBorrowed` together with `marginFrom` into `amountReceived`. The amount is then locked in contract.
* Accounting: The protocol ensures enough amount is swapped, i.e. `amountReceived` + `amountToBorrowd` + `marginTo` is enough to cover `collateralTo`. The leftover amount are kept as premiums.

Similarly, the basic flow of closing an LAMM position contains 3 steps:

* Accounting: Based on `liquidity` in the record (a `lien`), determine the amount to repay, i.e. `amountFromAdd` and `amountToAdd`.&#x20;
* Swap: In one swap, at least swap enough to (1) get `amountToAdd` and (2) leave enough for `amountFromAdd`.
* Repay: increase `liquidity` back for LP and add `tokensOwed`, the leftover amounts (premiums subtract owed and PnLs) are all refund to the borrower.

For more details, please refer to the illustrative figures and the notes in the link above. The same figures are provided in the sections next.


---

# 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://erc20-docs.particle.trade/logic/overview.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.
