0ptr/nullptr/models/contract.py

70 lines
2.0 KiB
Python

from time import time
from nullptr.util import *
from .base import Base
class Contract(Base):
identifier = 'id'
def define(self):
self.type: str = ''
self.deliveries: list = []
self.accepted: bool = False
self.fulfilled: bool = False
self.expires: int = 0
self.expires_str: str = ''
self.pay: int = 0
@classmethod
def ext(cls):
return 'cnt'
def is_expired(self):
return time() > self.expires
def api_dict(self):
return {
'id': self.symbol.lower(),
'expiration': self.expires_str,
}
def is_done(self):
for d in self.deliveries:
if d['units_fulfilled'] > d['units_requires']:
return False
return False
def unfinished_delivery(self):
for d in self.deliveries:
if d['units_required'] > d['units_fulfilled']:
return d
return None
def update(self, d):
self.seta('expires',d, 'terms.deadline',parse_timestamp)
self.seta('expires_str', d,'terms.deadline')
self.seta('accepted', d, 'accepted')
self.seta('fulfilled', d, 'fulfilled')
self.seta('type', d, 'type')
self.pay = mg(d, 'terms.payment.onAccepted') + mg(d, 'terms.payment.onFulfilled')
deliveries = must_get(d, 'terms.deliver')
self.deliveries = []
for e in deliveries:
delivery = {}
delivery['trade_symbol'] = must_get(e, 'tradeSymbol')
delivery['units_fulfilled'] = must_get(e, 'unitsFulfilled')
delivery['units_required'] = must_get(e, 'unitsRequired')
delivery['destination'] = must_get(e, 'destinationSymbol')
self.deliveries.append(delivery)
def f(self, detail=1):
hours = int(max(0, self.expires - time()) / 3600)
accepted = 'A' if self.accepted else '-'
fulfilled = 'F' if self.fulfilled else '-'
result = f'{self.symbol} {hours}h {accepted}{fulfilled}'
if detail > 1:
result += '\n'
for d in self.deliveries:
result += f"({d['units_fulfilled']} / {d['units_required']}) {d['trade_symbol']} to {d['destination']}"
return result