Skip to content

Commit

Permalink
feat: use constant folding
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler committed Dec 22, 2024
1 parent 6f6e888 commit ee78e78
Show file tree
Hide file tree
Showing 19 changed files with 90 additions and 89 deletions.
2 changes: 1 addition & 1 deletion y/_db/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _user_approves_sqlite_deletion(self) -> bool:
click.prompt(
self.msg + "\n\nWould you like to delete your db now?",
default="no",
type=click.Choice(["yes", "no"], case_sensitive=False),
type=click.Choice(("yes", "no"), case_sensitive=False),
confirmation_prompt=True,
show_default=False,
)
Expand Down
2 changes: 1 addition & 1 deletion y/_db/utils/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def __init__(self, addresses, topics):

def __repr__(self) -> str:
string = f"{type(self).__name__}(addresses={self.addresses}"
for topic in ["topic0", "topic1", "topic2", "topic3"]:
for topic in ("topic0", "topic1", "topic2", "topic3"):
if value := getattr(self, topic):
string += f", {topic}={value}"
return f"{string})"
Expand Down
2 changes: 1 addition & 1 deletion y/_db/utils/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async def _set_price(address: str, block: int, price: Decimal) -> None:


@cached(TTLCache(maxsize=1_000, ttl=5 * 60), lock=threading.Lock())
@log_result_count("prices", ["block"])
@log_result_count("prices", ("block",))
def known_prices_at_block(number: int) -> Dict[Address, Decimal]:
"""
Cache and return all known prices at a specific block to minimize database reads.
Expand Down
4 changes: 2 additions & 2 deletions y/_db/utils/traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ async def _trace_block(self, block: int) -> List[dict]:


trace_is_from = lambda addresses, trace: any(
"from" in x and x["from"] in addresses for x in [trace, trace.values()]
"from" in x and x["from"] in addresses for x in (trace, trace.values())
)
"""Check if a trace is from any of the specified addresses.
Expand All @@ -405,7 +405,7 @@ async def _trace_block(self, block: int) -> List[dict]:
"""

trace_is_to = lambda addresses, trace: any(
"to" in x and x["to"] in addresses for x in [trace, trace.values()]
"to" in x and x["to"] in addresses for x in (trace, trace.values())
)
"""Check if a trace is to any of the specified addresses.
Expand Down
8 changes: 4 additions & 4 deletions y/classes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,11 @@ async def _symbol(self) -> str:
"""
symbol = await probe(
self.address,
["symbol()(string)", "SYMBOL()(string)", "getSymbol()(string)"],
("symbol()(string)", "SYMBOL()(string)", "getSymbol()(string)"),
)
if symbol is None:
# Sometimes the above will fail if the symbol method returns bytes32, as with MKR. Let's try this.
symbol = await probe(self.address, ["symbol()(bytes32)"])
symbol = await probe(self.address, ("symbol()(bytes32)",))
if symbol:
symbol = hex_to_string(symbol)
if symbol:
Expand All @@ -491,11 +491,11 @@ async def _name(self) -> str:
'TokenName'
"""
name = await probe(
self.address, ["name()(string)", "NAME()(string)", "getName()(string)"]
self.address, ("name()(string)", "NAME()(string)", "getName()(string)")
)
if name is None:
# Sometimes the above will fail if the name method returns bytes32, as with MKR. Let's try this.
name = await probe(self.address, ["name()(bytes32)"])
name = await probe(self.address, ("name()(bytes32)",))
if name:
name = hex_to_string(name)
if name:
Expand Down
2 changes: 1 addition & 1 deletion y/prices/chainlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def contract(self) -> Contract:

# @a_sync.future
async def decimals(self) -> int:
return await Call(self.address, ["decimals()(uint)"], [])
return await Call(self.address, "decimals()(uint)")

