forked from yearn/yearn-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add requirements * feat: move contract creation to utils * feat: add contract creation block fallback * feat: add backscratcher * feat: add ygov * feat: add api and gross historical tvl exporter * chore: remove old script, fix requirements, add sleep interval back * fix: requirements
- Loading branch information
Showing
16 changed files
with
286 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
eth-brownie>=1.13.4 | ||
web3>=5.11.1 | ||
click>=7.1.2 | ||
tabulate>=0.8.7 | ||
joblib>=1.0.1 | ||
cachetools>=4.1.1 | ||
fastapi>=0.63.0 | ||
uvicorn>=0.13.4 | ||
pony>=0.7.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import logging | ||
import time | ||
from datetime import datetime, timedelta, timezone | ||
from itertools import count | ||
|
||
from yearn.entities import Block, Snapshot, db_session | ||
from yearn.utils import closest_block_after_timestamp, get_block_timestamp | ||
from yearn.yearn import Yearn | ||
|
||
logger = logging.getLogger("yearn.historical_tvl") | ||
|
||
|
||
def generate_snapshot_blocks(start, interval): | ||
for i in count(): | ||
while start + i * interval > datetime.now().astimezone(timezone.utc): | ||
time.sleep(60) | ||
|
||
timestamp = start + i * interval | ||
block = closest_block_after_timestamp(timestamp.timestamp()) | ||
yield block, timestamp | ||
|
||
|
||
def main(): | ||
yearn = Yearn() | ||
start = datetime(2020, 2, 12, tzinfo=timezone.utc) # first iearn deployment | ||
interval = timedelta(days=1) | ||
for block, snapshot_ts in generate_snapshot_blocks(start, interval): | ||
with db_session: | ||
if Block.get(block=block): | ||
continue | ||
|
||
assets = yearn.total_value_at(block) | ||
block_ts = datetime.fromtimestamp(get_block_timestamp(block)).astimezone(timezone.utc) | ||
new_block = Block(block=block, timestamp=block_ts, snapshot=snapshot_ts) | ||
|
||
for product in assets: | ||
for name in assets[product]: | ||
Snapshot(block=new_block, product=product, name=name, assets=assets[product][name]) | ||
|
||
total = sum(sum(x.values()) for x in assets.values()) | ||
logger.info(f"%s block %d tvl %.0f", snapshot_ts.date(), block, total) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from collections import defaultdict | ||
|
||
from cachetools.func import ttl_cache | ||
from fastapi import FastAPI | ||
|
||
from yearn.entities import Snapshot, db_session, select | ||
|
||
app = FastAPI(title="Yearn Exporter") | ||
tree = lambda: defaultdict(tree) | ||
|
||
|
||
@ttl_cache(600) | ||
def get_daily_tvl(): | ||
with db_session: | ||
return select( | ||
(snap.block.snapshot.date(), sum(snap.assets)) for snap in Snapshot | ||
).order_by(1)[:] | ||
|
||
|
||
@ttl_cache(600) | ||
def get_daily_tvl_detailed(): | ||
data = tree() | ||
with db_session: | ||
for timestamp, product, name, assets in select( | ||
(snap.block.snapshot.date(), snap.product, snap.name, snap.assets) for snap in Snapshot | ||
).order_by(1): | ||
if assets > 0: | ||
data[timestamp][product][name] = assets | ||
return data | ||
|
||
|
||
@app.get("/v1/tvl", name="tvl") | ||
def read_daily_tvl(): | ||
"""Daily historical TVL snapshot.""" | ||
return get_daily_tvl() | ||
|
||
|
||
@app.get("/v1/tvl/detailed", name="tvl detailed") | ||
def read_daily_tvl_detailed(): | ||
"""Detailed daily historical TVL snapshot broken down by product and contract.""" | ||
return get_daily_tvl_detailed() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import os | ||
from datetime import datetime | ||
from pony.orm import * | ||
|
||
db = Database() | ||
|
||
|
||
class Block(db.Entity): | ||
_table_ = "blocks" | ||
|
||
block = PrimaryKey(int) | ||
timestamp = Required(datetime, sql_type="timestamptz") | ||
snapshot = Optional(datetime, sql_type="timestamptz") | ||
|
||
snapshots = Set("Snapshot") | ||
|
||
|
||
class Snapshot(db.Entity): | ||
_table_ = "snapshots" | ||
|
||
block = Required(Block) | ||
product = Required(str) | ||
name = Required(str) | ||
assets = Required(float) | ||
delegated = Optional(float) # to be filled later | ||
|
||
|
||
db.bind( | ||
provider="postgres", | ||
user=os.environ.get("PGUSER", "postgres"), | ||
host=os.environ.get("PGHOST", "127.0.0.1"), | ||
database="yearn", | ||
) | ||
|
||
db.generate_mapping(create_tables=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.