Skip to content

Commit

Permalink
chore(BACK-7878): minor improvements to resolved form (#111)
Browse files Browse the repository at this point in the history
* chore(BACK-7878): tolerate `ETHERSCAN_API_KEY` or `SCAN_ETHERSCAN_API_KEY`

* chore(BACK-7878): normalize token amount parameters in resolved form
  • Loading branch information
jnicoulaud-ledger authored Oct 23, 2024
1 parent ce51bfe commit a61e803
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 21 deletions.
4 changes: 3 additions & 1 deletion src/erc7730/common/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ def handle_request(self, request: Request) -> Response:
return super().handle_request(request)

# add API key
if (api_key := os.environ.get(self.ETHERSCAN_API_KEY)) is None:
if (api_key := os.environ.get(self.ETHERSCAN_API_KEY)) is None and (
api_key := os.environ.get(f"SCAN_{self.ETHERSCAN_API_KEY}")
) is None:
raise ValueError(f"{self.ETHERSCAN_API_KEY} environment variable is required")
request.url = request.url.copy_add_param("apikey", api_key)

Expand Down
26 changes: 22 additions & 4 deletions src/erc7730/convert/resolved/parameters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import assert_never
from typing import assert_never, cast

from erc7730.common.output import OutputAdder
from erc7730.convert.resolved.constants import ConstantProvider
Expand Down Expand Up @@ -26,7 +26,7 @@
ResolvedTokenAmountParameters,
ResolvedUnitParameters,
)
from erc7730.model.types import Id
from erc7730.model.types import Address, HexStr, Id


def resolve_field_parameters(
Expand Down Expand Up @@ -80,10 +80,28 @@ def resolve_token_amount_parameters(
prefix: DataPath, params: InputTokenAmountParameters, constants: ConstantProvider, out: OutputAdder
) -> ResolvedTokenAmountParameters | None:
token_path = constants.resolve_path_or_none(params.tokenPath, out)

input_addresses = cast(Address | list[Address] | None, constants.resolve_or_none(params.nativeCurrencyAddress, out))
resolved_addresses: list[Address] | None
if input_addresses is not None:
resolved_addresses = [input_addresses] if isinstance(input_addresses, str) else input_addresses
else:
resolved_addresses = None

input_threshold = cast(HexStr | int | None, constants.resolve_or_none(params.threshold, out))
resolved_threshold: HexStr | None
if input_threshold is not None:
if isinstance(input_threshold, int):
resolved_threshold = "0x" + input_threshold.to_bytes(byteorder="big", signed=False).hex()
else:
resolved_threshold = input_threshold
else:
resolved_threshold = None

return ResolvedTokenAmountParameters(
tokenPath=None if token_path is None else data_or_container_path_concat(prefix, token_path),
nativeCurrencyAddress=constants.resolve_or_none(params.nativeCurrencyAddress, out), # type:ignore
threshold=constants.resolve_or_none(params.threshold, out),
nativeCurrencyAddress=resolved_addresses,
threshold=resolved_threshold,
message=constants.resolve_or_none(params.message, out),
)

Expand Down
6 changes: 3 additions & 3 deletions src/erc7730/model/input/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
FormatBase,
)
from erc7730.model.input.path import ContainerPathStr, DataPathStr, DescriptorPathStr
from erc7730.model.types import Address, Id
from erc7730.model.types import Address, HexStr, Id
from erc7730.model.unions import field_discriminator, field_parameters_discriminator

# ruff: noqa: N815 - camel case field names are tolerated to match schema
Expand Down Expand Up @@ -78,11 +78,11 @@ class InputTokenAmountParameters(Model):
"rather than a token.",
)

threshold: DescriptorPathStr | str | None = Field(
threshold: DescriptorPathStr | HexStr | int | None = Field(
default=None,
title="Unlimited Threshold",
description="The threshold above which the amount should be displayed using the message parameter rather than "
"the real amount.",
"the real amount (encoded as an int or byte array).",
)

message: DescriptorPathStr | str | None = Field(
Expand Down
8 changes: 4 additions & 4 deletions src/erc7730/model/resolved/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
from erc7730.model.paths import ContainerPath, DataPath
from erc7730.model.resolved.path import ResolvedPath
from erc7730.model.types import Address, Id
from erc7730.model.types import Address, HexStr, Id
from erc7730.model.unions import field_discriminator, field_parameters_discriminator

# ruff: noqa: N815 - camel case field names are tolerated to match schema
Expand All @@ -31,18 +31,18 @@ class ResolvedTokenAmountParameters(Model):
'"Unknown token" warning.',
)

nativeCurrencyAddress: Address | list[Address] | None = Field(
nativeCurrencyAddress: list[Address] | None = Field(
default=None,
title="Native Currency Address",
description="An address or array of addresses, any of which are interpreted as an amount in native currency "
"rather than a token.",
)

threshold: str | None = Field(
threshold: HexStr | None = Field(
default=None,
title="Unlimited Threshold",
description="The threshold above which the amount should be displayed using the message parameter rather than "
"the real amount.",
"the real amount (encoded as a byte array).",
)

message: str | None = Field(
Expand Down
12 changes: 11 additions & 1 deletion src/erc7730/model/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from typing import Annotated

from pydantic import Field
from pydantic import BeforeValidator, Field

Id = Annotated[
str,
Expand All @@ -30,4 +30,14 @@
),
]

HexStr = Annotated[
str,
Field(
title="Hexadecimal string",
description="A byte array encoded as an hexadecimal string.",
pattern=r"^0x[a-f0-9]+$",
),
BeforeValidator(lambda v: v.lower()),
]

ScalarType = str | int | bool | float
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@
}
]
},
"nativeCurrencyAddress": "0x0000000000000000000000000000000000000001",
"threshold": "0xFFFFFFFF",
"nativeCurrencyAddress": [
"0x0000000000000000000000000000000000000001"
],
"threshold": "0xffffffff",
"message": "Max"
}
}
Expand Down
8 changes: 5 additions & 3 deletions tests/convert/resolved/data/format_token_amount_resolved.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@
}
]
},
"nativeCurrencyAddress": "0x0000000000000000000000000000000000000001",
"threshold": "0xFFFFFFFF",
"nativeCurrencyAddress": [
"0x0000000000000000000000000000000000000001"
],
"threshold": "0xffffffff",
"message": "Max"
}
},
Expand Down Expand Up @@ -118,7 +120,7 @@
"0x0000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000002"
],
"threshold": "0xFFFFFFFF",
"threshold": "0xffffffff",
"message": "Max"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@
}
]
},
"nativeCurrencyAddress": "0x0000000000000000000000000000000000000001",
"threshold": "0xFFFFFFFF",
"nativeCurrencyAddress": [
"0x0000000000000000000000000000000000000001"
],
"threshold": "0xffffffff",
"message": "Max"
}
},
Expand Down Expand Up @@ -118,7 +120,7 @@
"0x0000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000002"
],
"threshold": "0xFFFFFFFF",
"threshold": "0xffffffff",
"message": "Max"
}
}
Expand Down

0 comments on commit a61e803

Please sign in to comment.