Skip to content

Commit

Permalink
fix: time_left max C int issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeshultz committed Oct 27, 2023
1 parent b336b00 commit b3fc3df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
20 changes: 17 additions & 3 deletions sdk/py/apepay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
TokenNotAccepted,
ValidatorFailed,
)
from .settings import Settings
from .utils import time_unit_to_timedelta

settings = Settings()


class Validator(BaseInterfaceModel):
contract: ContractInstance
Expand Down Expand Up @@ -427,17 +430,28 @@ def amount_left(self) -> int:

@property
def time_left(self) -> timedelta:
return timedelta(seconds=self.contract.time_left(self.creator, self.stream_id))
seconds = self.contract.time_left(self.creator, self.stream_id)
return timedelta(
seconds=settings.MAX_STREAM_DURATION
if seconds > settings.MAX_STREAM_DURATION
else seconds
)

@property
def total_time(self) -> timedelta:
info = self.info # NOTE: Avoid calling contract twice
# NOTE: Measure time-duration of unclaimed amount remaining (locked and unlocked)
max_life = int(info.funded_amount / info.amount_per_second)

return (
# NOTE: `last_pull == start_time` if never pulled
datetime.fromtimestamp(info.last_pull)
- datetime.fromtimestamp(info.start_time)
# NOTE: Measure time-duration of unclaimed amount remaining (locked and unlocked)
+ timedelta(seconds=int(info.funded_amount / info.amount_per_second))
+ timedelta(
seconds=settings.MAX_STREAM_DURATION
if max_life > settings.MAX_STREAM_DURATION
else max_life
)
)

@property
Expand Down
1 change: 1 addition & 0 deletions sdk/py/apepay/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Settings(BaseSettings):
WARNING_LEVEL: timedelta = timedelta(days=2)
CRITICAL_LEVEL: timedelta = timedelta(hours=12)
MAX_STREAM_DURATION: int = int(timedelta.max.total_seconds()) - 1

@validator("WARNING_LEVEL", "CRITICAL_LEVEL", pre=True)
def _normalize_timedelta(cls, value: Any) -> timedelta:
Expand Down

0 comments on commit b3fc3df

Please sign in to comment.