Skip to content

Commit

Permalink
fix: int serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Aug 20, 2024
1 parent 7153f95 commit a1a9b84
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/ape/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
)
from ethpm_types.abi import EventABI
from ethpm_types.source import Closure
from pydantic import BaseModel, BeforeValidator, field_validator, model_validator
from pydantic import BaseModel, BeforeValidator, field_serializer, field_validator, model_validator
from pydantic_core.core_schema import (
CoreSchema,
ValidationInfo,
Expand Down Expand Up @@ -258,6 +258,15 @@ def __eq__(self, other: Any) -> bool:

return True

@field_serializer("event_arguments")
def _serialize_event_arguments(self, event_arguments, info):
"""
Because of an issue with BigInt in Pydantic,
(https://github.com/pydantic/pydantic/issues/10152)
we have to ensure these are regular ints.
"""
return {k: int(v) if isinstance(v, int) else v for k, v in event_arguments.items()}


class ContractLog(ExtraAttributesMixin, BaseContractLog):
"""
Expand Down
21 changes: 20 additions & 1 deletion tests/functional/test_contract_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ape.api import ReceiptAPI
from ape.exceptions import ProviderError
from ape.types import ContractLog
from ape.types import ContractLog, CurrencyValueComparable


@pytest.fixture
Expand Down Expand Up @@ -363,3 +363,22 @@ def test_info(solidity_contract_instance):
{spec}
""".strip()
assert actual == expected


def test_model_dump(solidity_contract_container, owner):
# NOTE: deploying a new contract with a new number to lessen x-dist conflicts.
contract = owner.deploy(solidity_contract_container, 29620000000003)

# First, get an event (a normal way).
number = int(10e18)
tx = contract.setNumber(number, sender=owner)
event = tx.events[0]

# Next, invoke `.model_dump()` to get the serialized version.
log = event.model_dump()
actual = log["event_arguments"]
assert actual["newNum"] == number

# This next assertion is important because of this Pydantic bug:
# https://github.com/pydantic/pydantic/issues/10152
assert not isinstance(actual["newNum"], CurrencyValueComparable)

0 comments on commit a1a9b84

Please sign in to comment.