Skip to content

Commit

Permalink
Revert "Async load registry (#580)"
Browse files Browse the repository at this point in the history
This reverts commit 11d835e.
  • Loading branch information
BobTheBuidler committed Sep 5, 2023
1 parent 59358ee commit cb030dc
Show file tree
Hide file tree
Showing 25 changed files with 210 additions and 269 deletions.
3 changes: 1 addition & 2 deletions scripts/abracadabra_wrappers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sentry_sdk
from multicall.utils import await_awaitable
from rich import print
from y import Contract

Expand Down Expand Up @@ -32,7 +31,7 @@

def main():
v2 = Registry()
vaults = [str(vault.vault) for vault in await_awaitable(v2.vaults)]
vaults = [str(vault.vault) for vault in v2.vaults]
wrappers = []
for cauldron in CAULDRONS:
collateral = Contract(cauldron).collateral()
Expand Down
7 changes: 3 additions & 4 deletions scripts/apy.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ async def _main():
v1_registry = RegistryV1()
v2_registry = RegistryV2()

v1_data, v2_vaults = await asyncio.gather(
v1_data, v2_data = await asyncio.gather(
asyncio.gather(*[vault.apy(samples) for vault in v1_registry.vaults]),
v2_registry.vaults,
asyncio.gather(*[_get_v2_data(vault, samples) for vault in v2_registry.vaults]),
)
v2_data = asyncio.gather(*[_get_v2_data(vault, samples) for vault in v2_vaults])


data.extend([
{"product": apy.type, "name": vault.name, "apy": apy.net_apy}
for vault, apy in zip(v1_registry.vaults, v1_data)
Expand Down
2 changes: 1 addition & 1 deletion scripts/exporters/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

warnings.simplefilter("ignore", BrownieEnvironmentWarning)

yearn = Yearn()
yearn = Yearn(load_strategies=False)

logger = logging.getLogger('yearn.transactions_exporter')

Expand Down
2 changes: 1 addition & 1 deletion scripts/exporters/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

logger = logging.getLogger('yearn.wallet_exporter')

yearn = Yearn(watch_events_forever=False)
yearn = Yearn(load_strategies=False, watch_events_forever=False)

# start: 2020-02-12 first iearn deployment
# start opti: 2022-01-01 an arbitrary start timestamp because the default start is < block 1 on opti and messes things up
Expand Down
2 changes: 1 addition & 1 deletion scripts/historical_tvl.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def generate_snapshot_range(start, interval):


def main():
yearn = Yearn()
yearn = Yearn(load_strategies=False)
start = START_DATE[chain.id]
interval = timedelta(hours=24)

Expand Down
10 changes: 4 additions & 6 deletions scripts/print_strategies.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import asyncio
from datetime import datetime

import click
import sentry_sdk
from brownie.utils.output import build_tree
from multicall.utils import await_awaitable

sentry_sdk.set_tag('script','print_strategies')

Expand All @@ -13,8 +11,9 @@ def main():
from yearn.v2.registry import Registry
registry = Registry()
print(registry)
registry.load_strategies()
tree = []
for vault in await_awaitable(registry.vaults):
for vault in registry.vaults:
transforms = {
'performanceFee': lambda bps: f'{bps / 10000:.2%}',
'activation': lambda ts: datetime.utcfromtimestamp(ts),
Expand All @@ -29,10 +28,9 @@ def main():
'totalLoss': lambda tokens: f'{tokens / vault.scale}',
}
strategies = []
strats, revoked_strats = await_awaitable(asyncio.gather(vault.strategies, vault.revoked_strategies))
for strategy in strats + revoked_strats:
for strategy in vault.strategies + vault.revoked_strategies:
config = vault.vault.strategies(strategy.strategy).dict()
color = 'green' if strategy in strats else 'red'
color = 'green' if strategy in vault.strategies else 'red'
strategies.append([
f'{config.get("debtRatio", 0) / 10000:.2%} ' + click.style(strategy.name, fg=color) + f' {strategy.strategy}',
*[f'{k} = {transforms[k](v)}' for k, v in config.items()]
Expand Down
10 changes: 4 additions & 6 deletions scripts/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ async def wrap_vault(
"name": await vault.strategy.getName.coroutine() if hasattr(vault.strategy, "getName") else vault.strategy._name,
}
]
elif isinstance(vault, VaultV2):
strategies = [{"address": str(strategy.strategy), "name": strategy.name} for strategy in await vault.strategies]
else:
strategies = [{"address": str(strategy.strategy), "name": strategy.name} for strategy in vault.strategies]

Expand Down Expand Up @@ -91,7 +89,7 @@ async def wrap_vault(
"tvl": dataclasses.asdict(await tvl_fut),
"apy": dataclasses.asdict(await apy_fut),
"strategies": strategies,
"endorsed": await vault.is_endorsed if hasattr(vault, "is_endorsed") else True,
"endorsed": vault.is_endorsed if hasattr(vault, "is_endorsed") else True,
"version": vault.api_version if hasattr(vault, "api_version") else "0.1",
"decimals": vault.decimals if hasattr(vault, "decimals") else await ERC20(vault.vault, asynchronous=True).decimals,
"type": "v2" if isinstance(vault, VaultV2) else "v1",
Expand Down Expand Up @@ -215,14 +213,14 @@ async def _main():
if chain.id == Network.Mainnet:
special = [YveCRVJar(), Backscratcher()]
registry_v1 = RegistryV1()
vaults = list(itertools.chain(special, registry_v1.vaults, *await asyncio.gather(registry_v2.vaults, registry_v2.experiments)))
vaults = list(itertools.chain(special, registry_v1.vaults, registry_v2.vaults, registry_v2.experiments))
else:
vaults = await registry_v2.vaults
vaults = registry_v2.vaults

if len(vaults) == 0:
raise ValueError(f"No vaults found for chain_id: {chain.id}")

assets_metadata = await get_assets_metadata(await registry_v2.vaults)
assets_metadata = await get_assets_metadata(registry_v2.vaults)

data = await tqdm_asyncio.gather(*[wrap_vault(vault, samples, aliases, icon_url, assets_metadata) for vault in vaults])

Expand Down
8 changes: 3 additions & 5 deletions scripts/s3_loanscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from brownie import web3
from brownie.exceptions import BrownieEnvironmentWarning
from dotenv import find_dotenv, load_dotenv
from multicall.utils import await_awaitable
from y import Contract

from yearn import logs
Expand Down Expand Up @@ -68,8 +67,7 @@ def write_json(json_dict: dict, path: str):
def main():
samples = get_samples()
registry_v2 = RegistryV2()
vaults = await_awaitable(registry_v2.vaults)
assets_metadata = get_assets_metadata(vaults)
assets_metadata = get_assets_metadata(registry_v2.vaults)

loanscan_vault_symbols = []
loanscan_vault_json = []
Expand All @@ -88,9 +86,9 @@ def main():
logger.info(f'failed to reduce yveCrv lendRate, {str(yveCrvVault.vault)} {yveCrvVault}')
logger.error(yveCrvException)

for vault in vaults:
for vault in registry_v2.vaults:
try:
vault_not_endorsed = not (hasattr(vault, "is_endorsed") and await_awaitable(vault.is_endorsed))
vault_not_endorsed = not (hasattr(vault, "is_endorsed") and vault.is_endorsed)
if vault_not_endorsed:
continue

Expand Down
8 changes: 2 additions & 6 deletions scripts/tokenlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import requests
import sentry_sdk
from multicall.utils import await_awaitable
from semantic_version import Version
from tokenlists import TokenInfo, TokenList
from toolz import unique
Expand All @@ -19,7 +18,7 @@


def main():
yearn = Yearn()
yearn = Yearn(load_strategies=False)
excluded = {
"0xBa37B002AbaFDd8E89a1995dA52740bbC013D992",
"0xe2F6b9773BF3A015E2aA70741Bde1498bdB9425b",
Expand All @@ -31,10 +30,7 @@ def main():

# Token derived by products
for product in yearn.registries:
vaults = yearn.registries[product].vaults
if not isinstance(vaults, list):
vaults = await_awaitable(vaults)
vaults = [item.vault for item in vaults if str(item.vault) not in excluded]
vaults = [item.vault for item in yearn.registries[product].vaults if str(item.vault) not in excluded]
metadata = multicall_matrix(vaults, ["name", "symbol", "decimals"])
for vault in vaults:
tokens.append(
Expand Down
2 changes: 1 addition & 1 deletion scripts/tvl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

def main():
data = []
yearn = Yearn()
yearn = Yearn(load_strategies=False)

for product, registry in yearn.registries.items():
for name, tvl in registry.total_value_at().items():
Expand Down
10 changes: 3 additions & 7 deletions tests/apy/test_convex.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from unittest.mock import MagicMock

import pytest
from multicall.utils import await_awaitable

from unittest.mock import MagicMock
from yearn.apy.curve.simple import _ConvexVault
from yearn.v2.vaults import Vault as VaultV2


@pytest.fixture
def v2_vault():
return MagicMock(spec=VaultV2)
Expand All @@ -25,11 +21,11 @@ def non_convex_strategy():

def test_is_convex_vault(v2_vault, convex_strategy):
v2_vault.strategies = [convex_strategy]
assert await_awaitable(_ConvexVault.is_convex_vault(v2_vault)) == True
assert _ConvexVault.is_convex_vault(v2_vault) == True

def test_is_not_convex_vault(v2_vault, non_convex_strategy):
v2_vault.strategies = [non_convex_strategy]
assert await_awaitable(_ConvexVault.is_convex_vault(v2_vault)) == False
assert _ConvexVault.is_convex_vault(v2_vault) == False

def test_is_convex_strategy(convex_strategy):
assert _ConvexVault.is_convex_strategy(convex_strategy) == True
Expand Down
5 changes: 2 additions & 3 deletions tests/yearn/v1/test_vault_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pytest
from brownie import ZERO_ADDRESS, chain
from brownie.exceptions import VirtualMachineError
from multicall.utils import await_awaitable
from y import Contract
from y.contracts import contract_creation_block

Expand Down Expand Up @@ -78,7 +77,7 @@ def test_describe_vault_v1(vault: VaultV1):
assert "max" not in description
assert "strategy buffer" in description

if await_awaitable(vault.is_curve_vault) and hasattr(strategy, "proxy"):
if vault.is_curve_vault and hasattr(strategy, "proxy"):
vote_proxy, gauge = fetch_multicall(
[strategy, "voter"], # voter is static, can pin
[strategy, "gauge"], # gauge is static per strategy, can cache
Expand Down Expand Up @@ -141,4 +140,4 @@ def test_is_curve_vault():
for vault in registry.vaults:
if vault.name in non_curve:
continue
assert await_awaitable(vault.is_curve_vault)
assert vault.is_curve_vault
11 changes: 5 additions & 6 deletions tests/yearn/v2/test_vault_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from yearn.v2.vaults import Vault

registry = Registry(watch_events_forever=False)
start_block = start_block = min(contract_creation_block(vault.vault.address)for vault in await_awaitable(registry.vaults))
start_block = start_block = min(contract_creation_block(vault.vault.address)for vault in registry.vaults)
blocks = [randint(start_block,chain.height) for i in range(50)]


Expand All @@ -19,7 +19,7 @@ def test_describe_v2(block):
assert registry.describe(block=block)


@pytest.mark.parametrize('vault', await_awaitable(registry.vaults))
@pytest.mark.parametrize('vault',registry.vaults)
def test_describe_vault_v2(vault: Vault):
blocks = [randint(contract_creation_block(vault.vault.address), chain.height) for i in range(25)]
for block in blocks:
Expand All @@ -35,11 +35,10 @@ def test_describe_vault_v2(vault: Vault):
assert "tvl" in description, f"Unable to fetch tvl for {vault.name}."

assert "strategies" in description, f"No strategies fetched for {vault.name}."
for strategy in await_awaitable(vault.strategies):
unique_name = await_awaitable(strategy.unique_name)
assert strategy._views, f"Unable to fetch views for strategy {unique_name}."
for strategy in vault.strategies:
assert strategy._views, f"Unable to fetch views for strategy {strategy.unique_name}."
for view in strategy._views:
assert view in description["strategies"][unique_name], f"Unable to fetch {view} for strategy {strategy.name}."
assert view in description["strategies"][strategy.unique_name], f"Unable to fetch {view} for strategy {strategy.name}."


def test_active_vaults_at_v2_current():
Expand Down
27 changes: 12 additions & 15 deletions yearn/apy/curve/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,10 @@ async def calculate_simple(vault, gauge: Gauge, samples: ApySamples) -> Apy:
if vault:
if isinstance(vault, VaultV2):
vault_contract = vault.vault
strats = await vault.strategies
if len(strats) > 0 and hasattr(strats[0].strategy, "keepCRV"):
crv_keep_crv = await strats[0].strategy.keepCRV.coroutine(block_identifier=block) / 1e4
elif len(strats) > 0 and hasattr(strats[0].strategy, "keepCrvPercent"):
crv_keep_crv = await strats[0].strategy.keepCrvPercent.coroutine(block_identifier=block) / 1e4
if len(vault.strategies) > 0 and hasattr(vault.strategies[0].strategy, "keepCRV"):
crv_keep_crv = await vault.strategies[0].strategy.keepCRV.coroutine(block_identifier=block) / 1e4
elif len(vault.strategies) > 0 and hasattr(vault.strategies[0].strategy, "keepCrvPercent"):
crv_keep_crv = await vault.strategies[0].strategy.keepCrvPercent.coroutine(block_identifier=block) / 1e4
else:
crv_keep_crv = 0
performance = await vault_contract.performanceFee.coroutine(block_identifier=block) / 1e4 if hasattr(vault_contract, "performanceFee") else 0
Expand All @@ -289,25 +288,24 @@ async def calculate_simple(vault, gauge: Gauge, samples: ApySamples) -> Apy:
cvx_vault = None
# if the vault consists of only a convex strategy then return
# specialized apy calculations for convex
if await _ConvexVault.is_convex_vault(vault):
strats = await vault.strategies
cvx_strategy = strats[0].strategy
if _ConvexVault.is_convex_vault(vault):
cvx_strategy = vault.strategies[0].strategy
cvx_vault = _ConvexVault(cvx_strategy, vault, gauge.gauge)
return await cvx_vault.apy(base_asset_price, pool_price, base_apr, pool_apy, management, performance)

# if the vault has two strategies then the first is curve and the second is convex
if isinstance(vault, VaultV2) and len(strats := await vault.strategies) == 2: # this vault has curve and convex
if isinstance(vault, VaultV2) and len(vault.strategies) == 2: # this vault has curve and convex

# The first strategy should be curve, the second should be convex.
# However the order on the vault object here does not correspond
# to the order on the withdrawal queue on chain, therefore we need to
# re-order so convex is always second if necessary
first_strategy = strats[0].strategy
second_strategy = strats[1].strategy
first_strategy = vault.strategies[0].strategy
second_strategy = vault.strategies[1].strategy

crv_strategy = first_strategy
cvx_strategy = second_strategy
if not _ConvexVault.is_convex_strategy(strats[1]):
if not _ConvexVault.is_convex_strategy(vault.strategies[1]):
cvx_strategy = first_strategy
crv_strategy = second_strategy

Expand Down Expand Up @@ -375,7 +373,7 @@ def __init__(self, cvx_strategy, vault, gauge, block=None) -> None:
self.gauge = gauge

@staticmethod
async def is_convex_vault(vault) -> bool:
def is_convex_vault(vault) -> bool:
"""Determines whether the passed in vault is a Convex vault
i.e. it only has one strategy that's based on farming Convex.
"""
Expand All @@ -384,8 +382,7 @@ async def is_convex_vault(vault) -> bool:
if not isinstance(vault, VaultV2):
return False

strats = await vault.strategies
return len(strats) == 1 and _ConvexVault.is_convex_strategy(strats[0])
return len(vault.strategies) == 1 and _ConvexVault.is_convex_strategy(vault.strategies[0])

@staticmethod
def is_convex_strategy(strategy) -> bool:
Expand Down
6 changes: 3 additions & 3 deletions yearn/apy/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def closest(haystack, needle):


async def simple(vault, samples: ApySamples) -> Apy:
harvests = sorted([harvest for strategy in await vault.strategies for harvest in await run_in_thread(getattr, strategy, "harvests")])
harvests = sorted([harvest for strategy in vault.strategies for harvest in await run_in_thread(getattr, strategy, "harvests")])

# we don't want to display APYs when vaults are ramping up
if len(harvests) < 2:
Expand Down Expand Up @@ -95,7 +95,7 @@ async def simple(vault, samples: ApySamples) -> Apy:

# for performance fee, half comes from strategy (strategist share) and half from the vault (treasury share)
strategy_fees = []
for strategy in await vault.strategies: # look at all of our strategies
for strategy in vault.strategies: # look at all of our strategies
strategy_info = await contract.strategies.coroutine(strategy.strategy)
debt_ratio = strategy_info['debtRatio'] / 10000
performance_fee = strategy_info['performanceFee']
Expand Down Expand Up @@ -193,7 +193,7 @@ async def average(vault: "Vault", samples: ApySamples) -> Apy:

# for performance fee, half comes from strategy (strategist share) and half from the vault (treasury share)
strategy_fees = []
for strategy in await vault.strategies: # look at all of our strategies
for strategy in vault.strategies: # look at all of our strategies
strategy_info = await contract.strategies.coroutine(strategy.strategy)
debt_ratio = strategy_info['debtRatio'] / 10000
performance_fee = strategy_info['performanceFee']
Expand Down
5 changes: 2 additions & 3 deletions yearn/apy/velo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ async def get_staking_pool(underlying: str) -> Optional[Contract]:
return None if staking_pool == ZERO_ADDRESS else await Contract.coroutine(staking_pool)

async def staking(vault: "Vault", staking_rewards: Contract, samples: ApySamples, block: Optional[int]=None) -> float:
strats = await vault.strategies
if len(strats) == 0:
if len(vault.strategies) == 0:
return Apy("v2:velo_no_strats", 0, 0, ApyFees(0, 0), ApyPoints(0, 0, 0))

end = await staking_rewards.periodFinish.coroutine(block_identifier=block)
Expand All @@ -38,7 +37,7 @@ async def staking(vault: "Vault", staking_rewards: Contract, samples: ApySamples
rate = await staking_rewards.rewardRate.coroutine(block_identifier=block) if hasattr(staking_rewards, "rewardRate") else 0
performance = await vault.vault.performanceFee.coroutine(block_identifier=block) / 1e4 if hasattr(vault.vault, "performanceFee") else 0
management = await vault.vault.managementFee.coroutine(block_identifier=block) / 1e4 if hasattr(vault.vault, "managementFee") else 0
keep = await strats[0].strategy.localKeepVELO.coroutine(block_identifier=block) / 1e4 if hasattr(strats[0].strategy, "localKeepVELO") else 0
keep = await vault.strategies[0].strategy.localKeepVELO.coroutine(block_identifier=block) / 1e4 if hasattr(vault.strategies[0].strategy, "localKeepVELO") else 0
rate = rate * (1 - keep)
fees = ApyFees(performance=performance, management=management, keep_velo=keep)

Expand Down
Loading

0 comments on commit cb030dc

Please sign in to comment.