-
Notifications
You must be signed in to change notification settings - Fork 35
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
Changes from 1 commit
6df5f23
ddc315f
d1b7956
ff6e66b
1676856
f1ecd94
f224fa6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
178172 | ||
178124 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
311226 | ||
311244 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
189430 | ||
189464 |
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 |
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 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
163100 | ||
163103 |
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 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
87408 | ||
87442 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
7315 | ||
7318 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
10977 | ||
10980 |
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. | ||
function accountAppDeltaWithHookDelta( | ||
IVault vault, | ||
PoolKey memory key, | ||
BalanceDelta delta, | ||
BalanceDelta hookDelta, | ||
address settler | ||
) internal { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, cleaned up