# @a_sync.future(cache_type='memory')
@alru_cache(maxsize=None)
Expand Down
4 changes: 2 additions & 2 deletions y/prices/dex/balancer/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ async def id(self) -> PoolId:
Examples:
>>> pool_id = await pool.id
"""
return await Call(self.address, ["getPoolId()(bytes32)"])
return await Call(self.address, "getPoolId()(bytes32)")

__id__: HiddenMethodDescriptor[Self, PoolId]

Expand All @@ -434,7 +434,7 @@ async def vault(self) -> Optional[BalancerV2Vault]:
>>> vault = await pool.vault
"""
with suppress(ContractLogicError):
vault = await Call(self.address, ["getVault()(address)"])
vault = await Call(self.address, "getVault()(address)")
if vault == ZERO_ADDRESS:
return None
elif vault:
Expand Down
2 changes: 1 addition & 1 deletion y/prices/dex/mooniswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async def get_pool_price(
- :func:`y.prices.magic.get_price`
"""
address = await convert.to_address_async(token)
token0, token1 = await gather_methods(address, ["token0", "token1"])
token0, token1 = await gather_methods(address, ("token0", "token1"))
bal0, bal1, price0, price1, total_supply = await asyncio.gather(
(
dank_mids.eth.get_balance(address, block_identifier=block)
Expand Down
4 changes: 2 additions & 2 deletions y/prices/dex/uniswap/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async def tokens(self) -> Tuple[ERC20, ERC20]:
@a_sync.aka.cached_property
async def token0(self) -> ERC20:
try:
if token0 := await Call(self.address, ["token0()(address)"]):
if token0 := await Call(self.address, "token0()(address)"):
return ERC20(token0, asynchronous=self.asynchronous)
except ValueError as e:
continue_if_call_reverted(e)
Expand All @@ -158,7 +158,7 @@ async def token0(self) -> ERC20:
@a_sync.aka.cached_property
async def token1(self) -> ERC20:
try:
if token1 := await Call(self.address, ["token1()(address)"]):
if token1 := await Call(self.address, "token1()(address)"):
return ERC20(token1, asynchronous=self.asynchronous)
except ValueError as e:
continue_if_call_reverted(e)
Expand Down
60 changes: 30 additions & 30 deletions y/prices/dex/uniswap/v2_forks.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,81 +349,81 @@
SPECIAL_PATHS = {
Network.Mainnet: {
"sushiswap": {
"0xEF69B5697f2Fb0345cC680210fD39b593a2f9684": [
"0xEF69B5697f2Fb0345cC680210fD39b593a2f9684": (
"0xEF69B5697f2Fb0345cC680210fD39b593a2f9684",
"0x6B3595068778DD592e39A122f4f5a5cF09C90fE2",
weth,
usdc,
],
"0xbf2179859fc6D5BEE9Bf9158632Dc51678a4100e": [
),
"0xbf2179859fc6D5BEE9Bf9158632Dc51678a4100e": (
"0xbf2179859fc6D5BEE9Bf9158632Dc51678a4100e",
"0xC28E27870558cF22ADD83540d2126da2e4b464c2",
weth,
usdc,
],
"0x3166C570935a7D8554c8f4eA792ff965D2EFe1f2": [
),
"0x3166C570935a7D8554c8f4eA792ff965D2EFe1f2": (
"0x3166C570935a7D8554c8f4eA792ff965D2EFe1f2",
"0x4954Db6391F4feB5468b6B943D4935353596aEC9",
usdc,
],
"0xE6279E1c65DD41b30bA3760DCaC3CD8bbb4420D6": [
),
"0xE6279E1c65DD41b30bA3760DCaC3CD8bbb4420D6": (
"0xE6279E1c65DD41b30bA3760DCaC3CD8bbb4420D6",
"0x87F5F9eBE40786D49D35E1B5997b07cCAA8ADbFF",
weth,
usdc,
],
"0x4954Db6391F4feB5468b6B943D4935353596aEC9": [
),
"0x4954Db6391F4feB5468b6B943D4935353596aEC9": (
"0x4954Db6391F4feB5468b6B943D4935353596aEC9",
usdc,
],
"0x1E18821E69B9FAA8e6e75DFFe54E7E25754beDa0": [
),
"0x1E18821E69B9FAA8e6e75DFFe54E7E25754beDa0": (
"0x1E18821E69B9FAA8e6e75DFFe54E7E25754beDa0",
"0xEF69B5697f2Fb0345cC680210fD39b593a2f9684",
"0x6B3595068778DD592e39A122f4f5a5cF09C90fE2",
weth,
usdc,
],
"0xfC1E690f61EFd961294b3e1Ce3313fBD8aa4f85d": [
),
"0xfC1E690f61EFd961294b3e1Ce3313fBD8aa4f85d": (
"0xfC1E690f61EFd961294b3e1Ce3313fBD8aa4f85d",
"0xba100000625a3754423978a60c9317c58a424e3D",
weth,
usdc,
],
"0xBA50933C268F567BDC86E1aC131BE072C6B0b71a": [
),
"0xBA50933C268F567BDC86E1aC131BE072C6B0b71a": (
"0xBA50933C268F567BDC86E1aC131BE072C6B0b71a",
weth,
usdc,
],
"0x6102407f07029892eB5Ff02164ADFaFb85f4d222": [
),
"0x6102407f07029892eB5Ff02164ADFaFb85f4d222": (
"0x6102407f07029892eB5Ff02164ADFaFb85f4d222",
usdt,
],
"0x85034b3b2e292493D029443455Cc62ab669573B3": [
),
"0x85034b3b2e292493D029443455Cc62ab669573B3": (
"0x85034b3b2e292493D029443455Cc62ab669573B3",
"0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984",
weth,
usdc,
],
"0xb220D53F7D0f52897Bcf25E47c4c3DC0bac344F8": [
),
"0xb220D53F7D0f52897Bcf25E47c4c3DC0bac344F8": (
"0xb220D53F7D0f52897Bcf25E47c4c3DC0bac344F8",
usdc,
],
"0x383518188C0C6d7730D91b2c03a03C837814a899": [
),
"0x383518188C0C6d7730D91b2c03a03C837814a899": (
"0x383518188C0C6d7730D91b2c03a03C837814a899",
dai,
],
"0xafcE9B78D409bF74980CACF610AFB851BF02F257": [
),
"0xafcE9B78D409bF74980CACF610AFB851BF02F257": (
"0xafcE9B78D409bF74980CACF610AFB851BF02F257",
wbtc,
weth,
usdc,
],
"0x62B9c7356A2Dc64a1969e19C23e4f579F9810Aa7": [
),
"0x62B9c7356A2Dc64a1969e19C23e4f579F9810Aa7": (
"0x62B9c7356A2Dc64a1969e19C23e4f579F9810Aa7",
"0xD533a949740bb3306d119CC777fa900bA034cd52",
weth,
usdc,
],
),
},
},
}.get(chain.id, {})
Expand All @@ -447,12 +447,12 @@ def special_paths(router_address: str) -> Dict[str, Dict[Address, List[Address]]
Examples:
>>> special_paths("0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D")
{
"0xEF69B5697f2Fb0345cC680210fD39b593a2f9684": [
"0xEF69B5697f2Fb0345cC680210fD39b593a2f9684": (
"0xEF69B5697f2Fb0345cC680210fD39b593a2f9684",
"0x6B3595068778DD592e39A122f4f5a5cF09C90fE2",
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
],
),
...
}
Expand Down
2 changes: 1 addition & 1 deletion y/prices/dex/uniswap/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ def _encode_path(path) -> bytes:
>>> path = ["0xToken0Address", 3000, "0xToken1Address"]
>>> encoded_path = _encode_path(path)
"""
types = [type for _, type in zip(path, cycle(["address", "uint24"]))]
types = [type for _, type in zip(path, cycle(("address", "uint24")))]
return encode_packed(types, path)


Expand Down
7 changes: 4 additions & 3 deletions y/prices/dex/velodrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __init__(self, *args, **kwargs) -> None:
self.default_factory = default_factory[chain.id]
"""The default factory address for the current network."""

self._all_pools = Call(self.factory, ["allPools(uint256)(address)"])
self._all_pools = Call(self.factory, "allPools(uint256)(address)")
"""A prepared call for fetching all pools from the factory."""

@stuck_coro_debugger
Expand Down Expand Up @@ -399,9 +399,10 @@ async def is_contract(pool_address: Address) -> bool:
"""
if pool_address in __pools:
return True
if result := await dank_mids.eth.get_code(pool_address) not in ["0x", b""]:
__pools.append(pool_address)
if result := await dank_mids.eth.get_code(pool_address) not in ("0x", b""):
__pools_append(pool_address)
return result


__pools = []
__pools_append = __pools.append
50 changes: 25 additions & 25 deletions y/prices/lending/aave.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,49 @@


v1_pools = {
Network.Mainnet: ["0x398eC7346DcD622eDc5ae82352F02bE94C62d119"],
}.get(chain.id, [])
Network.Mainnet: ("0x398eC7346DcD622eDc5ae82352F02bE94C62d119",),
}.get(chain.id, ())

v2_pools = {
Network.Mainnet: [
Network.Mainnet: (
"0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9", # aave
"0x7937D4799803FbBe595ed57278Bc4cA21f3bFfCB", # aave amm
"0xcE744a9BAf573167B2CF138114BA32ed7De274Fa", # umee
],
Network.Polygon: [
),
Network.Polygon: (
"0x8dFf5E27EA6b7AC08EbFdf9eB090F32ee9a30fcf", # aave
],
Network.Avalanche: [
),
Network.Avalanche: (
"0x70BbE4A294878a14CB3CDD9315f5EB490e346163", # blizz
],
),
}.get(chain.id, [])

v3_pools = {
Network.Mainnet: [
Network.Mainnet: (
"0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2", # aave v3
],
Network.Optimism: [
),
Network.Optimism: (
"0x794a61358D6845594F94dc1DB02A252b5b4814aD", # aave v3
],
Network.Arbitrum: [
),
Network.Arbitrum: (
"0x794a61358D6845594F94dc1DB02A252b5b4814aD", # aave v3
],
Network.Harmony: [
),
Network.Harmony: (
"0x794a61358D6845594F94dc1DB02A252b5b4814aD", # aave v3
],
Network.Arbitrum: [
),
Network.Arbitrum: (
"0x794a61358D6845594F94dc1DB02A252b5b4814aD", # aave v3
],
Network.Fantom: [
),
Network.Fantom: (
"0x794a61358D6845594F94dc1DB02A252b5b4814aD", # aave v3
],
Network.Avalanche: [
),
Network.Avalanche: (
"0x794a61358D6845594F94dc1DB02A252b5b4814aD", # aave v3
],
Network.Polygon: [
),
Network.Polygon: (
"0x794a61358D6845594F94dc1DB02A252b5b4814aD", # aave v3
],
}.get(chain.id, [])
),
}.get(chain.id, ())


class AaveMarketBase(ContractBase):
Expand Down
2 changes: 1 addition & 1 deletion y/prices/lending/ib.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async def get_price(
"""
address = await convert.to_address_async(token)
token, total_bal, total_supply = await gather_methods(
address, ["token", "totalToken", "totalSupply"], block=block
address, ("token", "totalToken", "totalSupply"), block=block
)
token_scale, pool_scale = await asyncio.gather(
ERC20(token, asynchronous=True).scale, ERC20(address, asynchronous=True).scale
Expand Down
4 changes: 2 additions & 2 deletions y/prices/synthetix.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ async def get_currency_key(self, token: AnyAddressType) -> Optional[HexStr]:
'0x...'
"""
target = (
await Call(token, ["target()(address)"])
await Call(token, "target()(address)")
if await has_method(token, "target()(address)", sync=False)
else token
)
return (
await Call(target, ["currencyKey()(bytes32)"])
await Call(target, "currencyKey()(bytes32)")
if await has_method(token, "currencyKey()(bytes32)", sync=False)
else None
)
Expand Down
4 changes: 2 additions & 2 deletions y/prices/tokenized_fund/piedao.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def get_tokens(
"""
return [
ERC20(t)
for t in await Call(pie_address, ["getTokens()(address[])"], block_id=block)
for t in await Call(pie_address, "getTokens()(address[])", block_id=block)
]


Expand Down Expand Up @@ -164,7 +164,7 @@ async def get_balance(
1000.0
"""
balance, scale = await asyncio.gather(
Call(token.address, ["balanceOf(address)(uint)", bpool], block_id=block),
Call(token.address, ("balanceOf(address)(uint)", bpool), block_id=block),
token.__scale__,
)
return Decimal(balance) / scale
Expand Down
Loading

0 comments on commit ee78e78

Please sign in to comment.