Skip to content
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

[Part 2 of 2] feat: minor tweaks (add unchecked) #31

Merged
merged 11 commits into from
Jun 5, 2024
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134689
134601
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32311
32323
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143755
143645
Original file line number Diff line number Diff line change
@@ -1 +1 @@
289889
289801
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127816
127728
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116226
116154
Original file line number Diff line number Diff line change
@@ -1 +1 @@
971105
971033
Original file line number Diff line number Diff line change
@@ -1 +1 @@
329671
329599
Original file line number Diff line number Diff line change
@@ -1 +1 @@
339216
339144
Original file line number Diff line number Diff line change
@@ -1 +1 @@
141675
141603
Original file line number Diff line number Diff line change
@@ -1 +1 @@
174404
174313
Original file line number Diff line number Diff line change
@@ -1 +1 @@
180277
180186
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134199
134108
Original file line number Diff line number Diff line change
@@ -1 +1 @@
305997
305925
Original file line number Diff line number Diff line change
@@ -1 +1 @@
354338
354266
Original file line number Diff line number Diff line change
@@ -1 +1 @@
169775
169703
Original file line number Diff line number Diff line change
@@ -1 +1 @@
240472
240436
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
165801
165729
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
110199
110163
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116943
116888
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133733
133642
Original file line number Diff line number Diff line change
@@ -1 +1 @@
166497
166407
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153863
153772
Original file line number Diff line number Diff line change
@@ -1 +1 @@
145886
145795
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#Vault.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5663
5631
Original file line number Diff line number Diff line change
@@ -1 +1 @@
80269
82697
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119861
119741
Original file line number Diff line number Diff line change
@@ -1 +1 @@
43540
43449
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#lockSettledWhenSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
43536
43445
20 changes: 13 additions & 7 deletions src/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,18 @@ contract Vault is IVault, VaultToken, Ownable {

/// @inheritdoc IVault
function take(Currency currency, address to, uint256 amount) external override isLocked {
SettlementGuard.accountDelta(msg.sender, currency, -(amount.toInt128()));
currency.transfer(to, amount);
unchecked {
chefburger marked this conversation as resolved.
Show resolved Hide resolved
SettlementGuard.accountDelta(msg.sender, currency, -(amount.toInt128()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same reason as mint

currency.transfer(to, amount);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure whether there are any security issues .
Now we update the reservesOfVault after transfer token.
But after we merge the PR 33(reserveOf transient) , should be fine.

}
}

/// @inheritdoc IVault
function mint(address to, Currency currency, uint256 amount) external override isLocked {
SettlementGuard.accountDelta(msg.sender, currency, -(amount.toInt128()));
_mint(to, currency, amount);
unchecked {
SettlementGuard.accountDelta(msg.sender, currency, -(amount.toInt128()));
_mint(to, currency, amount);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. amount is positive
  2. if amount.toInt128 doesn't revert, then amount is a valid int128
  3. combine 1 & 2, converting from positive to negative will never go overflow

}

function sync(Currency currency) public returns (uint256 balance) {
Expand All @@ -135,9 +139,11 @@ contract Vault is IVault, VaultToken, Ownable {
function settleFor(Currency currency, address target, uint256 amount) external isLocked {
/// @notice settle all outstanding debt if amount is 0
/// It will revert if target has positive delta
if (amount == 0) amount = (-SettlementGuard.getCurrencyDelta(target, currency)).toUint256();
SettlementGuard.accountDelta(msg.sender, currency, -(amount.toInt128()));
SettlementGuard.accountDelta(target, currency, amount.toInt128());
unchecked {
if (amount == 0) amount = (-SettlementGuard.getCurrencyDelta(target, currency)).toUint256();
SettlementGuard.accountDelta(msg.sender, currency, -(amount.toInt128()));
SettlementGuard.accountDelta(target, currency, amount.toInt128());
}
}

Copy link
Collaborator

@chefburger chefburger Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. amount is positive
  2. SettlementGuard.getCurrencyDelta(target, currency) cant be over int128 otherwise it will revert in line 144

/// @inheritdoc IVault
Expand Down
Loading