Skip to content

Commit

Permalink
feat: Added utilization field to pool
Browse files Browse the repository at this point in the history
  • Loading branch information
tsifrer committed Jul 19, 2023
1 parent 2236894 commit 6f5d245
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 17 deletions.
39 changes: 39 additions & 0 deletions ajna/management/commands/fix_pool_snapshot_utilization.py
@@ -0,0 +1,39 @@
from decimal import Decimal
from django.core.management.base import BaseCommand

from ajna.v1.goerli.chain import GoerliModels
from ajna.v1.ethereum.chain import EthereumModels


class Command(BaseCommand):
def _fix_goerli(self):
models = GoerliModels()
snapshots = models.pool_snapshot.objects.filter(utilization__isnull=True)
self.stdout.write(
"Fixing {} goerli pool snapshot utilization".format(snapshots.count())
)
for snapshot in snapshots:
utilization = Decimal("0")
if snapshot.pool_size > 0:
utilization = snapshot.debt / snapshot.pool_size

snapshot.utilization = utilization
snapshot.save(update_fields=["utilization"])

def _fix_ethereum(self):
models = EthereumModels()
snapshots = models.pool_snapshot.objects.filter(utilization__isnull=True)
self.stdout.write(
"Fixing {} ethereum pool snapshot utilization".format(snapshots.count())
)
for snapshot in snapshots:
utilization = Decimal("0")
if snapshot.pool_size > 0:
utilization = snapshot.debt / snapshot.pool_size

snapshot.utilization = utilization
snapshot.save(update_fields=["utilization"])

def handle(self, *args, **options):
self._fix_goerli()
self._fix_ethereum()
32 changes: 32 additions & 0 deletions ajna/migrations/0024_v1ethereumpool_utilization_and_more.py
@@ -0,0 +1,32 @@
# Generated by Django 4.2.3 on 2023-07-19 08:12

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("ajna", "0023_v1ethereumliquidationauction_wallet_address_and_more"),
]

operations = [
migrations.AddField(
model_name="v1ethereumpool",
name="utilization",
field=models.DecimalField(decimal_places=18, max_digits=32, null=True),
),
migrations.AddField(
model_name="v1ethereumpoolsnapshot",
name="utilization",
field=models.DecimalField(decimal_places=18, max_digits=32, null=True),
),
migrations.AddField(
model_name="v1goerlipool",
name="utilization",
field=models.DecimalField(decimal_places=18, max_digits=32, null=True),
),
migrations.AddField(
model_name="v1goerlipoolsnapshot",
name="utilization",
field=models.DecimalField(decimal_places=18, max_digits=32, null=True),
),
]
17 changes: 4 additions & 13 deletions ajna/models/base.py
Expand Up @@ -29,8 +29,10 @@ class PoolBase(models.Model):
burn_epoch = models.BigIntegerField()
total_ajna_burned = models.DecimalField(max_digits=32, decimal_places=18)
min_debt_amount = models.DecimalField(max_digits=32, decimal_places=18)
actual_utilization = models.DecimalField(max_digits=32, decimal_places=18)
target_utilization = models.DecimalField(max_digits=32, decimal_places=18)
# Our calculation of utilization
utilization = models.DecimalField(max_digits=32, decimal_places=18, null=True)
actual_utilization = models.DecimalField(max_digits=32, decimal_places=18) # MAU
target_utilization = models.DecimalField(max_digits=32, decimal_places=18) # TU
total_bond_escrowed = models.DecimalField(max_digits=32, decimal_places=18)

datetime = models.DateTimeField(db_index=True)
Expand Down Expand Up @@ -76,17 +78,6 @@ class Meta:
unique_together = ("address", "datetime")


# class PoolCreated(models.Model):
# pool = models.CharField(max_length=42, unique=True)
# block_number = models.BigIntegerField()
# timestamp = models.BigIntegerField()
# datetime = models.DateTimeField()
# transaction_hash = models.CharField(max_length=66, null=True)

# def __str__(self):
# return self.pool


class Token(models.Model):
underlying_address = models.CharField(max_length=42, unique=True, db_index=True)
symbol = models.CharField(max_length=64, db_index=True)
Expand Down
7 changes: 4 additions & 3 deletions ajna/v1/modules/pools.py
Expand Up @@ -76,9 +76,9 @@ def fetch_and_save_pool_data(
pending_inflator = Decimal(inflators.get(pool_data["id"], 1))
debt = t0debt * pending_inflator

actual_utilization = Decimal("0")
utilization = Decimal("0")
if pool_size > 0:
actual_utilization = debt / pool_size
utilization = debt / pool_size

# If lup is max price, set it to 0 as max price means there are no loans
lup = Decimal(pool_data["lup"])
Expand Down Expand Up @@ -118,7 +118,8 @@ def fetch_and_save_pool_data(
"burn_epoch": pool_data["burnEpoch"],
"total_ajna_burned": pool_data["totalAjnaBurned"],
"min_debt_amount": pool_data["minDebtAmount"],
"actual_utilization": actual_utilization,
"utilization": utilization,
"actual_utilization": utilization,
"target_utilization": pool_data["targetUtilization"],
"total_bond_escrowed": pool_data["totalBondEscrowed"],
"quote_token_balance": pool_data["quoteTokenBalance"],
Expand Down
2 changes: 1 addition & 1 deletion latest_migrations.manifest
@@ -1,5 +1,5 @@
admin: 0003_logentry_add_action_flag_choices
ajna: 0023_v1ethereumliquidationauction_wallet_address_and_more
ajna: 0024_v1ethereumpool_utilization_and_more
auth: 0012_alter_user_first_name_max_length
contenttypes: 0002_remove_content_type_name
django_celery_beat: 0018_improve_crontab_helptext
Expand Down

0 comments on commit 6f5d245

Please sign in to comment.