robosats/control/models.py

135 lines
4.9 KiB
Python
Raw Permalink Normal View History

from django.db import models
from django.utils import timezone
2022-06-05 21:16:03 +00:00
from api.lightning.node import LNNode
2022-10-20 09:56:10 +00:00
class AccountingDay(models.Model):
day = models.DateTimeField(primary_key=True, auto_now=False, auto_now_add=False)
# Every field is denominated in Sats with (3 decimals for millisats)
# Total volume contracted
2022-10-20 09:56:10 +00:00
contracted = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
# Number of contracts
num_contracts = models.BigIntegerField(default=0, null=False, blank=False)
# Net volume of trading invoices settled (excludes disputes)
2022-10-20 09:56:10 +00:00
net_settled = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
# Net volume of trading invoices paid (excludes rewards and disputes)
2022-10-20 09:56:10 +00:00
net_paid = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
# Sum of net settled and net paid
2022-10-20 09:56:10 +00:00
net_balance = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
# Total volume of invoices settled
2022-10-20 09:56:10 +00:00
inflow = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
# Total volume of invoices paid
2022-10-20 09:56:10 +00:00
outflow = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
# Total cost in routing fees
2022-10-20 09:56:10 +00:00
routing_fees = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
# Total cost in minig fees
2022-10-20 09:56:10 +00:00
mining_fees = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
# Total inflows minus outflows and routing fees
2022-10-20 09:56:10 +00:00
cashflow = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
# Balance on earned rewards (referral rewards, slashed bonds and solved disputes)
2022-10-20 09:56:10 +00:00
outstanding_earned_rewards = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
# Balance on pending disputes (not resolved yet)
2022-10-20 09:56:10 +00:00
outstanding_pending_disputes = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
# Rewards claimed lifetime
2022-10-20 09:56:10 +00:00
lifetime_rewards_claimed = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
# Balance change from last day on earned rewards (referral rewards, slashed bonds and solved disputes)
2022-10-20 09:56:10 +00:00
earned_rewards = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
# Balance change on pending disputes (not resolved yet)
2022-10-20 09:56:10 +00:00
disputes = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
# Rewards claimed on day
2022-10-20 09:56:10 +00:00
rewards_claimed = models.DecimalField(
max_digits=15, decimal_places=3, default=0, null=False, blank=False
)
2022-06-05 21:16:03 +00:00
class BalanceLog(models.Model):
2022-06-07 22:14:56 +00:00
def get_total():
2022-10-20 09:56:10 +00:00
return (
LNNode.wallet_balance()["total_balance"]
+ LNNode.channel_balance()["local_balance"]
)
2022-06-07 22:14:56 +00:00
def get_frac():
2022-10-20 09:56:10 +00:00
return LNNode.wallet_balance()["total_balance"] / (
LNNode.wallet_balance()["total_balance"]
+ LNNode.channel_balance()["local_balance"]
)
def get_oc_total():
return LNNode.wallet_balance()["total_balance"]
def get_oc_conf():
return LNNode.wallet_balance()["confirmed_balance"]
2022-06-07 22:14:56 +00:00
def get_oc_unconf():
2022-10-20 09:56:10 +00:00
return LNNode.wallet_balance()["unconfirmed_balance"]
2022-06-07 22:14:56 +00:00
def get_ln_local():
2022-10-20 09:56:10 +00:00
return LNNode.channel_balance()["local_balance"]
2022-06-07 22:14:56 +00:00
def get_ln_remote():
2022-10-20 09:56:10 +00:00
return LNNode.channel_balance()["remote_balance"]
2022-06-07 22:14:56 +00:00
def get_ln_local_unsettled():
2022-10-20 09:56:10 +00:00
return LNNode.channel_balance()["unsettled_local_balance"]
2022-06-07 22:14:56 +00:00
def get_ln_remote_unsettled():
2022-10-20 09:56:10 +00:00
return LNNode.channel_balance()["unsettled_remote_balance"]
2022-06-07 22:14:56 +00:00
2022-06-05 21:16:03 +00:00
time = models.DateTimeField(primary_key=True, default=timezone.now)
# Every field is denominated in Sats
2022-06-07 22:14:56 +00:00
total = models.PositiveBigIntegerField(default=get_total)
2022-10-20 09:56:10 +00:00
onchain_fraction = models.DecimalField(
max_digits=6, decimal_places=5, default=get_frac
)
2022-06-07 22:14:56 +00:00
onchain_total = models.PositiveBigIntegerField(default=get_oc_total)
onchain_confirmed = models.PositiveBigIntegerField(default=get_oc_conf)
onchain_unconfirmed = models.PositiveBigIntegerField(default=get_oc_unconf)
ln_local = models.PositiveBigIntegerField(default=get_ln_local)
ln_remote = models.PositiveBigIntegerField(default=get_ln_remote)
ln_local_unsettled = models.PositiveBigIntegerField(default=get_ln_local_unsettled)
2022-10-20 09:56:10 +00:00
ln_remote_unsettled = models.PositiveBigIntegerField(
default=get_ln_remote_unsettled
)
2022-06-07 22:14:56 +00:00
def __str__(self):
return f"Balance at {self.time.strftime('%d/%m/%Y %H:%M:%S')}"
2022-06-05 21:16:03 +00:00
class Meta:
get_latest_by = "time"
2022-10-20 09:56:10 +00:00
class Dispute(models.Model):
2022-10-20 09:56:10 +00:00
pass