# Swap

The `Swap` entity represents a fungible token trade on a DEX indexed from Casper smart contracts. Each swap provides the pair, tokens, amounts, and timestamp for a single trade.

## Properties

The `Swap` entity has the following properties:

| Property                       | Type         | Description                                                                     |
| ------------------------------ | ------------ | ------------------------------------------------------------------------------- |
| `pair_contract_package_hash`   | `string(64)` | Pair contract package hash represented as a hexadecimal string                  |
| `transaction_hash`             | `string(64)` | Transaction hash represented as a hexadecimal string                            |
| `transform_id`                 | `uint`       | Transform identifier of the swap within the transaction                         |
| `block_height`                 | `uint64`     | Block height at which the swap occurred                                         |
| `sender_hash`                  | `string(64)` | Sender account hash represented as a hexadecimal string                         |
| `dex_id`                       | `uint16`     | DEX identifier                                                                  |
| `token0_contract_package_hash` | `string(64)` | Token0 contract package hash represented as a hexadecimal string                |
| `token1_contract_package_hash` | `string(64)` | Token1 contract package hash represented as a hexadecimal string                |
| `decimals0`                    | `uint8`      | Token0 decimals                                                                 |
| `decimals1`                    | `uint8`      | Token1 decimals                                                                 |
| `amount0_in`                   | `string`     | Token0 amount in (raw units). Can be `null`                                     |
| `amount1_in`                   | `string`     | Token1 amount in (raw units). Can be `null`                                     |
| `amount0_out`                  | `string`     | Token0 amount out (raw units). Can be `null`                                    |
| `amount1_out`                  | `string`     | Token1 amount out (raw units). Can be `null`                                    |
| `timestamp`                    | `string`     | Swap timestamp in the [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format |

Swap amounts (`amount0_in`, `amount1_in`, `amount0_out`, `amount1_out`) are returned in raw on-chain units. They are not normalized by token decimals.

**Example**

```json
{
  "pair_contract_package_hash": "0f6b7e1a8d6e4f1b5a2c3d4e5f60718293a4b5c6d7e8f90123456789abcdef12",
  "transaction_hash": "9c1b7f2a5d6e4f1b2c3d4e5f60718293a4b5c6d7e8f90123456789abcdef1234",
  "transform_id": 42,
  "block_height": 2404577,
  "sender_hash": "2a1d7f6c8b9e0f1a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f70819",
  "dex_id": 1,
  "token0_contract_package_hash": "ad0cd4ef3cfd9e7222706786e51773af771f063ecce4606282999a7a6d6ac495",
  "token1_contract_package_hash": "3b7f9c2d4e5f60718293a4b5c6d7e8f90123456789abcdef0123456789abcdef",
  "decimals0": 9,
  "decimals1": 6,
  "amount0_in": "1000000000",
  "amount1_in": "0",
  "amount0_out": "0",
  "amount1_out": "2500000",
  "timestamp": "2024-02-01T10:00:00Z"
}
```

## Optional properties

Depending on the endpoint, some of the following optional properties may be included in the `Swap` entity:

| Property                          | Type                                                                                  | Description                                                                                                                                                           |
| --------------------------------- | ------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `sender_public_key`               | `string(68)`                                                                          | Sender public key represented as a hexadecimal string                                                                                                                 |
| `sender_account_info`             | [`AccountInfo`](https://docs.cspr.cloud/rest-api/account-info)                        | Sender account info                                                                                                                                                   |
| `sender_centralized_account_info` | [`CentralizedAccountInfo`](https://docs.cspr.cloud/rest-api/centralized-account-info) | Sender account information available for known accounts provided by CSPR.cloud team                                                                                   |
| `pair_contract_package`           | [`ContractPackage`](https://docs.cspr.cloud/rest-api/contract-package)                | Pair contract package details                                                                                                                                         |
| `token0_contract_package`         | [`ContractPackage`](https://docs.cspr.cloud/rest-api/contract-package)                | Token0 contract package details                                                                                                                                       |
| `token1_contract_package`         | [`ContractPackage`](https://docs.cspr.cloud/rest-api/contract-package)                | Token1 contract package details                                                                                                                                       |
| `token0_ft_rate`                  | `float64`                                                                             | Token0 rate in the specified currency at the swap timestamp. It's a [function includer](https://docs.cspr.cloud/documentation/overview/optional-properties#functions) |
| `token1_ft_rate`                  | `float64`                                                                             | Token1 rate in the specified currency at the swap timestamp. It's a [function includer](https://docs.cspr.cloud/documentation/overview/optional-properties#functions) |

## Relations

The `Swap` entity has the following relations with other entities:

| Entity                                                                  | Mapping property                                                                             | Description                                 |
| ----------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | ------------------------------------------- |
| [`Account`](https://docs.cspr.cloud/rest-api/account)                   | `sender_hash`                                                                                | Sender account of the swap trade            |
| [`Contract package`](https://docs.cspr.cloud/rest-api/contract-package) | `token0_contract_package_hash`, `token1_contract_package_hash`, `pair_contract_package_hash` | Swap trade token and pair contract packages |
| [`DEX`](https://docs.cspr.cloud/rest-api/dex)                           | `dex_id`                                                                                     | DEX that produced the swap                  |

## Endpoints

The `Swap` entity has the following endpoints:

* [Get swaps](https://docs.cspr.cloud/rest-api/swap/get-swaps)

## Relation endpoints

None
