Skip to content
Snippets Groups Projects
exchange.py 2.17 KiB
Newer Older
  • Learn to ignore specific revisions
  • from pint import UnitRegistry
    
    import time
    
    from datetime import datetime, timedelta
    
    
    
    class Exchange:
    
        def __init__(self, pk: bytes, p: float, ppw: float, extime: datetime):
            self.ureg = None
            self.Q_ = None
    
    Johannes Hitzinger's avatar
    Johannes Hitzinger committed
            self.pubicKey: bytes = pk
            self.timestamp: int = time.time_ns()
    
            self.executiontime: datetime = extime
    
    Johannes Hitzinger's avatar
    Johannes Hitzinger committed
            self.__isActive: bool = False
    
            self.power = p
            self.pricePerWatt = ppw  # this is in EUR, no new cryptocurrency should be created for this
    
    Johannes Hitzinger's avatar
    Johannes Hitzinger committed
            self.__sig: bytes
    
    
        def set_ureg(self, ureg: UnitRegistry) -> None:
            self.ureg = ureg
            self.Q_ = ureg.Quantity
            self.power = self.Q_(self.power, ureg.watt)
    
    
    Johannes Hitzinger's avatar
    Johannes Hitzinger committed
        def activate(self) -> None:
            self.__isActive = True
    
        def getActive(self) -> bool:
            return self.__isActive
    
        def is_min_now(self) -> bool:
            now = datetime.now()
    
            # Calculate the first second of the current minute
            start_of_minute = datetime(now.year, now.month, now.day, now.hour, now.minute, 0)
            start_of_minute_unix = int(start_of_minute.timestamp())
    
            # Calculate the last second of the current minute
            end_of_minute = start_of_minute + timedelta(seconds=59)
            end_of_minute_unix = int(end_of_minute.timestamp())
    
    
            if start_of_minute_unix <= self.executiontime.timestamp() <= end_of_minute_unix:
    
    Johannes Hitzinger's avatar
    Johannes Hitzinger committed
                return True
            else:
                return False
    
        def is_min_next(self) -> bool:
            now = datetime.now()
    
            # Calculate the first second of the next minute
            start_of_minute = datetime(now.year, now.month, now.day, now.hour, now.minute, 0)
            start_of_minute += timedelta(minutes=1)
            start_of_minute_unix = int(start_of_minute.timestamp())
    
            # Calculate the last second of the next minute
            end_of_minute = start_of_minute + timedelta(seconds=59)
            end_of_minute_unix = int(end_of_minute.timestamp())
    
    
            if start_of_minute_unix <= self.executiontime.timestamp() <= end_of_minute_unix:
    
    Johannes Hitzinger's avatar
    Johannes Hitzinger committed
                return True
            else:
    
                return False
    
        def is_old(self) -> bool:
            now = datetime.now()
    
            return self.executiontime.timestamp() < int(now.timestamp())