Skip to content

Commit

Permalink
refactor(StreamManager): some docs updates, added a new internal fn
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu committed Sep 10, 2024
1 parent f345962 commit ca14278
Showing 1 changed file with 56 additions and 7 deletions.
63 changes: 56 additions & 7 deletions contracts/StreamManager.vy
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ event StreamFunded:
amount_added: uint256


event StreamsMigrated:
token: indexed(IERC20)
creator: indexed(address)
stream_ids: DynArray[uint256, 20]
next_stream_id: uint256
amount_per_second: uint256
reason: Bytes[MAX_REASON_SIZE]


event StreamCancelled:
creator: indexed(address)
stream_id: indexed(uint256)
Expand Down Expand Up @@ -118,7 +127,7 @@ def create_stream(
start_time: uint256 = block.timestamp,
) -> uint256:
assert self.token_is_accepted[token] # dev: token not accepted
assert start_time <= block.timestamp # dev: start time < block
assert start_time <= block.timestamp # dev: start time in future

funded_amount: uint256 = staticcall token.allowance(msg.sender, self)
if funded_amount == max_value(uint256):
Expand All @@ -132,10 +141,10 @@ def create_stream(
extcall validator.validate(msg.sender, token, amount_per_second, reason),
)

assert max_stream_life >= funded_amount // amount_per_second # dev: max stream life small
assert max_stream_life >= funded_amount // amount_per_second # dev: max stream life too small

prefunded_stream_life: uint256 = max(MIN_STREAM_LIFE, block.timestamp - start_time)
assert max_stream_life >= prefunded_stream_life # dev: prefunded stream life large
assert max_stream_life >= prefunded_stream_life # dev: prefunded stream life too large
assert funded_amount >= prefunded_stream_life * amount_per_second # dev: not enough funds

assert extcall token.transferFrom( # dev: transfer fail
Expand Down Expand Up @@ -207,10 +216,51 @@ def add_funds(creator: address, stream_id: uint256, amount: uint256) -> uint256:
return time_left


@view
def _stream_is_cancelable(creator: address, stream_id: uint256) -> bool:
# Creator needs to wait `MIN_STREAM_LIFE` to cancel a stream
return self.streams[creator][stream_id].start_time + MIN_STREAM_LIFE <= block.timestamp


@view
@external
def stream_is_cancelable(creator: address, stream_id: uint256) -> bool:
return self.streams[creator][stream_id].start_time + MIN_STREAM_LIFE <= block.timestamp
return self._stream_is_cancelable(creator, stream_id)


@external
def migrate_streams(stream_ids: DynArray[uint256, 20]) -> uint256:
token: IERC20 = self.streams[msg.sender][stream_ids[0]].token
amount_per_second: uint256 = 0 # accumlated
amount_unlocked: uint256 = 0
max_stream_life: uint256 = 0

for stream_id: uint256 in stream_ids:
assert self._stream_is_cancelable(msg.sender, stream_id)
assert token == self.streams[msg.sender][stream_id].token
amount_per_second += self.streams[msg.sender][stream_id].amount_per_second
funded_amount: uint256 = self.streams[creator][stream_id].funded_amount
amount_locked: uint256 = funded_amount - self._amount_unlocked(creator, stream_id)
assert amount_locked > 0 # NOTE: reverts if stream doesn't exist, or already cancelled
self.streams[creator][stream_id].funded_amount = funded_amount - amount_locked
amount_unlocked += funded_amount - amount_locked
max_stream_life = min(max_stream_life, self.streams[msg.sender][stream_id].max_stream_life)

next_stream_id: uint256 = self.num_streams[msg.sender]
self.streams[msg.sender][next_stream_id] = Stream({
token: token,
amount_per_second: amount_per_second,
max_stream_life: max_stream_life,
funded_amount: amount_unlocked,
start_time: block.timestamp,
last_pull: block.timestamp,
reason: reason,
})
self.num_streams[msg.sender] = next_stream_id + 1

log StreamsMigrated(token, msg.sender, stream_ids, next_stream_id, amount_per_second, reason)

return next_stream_id


@external
Expand All @@ -220,14 +270,13 @@ def cancel_stream(
creator: address = msg.sender,
) -> uint256:
if msg.sender == creator:
# Creator needs to wait `MIN_STREAM_LIFE` to cancel a stream
assert self.streams[creator][stream_id].start_time + MIN_STREAM_LIFE <= block.timestamp
assert self._stream_is_cancelable(creator, stream_id)
else:
# Owner can cancel at any time
assert msg.sender == self.owner

funded_amount: uint256 = self.streams[creator][stream_id].funded_amount
amount_locked: uint256 = funded_amount - self._amount_unlocked(creator, stream_id)
amount_unlocked: uint256 = funded_amount - self._amount_unlocked(creator, stream_id)
assert amount_locked > 0 # NOTE: reverts if stream doesn't exist, or already cancelled
self.streams[creator][stream_id].funded_amount = funded_amount - amount_locked

Expand Down

0 comments on commit ca14278

Please sign in to comment.