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 all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
142390
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178172
178130
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
311226
311254
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
189430
189473
1 change: 1 addition & 0 deletions .forge-snapshots/BinMintBurnFeeHookTest#test_Burn.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
170035
1 change: 1 addition & 0 deletions .forge-snapshots/BinMintBurnFeeHookTest#test_Mint.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
410226
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24175
23296
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133811
133857
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142616
142673
Original file line number Diff line number Diff line change
@@ -1 +1 @@
289602
289648
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126984
127029
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
118513
118628
Original file line number Diff line number Diff line change
@@ -1 +1 @@
968244
968387
Original file line number Diff line number Diff line change
@@ -1 +1 @@
327556
327699
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337280
337423
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139831
139974
Original file line number Diff line number Diff line change
@@ -1 +1 @@
172918
173054
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178946
179082
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132949
133085
Original file line number Diff line number Diff line change
@@ -1 +1 @@
304363
304484
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
149600
1 change: 1 addition & 0 deletions .forge-snapshots/CLMintBurnFeeHookTest#test_Burn.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
170881
1 change: 1 addition & 0 deletions .forge-snapshots/CLMintBurnFeeHookTest#test_Mint.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
421255
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21103
20745
Original file line number Diff line number Diff line change
@@ -1 +1 @@
347359
347480
Original file line number Diff line number Diff line change
@@ -1 +1 @@
162820
162941
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238365
238442
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
163100
163218
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
108172
108268
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
112784
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130739
130830
Original file line number Diff line number Diff line change
@@ -1 +1 @@
163100
163213
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148519
148610
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
71289
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143103
143194
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
87479
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
71292
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
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7792
8131
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2854
2876
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1858
1880
24 changes: 24 additions & 0 deletions src/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,30 @@ contract Vault is IVault, VaultToken, Ownable {
SettlementGuard.setLocker(address(0));
}

/// @inheritdoc IVault
function accountAppBalanceDelta(
Currency currency0,
Currency currency1,
BalanceDelta delta,
address settler,
BalanceDelta hookDelta,
address hook
) external override isLocked onlyRegisteredApp {
(int128 delta0, int128 delta1) = (delta.amount0(), delta.amount1());
(int128 hookDelta0, int128 hookDelta1) = (hookDelta.amount0(), hookDelta.amount1());

/// @dev call _accountDeltaForApp once with both delta/hookDelta to save gas and prevent
/// reservesOfApp from underflow when it deduct before addition
_accountDeltaForApp(currency0, delta0 + hookDelta0);
_accountDeltaForApp(currency1, delta1 + hookDelta1);

// keep track of the balance on vault level
SettlementGuard.accountDelta(settler, currency0, delta0);
SettlementGuard.accountDelta(settler, currency1, delta1);
SettlementGuard.accountDelta(hook, currency0, hookDelta0);
SettlementGuard.accountDelta(hook, currency1, hookDelta1);
}

/// @inheritdoc IVault
function accountAppBalanceDelta(Currency currency0, Currency currency1, BalanceDelta delta, address settler)
external
Expand Down
18 changes: 18 additions & 0 deletions src/interfaces/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ interface IVault is IVaultToken {
/// @return The data returned by the call to `ILockCallback(msg.sender).lockCallback(data)`
function lock(bytes calldata data) external returns (bytes memory);

/// @notice Called by registered app to account for a change in the pool balance,
/// convenient for AMM pool manager, typically after modifyLiquidity, swap, donate,
/// include the case where hookDelta is involved
/// @param currency0 The PoolKey currency0 to update
/// @param currency1 The PoolKey currency1 to update
/// @param delta The change in the pool's balance
/// @param settler The address whose delta will be updated
/// @param hookDelta The change in the pool's balance from hook
/// @param hook The address whose hookDelta will be updated
function accountAppBalanceDelta(
Currency currency0,
Currency currency1,
BalanceDelta delta,
address settler,
BalanceDelta hookDelta,
address hook
) external;

/// @notice Called by registered app to account for a change in the pool balance,
/// convenient for AMM pool manager, typically after modifyLiquidity, swap, donate
/// @param currency0 The PoolKey currency0 to update
Expand Down
22 changes: 22 additions & 0 deletions src/libraries/VaultAppDeltaSettlement.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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`
function accountAppDeltaWithHookDelta(IVault vault, PoolKey memory key, BalanceDelta delta, BalanceDelta hookDelta)
internal
{
if (hookDelta == BalanceDeltaLibrary.ZERO_DELTA) {
/// @dev default case when no hook return delta is set
vault.accountAppBalanceDelta(key.currency0, key.currency1, delta, msg.sender);
} else {
vault.accountAppBalanceDelta(key.currency0, key.currency1, delta, msg.sender, hookDelta, 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);
}

/// @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);
}

/// @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);
}

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);
}

/// @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);
}

/// @inheritdoc ICLPoolManager
Expand Down
Loading
Loading