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

fix daily apy #376

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 34 additions & 4 deletions eagleproject/core/blockchain/harvest/transaction_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
explode_log_data
)
from core.blockchain.harvest.snapshots import (
build_asset_block
build_asset_block,
latest_snapshot_block_number
)
from core.blockchain.rpc import (
creditsBalanceOf,
Expand Down Expand Up @@ -101,13 +102,17 @@
from core.coingecko import get_coin_history
from core.defillama import get_stablecoin_market_cap

from core.logging import get_logger

import simplejson as json

from django.core.exceptions import ObjectDoesNotExist

from eth_abi import decode_single
from eth_utils import decode_hex

log = get_logger(__name__)

ACCOUNT_ANALYZE_PARALLELISM=30

class rebase_log:
Expand Down Expand Up @@ -744,6 +749,24 @@ def backfill_subscribers():
sub.save()


def get_block_time_from_block_number(number):
result = Block.objects.filter(block_number__gte=number).order_by('block_time')[:1]

if len(result) != 1:
raise Exception('Can not find block time for block number', START_OF_PROJECT)

return result[0].block_time


def backfill_daily_stats(project=OriginTokens.OUSD):
START_OF_PROJECT = START_OF_OUSD_V2 if project == OriginTokens.OUSD else START_OF_OETH
start_time = get_block_time_from_block_number(START_OF_PROJECT)
latest_time = get_block_time_from_block_number(latest_snapshot_block_number(project))
days = (latest_time - start_time).days
_daily_rows(int(days), latest_snapshot_block_number(project), project=project)
return


# get all accounts that at some point held OUSD
def fetch_all_holders(project=OriginTokens.OUSD):
to_addresses = list(map(lambda log: log['to_address'], TokenTransfer.objects.filter(project=project).values('to_address').distinct()))
Expand Down Expand Up @@ -1602,7 +1625,6 @@ def _daily_rows(steps, latest_block_number, project, start_at=0):
block.block_time - timedelta(seconds=24 * 60 * 60)
).replace(tzinfo=timezone.utc)
if last_snapshot:

contract_address = OUSD_VAULT if project == OriginTokens.OUSD else OETH_VAULT

rebase_logs = get_rebase_logs(last_snapshot.block_number, block_number, project)
Expand Down Expand Up @@ -1638,9 +1660,17 @@ def _daily_rows(steps, latest_block_number, project, start_at=0):
# other_change = 1 - (s.rebasing_credits_per_token / last_snapshot.rebasing_credits_per_token)
change = Decimal(sum(event['amount'] for event in s.rebase_events)) / (s.computed_supply - s.non_rebasing_supply)


if change:
interval = block.block_time - last_snapshot.block_time
if (interval.total_seconds() > 90000):
log.warning("{}: daily stats interval too long ({})".format(last_snapshot.block_time, interval))
elif (interval.total_seconds() < 82800):
log.warning("{}: daily stats interval too short ({})".format(last_snapshot.block_time, interval))

# apr based on cumulative yield from yield events taken after block.block_time, and before next snapshot
# the number of blocks in this period doesn't matter, as calculation includes all yield events in 24h period
s.apr = (
Decimal(100) * change * (Decimal(365) * BLOCKS_PER_DAY) / blocks
Decimal(100) * change * Decimal(365)
sparrowDom marked this conversation as resolved.
Show resolved Hide resolved
)
s.apy = to_apy(s.apr, 1)

Expand Down
5 changes: 5 additions & 0 deletions eagleproject/scripts/backfill_daily_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from core.blockchain.harvest.transaction_history import backfill_daily_stats

def run(*script_args):
project = script_args[0] if len(script_args) > 0 else None
backfill_daily_stats(project)
Loading