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

Update vault.accountAppBalanceDelta logic flow from app #203

Merged
merged 7 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178172
178124
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
311226
311244
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
189430
189464
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24175
24467
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133811
133795
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142616
142596
Original file line number Diff line number Diff line change
@@ -1 +1 @@
289602
289586
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126984
126968
Original file line number Diff line number Diff line change
@@ -1 +1 @@
968244
968262
Original file line number Diff line number Diff line change
@@ -1 +1 @@
327556
327574
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337280
337298
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139831
139849
Original file line number Diff line number Diff line change
@@ -1 +1 @@
172918
172952
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178946
178980
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132949
132983
Original file line number Diff line number Diff line change
@@ -1 +1 @@
304363
304381
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21103
22048
Original file line number Diff line number Diff line change
@@ -1 +1 @@
347359
347387
Original file line number Diff line number Diff line change
@@ -1 +1 @@
162820
162848
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238365
238393
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
163100
163103
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
108172
108175
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149202
149205
Original file line number Diff line number Diff line change
@@ -1 +1 @@
112713
112740
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130739
130768
Original file line number Diff line number Diff line change
@@ -1 +1 @@
163100
163129
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148519
148548
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
71220
71249
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143103
143132
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
87408
87442
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
71223
71252
Original file line number Diff line number Diff line change
@@ -1 +1 @@
31953
31956
2 changes: 1 addition & 1 deletion .forge-snapshots/ExtsloadTest#extsload.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7315
7318
2 changes: 1 addition & 1 deletion .forge-snapshots/ExtsloadTest#extsloadInBatch.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10977
10980
47 changes: 47 additions & 0 deletions src/libraries/VaultAppDeltaSettlement.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2024 PancakeSwap
pragma solidity ^0.8.0;

import {BalanceDelta, BalanceDeltaLibrary} from "../types/BalanceDelta.sol";
import {PoolKey} from "../types/PoolKey.sol";
import {IVault} from "../interfaces/IVault.sol";

/// @notice Library for handling AppDeltaSettlement for the apps (eg. CL, Bin etc..)
library VaultAppDeltaSettlement {
/// @notice helper method to call `vault.accountAppBalanceDelta`
/// @dev Vault maintains a `reserveOfApp` to protect against exploits in one app from accessing funds in another.
/// To prevent underflow in `reserveOfApp`, it is essential to handle `appDelta` and `hookDelta` in a specific order.
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we still need this comments ,"it is essential to handle ...."?
because now no order logic.

_accountDeltaForApp(currency0, delta0 + hookDelta0);
_accountDeltaForApp(currency1, delta1 + hookDelta1);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thanks, cleaned up

function accountAppDeltaWithHookDelta(
IVault vault,
PoolKey memory key,
BalanceDelta delta,
BalanceDelta hookDelta,
address settler
) internal {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Still thinking if we add a new function accountAppBalanceDelta in vault, pass all delta once , will reduce call times , maybe will save some gas

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeh, will check on this after all tests added

if (hookDelta == BalanceDeltaLibrary.ZERO_DELTA) {
/// @dev default case when no hook return delta is set
vault.accountAppBalanceDelta(key.currency0, key.currency1, delta, settler);
} else {
/// @dev if hookDelta is not 0, call vault.accountAppBalanceDelta with negative delta first
/// negative delta means user/hook owes vault money, so reservesOfApp in vault will not underflow
(int128 hookDelta0, int128 hookDelta1) = (hookDelta.amount0(), hookDelta.amount1());
(int128 delta0, int128 delta1) = (delta.amount0(), delta.amount1());

if (hookDelta0 < 0) {
vault.accountAppBalanceDelta(key.currency0, hookDelta0, address(key.hooks));
if (delta0 != 0) vault.accountAppBalanceDelta(key.currency0, delta0, settler);
} else {
if (delta0 != 0) vault.accountAppBalanceDelta(key.currency0, delta0, settler);
if (hookDelta0 != 0) vault.accountAppBalanceDelta(key.currency0, hookDelta0, address(key.hooks));
}

if (hookDelta1 < 0) {
vault.accountAppBalanceDelta(key.currency1, hookDelta1, address(key.hooks));
if (delta1 != 0) vault.accountAppBalanceDelta(key.currency1, delta1, settler);
} else {
if (delta1 != 0) vault.accountAppBalanceDelta(key.currency1, delta1, settler);
if (hookDelta1 != 0) vault.accountAppBalanceDelta(key.currency1, hookDelta1, address(key.hooks));
}
}
}
}
18 changes: 5 additions & 13 deletions src/pool-bin/BinPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {PriceHelper} from "./libraries/PriceHelper.sol";
import {BeforeSwapDelta} from "../types/BeforeSwapDelta.sol";
import "./interfaces/IBinHooks.sol";
import {BinSlot0} from "./types/BinSlot0.sol";
import {VaultAppDeltaSettlement} from "../libraries/VaultAppDeltaSettlement.sol";

