fix(contracts): preserve accrued vest time on stream top-up (closes #786)#961
Open
samjay8 wants to merge 1 commit into
Open
fix(contracts): preserve accrued vest time on stream top-up (closes #786)#961samjay8 wants to merge 1 commit into
samjay8 wants to merge 1 commit into
Conversation
top_up_stream reset stream.last_update_time to the current ledger timestamp on every call. Since calculate_claimable uses last_update_time as the accrual anchor, this silently zeroed out any vested-but-unwithdrawn balance: a sender could top up a stream right before cancelling it to reclaim tokens the recipient had already earned. Drop the reassignment so prior accrued time carries through top-ups, including while the stream is paused (last_update_time must never be pushed past paused_at). Add regression coverage for the claimable amount surviving a top-up, a cancel-immediately-after-top-up payout, and the paused-stream case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
(closes #786)
Summary
top_up_streamwas unconditionally resettingstream.last_update_timeto thecurrent ledger timestamp on every call. Since
calculate_claimableuseslast_update_timeas the accrual anchor (elapsed = effective_now - last_update_time), this silently discarded any vested-but-unwithdrawnbalance the moment a top-up happened.
Concrete impact: for a 1000-token / 1000s stream (rate 1/s), if the recipient
had 900 unwithdrawn tokens vested at
t=900and the sender calledtop_up_stream(+100),last_update_timejumped to900, zeroingget_claimable_amount. A subsequentcancel_streamatt=901would then paythe recipient only
1token and refund1099to the sender — letting thesender reclaim ~899 tokens the recipient had already earned.
Fix
stream.last_update_time = env.ledger().timestamp()assignmentin
top_up_stream(contracts/stream_contract/src/lib.rs) so prior accruedtime is preserved across top-ups, including while a stream is paused
(
last_update_timeis never pushed pastpaused_at).Tests added
test_top_up_preserves_already_accrued_claimable— aftercreate_stream(1000, 1000s), advancing 900s and topping up by 100 keepsget_claimable_amountat 900 instead of dropping to 0.test_top_up_then_cancel_pays_pre_topup_accrued— topping up andimmediately cancelling pays the recipient exactly the pre-top-up accrued
amount.
test_top_up_while_paused_does_not_advance_last_update_time— topping up apaused stream does not push
last_update_timepastpaused_at, andclaimable amount still reflects only the time accrued before the pause.
Scope
wiring were left untouched.
contracts/stream_contract/src/lib.rs,contracts/stream_contract/src/test.rs.