2023-06-16 11:03:19 +00:00
|
|
|
|
|
|
|
from time import time
|
|
|
|
from nullptr.util import *
|
|
|
|
from .base import Base
|
|
|
|
|
|
|
|
class Contract(Base):
|
|
|
|
identifier = 'id'
|
|
|
|
type: str
|
2023-06-16 12:41:11 +00:00
|
|
|
deliveries: list
|
2023-06-16 11:03:19 +00:00
|
|
|
accepted: bool
|
|
|
|
fulfilled: bool
|
|
|
|
expires: int
|
|
|
|
expires_str: str
|
|
|
|
pay: int
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def ext(cls):
|
|
|
|
return 'cnt'
|
|
|
|
|
|
|
|
def path(self):
|
|
|
|
return f'contracts/{self.symbol}.{self.ext()}'
|
|
|
|
|
|
|
|
def is_expired(self):
|
|
|
|
return time() > self.expires
|
|
|
|
|
|
|
|
def api_dict(self):
|
|
|
|
return {
|
2023-06-17 12:59:52 +00:00
|
|
|
'id': self.symbol.lower(),
|
2023-06-16 11:03:19 +00:00
|
|
|
'expiration': self.expires_str,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|