# Sandwich trading

At its core, the sandwich attack involves an attacker strategically positioning their transactions around a victim’s transaction to capitalize on price variances. The attacker initially inflates the asset’s price with the transaction (1), waits for the victim’s transaction to materialize and subsequently deflates the asset’s price with the transaction (3), thereby securing a profit.

Assume a Bonding Curve holding crypto-token in reserves (τ0) that can be swapped with another token (τ1). Now, suppose that user A wants to swap 20 units of τ0 in her wallet for at least 15 units of τ1. This requires to append to the blockchain a transaction of the form&#x20;

$$
A : swap^0 (20 : \tau\_0 , 15 : τ\_1),
$$

where the prefix A indicates the wallet involved in the transaction, swap is the called AMM function, and the superscript **0** indicates the swap direction, i.e. deposit **20** : $$τ\_0$$ to receive back at least **15** : $$τ\_1$$ (a superscript 1 would indicate the opposite direction). In a bonding curve, the actual amount of $$τ\_1$$ transferred to A must be such that the “invariant" is preserved before and after a swap. Now, suppose that an adversary M observes A’s transaction, and appends to the blockchain the following sandwich: &#x20;

$$M : swap^0 (5.9 : τ\_0, 5.5 : τ\_1)$$&#x20;

$$A: swap^0 (20 : τ\_0, 15 : τ\_1)$$&#x20;

$$M : swap^1 (25.9 : τ\_0, 20.6 : τ\_1)$$&#x20;

where the last transaction is in the opposite direction, i.e. M sends **20.6** : $$τ\_1$$ to receive at least **25.9** : $$τ\_0$$. As a result, A only yields the minimum amount of **15** : τ1 in return for **20** : $$τ\_0$$. This implies that an extra amount has been gained by **M** and lost by **A**. This is how a sandwich attack takes place.

<br>


---

# 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://bonding-curve-research-group.gitbook.io/bonding-curve-research-group-library/attack-vectors/sandwich-trading.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.
