-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add settleAndRefund
#2
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
095503b
feat: Add settleAndRefund
ChefMist 7cf771c
feat: Add to in parameter for settleAndRefund
ChefMist c9123ff
feat: update to Snoopy implementation
ChefMist 919b164
feat: add negative balance delta test
ChefMist 236a1d3
bug: move transfer only at the end to prevent reentrancy
ChefMist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
6762 | ||
7018 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
122540 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
24484 | ||
24506 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
11502 | ||
11597 |
1 change: 1 addition & 0 deletions
1
.forge-snapshots/VaultTest#testSettleAndRefund_WithErc20Transfer.snap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
79085 |
1 change: 1 addition & 0 deletions
1
.forge-snapshots/VaultTest#testSettleAndRefund_WithoutErc20Transfer.snap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
36485 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,14 +118,35 @@ contract Vault is IVault, VaultToken, Ownable { | |
} | ||
|
||
/// @inheritdoc IVault | ||
function settle(Currency currency) external payable override isLocked returns (uint256 paid) { | ||
function settle(Currency currency) public payable override isLocked returns (uint256 paid) { | ||
uint256 reservesBefore = reservesOfVault[currency]; | ||
reservesOfVault[currency] = currency.balanceOfSelf(); | ||
paid = reservesOfVault[currency] - reservesBefore; | ||
// subtraction must be safe | ||
SettlementGuard.accountDelta(msg.sender, currency, -(paid.toInt128())); | ||
} | ||
|
||
function settleAndRefund(Currency currency, address to) | ||
external | ||
payable | ||
isLocked | ||
returns (uint256 paid, uint256 refund) | ||
{ | ||
paid = settle(currency); | ||
|
||
int256 afterCurrencyDelta = SettlementGuard.getCurrencyDelta(msg.sender, currency); | ||
if (afterCurrencyDelta < 0) { | ||
/// If user currencyDelta is negative (vault owes owner) after settle: either user have overpaid | ||
/// or someone have transfer token to the vault. | ||
refund = uint256(-afterCurrencyDelta); | ||
|
||
/// thus refund msg.sender and update accountDelta | ||
SettlementGuard.accountDelta(msg.sender, currency, refund.toInt128()); | ||
reservesOfVault[currency] -= refund; | ||
currency.transfer(to, refund); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to think about whether there are re-entrant issues. |
||
} | ||
} | ||
|
||
/// @inheritdoc IVault | ||
function settleFor(Currency currency, address target, uint256 amount) external isLocked { | ||
/// @notice settle all outstanding debt if amount is 0 | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we calculate the amount we need to refund in advance? then just need to execute SettlementGuard.accountDelta once if have excess token.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks! adopted this idea since it'll save 1
sload
for refund case. though did a small tweak to handle negative currency delta assettle()
wont revert withSafeCastOverflow
for negative balance delta