Ejemplo n.º 1
0
def get_samples() -> ApySamples:
    today = datetime.today()
    now = web3.eth.block_number
    week_ago = closest_block_after_timestamp(
        (today - timedelta(days=7)).timestamp())
    month_ago = closest_block_after_timestamp(
        (today - timedelta(days=31)).timestamp())
    return ApySamples(now, week_ago, month_ago)
Ejemplo n.º 2
0
def get_samples(now_time: Optional[datetime] = None) -> ApySamples:
    if now_time is None:
        now_time = datetime.today()
        now = web3.eth.block_number
    else:
        now = closest_block_after_timestamp(now_time.timestamp())
    week_ago = closest_block_after_timestamp((now_time - timedelta(days=7)).timestamp())
    month_ago = closest_block_after_timestamp((now_time - timedelta(days=31)).timestamp())
    return ApySamples(now, week_ago, month_ago)
Ejemplo n.º 3
0
def calculate_roi(after: SharePricePoint, before: SharePricePoint) -> float:
    # calculate our average blocks per day in the past week
    now = web3.eth.block_number
    now_time = datetime.today()
    blocks_per_day = int((now - closest_block_after_timestamp((now_time - timedelta(days=7)).timestamp())) / 7)
    
    # calculate our annualized return for a vault
    pps_delta = (after.price - before.price) / (before.price or 1)
    block_delta = after.block - before.block
    days = block_delta / blocks_per_day
    annualized_roi = (1 + pps_delta) ** (365.2425 / days) - 1
    return annualized_roi
Ejemplo n.º 4
0
def main():
    yearn = Yearn(load_strategies=False)
    start = START_DATE[chain.id]
    interval = timedelta(hours=24)

    for snapshot in generate_snapshot_range(start, interval):
        while chain[-1].timestamp < snapshot.timestamp():
            time.sleep(60)

        with Session(engine) as session:
            insert_start = time.perf_counter()

            if session.exec(
                    select(Block).where(Block.snapshot == snapshot,
                                        Block.chain_id == chain.id)).first():
                logger.debug("block exists for snapshot=%s", snapshot)
                continue

            logger.debug("inserting snapshot=%s", snapshot)
            block = closest_block_after_timestamp(snapshot.timestamp())
            assert block is not None, "no block after timestamp found"
            assets = yearn.total_value_at(block)

            new_block = Block(
                chain_id=chain.id,
                height=block,
                timestamp=chain[block].timestamp,
                snapshot=snapshot,
            )
            session.add(new_block)

            for product in assets:
                for name in assets[product]:
                    snap = Snapshot(
                        block=new_block,
                        product=product,
                        name=name,
                        assets=assets[product][name],
                    )
                    session.add(snap)

            session.commit()

            total = sum(sum(x.values()) for x in assets.values())
            elapsed = time.perf_counter() - insert_start
            logger.info(
                f"inserted snapshot={snapshot} block={block:,d} tvl={total:,.0f} in {elapsed:.2f}s"
            )
Ejemplo n.º 5
0
def main():
    yearn = Yearn(load_strategies=False)
    start = datetime(2020, 2, 12,
                     tzinfo=timezone.utc)  # first iearn deployment
    interval = timedelta(hours=1)
    buffer = timedelta(minutes=5)
    synced = False
    for snapshot in generate_snapshot_range(start, interval):
        while snapshot + buffer > datetime.now().astimezone(timezone.utc):
            if not synced:
                synced = True
                logger.info("synced")
            time.sleep(60)

        with db_session:
            if Block.get(snapshot=snapshot):
                logger.debug("block exists for snapshot=%s", snapshot)
                continue

            logger.debug("inserting snapshot=%s", snapshot)
            block = closest_block_after_timestamp(snapshot.timestamp())
            assert block is not None, "no block after timestamp found"
            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)

            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"inserted snapshot=%s block=%d tvl=%.0f", snapshot,
                        block, total)
def export_snapshot(treasury, snapshot, ts, exporter_name):
    block = closest_block_after_timestamp(ts)
    assert block is not None, "no block after timestamp found"
    treasury.export(block, ts)
    logger.info("exported treasury snapshot %s", snapshot)
Ejemplo n.º 7
0
def export_snapshot(yearn, snapshot, ts, exporter_name):
    block = closest_block_after_timestamp(ts)
    assert block is not None, "no block after timestamp found"
    yearn.export(block, ts)
    logger.info("exported historical snapshot %s", snapshot)