Skip to content

Commit

Permalink
chore: update to apibara v0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek committed Sep 14, 2022
1 parent bb5ae97 commit ecb1607
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 71 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ Then install `poetry` and use it to install the package dependencies.
python3 -m pip install poetry
poetry install

Start Apibara and MongoDB using the provided `docker-compose` file:
Start MongoDB using the provided `docker-compose` file:

docker-compose up

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

Then start the indexer by running the `indexer start` command. The `indexer` command runs the cli application defined in `src/indexer/main.py`. This is a standard Click application.

Notice that by default the indexer will start indexing from where it left off in the previous run. If you want restart, use the `--restart` flag.
Expand All @@ -39,4 +41,3 @@ You can change the id of the indexer by changing the value of the `indexer_id` v
## Running in production

This template includes a `Dockerfile` that you can use to package the indexer for production usage.

7 changes: 0 additions & 7 deletions configuration.toml

This file was deleted.

18 changes: 1 addition & 17 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,16 @@ services:
volumes:
- ./_docker/apibara_mongodb:/data/db

apibara:
image: apibara/apibara:0.3.2
restart: always
command: start --config /usr/etc/apibara/configuration.toml
environment:
RUST_LOG: "apibara=info"
ports:
- 7171:7171
volumes:
- ./configuration.toml:/usr/etc/apibara/configuration.toml
links:
- mongo

indexer:
build:
context: .
dockerfile: Dockerfile
restart: always
command:
- start
- --server-url
- apibara:7171
- --mongo-url
- "mongodb://apibara:apibara@mongo:27017"
environment:
PYTHONUNBUFFERED: "1"
links:
- mongo
- apibara
- mongo
13 changes: 0 additions & 13 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,3 @@ services:
- 27017:27017
volumes:
- ./_docker/apibara_mongodb:/data/db

apibara:
image: apibara/apibara:0.3.2
restart: always
command: start --config /usr/etc/apibara/configuration.toml
environment:
RUST_LOG: "apibara=info"
ports:
- 7171:7171
volumes:
- ./configuration.toml:/usr/etc/apibara/configuration.toml
links:
- mongo
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ indexer = "indexer.main:cli"

[tool.poetry.dependencies]
python = ">3.7.2,<3.10"
apibara = "^0.4.3"
apibara = "^0.5.0"
click = "^8.1.3"
"starknet.py" = "^0.4.4a0"
pymongo = {extras = ["srv"], version = "^4.1.1"}
Expand Down
31 changes: 4 additions & 27 deletions src/indexer/indexer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from apibara import Client, IndexerRunner, Info, NewBlock, NewEvents
from apibara.indexer.runner import IndexerRunnerConfiguration
from apibara.model import EventFilter
from pymongo import MongoClient
from apibara import EventFilter, IndexerRunner, Info, NewEvents
from apibara.indexer import IndexerRunnerConfiguration

indexer_id = "my-indexer"

Expand All @@ -21,42 +19,21 @@ async def handle_events(info: Info, block_events: NewEvents):
await info.storage.insert_many("events", events)


async def handle_block(info: Info, block: NewBlock):
"""Handle a new _live_ block."""
print(block.new_head)


async def run_indexer(server_url=None, mongo_url=None, restart=None):
print("Starting Apibara indexer")
if mongo_url is None:
mongo_url = "mongodb://apibara:apibara@localhost:27017"

if restart:
async with Client.connect(server_url) as client:
existing = await client.indexer_client().get_indexer(indexer_id)
if existing:
await client.indexer_client().delete_indexer(indexer_id)

# Delete old database entries.
# Notice that apibara maps indexer ids to database names by
# doing `indexer_id.replace('-', '_')`.
# In the future all data will be handled by Apibara and this step
# will not be necessary.
mongo = MongoClient(mongo_url)
mongo.drop_database(indexer_id.replace("-", "_"))

runner = IndexerRunner(
config=IndexerRunnerConfiguration(
apibara_url=server_url,
apibara_ssl=True,
storage_url=mongo_url,
),
reset_state=restart,
network_name="starknet-goerli",
indexer_id=indexer_id,
new_events_handler=handle_events,
)

runner.add_block_handler(handle_block)

# Create the indexer if it doesn't exist on the server,
# otherwise it will resume indexing from where it left off.
#
Expand Down
6 changes: 2 additions & 4 deletions src/indexer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ def cli():


@cli.command()
@click.option("--server-url", default=None, help="Apibara Server url.")
@click.option("--server-url", default=None, help="Apibara stream url.")
@click.option("--mongo-url", default=None, help="MongoDB url.")
@click.option("--restart", is_flag=True, help="Restart indexing from the beginning.")
@async_command
async def start(server_url, mongo_url, restart):
"""Start the Apibara indexer."""
# Use local apibara server url and mongodb url by default.
# Start them by running docker-compose.
if server_url is None:
server_url = "localhost:7171"
server_url = "goerli.starknet.stream.apibara.com"
if mongo_url is None:
mongo_url = "mongodb://apibara:apibara@localhost:27017"
await run_indexer(
Expand Down

0 comments on commit ecb1607

Please sign in to comment.