60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
|
|
from time import time
|
|
from nullptr.util import *
|
|
from .base import Base
|
|
from typing import List
|
|
|
|
class Contract(Base):
|
|
identifier = 'id'
|
|
type: str
|
|
deliveries: List
|
|
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 {
|
|
'id': self.symbol,
|
|
'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
|