Skip to content

Example Apibara project indexing a DEX and providing data through a GraphQL API.

License

Notifications You must be signed in to change notification settings

apibara/swap-graphql-example

Repository files navigation

DEX Indexer and GraphQL API with Apibara

This repository shows how to use Apibara to index a decentralized exchange (DEX) and build a GraphQL API.

This project separates the indexer from the API server so that it becomes easier to deploy and scale in a production environment.

You should always run a single instance of the indexer but can run multiple API servers side by side to serve more users.

The project uses:

Setting up

Create a new virtual environment for this project. While this step is not required, it is highly recommended to avoid conflicts between different installed packages.

python3 -m venv venv

Then activate the virtual environment.

source venv/bin/activate

Then install poetry and use it to install the package dependencies.

python3 -m pip install poetry
poetry install

Start MongoDB using the provided docker-compose file:

docker-compose up

Notice that you can use any managed MongoDB like MongoDB Atlas.

Getting started

This example shows how to index JediSwap on StarkNet. You can change it to index any Uniswap-V2-like DEX by changing the configuration in src/swap/indexer/jediswap.py.

Once setup, start indexing by running the following command:

poetry run swap-indexer indexer \
    --stream-url <stream-url> \
    --mongo-url <mongo-url> \
    --rpc-url <rpc-url>

where:

  • <stream-url>: the Apibara stream, should be mainnet.starknet.stream.apibara.com.
  • <mongo-url>: mongodb connection url. If you use the provided docker compose file use mongodb://apibara:apibara@localhost:27017.
  • <rpc-url>: the StarkNet RPC url. You can use Infura for this.

The indexer will then start indexing your DEX.

Start the GraphQL API server with:

poetry run swap-indexer server \
    --mongo-url <mongo-url>

where <mongo-url> is the same connection string you used above. The GraphQL API is served at http://localhost:8000/graphql and it includes a GrapihQL page for testing the API.

Production deployment & scaling

The repository contains a docker-compose.prod.yml file with a good starting point for a production deployment.

Here are some changes you should be considering:

  • Use a managed MongoDB, for example MongoDB Atlas.
  • Run the indexer and the API server on separate machines.

As the number of users grows, you may need to scale your API server:

  • Deploy multiple instances of the API server behind a load balancer.
  • Replicate the database and connect the API servers to the replicas.
  • Cache entities with e.g. redis.

License

Copyright 2022 GNC Labs Limited

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

Example Apibara project indexing a DEX and providing data through a GraphQL API.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages