Skip to content

Commit

Permalink
[Option 2] Update vault.accountAppBalanceDelta logic flow from app (#205
Browse files Browse the repository at this point in the history
)

* feat: add additional method on vault to account app balance delta

* feat: remove redundant non zero checkl

* feat: further optimise the vault logic

* feat: add more test on vault
  • Loading branch information
ChefMist authored Nov 7, 2024
1 parent 1676856 commit f1ecd94
Show file tree
Hide file tree
Showing 47 changed files with 336 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
156820
142390
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178118
178130
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
311242
311254
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
189461
189473
2 changes: 1 addition & 1 deletion .forge-snapshots/BinMintBurnFeeHookTest#test_Burn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175092
170035
2 changes: 1 addition & 1 deletion .forge-snapshots/BinMintBurnFeeHookTest#test_Mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
415231
410226
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24437
23296
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133790
133857
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142590
142673
Original file line number Diff line number Diff line change
@@ -1 +1 @@
289582
289648
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126963
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 @@
968260
968387
Original file line number Diff line number Diff line change
@@ -1 +1 @@
327572
327699
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337296
337423
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139847
139974
Original file line number Diff line number Diff line change
@@ -1 +1 @@
172949
173054
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178977
179082
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132980
133085
Original file line number Diff line number Diff line change
@@ -1 +1 @@
304379
304484
Original file line number Diff line number Diff line change
@@ -1 +1 @@
162629
149600
2 changes: 1 addition & 1 deletion .forge-snapshots/CLMintBurnFeeHookTest#test_Burn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175940
170881
2 changes: 1 addition & 1 deletion .forge-snapshots/CLMintBurnFeeHookTest#test_Mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
426252
421255
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21966
20745
Original file line number Diff line number Diff line change
@@ -1 +1 @@
347371
347480
Original file line number Diff line number Diff line change
@@ -1 +1 @@
162832
162941
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238377
238442
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
163103
163218
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
108175
108268
Original file line number Diff line number Diff line change
@@ -1 +1 @@
112719
112784
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130743
130830
Original file line number Diff line number Diff line change
@@ -1 +1 @@
163104
163213
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148523
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 @@
71224
71289
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143107
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 @@
87414
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 @@
71227
71292
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
21 changes: 1 addition & 20 deletions src/libraries/VaultAppDeltaSettlement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,7 @@ library VaultAppDeltaSettlement {
/// @dev default case when no hook return delta is set
vault.accountAppBalanceDelta(key.currency0, key.currency1, delta, msg.sender);
} 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, msg.sender);
} else {
if (delta0 != 0) vault.accountAppBalanceDelta(key.currency0, delta0, msg.sender);
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, msg.sender);
} else {
if (delta1 != 0) vault.accountAppBalanceDelta(key.currency1, delta1, msg.sender);
if (hookDelta1 != 0) vault.accountAppBalanceDelta(key.currency1, hookDelta1, address(key.hooks));
}
vault.accountAppBalanceDelta(key.currency0, key.currency1, delta, msg.sender, hookDelta, address(key.hooks));
}
}
}
2 changes: 1 addition & 1 deletion test/pool-cl/CLMintBurnFeeHook.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ contract CLMintBurnFeeHookTest is Test, Deployers, TokenFixture, GasSnapshot {
assertEq(token0.balanceOf(address(vault)), 1499550104979003780); // ~1.5 ether
assertEq(token1.balanceOf(address(vault)), 1499550104979003780); // ~1.5 ether

// // hook mint VaultToken instead of taking token from vault as vault does not have token in this case
// hook mint VaultToken instead of taking token from vault as vault does not have token in this case
assertEq(vault.balanceOf(address(clMintBurnFeeHook), key.currency0), 999700069986002520); // ~1 eth
assertEq(vault.balanceOf(address(clMintBurnFeeHook), key.currency1), 999700069986002520); // ~1 eth
}
Expand Down
25 changes: 25 additions & 0 deletions test/vault/FakeHook.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {IVault} from "../../src/interfaces/IVault.sol";
import {BalanceDelta, toBalanceDelta} from "../../src/types/BalanceDelta.sol";
import {CurrencySettlement} from "../helpers/CurrencySettlement.sol";
import {Currency} from "../../src/types/Currency.sol";

contract FakeHook {
using CurrencySettlement for Currency;

IVault public vault;

constructor(IVault _vault) {
vault = _vault;
}

function take(Currency currency, uint256 amount, bool claims) public {
currency.take(vault, address(this), amount, claims);
}

function settle(Currency currency, uint256 amount, bool burn) public {
currency.settle(vault, address(this), amount, burn);
}
}
17 changes: 17 additions & 0 deletions test/vault/FakePoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,22 @@ contract FakePoolManager is IPoolManager {
vault.accountAppBalanceDelta(poolKey.currency0, poolKey.currency1, toBalanceDelta(delta0, delta1), msg.sender);
}

function mockAccountingWithHookDelta(
PoolKey calldata poolKey,
int128 delta0,
int128 delta1,
int128 hookDelta0,
int128 hookDelta1
) external {
vault.accountAppBalanceDelta(
poolKey.currency0,
poolKey.currency1,
toBalanceDelta(delta0, delta1),
msg.sender,
toBalanceDelta(hookDelta0, hookDelta1),
address(poolKey.hooks)
);
}

function updateDynamicLPFee(PoolKey memory key, uint24 newDynamicSwapFee) external override {}
}
Loading

0 comments on commit f1ecd94

Please sign in to comment.