Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/vaults strats pg #654

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ console: build setup-network
docker build -f Dockerfile.dev -t ghcr.io/yearn/yearn-exporter .
$(compose_command) -p $$PROJECT_PREFIX run --rm --entrypoint "brownie console --network $$BROWNIE_NETWORK" exporter

shell: setup-network
shell: infra setup-network
source set_network_envs.sh
$(compose_command) -p $$PROJECT_PREFIX run --rm --entrypoint bash exporter

Expand Down
17 changes: 17 additions & 0 deletions yearn/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,28 @@ class Token(db.Entity):
address = Required(Address, column="address_id")
streams = Set('Stream', reverse="token")
vesting_escrows = Set("VestingEscrow", reverse="token")
vault = Optional("Vault", column="vault_id")

@property
def scale(self) -> int:
return 10 ** self.decimals

class Vault(db.Entity):
_table_ = "vaults"
vault_id = PrimaryKey(int, auto=True)
address = Required(str, index=True)
name = Required(str)
token = Required("Token", column="token_id", index=True)
strategy = Optional("Strategy", column="strategy_id")
version = Required(str)

class Strategy(db.Entity):
_table_ = "strategies"
strategy_id = PrimaryKey(int, auto=True)
address = Required(str, index=True)
name = Required(str)
vault = Required("Vault", column="vault_id", index=True)
version = Required(str)

# Used for wallet exporter and other analysis
class UserTx(db.Entity):
Expand Down
18 changes: 17 additions & 1 deletion yearn/outputs/postgres/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pony.orm import db_session, select
from y import Contract, ContractNotVerified, Network

from yearn.entities import (Address, Chain, Token, TreasuryTx, TxGroup, UserTx,
from yearn.entities import (Address, Chain, Token, TreasuryTx, TxGroup, UserTx, Vault, Strategy,
db)
from yearn.multicall2 import fetch_multicall
from yearn.utils import hex_to_string, is_contract
Expand Down Expand Up @@ -93,6 +93,22 @@ def cache_token(address: str) -> Token:
logger.info(f'token {symbol} added to postgres')
return token

@db_session
def cache_vault(address: str, name: str, version: str) -> Vault:
token = cache_token(address)
vault = Vault.get(token=token)
if not vault:
vault = Vault(token=token, address=address, name=name, version=version)
return vault

@db_session
def cache_strategy(vault_address: str, vault_name: str, vault_version: str, strategy_address: str, strategy_name: str, strategy_version: str) -> Strategy:
vault = cache_vault(vault_address, vault_name, vault_version)
strategy = Strategy.get(vault=vault, address=strategy_address)
if not strategy:
strategy = Strategy(vault=vault, address=strategy_address, name=strategy_name, version=strategy_version)
return strategy

@db_session
def cache_txgroup(name: str, parent: Optional[TxGroup] = None) -> TxGroup:
_txgroup = TxGroup.get(name=name)
Expand Down
13 changes: 12 additions & 1 deletion yearn/v1/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from yearn.typing import Block
from yearn.utils import contract
from yearn.v1.vaults import VaultV1
from yearn.outputs.postgres.utils import cache_strategy

logger = logging.getLogger(__name__)

Expand All @@ -32,7 +33,17 @@ def vaults(self) -> List[VaultV1]:
addresses_generator_v1_vaults = contract(addresses_provider.addressById("ADDRESSES_GENERATOR_V1_VAULTS"))

# NOTE: we assume no more v1 vaults are deployed
return [VaultV1(vault_address, *self.registry.getVaultInfo(vault_address)) for vault_address in addresses_generator_v1_vaults.assetsAddresses()]
vaults = [VaultV1(vault_address, *self.registry.getVaultInfo(vault_address)) for vault_address in addresses_generator_v1_vaults.assetsAddresses()]
for v in vaults:
strategy_name = v.strategy._name
if hasattr(v.strategy, "name"):
strategy_name = v.strategy.name()
vault_name = v.vault._name
if hasattr(v.vault, "name"):
vault_name = v.vault.name()
cache_strategy(str(v.vault), vault_name, "v1", str(v.strategy), strategy_name, "v1")

return vaults

def __repr__(self) -> str:
return f"<Registry V1 vaults={len(self.vaults)}>"
Expand Down
13 changes: 12 additions & 1 deletion yearn/v2/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from yearn.exceptions import UnsupportedNetwork
from yearn.multicall2 import fetch_multicall_async
from yearn.utils import Singleton, contract
from yearn.outputs.postgres.utils import cache_strategy
from yearn.v2.vaults import Vault

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -106,7 +107,17 @@ def load_from_ens(self):
@property
@wait_or_exit_before
def vaults(self) -> List[Vault]:
return list(self._vaults.values())
vaults = list(self._vaults.values())
for v in vaults:
for s in v.strategies:
strategy_name = s.strategy._name
if hasattr(s.strategy, "name"):
strategy_name = s.strategy.name()
vault_name = v.vault._name
if hasattr(v.vault, "name"):
vault_name = v.vault.name()
cache_strategy(str(v.vault), vault_name, v.vault.apiVersion(), str(s.strategy), strategy_name, s.strategy.apiVersion())
return vaults

@property
@wait_or_exit_before
Expand Down
Loading