Skip to content

Commit

Permalink
feat: Optimize logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefSnoopy committed Sep 10, 2024
1 parent 146b881 commit 154b8e5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178372
178384
Original file line number Diff line number Diff line change
@@ -1 +1 @@
188524
188536
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
311573
311585
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
189823
189835
27 changes: 15 additions & 12 deletions src/test/MockVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,33 @@ contract MockVault {
function accountAppBalanceDelta(Currency currency0, Currency currency1, BalanceDelta delta, address) external {
// Will not record balanceDeltaOfPool if currentPoolKey is not set
if (!currentPoolKey.currency0.isNative() || !currentPoolKey.currency1.isNative()) {
if (
!currencyEquals(currentPoolKey.currency0, currency0)
|| !currencyEquals(currentPoolKey.currency1, currency1)
) revert InvalidPoolKey();
PoolId poolId = currentPoolKey.toId();
balanceDeltaOfPool[poolId] = delta;
// Check if currency0/currency1 is the same as the currency in currentPoolKey
if (currentPoolKey.currency0 == currency0 && currentPoolKey.currency1 == currency1) {
PoolId poolId = currentPoolKey.toId();
balanceDeltaOfPool[poolId] = delta;
} else {
revert InvalidPoolKey();
}
}

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

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

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

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

0 comments on commit 154b8e5

Please sign in to comment.