> For the complete documentation index, see [llms.txt](https://erc20-docs.particle.trade/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://erc20-docs.particle.trade/logic/overview.md).

# 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.
