-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from curveresearch/refactor-utils
Refactor utils
- Loading branch information
Showing
8 changed files
with
261 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Added | ||
----- | ||
- Created utilities for managing datetime, addresses, and enums. | ||
- Created a constants module to house common constants / enums (currently only `Chain` and `Env`). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" | ||
Constants and Enum types used in Curvesim. | ||
""" | ||
from enum import Enum | ||
|
||
|
||
class StrEnum(str, Enum): | ||
""" | ||
Custom string enum type since the builtin `StrEnum` is not available | ||
until Python 3.11. | ||
""" | ||
|
||
def __str__(self): | ||
""" | ||
Regular Enum's __str__ is the name, rather than the value, | ||
e.g. | ||
>>> str(Chain.MAINNET) | ||
'Chain.MAINNET' | ||
so we need to explicitly use the value. | ||
This behaves like the builtin `StrEnum` (available in 3.11). | ||
""" | ||
return str.__str__(self) | ||
|
||
|
||
class Chain(StrEnum): | ||
"""Identifiers for chains & layer 2s.""" | ||
|
||
MAINNET = "mainnet" | ||
ARBITRUM = "arbitrum" | ||
OPTIMISM = "optimism" | ||
FANTOM = "fantom" | ||
AVALANCHE = "avalanche" | ||
MATIC = "matic" | ||
XDAI = "xdai" | ||
|
||
|
||
class Env(StrEnum): | ||
"""Names for different API environments.""" | ||
|
||
PROD = "prod" | ||
STAGING = "staging" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
Utility functions for working with EVM-like account addresses. | ||
This module provides custom versions of common address operations to ensure | ||
standard handling of EVM addresses. | ||
""" | ||
__all__ = ["to_address", "is_address", "Address"] | ||
|
||
from typing import NewType | ||
|
||
from eth_typing import ChecksumAddress | ||
from eth_utils import is_checksum_address, is_normalized_address, to_checksum_address | ||
|
||
Address = NewType("Address", str) | ||
|
||
|
||
def to_address(address_string: str) -> Address: | ||
""" | ||
Convert given string to an EVM-like checksum address. | ||
Parameters | ||
---------- | ||
address_string: str | ||
string to convert to checksum address. | ||
Raises | ||
------ | ||
ValueError | ||
string cannot be coerced to a checksum address | ||
""" | ||
checksum_address: ChecksumAddress = to_checksum_address(address_string) | ||
return Address(checksum_address) | ||
|
||
|
||
def is_address(address_string: str, checksum=True) -> bool: | ||
""" | ||
Check if given string is properly formatted as an | ||
EVM-like address, optionally as a checksum address. | ||
Parameters | ||
---------- | ||
address_string: str | ||
string to check for proper address formatting | ||
checksum: bool, default=True | ||
verify it is properly checksummed | ||
""" | ||
if checksum: # pylint: disable=no-else-return | ||
return is_checksum_address(address_string) | ||
else: | ||
return is_normalized_address(address_string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
""" | ||
Utility functions for working with datetimes in UTC format. | ||
This module provides custom versions of common datetime operations to ensure | ||
returned datetimes are always in the UTC timezone and are not naive. | ||
The main goal is to prevent potential issues related to timezone-aware and | ||
timezone-naive datetime objects. | ||
Recommended style for importing is: | ||
from curvesim.utils import datetime | ||
datetime.now() | ||
This syntactically substitutes the `datetime.datetime` class methods with | ||
this module's functions. | ||
""" | ||
from datetime import datetime, timezone | ||
|
||
|
||
def now() -> datetime: | ||
""" | ||
Customized version of `datetime.datetime.now` to ensure | ||
UTC timezone and format. | ||
Returns | ||
------- | ||
datetime | ||
Current UTC datetime with hour, minute, second, and microsecond set to 0. | ||
This datetime is timezone-aware and is not naive. | ||
""" | ||
utc_dt = datetime.now(timezone.utc).replace( | ||
hour=0, minute=0, second=0, microsecond=0 | ||
) | ||
return utc_dt | ||
|
||
|
||
def fromtimestamp(timestamp: int) -> datetime: | ||
""" | ||
Customized version of `datetime.datetime.fromtimestamp` to ensure | ||
UTC timezone and format. | ||
Parameters | ||
---------- | ||
timestamp : int | ||
Unix timestamp to be converted to datetime. | ||
Returns | ||
------- | ||
datetime | ||
The UTC datetime representation of the given timestamp. | ||
This datetime is timezone-aware and is not naive. | ||
""" | ||
utc_timestamp = datetime.fromtimestamp(timestamp, tz=timezone.utc) | ||
return utc_timestamp |
Oops, something went wrong.