Skip to content

Commit

Permalink
feat(ipfs): support yvecrv-jar (yearn#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
nymmrx authored Jun 11, 2021
1 parent 408b349 commit 63a4e2b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 17 deletions.
27 changes: 12 additions & 15 deletions scripts/ipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from typing import Union
from time import time
from yearn.special import Backscratcher
from yearn.special import Backscratcher, YveCRVJar

import ipfshttpclient
import requests
Expand Down Expand Up @@ -37,30 +37,27 @@

def wrap_vault(vault: Union[VaultV1, VaultV2], samples: ApySamples, aliases: dict) -> dict:
apy = vault.apy(samples)
if isinstance(vault, VaultV2) or isinstance(vault, Backscratcher):
strategies = [{"address": str(strategy.strategy), "name": strategy.name} for strategy in vault.strategies]
else:
if isinstance(vault, VaultV1):
strategies = [
{
"address": str(vault.strategy),
"name": vault.strategy.getName() if hasattr(vault.strategy, "getName") else vault.strategy._name,
}
]
else:
strategies = [{"address": str(strategy.strategy), "name": strategy.name} for strategy in vault.strategies]

inception = contract_creation_block(str(vault.vault))
total_assets = vault.vault.totalAssets() if hasattr(vault.vault, "totalAssets") else vault.vault.balance()
try:
price = magic.get_price(vault.token)
except magic.PriceError:
price = None

token_alias = aliases[str(vault.token)]["name"] if str(vault.token) in aliases else vault.token.symbol()
vault_alias = aliases[str(vault.vault)]["name"] if str(vault.vault) in aliases else token_alias

tvl = total_assets * price / 10 ** vault.vault.decimals() if price else None
tvl = vault.tvl()

return {
"inception": inception,
"address": str(vault.vault),
"symbol": vault.vault.symbol(),
"symbol": vault.symbol if hasattr(vault, "symbol") else vault.vault.symbol(),
"name": vault.name,
"display_name": vault_alias,
"icon": ICON % str(vault.vault),
Expand All @@ -71,15 +68,15 @@ def wrap_vault(vault: Union[VaultV1, VaultV2], samples: ApySamples, aliases: dic
"decimals": vault.token.decimals() if hasattr(vault.token, "decimals") else None,
"display_name": token_alias,
"icon": ICON % str(vault.token),
"price": price,
"price": tvl.price,
},
"tvl": {"total": total_assets, "value": tvl},
"tvl": dataclasses.asdict(tvl),
"apy": dataclasses.asdict(apy),
"fees": dataclasses.asdict(apy.fees),
"strategies": strategies,
"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.vault.decimals(),
"decimals": vault.decimals if hasattr(vault, "decimals") else vault.vault.decimals(),
"type": "v2" if isinstance(vault, VaultV2) else "v1",
"emergency_shutdown": vault.vault.emergencyShutdown() if hasattr(vault.vault, "emergencyShutdown") else False,
"tags": [],
Expand All @@ -103,7 +100,7 @@ def main():

samples = get_samples()

special = [Backscratcher()]
special = [YveCRVJar(), Backscratcher()]
v1_registry = RegistryV1()
v2_registry = RegistryV2()

Expand Down
7 changes: 7 additions & 0 deletions yearn/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from dataclasses import dataclass

@dataclass
class Tvl:
total_assets: int = 0
price: float = 0
tvl: float = 0
44 changes: 43 additions & 1 deletion yearn/special.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import math

from time import time
from yearn.apy.common import Apy, ApyFees
from yearn.common import Tvl
from yearn.apy.common import Apy, ApyFees, ApyPoints

import requests

Expand All @@ -13,6 +14,36 @@
from yearn.utils import contract_creation_block
from yearn.apy import ApySamples

class YveCRVJar:
def __init__(self):
self.id = "yvecrv-eth"
self.name = "pickling SushiSwap LP Token"
self.vault = Contract("0xbD17B1ce622d73bD438b9E658acA5996dc394b0d")
self.token = Contract("0x5Eff6d166D66BacBC1BF52E2C54dD391AE6b1f48")

@property
def strategies(self):
return []

@property
def decimals(self):
return 18

@property
def symbol(self):
return 'pSLP'

def apy(self, _: ApySamples) -> Apy:
data = requests.get("https://stkpowy01i.execute-api.us-west-1.amazonaws.com/prod/protocol/jar/yvecrv-eth/performance").json()
apy = data["thirtyDayFarm"] / 100.
points = ApyPoints(data["sevenDayFarm"] / 100., apy, apy)
return Apy("yvecrv-jar", apy, apy, ApyFees(), points=points)

def tvl(self, block=None) -> Tvl:
data = requests.get("https://stkpowy01i.execute-api.us-west-1.amazonaws.com/prod/protocol/value").json()
tvl = data[self.id]
return Tvl(tvl=tvl)

class Backscratcher:
def __init__(self):
self.name = "yveCRV"
Expand Down Expand Up @@ -65,6 +96,17 @@ def apy(self, _: ApySamples) -> Apy:
}
return Apy("backscratcher", apy, apy, ApyFees(), composite=composite)

def tvl(self, block=None) -> Tvl:
total_assets = self.vault.totalSupply(block_identifier=block)
try:
price = magic.get_price(self.token, block=block)
except magic.PriceError:
price = None
tvl = total_assets * price / 10 ** self.vault.decimals(block_identifier=block) if price else None
return Tvl(total_assets, price, tvl)




class Ygov:
def __init__(self):
Expand Down
12 changes: 11 additions & 1 deletion yearn/v1/vaults.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from dataclasses import dataclass
from typing import Optional
from yearn.common import Tvl

from brownie import Contract, ZERO_ADDRESS, interface, web3
from brownie.network.contract import InterfaceContainer
Expand Down Expand Up @@ -120,4 +121,13 @@ def apy(self, samples: ApySamples):
if is_curve_lp_token(self.token.address):
return apy.curve.simple(self, samples)
else:
return apy.v1.simple(self, samples)
return apy.v1.simple(self, samples)

def tvl(self, block=None):
total_assets = self.vault.balance(block_identifier=block)
try:
price = magic.get_price(self.token, block=block)
except magic.PriceError:
price = None
tvl = total_assets * price / 10 ** self.vault.decimals(block_identifier=block) if price else None
return Tvl(total_assets, price, tvl)
10 changes: 10 additions & 0 deletions yearn/v2/vaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import threading
import time
from typing import List
from yearn.common import Tvl

from semantic_version.base import Version

Expand Down Expand Up @@ -186,3 +187,12 @@ def apy(self, samples: ApySamples):
return apy.v2.average(self, samples)
else:
return apy.v2.simple(self, samples)

def tvl(self, block=None):
total_assets = self.vault.totalAssets(block_identifier=block)
try:
price = magic.get_price(self.token, block=None)
except magic.PriceError:
price = None
tvl = total_assets * price / 10 ** self.vault.decimals(block_identifier=block) if price else None
return Tvl(total_assets, price, tvl)

0 comments on commit 63a4e2b

Please sign in to comment.