# Saturo Indexer
This is a specific indexer for the Saturo project to index all events from the Perps DEX. The indexer connects to the StarkNet blockchain, fetches relevant events, and stores them in a PostgreSQL database.
## Getting Started
### Prerequisites
- Rust and Cargo installed
- PostgreSQL database running
- `.env` file with the following content:
DATABASE_URL=your_database_url
## Setting Up PostgreSQL
### Install PostgreSQL:
- **Ubuntu**:
```sh
sudo apt update
sudo apt install postgresql postgresql-contrib
```
- **MacOS**:
```sh
brew install postgresql
```
- **Windows**: Download and install PostgreSQL from [here](https://www.postgresql.org/download/windows/).
### Start PostgreSQL service:
- **Ubuntu**:
```sh
sudo service postgresql start
```
- **MacOS**:
```sh
brew services start postgresql
```
- **Windows**: Follow the instructions provided by the installer to start the PostgreSQL service.
### Create a new PostgreSQL user and database:
1. Open a terminal and run:
```sh
sudo -u postgres psql
```
2. In the PostgreSQL shell, run:
```sql
CREATE USER saturo_user WITH PASSWORD 'your_password';
CREATE DATABASE saturo_db;
GRANT ALL PRIVILEGES ON DATABASE saturo_db TO saturo_user;
\q
```
### Set up the `.env` file with your database URL:
```bash
DATABASE_URL=postgres://saturo_user:your_password@localhost/saturo_db
psql -U saturo_user -d saturo_db -f sql/db_setup.sql
-
Clone the repository:
git clone https://github.com/your-repo/saturo-indexer.git cd saturo-indexer
-
Install the dependencies:
cargo build
-
Start the PostgreSQL database and ensure the
DATABASE_URL
in your.env
file is correct. -
Run the indexer:
cargo run
main.rs
: The entry point of the application. Sets up the environment, database connection, and event provider, and starts the event fetching process.config.rs
: Contains configuration functions to get database and provider URLs from environment variables.database.rs
: Handles the database connection setup.provider.rs
: Sets up the StarkNet JSON-RPC provider.events/
: Contains modules related to event handling.mod.rs
: Declares thetypes
andhandler
sub-modules.types.rs
: Defines theOrder
,Deposit
, andWithdrawal
structs.handler.rs
: Contains the logic to fetch and process events from the StarkNet blockchain and insert them into the PostgreSQL database.
pub struct Order {
pub block_number: i64,
pub transaction_hash: String,
pub key: Option<String>,
pub order_type: Option<String>,
pub decrease_position_swap_type: Option<String>,
pub account: Option<String>,
pub receiver: Option<String>,
pub callback_contract: Option<String>,
pub ui_fee_receiver: Option<String>,
pub market: Option<String>,
pub initial_collateral_token: Option<String>,
pub swap_path: Option<String>,
pub size_delta_usd: Option<String>,
pub initial_collateral_delta_amount: Option<String>,
pub trigger_price: Option<String>,
pub acceptable_price: Option<String>,
pub execution_fee: Option<String>,
pub callback_gas_limit: Option<String>,
pub min_output_amount: Option<String>,
pub updated_at_block: Option<String>,
pub is_long: Option<String>,
pub is_frozen: Option<String>,
}
pub struct Deposit {
pub block_number: i64,
pub transaction_hash: String,
pub key: Option<String>,
pub account: Option<String>,
pub receiver: Option<String>,
pub callback_contract: Option<String>,
pub market: Option<String>,
pub initial_long_token: Option<String>,
pub initial_short_token: Option<String>,
pub long_token_swap_path: Option<String>,
pub short_token_swap_path: Option<String>,
pub initial_long_token_amount: Option<String>,
pub initial_short_token_amount: Option<String>,
pub min_market_tokens: Option<String>,
pub updated_at_block: Option<String>,
pub execution_fee: Option<String>,
pub callback_gas_limit: Option<String>,
}
pub struct Withdrawal {
pub block_number: i64,
pub transaction_hash: String,
pub key: Option<String>,
pub account: Option<String>,
pub receiver: Option<String>,
pub callback_contract: Option<String>,
pub market: Option<String>,
pub long_token_swap_path: Option<String>,
pub short_token_swap_path: Option<String>,
pub market_token_amount: Option<String>,
pub min_long_token_amount: Option<String>,
pub min_short_token_amount: Option<String>,
pub updated_at_block: Option<String>,
pub execution_fee: Option<String>,
pub callback_gas_limit: Option<String>,
}
The main function in handler.rs
fetches events from the StarkNet blockchain, processes them, and inserts them into the PostgreSQL database.
This project is licensed under the MIT License.
For any questions or issues, please contact [arif.bachir@hotmail.com].