|
1 | 1 | from datetime import datetime, timezone |
2 | 2 | from enum import Enum |
3 | | -from typing import NewType, Optional, Union, cast |
| 3 | +from typing import Annotated, NewType, Optional, Union, cast |
4 | 4 |
|
5 | 5 | from ape.types import AddressType, HexBytes |
6 | 6 | from eip712.common import SafeTxV1, SafeTxV2 |
7 | 7 | from eth_typing import HexStr |
8 | 8 | from eth_utils import add_0x_prefix, to_hex |
9 | | -from pydantic import BaseModel, Field |
| 9 | +from pydantic import BaseModel, BeforeValidator, Field, field_validator |
10 | 10 |
|
11 | 11 | from ape_safe.utils import get_safe_tx_hash |
12 | 12 |
|
13 | 13 | SafeTx = Union[SafeTxV1, SafeTxV2] |
14 | 14 | SafeTxID = NewType("SafeTxID", str) |
15 | 15 |
|
16 | 16 |
|
| 17 | +def clean_api_address(data: AddressType | dict) -> AddressType: |
| 18 | + # NOTE: Safe API returns `{'value':'<addr>', ...}` object |
| 19 | + if isinstance(data, dict): |
| 20 | + return data["value"] |
| 21 | + return data |
| 22 | + |
| 23 | + |
| 24 | +Address = Annotated[AddressType, BeforeValidator(clean_api_address)] |
| 25 | + |
| 26 | + |
17 | 27 | class SafeDetails(BaseModel): |
18 | | - address: AddressType |
| 28 | + address: Address |
19 | 29 | nonce: int |
20 | 30 | threshold: int |
21 | | - owners: list[AddressType] |
22 | | - master_copy: AddressType = Field(alias="masterCopy") |
23 | | - modules: list[AddressType] |
24 | | - fallback_handler: AddressType = Field(alias="fallbackHandler") |
| 31 | + owners: list[Address] |
| 32 | + master_copy: Address = Field(alias="implementation") |
| 33 | + modules: list[Address] = [] |
| 34 | + fallback_handler: Address = Field(alias="fallbackHandler") |
25 | 35 | guard: AddressType |
26 | 36 | version: str |
27 | 37 |
|
| 38 | + @field_validator("modules", mode="before") |
| 39 | + def convert_none_to_empty_list(cls, value): |
| 40 | + if not value: |
| 41 | + return [] |
| 42 | + return value |
| 43 | + |
28 | 44 |
|
29 | 45 | class SignatureType(str, Enum): |
30 | 46 | APPROVED_HASH = "APPROVED_HASH" |
|
0 commit comments