sablier-labs / flow

🍃 Smart contracts of the Sablier Flow protocol.
Other
8 stars 0 forks source link

Re introduce the `amount` param in `withdraw` #224

Closed andreivladbrg closed 1 month ago

andreivladbrg commented 1 month ago

Discussed in https://github.com/sablier-labs/flow/discussions/223

Originally posted by **andreivladbrg** September 3, 2024 After a private discussion with @gavriliumircea, he came up with a good idea to reintroduce the `amount` parameter in `withdraw`, which was initially removed due to the explanation provided [here](https://github.com/sablier-labs/flow/pull/4). In the meantime, we have introduced the storage variable `snapshotDebt`, which changes how things work. So, the proposed change in `withdraw` would be: - `time` --> `amount` - calculate `coveredDebt` to the current block (`block.timestamp`) - check wether the amount passed is greater than `coveredDebt` - the snapshot debt will be updated to `coveredDebt - withdrawAmount` - the snapshot time will be updated to `block.timestamp`
Actual implementation

```solidity // Check: the stream balance is not zero. if (balance == 0) { revert Errors.SablierFlow_WithdrawNoFundsAvailable(streamId); } uint128 newSnapshotDebt = _coveredDebtOf(streamId, uint40(block.timestamp)); // Revert if the withdrawal amount is greater than what is available. if (amount > newSnapshotDebt) { revert Errors.SablierFlow_WithdrawAmountTooBig(); } // Update the snapshot debt, with the amount to be withdrawn. unchecked { newSnapshotDebt -= amount; } // Effect: update the snapshot debt and time from storage. _streams[streamId].snapshotDebt = newSnapshotDebt; _streams[streamId].snapshotTime = uint40(block.timestamp); ```

--- This change is going to be helpful for the app, as well as for the users, due to a more concise API (and consistent with `lockup` contracts). One more thing: this change would allow us to enable the withdrawal of an amount less than what has been snapshot in the case of `adjustRps`, which was not possible with a `time` parameter.