/// @notice Holds the state for all bin pools
contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
Expand All @@ -32,6 +33,7 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
using LPFeeLibrary for uint24;
using PackedUint128Math for bytes32;
using Hooks for bytes32;
using VaultAppDeltaSettlement for IVault;

/// @inheritdoc IBinPoolManager
uint16 public constant override MIN_BIN_STEP = 1;
Expand Down Expand Up @@ -181,11 +183,7 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
BalanceDelta hookDelta;
(delta, hookDelta) = BinHooks.afterSwap(key, swapForY, amountSpecified, delta, hookData, beforeSwapDelta);

if (hookDelta != BalanceDeltaLibrary.ZERO_DELTA) {
vault.accountAppBalanceDelta(key.currency0, key.currency1, hookDelta, address(key.hooks));
}

vault.accountAppBalanceDelta(key.currency0, key.currency1, delta, msg.sender);
vault.accountAppDeltaWithHookDelta(key, delta, hookDelta, msg.sender);
}

/// @inheritdoc IBinPoolManager
Expand Down Expand Up @@ -229,10 +227,7 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
BalanceDelta hookDelta;
(delta, hookDelta) = BinHooks.afterMint(key, params, delta, hookData);

if (hookDelta != BalanceDeltaLibrary.ZERO_DELTA) {
vault.accountAppBalanceDelta(key.currency0, key.currency1, hookDelta, address(key.hooks));
}
vault.accountAppBalanceDelta(key.currency0, key.currency1, delta, msg.sender);
vault.accountAppDeltaWithHookDelta(key, delta, hookDelta, msg.sender);
}

/// @inheritdoc IBinPoolManager
Expand Down Expand Up @@ -264,10 +259,7 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
BalanceDelta hookDelta;
(delta, hookDelta) = BinHooks.afterBurn(key, params, delta, hookData);

if (hookDelta != BalanceDeltaLibrary.ZERO_DELTA) {
vault.accountAppBalanceDelta(key.currency0, key.currency1, hookDelta, address(key.hooks));
}
vault.accountAppBalanceDelta(key.currency0, key.currency1, delta, msg.sender);
vault.accountAppDeltaWithHookDelta(key, delta, hookDelta, msg.sender);
ChefMist marked this conversation as resolved.
Show resolved Hide resolved
}

function donate(PoolKey memory key, uint128 amount0, uint128 amount1, bytes calldata hookData)
Expand Down
17 changes: 6 additions & 11 deletions src/pool-cl/CLPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {BeforeSwapDelta} from "../types/BeforeSwapDelta.sol";
import {Currency} from "../types/Currency.sol";
import {TickMath} from "./libraries/TickMath.sol";
import {CLSlot0} from "./types/CLSlot0.sol";
import {VaultAppDeltaSettlement} from "../libraries/VaultAppDeltaSettlement.sol";

contract CLPoolManager is ICLPoolManager, ProtocolFees, Extsload {
using SafeCast for int256;
Expand All @@ -34,6 +35,7 @@ contract CLPoolManager is ICLPoolManager, ProtocolFees, Extsload {
using CLPool for *;
using CLPosition for mapping(bytes32 => CLPosition.Info);
using CLPoolGetters for CLPool.State;
using VaultAppDeltaSettlement for IVault;

mapping(PoolId id => CLPool.State poolState) private pools;

Expand Down Expand Up @@ -154,10 +156,7 @@ contract CLPoolManager is ICLPoolManager, ProtocolFees, Extsload {
// notice that both generated delta and feeDelta (from lpFee) will both be counted on the user
(delta, hookDelta) = CLHooks.afterModifyLiquidity(key, params, delta + feeDelta, feeDelta, hookData);

if (hookDelta != BalanceDeltaLibrary.ZERO_DELTA) {
vault.accountAppBalanceDelta(key.currency0, key.currency1, hookDelta, address(key.hooks));
}
vault.accountAppBalanceDelta(key.currency0, key.currency1, delta, msg.sender);
vault.accountAppDeltaWithHookDelta(key, delta, hookDelta, msg.sender);
}

/// @inheritdoc ICLPoolManager
Expand Down Expand Up @@ -206,15 +205,11 @@ contract CLPoolManager is ICLPoolManager, ProtocolFees, Extsload {
);

BalanceDelta hookDelta;
(delta, hookDelta) = CLHooks.afterSwap(key, params, delta, hookData, beforeSwapDelta);

if (hookDelta != BalanceDeltaLibrary.ZERO_DELTA) {
vault.accountAppBalanceDelta(key.currency0, key.currency1, hookDelta, address(key.hooks));
}

/// @dev delta already includes protocol fee
/// all tokens go into the vault
vault.accountAppBalanceDelta(key.currency0, key.currency1, delta, msg.sender);
(delta, hookDelta) = CLHooks.afterSwap(key, params, delta, hookData, beforeSwapDelta);

vault.accountAppDeltaWithHookDelta(key, delta, hookDelta, msg.sender);
}

/// @inheritdoc ICLPoolManager
Expand Down
Loading