# Swap Examples

## Simple swap with referral

Simple swap of one token to another.

`Bob` is user doing the swap, `Alice` is referrer, `Send` is send token, `Receive` is receive token.

{% @mermaid/diagram content="flowchart LR
A0(("Bob"))
A1\["Bob<br/>Send<br/>Wallet"]
A2\["Router<br/>Send<br/>Wallet"]
A3{"Router"}
A4(\["Pool<br/>Send-Receive"])
A5>"Alice<br/>Receive<br/>Vault"]
A6\["Router<br/>Receive<br/>Wallet"]
A7\["Bob<br/>Receive<br/>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::swap|A4
A4 -.-> |index: 5<br/>op::pay_vault|A3
A4 -.-> |index: 6<br/>op::pay_to|A3
A3 --> |index: 7<br/>op::deposit_ref_fee|A5
A3 --> |index: 8<br/>op::transfer|A6
A5 -.-> |index: 9<br/>op::excesses|A0
A6 --> |index: 10<br/>op::internal_transfer|A7
A7 -.-> |index: 11<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:#02dbdb,color:#02dbdb
linkStyle 6 stroke:#02dbdb,color:#02dbdb
linkStyle 7 stroke:#ff4747,color:#ff4747
linkStyle 8 stroke:#ff4747,color:#ff4747
linkStyle 9 stroke:#0400f0,color:#0400f0
linkStyle 10 stroke:#ff4747,color:#ff4747
linkStyle 11 stroke:#0400f0,color:#0400f0
```

" %}

## Cross-swap on the same Router

Swap of one token to another using an intermediary token. This method is used if there's no available pool for a pair of tokens but there're pools for each token and some other (the same for both) intermediary token. This type of swap is not limited to 1 intermediary: in practice an arbitrary amount of intermediaries can be used. This type of swap can be combined with cross-swap using multiple `Routers`.

`Bob` is user doing the swap, `Send` is send token, `Mid` is intermediary token, `Receive` is receive token.

{% @mermaid/diagram content="flowchart LR
A0(("Bob"))
A1\["Bob<br/>Send<br/>Wallet"]
A2\["Router<br/>Send<br/>Wallet"]
A3{"Router"}
A4(\["Pool<br/>Send-Mid"])
A5(\["Pool<br/>Mid-Receive"])
A6\["Router<br/>Receive<br/>Wallet"]
A7\["Bob<br/>Receive<br/>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::swap|A4
A4 -.-> |index: 5<br/>op::pay_to|A3
A3 --> |index: 6<br/>op::swap|A5
A5 -.-> |index: 7<br/>op::pay_to|A3
A3 --> |index: 8<br/>op::transfer|A6
A6 --> |index: 9<br/>op::internal_transfer|A7
A7 -.-> |index: 10<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:#02dbdb,color:#02dbdb
linkStyle 6 stroke:#ff4747,color:#ff4747
linkStyle 7 stroke:#02dbdb,color:#02dbdb
linkStyle 8 stroke:#ff4747,color:#ff4747
linkStyle 9 stroke:#ff4747,color:#ff4747
linkStyle 10 stroke:#0400f0,color:#0400f0
```

" %}

## Cross-swap using multiple Routers

Swap of one token to another using different `Routers`. This method is used if there's no available pool for a pair of tokens on the same Router but there're pools for each token and some other (the same for both) intermediary token on both `Routers`. This type of swap is not limited to 1 intermediary: in practice an arbitrary amount of intermediaries can be used. This type of swap can be combined with cross-swap on the same `Router`.

`Bob` is user doing the swap, `Send` is send token, `Mid` is intermediary token, `Receive` is receive token.

{% @mermaid/diagram content="flowchart LR
A0(("Bob"))
A1\["Bob<br/>Send<br/>Wallet"]
A2\["Router1<br/>Send<br/>Wallet"]
A3{"Router1"}
A4(\["Pool1<br/>Send-Mid"])
A5\["Router1<br/>Mid<br/>Wallet"]
A6\["Router2<br/>Mid<br/>Wallet"]
A7{"Router2"}
A8(\["Pool2<br/>Mid-Receive"])
A9\["Router2<br/>Receive<br/>Wallet"]
A10\["Bob<br/>Receive<br/>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::swap|A4
A4 -.-> |index: 5<br/>op::pay_to|A3
A3 --> |index: 6<br/>op::transfer|A5
A5 --> |index: 7<br/>op::internal_transfer|A6
A6 --> |index: 8<br/>op::transfer_notification|A7
A6 -.-> |index: 9<br/>op::excesses|A0
A7 --> |index: 10<br/>op::swap|A8
A8 -.-> |index: 11<br/>op::pay_to|A7
A7 --> |index: 12<br/>op::transfer|A9
A9 --> |index: 13<br/>op::internal_transfer|A10
A10 -.-> |index: 14<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:#02dbdb,color:#02dbdb
linkStyle 6 stroke:#ff4747,color:#ff4747
linkStyle 7 stroke:#ff4747,color:#ff4747
linkStyle 8 stroke:#ff4747,color:#ff4747
linkStyle 9 stroke:#0400f0,color:#0400f0
linkStyle 10 stroke:#ff4747,color:#ff4747
linkStyle 11 stroke:#02dbdb,color:#02dbdb
linkStyle 12 stroke:#ff4747,color:#ff4747
linkStyle 13 stroke:#ff4747,color:#ff4747
linkStyle 14 stroke:#0400f0,color:#0400f0
```

" %}

## Refund swap

Refund swap if there's not enough liquidity in the pool or the expected amount of receive tokens is less than expected minimum. Since multi-contract transactions in TON are not atomic it is not possible to fully refund cross swap (on the same router or multiple), in such event the user will receive some intermediate token based on the pool used to route the swap.

`Bob` is user doing the swap, `Send` is send token, `Receive` is receive token.

{% @mermaid/diagram content="flowchart LR
A0(("Bob"))
A1\["Bob<br/>Send<br/>Wallet"]
A2\["Router<br/>Send<br/>Wallet"]
A3{"Router"}
A4(\["Pool<br/>Send-Receive"])

```
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::swap|A4
A4 -.-> |index: 5<br/>op::pay_to|A3
A3 -.-> |index: 6<br/>op::transfer|A2
A2 -.-> |index: 7<br/>op::internal_transfer|A1
A1 -.-> |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:#02dbdb,color:#02dbdb
linkStyle 6 stroke:#02dbdb,color:#02dbdb
linkStyle 7 stroke:#02dbdb,color:#02dbdb
linkStyle 8 stroke:#0400f0,color:#0400f0
```

" %}
