Skip to content

Commit

Permalink
debugging and improvements to work with apibara
Browse files Browse the repository at this point in the history
  • Loading branch information
starknetdev committed Apr 6, 2023
1 parent ec4277c commit 12f6022
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 210 deletions.
8 changes: 2 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ COPY pyproject.toml .

RUN python3 -m pip install poetry
RUN python3 -m pip install pycryptodome
RUN python3 -m pip uninstall apibara
RUN python3 -m pip install apibara
RUN poetry config virtualenvs.create false
RUN poetry install --no-dev
RUN poetry install

ENTRYPOINT [ "indexer" ]
CMD ["indexer", "start", "--restart"]
CMD ["indexer", "graphql"]
ENTRYPOINT [ "indexer" ]
34 changes: 34 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,39 @@ services:
volumes:
- mongo-data:/data/db

indexer:
build:
context: .
dockerfile: starknetdev/loot-survivor-indexer:latest
restart: always
command:
- start
- --mongo-url
- "mongodb://apibara:apibara@mongo:27017"
environment:
PYTHONUNBUFFERED: "1"
links:
- mongo

graphql:
build:
context: .
dockerfile: starknetdev/loot-survivor-indexer:latest
restart: always
command:
- graphql
- --mongo-url
- "mongodb://apibara:apibara@mongo:27017"
- --port
- "8080"
environment:
PYTHONUNBUFFERED: "1"
ports:
- 8080:8080
links:
- mongo



volumes:
mongo-data:
14 changes: 0 additions & 14 deletions docker-compose.prod.yml → old-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,5 @@ services:
volumes:
- mongo-data:/data/db

indexer:
build:
context: .
dockerfile: Dockerfile
restart: always
command:
- start
- --mongo-url
- "mongodb://apibara:apibara@mongo:27017"
environment:
PYTHONUNBUFFERED: "1"
links:
- mongo

volumes:
mongo-data:
26 changes: 20 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ indexer = "indexer.main:cli"

[tool.poetry.dependencies]
python = ">3.8,<3.10"
apibara = { version = "^0.6.5", extras = ["indexer"] }
click = "^8.1.3"
starknet-py = "^0.13.0a0"
pymongo = {extras = ["srv"], version = "^4.3.3"}
strawberry-graphql = {extras = ["debug-server"], version = "^0.168.1"}
aiohttp = "^3.8.4"
aiohttp_cors = "^0.7.0"
aiohttp-cors = "^0.7.0"
apibara = "^0.6.5"

[tool.poetry.dev-dependencies]
black = "^22.6.0"
Expand Down
60 changes: 25 additions & 35 deletions src/indexer/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@
"type": "struct",
}

thief_state_abi = {
"members": [
{"name": "AdventurerId", "offset": 0, "type": "Uint256"},
{"name": "StartTime", "offset": 2, "type": "felt"},
{"name": "Gold", "offset": 3, "type": "felt"},
],
"name": "ThiefState",
"size": 4,
"type": "struct",
}

beast_state_abi = {
"members": [
{"name": "Id", "offset": 0, "type": "felt"},
Expand Down Expand Up @@ -148,45 +159,13 @@
"type": "event",
}

adventurer_king_hiest_abi = {
"outputs": [
{"name": "adventurer_id", "type": "Uint256"},
{"name": "adventurer_state", "type": "AdventurerState"},
],
"keys": [],
"name": "AdventurerInitiatedKingHiest",
"type": "event",
}

adventurer_rob_king_abi = {
"outputs": [
{"name": "adventurer_id", "type": "Uint256"},
{"name": "adventurer_state", "type": "AdventurerState"},
],
"keys": [],
"name": "AdventurerRobbedKing",
"type": "event",
}

adventurer_died_robbing_king_abi = {
"outputs": [
{"name": "adventurer_id", "type": "Uint256"},
{"name": "adventurer_state", "type": "AdventurerState"},
],
update_thief_state_abi = {
"outputs": [{"name": "thief_state", "type": "ThiefState"}],
"keys": [],
"name": "AdventurerDiedRobbingKing",
"name": "UpdateThiefState",
"type": "event",
}

adventurer_killed_thief_abi = {
"outputs": [
{"name": "adventurer_id", "type": "Uint256"},
{"name": "adventurer_state", "type": "AdventurerState"},
],
"keys": [],
"name": "AdventurerKilledThief",
"type": "event",
}

# BEAST EVENTS

Expand Down Expand Up @@ -360,6 +339,13 @@
identifier_manager=identifier_manager_from_abi([discovery_abi, uint256_abi]),
)

update_thief_state_decoder = FunctionCallSerializer(
abi=update_thief_state_abi,
identifier_manager=identifier_manager_from_abi(
[update_thief_state_abi, thief_state_abi]
),
)


def decode_mint_adventurer_event(data):
return mint_adventurer_decoder.to_python([felt.to_int(d) for d in data])
Expand All @@ -377,6 +363,10 @@ def decode_discovery_event(data):
return discovery_decoder.to_python([felt.to_int(d) for d in data])


def decode_update_thief_state_event(data):
return update_thief_state_decoder.to_python([felt.to_int(d) for d in data])


## BEAST DECODERS

create_beast_decoder = FunctionCallSerializer(
Expand Down
12 changes: 8 additions & 4 deletions src/indexer/graphql.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
from datetime import datetime
from typing import List, NewType, Optional, Dict
import socket

import strawberry
import aiohttp_cors
Expand Down Expand Up @@ -719,7 +720,9 @@ def from_mongo(cls, data):

@strawberry.type
class Heist:
adventurer_id: Optional[FeltValue]
thiefId: Optional[FeltValue]
heist_time: Optional[datetime]
gold: Optional[FeltValue]


@strawberry.type
Expand Down Expand Up @@ -1110,7 +1113,7 @@ async def get_context(self, _request, _response):
return {"db": self._db}


async def run_graphql_api(mongo_url=None):
async def run_graphql_api(mongo_url=None, port="8080"):
mongo = MongoClient(mongo_url)
indexer = LootSurvivorIndexer()
db_name = indexer.indexer_id().replace("-", "_")
Expand All @@ -1121,6 +1124,7 @@ async def run_graphql_api(mongo_url=None):

app = web.Application()
app.router.add_route("*", "/graphql", view)

# cors = aiohttp_cors.setup(app)
# resource = cors.add(app.router.add_resource("/graphql"))
# cors.add(
Expand All @@ -1142,10 +1146,10 @@ async def run_graphql_api(mongo_url=None):

runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, "localhost", "8080")
site = web.TCPSite(runner, "0.0.0.0", int(port))
await site.start()

print(f"GraphQL server started on port 8080")
print(f"GraphQL server started on port {port}")

while True:
await asyncio.sleep(5_000)
Loading

0 comments on commit 12f6022

Please sign in to comment.