> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dune.com/llms.txt
> Use this file to discover all available pages before exploring further.

# dex.sandwiches

> The `dex.sandwiches` table captures detailed data on the outer trades of sandwich attacks in decentralized exchanges (DEXs), recording front-running and back-running trades across various EVM networks.

export const TableSample = ({tableName, tableSchema}) => <>
    <div className="hidden dark:block">
      <iframe src={`https://dune.com/embeds/3419983/5785629?table_schema_t6f0df=${tableSchema}&table_name_t6f0df=${tableName}&darkMode=true`} style={{
  width: '100%',
  height: '500px',
  border: 'none',
  marginTop: '10px'
}} />
    </div>
    <div className="dark:hidden">
      <iframe src={`https://dune.com/embeds/3419983/5785629?table_schema_t6f0df=${tableSchema}&table_name_t6f0df=${tableName}`} style={{
  width: '100%',
  height: '500px',
  border: 'none',
  marginTop: '10px'
}} />
    </div>
  </>;

## Table Description

The `dex.sandwiches` table captures detailed data on the outer trades of sandwich attacks executed via decentralized exchanges (DEXs). This table records the trades that constitute the "bread" of the sandwich attack, which can include both front-running and back-running trades, as well as more complex patterns. It provides a comprehensive view of sandwich attack patterns across various EVM networks.

## Functional Overview

The `dex.sandwiches` table provides an in-depth view of the outer trades in sandwich attacks on decentralized exchanges. A sandwich attack typically occurs when a searcher spots a pending transaction in the mempool, quickly executes a trade before it (front-running), and then immediately after it (back-running) to profit from the price movement caused by the victim's trade.

However, this table also captures more complex sandwich attack patterns. For example:

bot1: ETH>USDC
victim1: USDC>ETH
bot1: USDC>ETH
victim2: ETH>USDC
bot1: ETH>USDC

In this scenario, the bot executes trades on both sides of the liquidity pool, sandwiching multiple victim trades in a single sequence.

This table includes entries for all bot trades involved in these attack patterns.

Note that the victim trades sandwiched between these outer trades are captured in a separate table, `dex.sandwiched`.

This detailed approach allows for granular analysis of sandwich attack patterns, enabling users to:

* **Identify Sandwich Attacks**: Easily spot instances of sandwich attacks across different DEXs and networks, including complex multi-trade patterns.
* **Analyze Attack Patterns**: Understand the frequency, size, and potential profitability of sandwich attacks, including sophisticated strategies that target multiple victims in a single sequence.
* **Monitor Market Manipulation**: Gain insights into various forms of market manipulation across different DEXs and EVM networks.

By providing comprehensive details of the trades involved in sandwich attacks, the `dex.sandwiches` table supports advanced analytics and research into DEX trading behavior, market manipulation, and potential areas for improved trade execution or protocol design.

## Table Schema

| Column                     | Type        | Description                                                    |
| -------------------------- | ----------- | -------------------------------------------------------------- |
| `blockchain`               | `VARCHAR`   | Blockchain on which this trade occurred                        |
| `project`                  | `VARCHAR`   | Name of the DEX on which the trade occurred                    |
| `version`                  | `VARCHAR`   | Version of the DEX protocol/contract                           |
| `block_time`               | `TIMESTAMP` | UTC event block time                                           |
| `block_month`              | `DATE`      | UTC event block month (partition key)                          |
| `block_number`             | `BIGINT`    | Block number in which the trade occurred                       |
| `token_sold_address`       | `VARBINARY` | Contract address of the token sold                             |
| `token_bought_address`     | `VARBINARY` | Contract address of the token bought                           |
| `token_sold_symbol`        | `VARCHAR`   | Symbol of the token sold                                       |
| `token_bought_symbol`      | `VARCHAR`   | Symbol of the token bought                                     |
| `maker`                    | `VARBINARY` | Address that sold tokens (can be contract or EOA)              |
| `taker`                    | `VARBINARY` | Address that purchased tokens (can be contract or EOA)         |
| `tx_hash`                  | `VARBINARY` | Transaction hash                                               |
| `tx_from`                  | `VARBINARY` | EOA address that sent the transaction                          |
| `tx_to`                    | `VARBINARY` | Address called in the first call of this transaction           |
| `project_contract_address` | `VARBINARY` | Smart contract address which emitted the trade event           |
| `token_pair`               | `VARCHAR`   | Symbol pair for the tokens involved, always alphabetical order |
| `tx_index`                 | `BIGINT`    | Index of the transaction in the block                          |
| `token_sold_amount_raw`    | `UINT256`   | Raw amount of the token sold                                   |
| `token_bought_amount_raw`  | `UINT256`   | Raw amount of the token bought                                 |
| `token_sold_amount`        | `DOUBLE`    | Amount of the token sold in display units                      |
| `token_bought_amount`      | `DOUBLE`    | Amount of the token bought in display units                    |
| `amount_usd`               | `DOUBLE`    | USD value of the trade at time of execution                    |
| `evt_index`                | `BIGINT`    | Index of the event in the transaction                          |

## Table Sample

<TableSample tableSchema="dex" tableName="sandwiches" />

## Examples

The following query demonstrates how to use the `dex.sandwiches` table to calculate the total volume of sandwich attack trades on a weekly basis.

```sql theme={null}
SELECT
  date_trunc('week', block_time) AS week,
  COUNT(*) AS sandwich_count,
  SUM(token_sold_amount_usd) AS total_volume_usd
FROM dex.sandwiches
WHERE blockchain = 'ethereum'
  AND block_time >= NOW() - INTERVAL '90' DAY
GROUP BY 1
ORDER BY 1
```

## Related Tables

* `dex.sandwiched`: This complementary table captures the victim trades that are sandwiched between the trades recorded in `dex.sandwiches`.
* `dex.trades`: This table captures all trades and is the basis for the `dex.sandwiches` table.
