Load Snapshot from RPC request
This commit is contained in:
@@ -5,7 +5,7 @@ from fractions import Fraction
|
||||
from logging import getLogger
|
||||
from typing import Union
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import BaseModel, Field, PrivateAttr
|
||||
|
||||
Address = str
|
||||
|
||||
@@ -30,6 +30,7 @@ class EthereumToken(BaseModel):
|
||||
address: str
|
||||
decimals: int
|
||||
gas: Union[int, list[int]] = 29000
|
||||
_hash: int = PrivateAttr(default=None)
|
||||
|
||||
def to_onchain_amount(self, amount: Union[float, Decimal, str]) -> int:
|
||||
"""Converts floating-point numerals to an integer, by shifting right by the
|
||||
@@ -62,7 +63,7 @@ class EthereumToken(BaseModel):
|
||||
|
||||
Quantize is needed for UniswapV2.
|
||||
"""
|
||||
with localcontext(self._dec_context):
|
||||
with localcontext(Context(rounding=ROUND_FLOOR, prec=256)):
|
||||
if isinstance(onchain_amount, Fraction):
|
||||
return (
|
||||
Decimal(onchain_amount.numerator)
|
||||
@@ -80,6 +81,22 @@ class EthereumToken(BaseModel):
|
||||
amount = Decimal(str(onchain_amount)) / Decimal(10 ** self.decimals)
|
||||
return amount
|
||||
|
||||
def __repr__(self):
|
||||
return self.symbol
|
||||
|
||||
def __str__(self):
|
||||
return self.symbol
|
||||
|
||||
def __eq__(self, other) -> bool:
|
||||
# this is faster than calling custom __hash__, due to cache check
|
||||
return other.address == self.address
|
||||
|
||||
def __hash__(self) -> int:
|
||||
if self._hash is None:
|
||||
# caching the hash saves time during graph search
|
||||
self._hash = hash(self.address)
|
||||
return self._hash
|
||||
|
||||
|
||||
class DatabaseType(Enum):
|
||||
# Make call to the node each time it needs a storage (unless cached from a previous call).
|
||||
|
||||
Reference in New Issue
Block a user