Skip to content

Commit

Permalink
refactor: flip the sign for balanceDelta (#49)
Browse files Browse the repository at this point in the history
* refactor: flip the sign for balanceDelta

* chore: remove unnecessary console2.log

* chore: docs

* docs: flips omitted comments
  • Loading branch information
chefburger authored May 29, 2024
1 parent 47315dc commit 4336d26
Show file tree
Hide file tree
Showing 72 changed files with 384 additions and 384 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
182049
181729
Original file line number Diff line number Diff line change
@@ -1 +1 @@
185296
185616
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
331250
331570
Original file line number Diff line number Diff line change
@@ -1 +1 @@
138533
138277
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148635
148315
Original file line number Diff line number Diff line change
@@ -1 +1 @@
296486
296230
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131743
131487
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120042
120359
Original file line number Diff line number Diff line change
@@ -1 +1 @@
977200
977517
Original file line number Diff line number Diff line change
@@ -1 +1 @@
339390
339707
Original file line number Diff line number Diff line change
@@ -1 +1 @@
342867
343184
Original file line number Diff line number Diff line change
@@ -1 +1 @@
145810
146127
Original file line number Diff line number Diff line change
@@ -1 +1 @@
309383
309700
Original file line number Diff line number Diff line change
@@ -1 +1 @@
362972
362978
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178140
178146
Original file line number Diff line number Diff line change
@@ -1 +1 @@
248255
248182
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170425
170745
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114348
114589
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124482
124243
Original file line number Diff line number Diff line change
@@ -1 +1 @@
141710
141453
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175389
174968
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25094185
161331
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
79576
79416
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153925
153614
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
96383
96223
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
79579
79419
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32291
29467
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2237
2239
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3022
3029
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1994
1981
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3022
3029
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2227
2229
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3175
3182
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1984
1971
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3175
3182
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#Vault.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7136
7171
Original file line number Diff line number Diff line change
@@ -1 +1 @@
81625
81624
28 changes: 12 additions & 16 deletions src/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ contract Vault is IVault, VaultToken, Ownable {

/// @inheritdoc IVault
function take(Currency currency, address to, uint256 amount) external override isLocked {
SettlementGuard.accountDelta(msg.sender, currency, amount.toInt128());
SettlementGuard.accountDelta(msg.sender, currency, -(amount.toInt128()));
currency.transfer(to, amount);
}

/// @inheritdoc IVault
function mint(address to, Currency currency, uint256 amount) external override isLocked {
SettlementGuard.accountDelta(msg.sender, currency, amount.toInt128());
SettlementGuard.accountDelta(msg.sender, currency, -(amount.toInt128()));
_mint(to, currency, amount);
}

Expand All @@ -128,22 +128,21 @@ contract Vault is IVault, VaultToken, Ownable {
paid = msg.value;
}

// subtraction must be safe
SettlementGuard.accountDelta(msg.sender, currency, -(paid.toInt128()));
SettlementGuard.accountDelta(msg.sender, currency, paid.toInt128());
}

/// @inheritdoc IVault
function settleFor(Currency currency, address target, uint256 amount) external isLocked {
/// @notice settle all outstanding debt if amount is 0
/// It will revert if target has negative delta
if (amount == 0) amount = SettlementGuard.getCurrencyDelta(target, currency).toUint256();
SettlementGuard.accountDelta(msg.sender, currency, amount.toInt128());
SettlementGuard.accountDelta(target, currency, -(amount.toInt128()));
/// It will revert if target has positive delta
if (amount == 0) amount = (-SettlementGuard.getCurrencyDelta(target, currency)).toUint256();
SettlementGuard.accountDelta(msg.sender, currency, -(amount.toInt128()));
SettlementGuard.accountDelta(target, currency, amount.toInt128());
}

/// @inheritdoc IVault
function burn(address from, Currency currency, uint256 amount) external override isLocked {
SettlementGuard.accountDelta(msg.sender, currency, -(amount.toInt128()));
SettlementGuard.accountDelta(msg.sender, currency, amount.toInt128());
_burnFrom(from, currency, amount);
}

Expand All @@ -162,14 +161,11 @@ contract Vault is IVault, VaultToken, Ownable {
if (delta == 0) return;

if (delta >= 0) {
reservesOfPoolManager[poolManager][currency] += uint128(delta);
/// @dev arithmetic underflow make sure trader can't withdraw too much from poolManager
reservesOfPoolManager[poolManager][currency] -= uint128(delta);
} else {
/// @dev arithmetic underflow is possible in following two cases:
/// 1. delta == type(int128).min
/// This occurs when withdrawing amount is too large
/// 2. reservesOfPoolManager[poolManager][currency] < delta0
/// This occurs when insufficient balance in pool
reservesOfPoolManager[poolManager][currency] -= uint128(-delta);
/// @dev arithmetic overflow make sure trader won't deposit too much into poolManager
reservesOfPoolManager[poolManager][currency] += uint128(-delta);
}
}
}
6 changes: 3 additions & 3 deletions src/pool-bin/interfaces/IBinHooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ interface IBinHooks is IHooks {
/// @param sender The initial msg.sender for the modify position call
/// @param key The key for the pool
/// @param params The parameters for adding liquidity
/// @param delta The amount owed to the locker (negative) or owed to the pool (positive)
/// @param delta The amount owed to the locker (positive) or owed to the pool (negative)
/// @param hookData Arbitrary data handed into the PoolManager by the liquidty provider to be be passed on to the hook
/// @return bytes4 The function selector for the hook
/// @return BalanceDelta The hook's delta in token0 and token1.
Expand Down Expand Up @@ -93,7 +93,7 @@ interface IBinHooks is IHooks {
/// @param sender The initial msg.sender for the modify position call
/// @param key The key for the pool
/// @param params The parameters for removing liquidity
/// @param delta The amount owed to the locker (negative) or owed to the pool (positive)
/// @param delta The amount owed to the locker (positive) or owed to the pool (negative)
/// @param hookData Arbitrary data handed into the PoolManager by the liquidty provider to be be passed on to the hook
/// @return bytes4 The function selector for the hook
/// @return BalanceDelta The hook's delta in token0 and token1.
Expand Down Expand Up @@ -126,7 +126,7 @@ interface IBinHooks is IHooks {
/// @param key The key for the pool
/// @param swapForY If true, indicate swap X for Y or if false, swap Y for X
/// @param amountIn Amount of tokenX or tokenY in
/// @param delta The amount owed to the locker (negative) or owed to the pool (positive)
/// @param delta The amount owed to the locker (positive) or owed to the pool (negtive)
/// @param hookData Arbitrary data handed into the PoolManager by the swapper to be be passed on to the hook
/// @return bytes4 The function selector for the hook
/// @return int128 The hook's delta in unspecified currency
Expand Down
5 changes: 3 additions & 2 deletions src/pool-bin/interfaces/IBinPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,14 @@ interface IBinPoolManager is IProtocolFees, IPoolManager, IExtsload {
function initialize(PoolKey memory key, uint24 activeId, bytes calldata hookData) external;

/// @notice Add liquidity to a pool
/// @return delta BalanceDelta, will be positive indicating how much total amt0 and amt1 liquidity added
/// @return delta BalanceDelta, will be negative indicating how much total amt0 and amt1 liquidity added
/// @return mintArray Liquidity added in which ids, how much amt0, amt1 and how much liquidity added
function mint(PoolKey memory key, IBinPoolManager.MintParams calldata params, bytes calldata hookData)
external
returns (BalanceDelta delta, BinPool.MintArrays memory mintArray);

/// @notice Remove liquidity from a pool
/// @return delta BalanceDelta, will be positive indicating how much total amt0 and amt1 liquidity removed
function burn(PoolKey memory key, IBinPoolManager.BurnParams memory params, bytes calldata hookData)
external
returns (BalanceDelta delta);
Expand All @@ -171,7 +172,7 @@ interface IBinPoolManager is IProtocolFees, IPoolManager, IExtsload {
returns (BalanceDelta delta);

/// @notice Donate the given currency amounts to the pool with the given pool key.
/// @return delta Positive amt means the caller owes the vault, while negative amt means the vault owes the caller
/// @return delta Negative amt means the caller owes the vault, while positive amt means the vault owes the caller
/// @return binId The donated bin id, which is the current active bin id. if no-op happen, binId will be 0
function donate(PoolKey memory key, uint128 amount0, uint128 amount1, bytes calldata hookData)
external
Expand Down
12 changes: 7 additions & 5 deletions src/pool-bin/libraries/BinHooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ library BinHooks {

if (hookDeltaSpecified != 0) {
/// @dev default overflow check make sure the swap amount is always valid
if (hookDeltaSpecified > 0) {
amountToSwap += uint128(hookDeltaSpecified);
} else {
if (hookDeltaSpecified < 0) {
amountToSwap -= uint128(-hookDeltaSpecified);
} else {
amountToSwap += uint128(hookDeltaSpecified);
}
}
}
Expand Down Expand Up @@ -163,9 +163,11 @@ library BinHooks {
hookDeltaUnspecified += beforeSwapDelta.getUnspecifiedDelta();

if (hookDeltaUnspecified != 0 || hookDeltaSpecified != 0) {
// the hookDelta is the delta added into amountIn which is in the opposite direction of the balanceDelta
// i.e. in a reasonable case, amountIn will basiaclly equal to "-BalanceDelta.amount0()/amount1()"
hookDelta = swapForY
? toBalanceDelta(hookDeltaSpecified, hookDeltaUnspecified)
: toBalanceDelta(hookDeltaUnspecified, hookDeltaSpecified);
? toBalanceDelta(-hookDeltaSpecified, -hookDeltaUnspecified)
: toBalanceDelta(-hookDeltaUnspecified, -hookDeltaSpecified);

// the caller has to pay for (or receive) the hook's delta
swapperDelta = delta - hookDelta;
Expand Down
13 changes: 7 additions & 6 deletions src/pool-bin/libraries/BinPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ library BinPool {

if (swapForY) {
uint128 consumed = amountIn - amountsLeft.decodeX();
result = toBalanceDelta(consumed.safeInt128(), -(amountsOut.decodeY().safeInt128()));
result = toBalanceDelta(-consumed.safeInt128(), amountsOut.decodeY().safeInt128());
} else {
uint128 consumed = amountIn - amountsLeft.decodeY();
result = toBalanceDelta(-(amountsOut.decodeX().safeInt128()), consumed.safeInt128());
result = toBalanceDelta(amountsOut.decodeX().safeInt128(), -(consumed.safeInt128()));
}
}

Expand Down Expand Up @@ -303,7 +303,9 @@ library BinPool {
compositionFee = compoFee;

(uint128 x1, uint128 x2) = params.amountIn.sub(amountsLeft).decode();
result = toBalanceDelta(x1.safeInt128(), x2.safeInt128());

// set balanceDelta to negative (so user must settle()) from the vault
result = toBalanceDelta(-(x1.safeInt128()), -(x2.safeInt128()));
}

/// @notice Returns the reserves of a bin
Expand Down Expand Up @@ -372,8 +374,7 @@ library BinPool {
}
}

// set amoutsOut to negative (so user can take/mint()) from the vault
result = toBalanceDelta(-(amountsOut.decodeX().safeInt128()), -(amountsOut.decodeY().safeInt128()));
result = toBalanceDelta(amountsOut.decodeX().safeInt128(), amountsOut.decodeY().safeInt128());
}

function donate(State storage self, uint16 binStep, uint128 amount0, uint128 amount1)
Expand All @@ -391,7 +392,7 @@ library BinPool {
binReserves.add(amountIn).getLiquidity(price);

self.reserveOfBin[activeId] = binReserves.add(amountIn);
result = toBalanceDelta(amount0.safeInt128(), amount1.safeInt128());
result = toBalanceDelta(-(amount0.safeInt128()), -(amount1.safeInt128()));
}

/// @dev Helper function to mint liquidity in each bin in the liquidity configurations
Expand Down
2 changes: 2 additions & 0 deletions src/pool-cl/interfaces/ICLHooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ interface ICLHooks is IHooks {
/// @param sender The initial msg.sender for the add liquidity call
/// @param key The key for the pool
/// @param params The parameters for adding liquidity
/// @param delta The amount owed to the locker (positive) or owed to the pool (negative)
/// @param hookData Arbitrary data handed into the PoolManager by the liquidty provider to be be passed on to the hook
/// @return bytes4 The function selector for the hook
/// @return BalanceDelta The hook's delta in token0 and token1.
Expand Down Expand Up @@ -99,6 +100,7 @@ interface ICLHooks is IHooks {
/// @param sender The initial msg.sender for the remove liquidity call
/// @param key The key for the pool
/// @param params The parameters for removing liquidity
/// @param delta The amount owed to the locker (positive) or owed to the pool (negative)
/// @param hookData Arbitrary data handed into the PoolManager by the liquidty provider to be be passed on to the hook
/// @return bytes4 The function selector for the hook
/// @return BalanceDelta The hook's delta in token0 and token1.
Expand Down
Loading

0 comments on commit 4336d26

Please sign in to comment.