> For the complete documentation index, see [llms.txt](https://ston-fi.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ston-fi.gitbook.io/docs/developer-section/dex/smart-contracts/v2/examples/lp-provide.md).

# LP Provide Examples

## Deposit liquidity (both tokens)

Liquidity provision is done via 2 transactions: deposit of the first token and deposit of the second token.

### Deposit of the first token

The amount of the first token sent is stored on a user's lp account awaiting a transaction of the second token.

```mermaid
flowchart LR
	A0(("Bob"))
	A1["Bob<br/>Token1<br/>Wallet"]
	A2["Router<br/>Token1<br/>Wallet"]
	A3{"Router"}
	A4(["Pool<br/>Token1-Token2"])
	A5[["Bob<br/>Token1-Token2<br/>Lp Account"]]

	A0 --> |index: 0<br/>op::transfer|A1
	A1 --> |index: 1<br/>op::internal_transfer|A2
	A2 --> |index: 2<br/>op::transfer_notification|A3
	A2 -.-> |index: 3<br/>op::excesses|A0
	A3 --> |index: 4<br/>op::provide_lp|A4
	A4 --> |index: 5<br/>op::add_liquidity|A5
	A5 -.-> |index: 6<br/>op::excesses|A0

	linkStyle 0 stroke:#ff4747,color:#ff4747
	linkStyle 1 stroke:#ff4747,color:#ff4747
	linkStyle 2 stroke:#ff4747,color:#ff4747
	linkStyle 3 stroke:#0400f0,color:#0400f0
	linkStyle 4 stroke:#ff4747,color:#ff4747
	linkStyle 5 stroke:#ff4747,color:#ff4747
	linkStyle 6 stroke:#0400f0,color:#0400f0

```

### Deposit of the second token

When the second token is sent, lp account automatically initiates liquidity addition into the pool. As a result, full amounts of deposited tokens are used as new liquidity and the user receives lp tokens.

```mermaid
flowchart LR
	A0(("Bob"))
	A1["Bob<br/>Token2<br/>Wallet"]
	A2["Router<br/>Token2<br/>Wallet"]
	A3{"Router"}
	A4(["Pool<br/>Token1-Token2"])
	A5[["Bob<br/>Token1-Token2<br/>Lp Account"]]
	A6["Bob<br/>Token1-Token2<br/>Lp Wallet"]

	A0 --> |index: 0<br/>op::transfer|A1
	A1 --> |index: 1<br/>op::internal_transfer|A2
	A2 --> |index: 2<br/>op::transfer_notification|A3
	A2 -.-> |index: 3<br/>op::excesses|A0
	A3 --> |index: 4<br/>op::provide_lp|A4
	A4 --> |index: 5<br/>op::add_liquidity|A5
	A5 -.-> |index: 6<br/>op::cb_add_liquidity|A4
	A4 --> |index: 7<br/>op::internal_transfer|A6
	A6 -.-> |index: 8<br/>op::excesses|A0

	linkStyle 0 stroke:#ff4747,color:#ff4747
	linkStyle 1 stroke:#ff4747,color:#ff4747
	linkStyle 2 stroke:#ff4747,color:#ff4747
	linkStyle 3 stroke:#0400f0,color:#0400f0
	linkStyle 4 stroke:#ff4747,color:#ff4747
	linkStyle 5 stroke:#ff4747,color:#ff4747
	linkStyle 6 stroke:#02dbdb,color:#02dbdb
	linkStyle 7 stroke:#ff4747,color:#ff4747
	linkStyle 8 stroke:#0400f0,color:#0400f0

```

**Notes:**

* the `Lp Account` contract is deleted on `cb_add_liquidity` in order to not pay storage fees (each deposit sends state init so it will be redeployed as soon as any new deposit occurs)
* when depositing initial liquidity `0.000001001` lp tokens will be reserved on pool, the rest of liquidity will be sent to the user

## Deposit liquidity (single side provision)

It is possible to deposit liquidity by sending just 1 type of token, the pool will automatically perform a swap and use the resulting amount to mint lp tokens

```mermaid
flowchart LR
	A0(("Bob"))
	A1["Bob<br/>Token1<br/>Wallet"]
	A2["Router<br/>Token1<br/>Wallet"]
	A3{"Router"}
	A4(["Pool<br/>Token1-Token2"])
	A5[["Bob<br/>Token1-Token2<br/>Lp Account"]]
	A6["Bob<br/>Token1-Token2<br/>Lp Wallet"]

	A0 --> |index: 0<br/>op::transfer|A1
	A1 --> |index: 1<br/>op::internal_transfer|A2
	A2 --> |index: 2<br/>op::transfer_notification|A3
	A2 -.-> |index: 3<br/>op::excesses|A0
	A3 --> |index: 4<br/>op::provide_lp|A4
	A4 --> |index: 5<br/>op::add_liquidity|A5
	A5 -.-> |index: 6<br/>op::cb_add_liquidity|A4
	A4 --> |index: 7<br/>op::internal_transfer|A6
	A6 -.-> |index: 8<br/>op::excesses|A0

	linkStyle 0 stroke:#ff4747,color:#ff4747
	linkStyle 1 stroke:#ff4747,color:#ff4747
	linkStyle 2 stroke:#ff4747,color:#ff4747
	linkStyle 3 stroke:#0400f0,color:#0400f0
	linkStyle 4 stroke:#ff4747,color:#ff4747
	linkStyle 5 stroke:#ff4747,color:#ff4747
	linkStyle 6 stroke:#02dbdb,color:#02dbdb
	linkStyle 7 stroke:#ff4747,color:#ff4747
	linkStyle 8 stroke:#0400f0,color:#0400f0

```

## Deposit liquidity (with payload)

It is possible to specify who will receive lp tokens and to include a payload which will be sent as `transfer_notification` (e.g. `Farm` contract)

```mermaid
flowchart LR
	A0(("Alice"))
	A1["Alice<br/>Token1<br/>Wallet"]
	A2["Router0<br/>Token1<br/>Wallet"]
	A3{"Router0"}
	A4(["Pool0<br/>Token1-Token2"])
	A5[["Alice<br/>Token1-Token2<br/>Lp Account0"]]
	A6["SomeContract<br/>Token1-Token2<br/>Lp Wallet0"]
	A7(("SomeContract"))

	A0 --> |index: 0<br/>op: transfer|A1
	A1 --> |index: 1<br/>op: internal_transfer|A2
	A2 --> |index: 2<br/>op: transfer_notification|A3
	A2 -.-> |index: 3<br/>op: excesses|A0
	A3 --> |index: 4<br/>op: provide_lp|A4
	A4 --> |index: 5<br/>op: add_liquidity|A5
	A5 -.-> |index: 6<br/>op: cb_add_liquidity|A4
	A4 --> |index: 7<br/>op: internal_transfer|A6
	A6 --> |index: 8<br/>op: transfer_notification|A7
	A6 -.-> |index: 9<br/>op: excesses|A0

	linkStyle 0 stroke:#ff4747,color:#ff4747
	linkStyle 1 stroke:#ff4747,color:#ff4747
	linkStyle 2 stroke:#ff4747,color:#ff4747
	linkStyle 3 stroke:#0400f0,color:#0400f0
	linkStyle 4 stroke:#ff4747,color:#ff4747
	linkStyle 5 stroke:#ff4747,color:#ff4747
	linkStyle 6 stroke:#02dbdb,color:#02dbdb
	linkStyle 7 stroke:#ff4747,color:#ff4747
	linkStyle 8 stroke:#ff4747,color:#ff4747
	linkStyle 9 stroke:#0400f0,color:#0400f0

```

## Refund deposited liquidity

In the event that the issued amount of lp tokens doesn't match the specified minimum value, all deposited liquidity is returned to lp account and can be withdrawn by user.

### Liquidity is refunded to lp account

```mermaid
flowchart LR
	A0(("Bob"))
	A1["Bob<br/>Token2<br/>Wallet"]
	A2["Router<br/>Token2<br/>Wallet"]
	A3{"Router"}
	A4(["Pool<br/>Token1-Token2"])
	A5[["Bob<br/>Token1-Token2<br/>Lp Account"]]

	A0 --> |index: 0<br/>op::transfer|A1
	A1 --> |index: 1<br/>op::internal_transfer|A2
	A2 --> |index: 2<br/>op::transfer_notification|A3
	A2 -.-> |index: 3<br/>op::excesses|A0
	A3 --> |index: 4<br/>op::provide_lp|A4
	A4 --> |index: 5<br/>op::add_liquidity|A5
	A5 -.-> |index: 6<br/>op::cb_add_liquidity|A4
	A4 --> |index: 7<br/>op::add_liquidity|A5
	A5 -.-> |index: 8<br/>op::excesses|A0

	linkStyle 0 stroke:#ff4747,color:#ff4747
	linkStyle 1 stroke:#ff4747,color:#ff4747
	linkStyle 2 stroke:#ff4747,color:#ff4747
	linkStyle 3 stroke:#0400f0,color:#0400f0
	linkStyle 4 stroke:#ff4747,color:#ff4747
	linkStyle 5 stroke:#ff4747,color:#ff4747
	linkStyle 6 stroke:#02dbdb,color:#02dbdb
	linkStyle 7 stroke:#ff4747,color:#ff4747
	linkStyle 8 stroke:#0400f0,color:#0400f0

```

### Withdraw refunded liquidity

```mermaid
flowchart LR
	A0(("Bob"))
	A1[["Bob<br/>Token1-Token2<br/>Lp Account"]]
	A2(["Pool<br/>Token1-Token2"])
	A3{"Router"}
	A4["Router<br/>Token2<br/>Wallet"]
	A5["Router<br/>Token1<br/>Wallet"]
	A6["Bob<br/>Token2<br/>Wallet"]
	A7["Bob<br/>Token1<br/>Wallet"]

	A0 --> |index: 0<br/>op::refund_me|A1
	A1 --> |index: 1<br/>op::cb_refund_me|A2
	A2 --> |index: 2<br/>op::pay_to|A3
	A2 --> |index: 3<br/>op::pay_to|A3
	A3 --> |index: 4<br/>op::transfer|A4
	A3 --> |index: 5<br/>op::transfer|A5
	A4 --> |index: 6<br/>op::internal_transfer|A6
	A5 --> |index: 7<br/>op::internal_transfer|A7
	A6 -.-> |index: 8<br/>op::excesses|A0
	A7 -.-> |index: 9<br/>op::excesses|A0

	linkStyle 0 stroke:#ff4747,color:#ff4747
	linkStyle 1 stroke:#ff4747,color:#ff4747
	linkStyle 2 stroke:#ff4747,color:#ff4747
	linkStyle 3 stroke:#ff4747,color:#ff4747
	linkStyle 4 stroke:#ff4747,color:#ff4747
	linkStyle 5 stroke:#ff4747,color:#ff4747
	linkStyle 6 stroke:#ff4747,color:#ff4747
	linkStyle 7 stroke:#ff4747,color:#ff4747
	linkStyle 8 stroke:#0400f0,color:#0400f0
	linkStyle 9 stroke:#0400f0,color:#0400f0

```

## Withdraw liquidly

Liquidly is withdrawn from a pool by burning lp tokens, a user then receives both pool tokens based on the current exchange rate.

```mermaid
flowchart LR
	A0(("Bob"))
	A1["Bob<br/>Token1-Token2<br/>Lp Wallet"]
	A2(["Pool<br/>Token1-Token2"])
	A3{"Router"}
	A4["Router<br/>Token2<br/>Wallet"]
	A5["Router<br/>Token1<br/>Wallet"]
	A6["Bob<br/>Token2<br/>Wallet"]
	A7["Bob<br/>Token1<br/>Wallet"]

	A0 --> |index: 0<br/>op::burn|A1
	A1 --> |index: 1<br/>op::burn_notification_ext|A2
	A2 --> |index: 2<br/>op::pay_to|A3
	A2 --> |index: 3<br/>op::pay_to|A3
	A3 --> |index: 4<br/>op::transfer|A4
	A3 --> |index: 5<br/>op::transfer|A5
	A4 --> |index: 6<br/>op::internal_transfer|A6
	A5 --> |index: 7<br/>op::internal_transfer|A7
	A6 -.-> |index: 8<br/>op::excesses|A0
	A7 -.-> |index: 9<br/>op::excesses|A0

	linkStyle 0 stroke:#ff4747,color:#ff4747
	linkStyle 1 stroke:#ff4747,color:#ff4747
	linkStyle 2 stroke:#ff4747,color:#ff4747
	linkStyle 3 stroke:#ff4747,color:#ff4747
	linkStyle 4 stroke:#ff4747,color:#ff4747
	linkStyle 5 stroke:#ff4747,color:#ff4747
	linkStyle 6 stroke:#ff4747,color:#ff4747
	linkStyle 7 stroke:#ff4747,color:#ff4747
	linkStyle 8 stroke:#0400f0,color:#0400f0
	linkStyle 9 stroke:#0400f0,color:#0400f0

```

## Providing already deposited liquidity

A user can deposit liquidity after it was refunded to lp account without doing a withdraw.

```mermaid
flowchart LR
	A0(("Bob"))
	A1[["Bob<br/>Token1-Token2<br/>Lp Account"]]
	A2(["Pool<br/>Token1-Token2"])
	A3["Bob<br/>Token1-Token2<br/>Lp Wallet"]

	A0 --> |index: 0<br/>op::direct_add_liquidity|A1
	A1 --> |index: 1<br/>op::cb_add_liquidity|A2
	A2 --> |index: 2<br/>op::internal_transfer|A3
	A3 -.-> |index: 3<br/>op::excesses|A0

	linkStyle 0 stroke:#ff4747,color:#ff4747
	linkStyle 1 stroke:#ff4747,color:#ff4747
	linkStyle 2 stroke:#ff4747,color:#ff4747
	linkStyle 3 stroke:#0400f0,color:#0400f0

```
