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

feat: gas optimization #28

Merged
merged 12 commits into from
May 7, 2024
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143903
143154
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135247
134501
Original file line number Diff line number Diff line change
@@ -1 +1 @@
109118
108737
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
301295
300549
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139320
138567
Original file line number Diff line number Diff line change
@@ -1 +1 @@
90617
89917
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2242
1866
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5062
4679
Original file line number Diff line number Diff line change
@@ -1 +1 @@
65810
65499
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149365
149045
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
66989
66678
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
52582
52325
Original file line number Diff line number Diff line change
@@ -1 +1 @@
968582
968335
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120096
119849
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337772
337527
Original file line number Diff line number Diff line change
@@ -1 +1 @@
54833
54588
Original file line number Diff line number Diff line change
@@ -1 +1 @@
89370
89025
Original file line number Diff line number Diff line change
@@ -1 +1 @@
91355
91010
Original file line number Diff line number Diff line change
@@ -1 +1 @@
70859
70574
Original file line number Diff line number Diff line change
@@ -1 +1 @@
319479
296760
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testNoOpGas_Burn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
41726
41345
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19494
19114
Original file line number Diff line number Diff line change
@@ -1 +1 @@
37255
36874
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testNoOpGas_Mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
39359
38978
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testNoOpGas_Swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22739
22359
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8148
7461
Original file line number Diff line number Diff line change
@@ -1 +1 @@
349902
349410
Original file line number Diff line number Diff line change
@@ -1 +1 @@
60450
59958
Original file line number Diff line number Diff line change
@@ -1 +1 @@
243492
220457
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
82627
82382
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
52589
52276
Original file line number Diff line number Diff line change
@@ -1 +1 @@
36705
36324
Original file line number Diff line number Diff line change
@@ -1 +1 @@
43384
42829
Original file line number Diff line number Diff line change
@@ -1 +1 @@
54761
54399
Original file line number Diff line number Diff line change
@@ -1 +1 @@
100840
100389
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25040547
24843277
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
35961
35478
Original file line number Diff line number Diff line change
@@ -1 +1 @@
101598
101168
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
41680
41197
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
35964
35481
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4857
4453
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19394
19014
Original file line number Diff line number Diff line change
@@ -1 +1 @@
37663
37282
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30109
29728
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#testNoOp_gas_Swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21824
21443
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPositionTest#Position_update_add.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1545
1427
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2014
1899
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#Vault.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7314
7437
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#collectFee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25576
25611
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120730
120866
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
156938
157144
Original file line number Diff line number Diff line change
@@ -1 +1 @@
45186
45289
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#lockSettledWhenSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
45185
45288
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#testLock_NoOp.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11327
11517
Original file line number Diff line number Diff line change
@@ -1 +1 @@
71681
71811
Original file line number Diff line number Diff line change
@@ -1 +1 @@
33288
33418
30 changes: 19 additions & 11 deletions src/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ contract Vault is IVault, VaultToken, Ownable {
_;
}

/// @notice receive native tokens for native pools
receive() external payable {}

/// @inheritdoc IVault
function registerPoolManager(address poolManager) external override onlyOwner {
isPoolManagerRegistered[poolManager] = true;
Expand Down Expand Up @@ -107,7 +104,7 @@ 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());
reservesOfVault[currency] -= amount;
if (!currency.isNative()) reservesOfVault[currency] -= amount;
currency.transfer(to, amount);
}

Expand All @@ -119,9 +116,15 @@ contract Vault is IVault, VaultToken, Ownable {

/// @inheritdoc IVault
function settle(Currency currency) external payable override isLocked returns (uint256 paid) {
uint256 reservesBefore = reservesOfVault[currency];
reservesOfVault[currency] = currency.balanceOfSelf();
paid = reservesOfVault[currency] - reservesBefore;
if (!currency.isNative()) {
if (msg.value > 0) revert SettleNonNativeCurrencyWithValue();
uint256 reservesBefore = reservesOfVault[currency];
reservesOfVault[currency] = currency.balanceOfSelf();
paid = reservesOfVault[currency] - reservesBefore;
} else {
paid = msg.value;
}

// subtraction must be safe
SettlementGuard.accountDelta(msg.sender, currency, -(paid.toInt128()));
}
Expand All @@ -133,9 +136,14 @@ contract Vault is IVault, VaultToken, Ownable {
isLocked
returns (uint256 paid, uint256 refund)
{
uint256 reservesBefore = reservesOfVault[currency];
reservesOfVault[currency] = currency.balanceOfSelf();
paid = reservesOfVault[currency] - reservesBefore;
if (!currency.isNative()) {
if (msg.value > 0) revert SettleNonNativeCurrencyWithValue();
uint256 reservesBefore = reservesOfVault[currency];
reservesOfVault[currency] = currency.balanceOfSelf();
paid = reservesOfVault[currency] - reservesBefore;
} else {
paid = msg.value;
}

int256 currentDelta = SettlementGuard.getCurrencyDelta(msg.sender, currency);
if (currentDelta >= 0) {
Expand Down Expand Up @@ -169,7 +177,7 @@ contract Vault is IVault, VaultToken, Ownable {
/// @inheritdoc IVault
function collectFee(Currency currency, uint256 amount, address recipient) external {
reservesOfPoolManager[IPoolManager(msg.sender)][currency] -= amount;
reservesOfVault[currency] -= amount;
if (!currency.isNative()) reservesOfVault[currency] -= amount;

currency.transfer(recipient, amount);
}
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ interface IVault is IVaultToken {
/// @param locker The address of the current locker
error LockerAlreadySet(address locker);

/// @notice Thrown when passing in msg.value for non-native currency
error SettleNonNativeCurrencyWithValue();

/// @notice Thrown when there is no locker
error NoLocker();

Expand Down
Loading
Loading