# Deploy

## Prerequisites

* [Python v3.10](https://www.python.org/downloads/)
* [pip](https://pip.pypa.io/en/stable/installation/)
* Postgres ([Local](https://www.postgresql.org/) or [Docker](https://hub.docker.com/_/postgres))
* [pyenv](https://github.com/pyenv/pyenv) (Optional)

## Setup

### Installation

#### Virtual Environment

Create the virtual environment using the following command:

```bash
make create_python_venv
```

To run the virtual environment, you can use the following command:

```bash
source venv/bin/activate
```

#### Dependencies

To install the dependencies, you can use the following command:

```bash
make deps
```

### Database Setup

#### Create Database Container

This Bot uses a Postgres database. You can either install Postgres locally or use Docker (recommended for development environment).

If you use Docker, you can use the following command to start a Postgres container:

```bash
make create_db container=<container> user=<user> password=<pwd> database=<db_name>
```

| Variable  | Description                                                                           |
| --------- | ------------------------------------------------------------------------------------- |
| container | The name of the docker container. If not provided, the default value is `mm-bot`      |
| user      | The user to create. If not provided, the default value is `user`                      |
| password  | The password for the user. If not provided, the default value is `123123123`          |
| database  | The name of the database to create. If not provided, the default value is `mm-bot-db` |

This container will have a database called `<db_name>`, by default it is `mm-bot-db`.

#### Run Database Container

If you want to run or re-run the database container, you can use the following command:

```bash
make start_db container=<container>
```

| Variable  | Description                                                                      |
| --------- | -------------------------------------------------------------------------------- |
| container | The name of the docker container. If not provided, the default value is `mm-bot` |

#### Stop Database Container

If you want to stop the database container, you can use the following command:

```bash
make stop_db container=<container>
```

| Variable  | Description                                                                      |
| --------- | -------------------------------------------------------------------------------- |
| container | The name of the docker container. If not provided, the default value is `mm-bot` |

### Environment Variables

This API uses environment variables to configure the application. You can create a `.env` file in the root of the project to set the environment variables.

To create your own `.env` file run the following command:

```bash
make create_env
```

The following table describes each environment variable:

| Variable                    | Description                                                                                                       |
| --------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| ENVIRONMENT                 | The environment of the application. It can be `dev` or `prod`                                                     |
| ETHEREUM\_CHAIN\_ID         | The chain ID of the Ethereum network. It can be `1` for Mainnet, 11155111 for Sepolia                             |
| STARKNET\_CHAIN\_ID         | The chain ID of the Starknet network. It can be `SN_MAINNET` for Mainnet, `SN_SEPOLIA` for Sepolia                |
| ETHEREUM\_RPC               | The URL of the Ethereum RPC. You can get one at [Blast](https://blastapi.io/) or [Infure](https://www.infura.io/) |
| STARKNET\_RPC               | The URL of the Starknet RPC. You can get one at [Blast](https://blastapi.io/) or [Infure](https://www.infura.io/) |
| ZKSYNC\_RPC                 | The URL of the ZkSync RPC. You can get one at [Blast](https://blastapi.io/)                                       |
| ETH\_FALLBACK\_RPC\_URL     | The URL of the Ethereum RPC fallback                                                                              |
| SN\_FALLBACK\_RPC\_URL      | The URL of the Starknet RPC fallback                                                                              |
| ZKSYNC\_FALLBACK\_RPC\_URL  | The URL of the ZkSync RPC fallback                                                                                |
| ETHEREUM\_CONTRACT\_ADDRESS | The address of the Payment Registry                                                                               |
| STARKNET\_CONTRACT\_ADDRESS | The address of the Starknet Escrow                                                                                |
| ZKS\_CONTRACT\_ADDRESS      | The address of the ZkSync Escrow                                                                                  |
| ETHEREUM\_PRIVATE\_KEY      | The private key of Market Maker on Ethereum                                                                       |
| STARKNET\_WALLET\_ADDRESS   | The wallet address of Market Maker on Starknet                                                                    |
| STARKNET\_PRIVATE\_KEY      | The private key of Market Maker on Starknet                                                                       |
| HERODOTUS\_API\_KEY         | (Optional) The API key of Herodotus. Needed if using herodotus payment claimer                                    |
| POSTGRES\_HOST              | The host of the Postgres database                                                                                 |
| POSTGRES\_USER              | The user of the Postgres database                                                                                 |
| POSTGRES\_PASSWORD          | The password of the Postgres database                                                                             |
| POSTGRES\_DATABASE          | The name of the Postgres database                                                                                 |
| LOGGING\_LEVEL              | The level of logging. It can be `DEBUG`, `INFO`, `WARNING`, `ERROR`, or `CRITICAL`                                |
| LOGGING\_DIRECTORY          | The directory to save the logs in prod `mode`. Only needed in `prod` mode                                         |
| PAYMENT\_CLAIMER            | The payment claimer. It can be `herodotus` or `ethereum`                                                          |

There is an example file called `.env.example` in the root of the project.

#### Database Population

To create the tables, you can use the following command:

```bash
TODO
```

You must run schema.sql into the database to create the tables. You can use pgAdmin or any other tool to run the script.

## Development

To start the Bot, you can use the following command:

```bash
make run
```

## Test \[TODO]

To run the tests, you can use the following command:

```bash
```
