Skip to content

Commit

Permalink
[Discuss]feat/refactor account delta for app (#171)
Browse files Browse the repository at this point in the history
* feat: Refactor accountAppBalanceDelta in vault

Remove PoolKey in accountAppBalanceDelta parameters.
Just pass currency0 and currency1 , will save some gas

* feat: Remove PoolKey in vault

* feat: Optimize logic

* feat: Optimize _accountDeltaForApp

Remove parameter app , will save some gas

* feat: Remove app from _accountDeltaForApp in MockVault

* fix: Update

* feat: Add comments

* fix: Remove unused import files
  • Loading branch information
ChefSnoopy authored Sep 12, 2024
1 parent 098f944 commit 905a96e
Show file tree
Hide file tree
Showing 34 changed files with 44 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178384
178372
Original file line number Diff line number Diff line change
@@ -1 +1 @@
188536
188524
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
311585
311573
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
189835
189823
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133896
133886
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142709
142697
Original file line number Diff line number Diff line change
@@ -1 +1 @@
290054
290044
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127070
127060
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
118808
118796
Original file line number Diff line number Diff line change
@@ -1 +1 @@
970989
970977
Original file line number Diff line number Diff line change
@@ -1 +1 @@
330300
330288
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337901
337889
Original file line number Diff line number Diff line change
@@ -1 +1 @@
140451
140439
Original file line number Diff line number Diff line change
@@ -1 +1 @@
173359
173347
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179388
179376
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133390
133378
Original file line number Diff line number Diff line change
@@ -1 +1 @@
304858
304846
Original file line number Diff line number Diff line change
@@ -1 +1 @@
347647
347635
Original file line number Diff line number Diff line change
@@ -1 +1 @@
163118
163106
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238402
238392
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
163373
163361
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
108318
108308
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114876
114866
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131112
131100
Original file line number Diff line number Diff line change
@@ -1 +1 @@
163690
163678
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149129
149117
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
71720
71712
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143365
143353
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
87992
87984
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
71723
71715
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7702
7651
13 changes: 7 additions & 6 deletions src/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ contract Vault is IVault, VaultToken, Ownable {
int128 delta1 = delta.amount1();

// keep track of the balance on app level
_accountDeltaForApp(msg.sender, currency0, delta0);
_accountDeltaForApp(msg.sender, currency1, delta1);
_accountDeltaForApp(currency0, delta0);
_accountDeltaForApp(currency1, delta1);

// keep track of the balance on vault level
SettlementGuard.accountDelta(settler, currency0, delta0);
Expand All @@ -98,7 +98,7 @@ contract Vault is IVault, VaultToken, Ownable {
isLocked
onlyRegisteredApp
{
_accountDeltaForApp(msg.sender, currency, delta);
_accountDeltaForApp(currency, delta);
SettlementGuard.accountDelta(settler, currency, delta);
}

Expand Down Expand Up @@ -166,15 +166,16 @@ contract Vault is IVault, VaultToken, Ownable {
return VaultReserve.getVaultReserve();
}

function _accountDeltaForApp(address app, Currency currency, int128 delta) internal {
function _accountDeltaForApp(Currency currency, int128 delta) internal {
if (delta == 0) return;

/// @dev optimization: msg.sender will always be app address, verification should be done on caller address
if (delta >= 0) {
/// @dev arithmetic underflow make sure trader can't withdraw too much from app
reservesOfApp[app][currency] -= uint128(delta);
reservesOfApp[msg.sender][currency] -= uint128(delta);
} else {
/// @dev arithmetic overflow make sure trader won't deposit too much into app
reservesOfApp[app][currency] += uint128(-delta);
reservesOfApp[msg.sender][currency] += uint128(-delta);
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/interfaces/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
pragma solidity ^0.8.0;

import {Currency} from "../types/Currency.sol";
import {PoolId} from "../types/PoolId.sol";
import {PoolKey} from "../types/PoolKey.sol";
import {BalanceDelta} from "../types/BalanceDelta.sol";
import {IVaultToken} from "./IVaultToken.sol";

Expand Down
13 changes: 6 additions & 7 deletions src/test/MockVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,24 @@ contract MockVault {
revert InvalidPoolKey();
}
}

_accountDeltaForApp(msg.sender, currency0, delta.amount0());
_accountDeltaForApp(msg.sender, currency1, delta.amount1());
_accountDeltaForApp(currency0, delta.amount0());
_accountDeltaForApp(currency1, delta.amount1());
}

function _accountDeltaForApp(address app, Currency currency, int128 delta) internal {
function _accountDeltaForApp(Currency currency, int128 delta) internal {
if (delta == 0) return;

if (delta >= 0) {
/// @dev arithmetic underflow make sure trader can't withdraw too much from app
reservesOfApp[app][currency] -= uint128(delta);
reservesOfApp[msg.sender][currency] -= uint128(delta);
} else {
/// @dev arithmetic overflow make sure trader won't deposit too much into app
reservesOfApp[app][currency] += uint128(-delta);
reservesOfApp[msg.sender][currency] += uint128(-delta);
}
}

function collectFee(Currency currency, uint256 amount, address recipient) external {
_accountDeltaForApp(msg.sender, currency, -amount.toInt128());
_accountDeltaForApp(currency, -amount.toInt128());
currency.transfer(recipient, amount);
}
}

0 comments on commit 905a96e

Please sign in to comment.