Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions app/datasources/db/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: FSL-1.1-MIT
import datetime
import json
from collections.abc import AsyncIterator
from typing import Self, cast
from collections.abc import AsyncIterator, Mapping, Sequence
from typing import Any, Self, cast

from eth_typing import ABI
from sqlalchemy import (
Expand Down Expand Up @@ -32,6 +32,10 @@

from .database import db_session

# JSON stored in ``Abi.abi_json``. ``Mapping`` accepts both plain dicts and the
# ``eth_typing`` ABI TypedDicts.
type AbiJson = Sequence[Mapping[str, Any]] | Mapping[str, Any]


class SqlQueryBase:
@classmethod
Expand All @@ -58,14 +62,14 @@ class TimeStampedSQLModel(SQLModel):

"""

created: datetime.datetime = Field( # type: ignore[call-overload]
created: datetime.datetime = Field(
default_factory=lambda: datetime.datetime.now(datetime.UTC),
nullable=False,
sa_type=DateTime(timezone=True),
index=True,
)

modified: datetime.datetime = Field( # type: ignore[call-overload]
modified: datetime.datetime = Field(
default_factory=lambda: datetime.datetime.now(datetime.UTC),
nullable=False,
sa_type=DateTime(timezone=True),
Expand Down Expand Up @@ -123,9 +127,7 @@ class Abi(SqlQueryBase, TimeStampedSQLModel, table=True):
),
)
relevance: int | None = Field(nullable=False, default=0, index=True)
abi_json: list[dict] | dict = Field(
sa_column=Column(JSON(none_as_null=True), nullable=False)
)
abi_json: AbiJson = Field(sa_column=Column(JSON(none_as_null=True), nullable=False))
source_id: int | None = Field(
nullable=False, default=None, foreign_key="abisource.id"
)
Expand Down Expand Up @@ -183,7 +185,7 @@ async def create(self):
@classmethod
async def get_abi(
cls,
abi_json: list[dict] | dict,
abi_json: AbiJson,
):
"""
Checks if an Abi with the given 'abi_json' exists by matching the
Expand All @@ -207,7 +209,7 @@ async def get_abi(
@classmethod
async def get_or_create_abi(
cls,
abi_json: list[dict] | dict,
abi_json: AbiJson,
source_id: int | None,
relevance: int | None = 0,
) -> tuple["Abi", bool]:
Expand Down
3 changes: 1 addition & 2 deletions app/services/abis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# SPDX-License-Identifier: FSL-1.1-MIT
from typing import cast

from eth_typing import ABI
from safe_eth.eth.contracts import (
Expand Down Expand Up @@ -75,7 +74,7 @@ async def _store_abis_in_database(
abi_source: AbiSource,
) -> None:
for abi_json in abi_jsons:
abi = await Abi.get_abi(cast(list[dict], abi_json))
abi = await Abi.get_abi(abi_json)
if abi is None:
await Abi(
abi_json=abi_json, source_id=abi_source.id, relevance=relevance
Expand Down
6 changes: 2 additions & 4 deletions app/tests/datasources/db/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ async def test_contract_get_abi_by_contract_address(self):

@db_session_context
async def test_project(self):
project = Project(
name="Safe", description="A Test Project", logo_file="logo.jpg"
)
project = Project(description="A Test Project", logo_file="logo.jpg")
await project.create()
result = await project.get_all()
self.assertEqual(result[0], project)
Expand Down Expand Up @@ -440,7 +438,7 @@ async def test_abi_json_not_null_constraint(self):
source = AbiSource(name="null_test_source", url="")
await source.create()

abi = Abi(abi_json=None, source_id=source.id)
abi = Abi(abi_json=None, source_id=source.id) # type: ignore[arg-type]
with self.assertRaises(IntegrityError):
await abi.create()
await db_session.rollback()
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dynamic = ["version"]
requires-python = ">=3.13"
dependencies = [
"aio-pika==10.0.1",
"alembic==1.19.2",
"alembic==1.20.0",
"async-lru==2.3.0",
"asyncpg==0.31.0",
"fastapi-camelcase==2.0.0",
Expand All @@ -14,8 +14,8 @@ dependencies = [
"redis[hiredis]==8.1.0",
"safe-eth-py==7.26.1",
"setuptools>=84.0.0,<85",
"sqladmin[full]==0.31.1",
"sqlmodel==0.0.42",
"sqladmin[full]==0.32.0",
"sqlmodel==0.0.47",
"taskiq==0.12.6",
"taskiq-redis==1.2.3",
"uvicorn-worker==0.4.0",
Expand Down
Loading
Loading