Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BACK-7925): implement erc7730 generate command #135

Merged
merged 6 commits into from
Nov 4, 2024
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
109 changes: 87 additions & 22 deletions pdm.lock

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

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ dependencies = [
"hishel>=0.0.33",
"xdg-base-dirs>=6.0.2",
"limiter>=0.5.0",
"eth-abi>=5.1.0",
"case-switcher>=1.3.13",
]

[project.urls]
Expand Down
14 changes: 14 additions & 0 deletions src/erc7730/common/abi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass
from enum import StrEnum, auto
from typing import Any, cast

from eth_typing import ABIFunction
Expand Down Expand Up @@ -114,3 +115,16 @@ def get_functions(abis: list[ABI]) -> Functions:
if abi.name in ("proxyType", "getImplementation", "implementation", "proxy__getImplementation"):
functions.proxy = True
return functions


class ABIDataType(StrEnum):
"""Solidity data type."""

UINT = auto()
INT = auto()
UFIXED = auto()
FIXED = auto()
ADDRESS = auto()
BOOL = auto()
BYTES = auto()
STRING = auto()
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from erc7730.common.output import ExceptionsToOutput, OutputAdder
from erc7730.convert import ERC7730Converter
from erc7730.model.context import EIP712JsonSchema
from erc7730.model.context import EIP712Schema
from erc7730.model.display import (
DateEncoding,
FieldFormat,
Expand Down Expand Up @@ -46,13 +46,13 @@ def convert(

for contract in descriptor.contracts:
formats: dict[str, InputFormat] = {}
schemas: list[EIP712JsonSchema | HttpUrl] = []
schemas: list[EIP712Schema | HttpUrl] = []

for message in contract.messages:
if (primary_type := self._get_primary_type(message.schema_, out)) is None:
return None

schemas.append(EIP712JsonSchema(primaryType=primary_type, types=message.schema_))
schemas.append(EIP712Schema(primaryType=primary_type, types=message.schema_))

formats[primary_type] = InputFormat(
intent=message.mapper.label,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from erc7730.common.ledger import ledger_network_id
from erc7730.common.output import ExceptionsToOutput, OutputAdder
from erc7730.convert import ERC7730Converter
from erc7730.model.context import EIP712JsonSchema
from erc7730.model.context import EIP712Schema
from erc7730.model.display import FieldFormat
from erc7730.model.paths import ContainerField, ContainerPath, DataPath
from erc7730.model.paths.path_ops import data_path_concat, to_relative
Expand Down Expand Up @@ -96,7 +96,7 @@ def _build_network_descriptor(

@classmethod
def _get_schema(
cls, primary_type: str, schemas: list[EIP712JsonSchema], out: OutputAdder
cls, primary_type: str, schemas: list[EIP712Schema], out: OutputAdder
) -> dict[str, list[EIP712SchemaField]] | None:
for schema in schemas:
if schema.primaryType == primary_type:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from erc7730.convert.resolved.parameters import resolve_field_parameters
from erc7730.convert.resolved.references import resolve_reference
from erc7730.model.abi import ABI
from erc7730.model.context import EIP712JsonSchema
from erc7730.model.context import EIP712Schema
from erc7730.model.display import (
FieldFormat,
)
Expand Down Expand Up @@ -232,27 +232,25 @@ def _resolve_domain(cls, domain: InputDomain, out: OutputAdder) -> ResolvedDomai
)

@classmethod
def _resolve_schemas(
cls, schemas: list[EIP712JsonSchema | HttpUrl], out: OutputAdder
) -> list[EIP712JsonSchema] | None:
def _resolve_schemas(cls, schemas: list[EIP712Schema | HttpUrl], out: OutputAdder) -> list[EIP712Schema] | None:
resolved_schemas = []
for schema in schemas:
if (resolved_schema := cls._resolve_schema(schema, out)) is not None:
resolved_schemas.append(resolved_schema)
return resolved_schemas

@classmethod
def _resolve_schema(cls, schema: EIP712JsonSchema | HttpUrl, out: OutputAdder) -> EIP712JsonSchema | None:
def _resolve_schema(cls, schema: EIP712Schema | HttpUrl, out: OutputAdder) -> EIP712Schema | None:
match schema:
case HttpUrl() as url:
try:
return client.get(url=url, model=EIP712JsonSchema)
return client.get(url=url, model=EIP712Schema)
except Exception as e:
return out.error(
title="Failed to fetch EIP-712 schema from URL",
message=f'Failed to fetch EIP-712 schema from URL "{url}": {e}',
)
case EIP712JsonSchema():
case EIP712Schema():
return schema
case _:
assert_never(schema)
Expand Down
Loading
Loading