Skip to content

Commit

Permalink
feat: remove settleFor (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
chefburger authored Jul 2, 2024
1 parent 52b2168 commit 55f48d3
Show file tree
Hide file tree
Showing 39 changed files with 41 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133598
133591
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142367
142359
Original file line number Diff line number Diff line change
@@ -1 +1 @@
288123
288116
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126702
126696
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114406
114274
Original file line number Diff line number Diff line change
@@ -1 +1 @@
967576
967444
Original file line number Diff line number Diff line change
@@ -1 +1 @@
325793
325661
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337275
337143
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139677
139545
Original file line number Diff line number Diff line change
@@ -1 +1 @@
171641
171571
Original file line number Diff line number Diff line change
@@ -1 +1 @@
177522
177452
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132508
132438
Original file line number Diff line number Diff line change
@@ -1 +1 @@
304232
304122
Original file line number Diff line number Diff line change
@@ -1 +1 @@
351855
351723
Original file line number Diff line number Diff line change
@@ -1 +1 @@
167296
167164
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238723
238657
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
164198
164066
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
109050
108962
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115271
115245
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131706
131636
Original file line number Diff line number Diff line change
@@ -1 +1 @@
164292
164182
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151737
151667
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
72036
71992
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143922
143874
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
88011
87967
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
72039
71995
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7348
7204
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#collectFee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25188
25166
Original file line number Diff line number Diff line change
@@ -1 +1 @@
82069
81976
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
118648
118569
Original file line number Diff line number Diff line change
@@ -1 +1 @@
42842
42788
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#lockSettledWhenSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
42838
42784
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#testLock_NoOp.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
31085
31011
11 changes: 0 additions & 11 deletions src/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,6 @@ contract Vault is IVault, VaultToken, Ownable {
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 positive delta
unchecked {
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());
Expand Down
6 changes: 0 additions & 6 deletions src/interfaces/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ interface IVault is IVaultToken {
/// @notice Called by the user to pay what is owed
function settle(Currency token) external payable returns (uint256 paid);

/// @notice move the delta from target to the msg.sender, only payment delta can be moved
/// @param currency The currency to settle
/// @param target The address whose delta will be updated
/// @param amount The amount to settle. 0 to settle all outstanding debt
function settleFor(Currency currency, address target, uint256 amount) external;

/// @notice Called by app to collect any fee related
/// @dev no restriction on caller, underflow happen if caller collect more than the reserve
function collectFee(Currency currency, uint256 amount, address recipient) external;
Expand Down
25 changes: 0 additions & 25 deletions test/vault/FakePoolManagerRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,6 @@ contract FakePoolManagerRouter is Test {
} else if (data[0] == 0x10) {
// call accountPoolBalanceDelta from arbitrary addr
vault.accountAppBalanceDelta(poolKey, toBalanceDelta(int128(-1), int128(0)), address(0));
} else if (data[0] == 0x11) {
// settleFor
Payer payer = new Payer();
payer.settleFor(vault, poolKey.currency0, 5 ether);

vault.sync(poolKey.currency0);
poolKey.currency0.transfer(address(vault), 5 ether);
payer.settle(vault, poolKey.currency0);

vault.take(poolKey.currency0, address(this), 5 ether);
} else if (data[0] == 0x12) {
// settleFor(, , 0)
Payer payer = new Payer();

vault.sync(poolKey.currency0);
uint256 amt = poolKey.currency0.balanceOfSelf();
poolKey.currency0.transfer(address(vault), amt);
payer.settle(vault, poolKey.currency0);

vault.take(poolKey.currency0, address(this), amt);
payer.settleFor(vault, poolKey.currency0, 0);
} else if (data[0] == 0x13) {
// mint
uint256 amt = poolKey.currency0.balanceOf(address(vault));
Expand Down Expand Up @@ -224,10 +203,6 @@ contract Forwarder {
}

contract Payer {
function settleFor(IVault vault, Currency currency, uint256 amt) public {
vault.settleFor(currency, msg.sender, amt);
}

function settle(IVault vault, Currency currency) public {
vault.settle(currency);
}
Expand Down
17 changes: 0 additions & 17 deletions test/vault/Vault.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -322,23 +322,6 @@ contract VaultTest is Test, NoIsolate, GasSnapshot {
assertEq(IERC20(Currency.unwrap(currency1)).balanceOf(address(fakePoolManagerRouter)), 3 ether);
}

function testVault_settleFor() public {
// make sure router has enough tokens
currency0.transfer(address(fakePoolManagerRouter), 10 ether);

vm.prank(address(fakePoolManagerRouter));
vault.lock(hex"11");
}

function testVaultFuzz_settleFor_arbitraryAmt(uint256 amt) public {
amt = bound(amt, 0, 10 ether);
// make sure router has enough tokens
currency0.transfer(address(fakePoolManagerRouter), amt);

vm.prank(address(fakePoolManagerRouter));
vault.lock(hex"12");
}

function testVaultFuzz_mint(uint256 amt) public noIsolate {
amt = bound(amt, 0, 10 ether);
// make sure router has enough tokens
Expand Down
37 changes: 2 additions & 35 deletions test/vault/VaultInvariant.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ contract VaultPoolManager is Test {
enum ActionType {
Take,
Settle,
SettleFor,
Mint,
Burn
}
Expand Down Expand Up @@ -85,19 +84,6 @@ contract VaultPoolManager is Test {
vault.lock(abi.encode(Action(ActionType.Settle, uint128(amt0), uint128(amt1))));
}

/// @dev In settleFor case, assume user is paying for hook
function settleFor(uint256 amt0, uint256 amt1) public {
amt0 = bound(amt0, 0, MAX_TOKEN_BALANCE);
amt1 = bound(amt1, 0, MAX_TOKEN_BALANCE);

// make sure the vault has enough liquidity at very beginning
settle(amt0, amt1);

token0.mint(address(this), amt0);
token1.mint(address(this), amt1);
vault.lock(abi.encode(Action(ActionType.SettleFor, uint128(amt0), uint128(amt1))));
}

/// @dev In mint case, assume user remove liquidity and mint nft as reciept
function mint(uint256 amt0, uint256 amt1) public {
amt0 = bound(amt0, 0, MAX_TOKEN_BALANCE);
Expand Down Expand Up @@ -167,24 +153,6 @@ contract VaultPoolManager is Test {
token0.transfer(address(vault), action.amt0);
token1.transfer(address(vault), action.amt1);

vault.settle(currency0);
vault.settle(currency1);
} else if (action.actionType == ActionType.SettleFor) {
// hook cash out the fee ahead
BalanceDelta delta = toBalanceDelta(int128(action.amt0), int128(action.amt1));
vault.accountAppBalanceDelta(poolKey, delta, makeAddr("hook"));

// transfer hook's fee to user
vault.settleFor(currency0, makeAddr("hook"), action.amt0);
vault.settleFor(currency1, makeAddr("hook"), action.amt1);

vault.sync(currency0);
vault.sync(currency1);

// handle user's own deltas
token0.transfer(address(vault), action.amt0);
token1.transfer(address(vault), action.amt1);

vault.settle(currency0);
vault.settle(currency1);
} else if (action.actionType == ActionType.Burn) {
Expand Down Expand Up @@ -219,13 +187,12 @@ contract VaultInvariant is Test, NoIsolate, GasSnapshot {
// Only call vaultPoolManager, otherwise all other contracts deployed in setUp will be called
targetContract(address(vaultPoolManager));

bytes4[] memory selectors = new bytes4[](6);
bytes4[] memory selectors = new bytes4[](5);
selectors[0] = VaultPoolManager.take.selector;
selectors[1] = VaultPoolManager.mint.selector;
selectors[2] = VaultPoolManager.settle.selector;
selectors[3] = VaultPoolManager.burn.selector;
selectors[4] = VaultPoolManager.settleFor.selector;
selectors[5] = VaultPoolManager.collectFee.selector;
selectors[4] = VaultPoolManager.collectFee.selector;
targetSelector(FuzzSelector({addr: address(vaultPoolManager), selectors: selectors}));
}

Expand Down
27 changes: 6 additions & 21 deletions test/vault/VaultReentrancy.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,18 @@ contract VaultReentrancyTest is Test, TokenFixture {
// 2. settle
// 3. mint
// 4. burn
// 5. settleFor
// 6. accountPoolBalanceDelta
// 5. accountPoolBalanceDelta

address callerAddr = makeAddr(string(abi.encode(i % SETTLERS_AMOUNT)));
uint256 paidAmount = i * 10;
if (i % 6 == 0) {
if (i % 5 == 0) {
// take
vm.startPrank(callerAddr);
vault.take(currency0, callerAddr, paidAmount);
vm.stopPrank();

currencyDelta[i % SETTLERS_AMOUNT] -= int256(paidAmount);
} else if (i % 6 == 1) {
} else if (i % 5 == 1) {
// settle
vault.sync(currency0);
currency0.transfer(address(vault), paidAmount);
Expand All @@ -198,37 +197,23 @@ contract VaultReentrancyTest is Test, TokenFixture {
vm.stopPrank();

currencyDelta[i % SETTLERS_AMOUNT] += int256(paidAmount);
} else if (i % 6 == 2) {
} else if (i % 5 == 2) {
// mint
vm.startPrank(callerAddr);
vault.mint(callerAddr, currency0, paidAmount);
vm.stopPrank();

currencyDelta[i % SETTLERS_AMOUNT] -= int256(paidAmount);
vaultTokenBalance[i % SETTLERS_AMOUNT] += paidAmount;
} else if (i % 6 == 3) {
} else if (i % 5 == 3) {
// burn
vm.startPrank(callerAddr);
vault.burn(callerAddr, currency0, paidAmount);
vm.stopPrank();

currencyDelta[i % SETTLERS_AMOUNT] += int256(paidAmount);
vaultTokenBalance[i % SETTLERS_AMOUNT] -= paidAmount;
} else if (i % 6 == 4) {
// settleFor
vault.sync(currency0);
currency0.transfer(address(vault), paidAmount);
vm.startPrank(callerAddr);
vault.settle(currency0);
vm.stopPrank();

address target = makeAddr(string(abi.encode((i + 1) % SETTLERS_AMOUNT)));
vm.startPrank(callerAddr);
vault.settleFor(currency0, target, paidAmount);
vm.stopPrank();

currencyDelta[(i + 1) % SETTLERS_AMOUNT] += int256(paidAmount);
} else if (i % 6 == 5) {
} else if (i % 5 == 4) {
// accountPoolBalanceDelta
vm.startPrank(makeAddr("poolManager"));
vault.accountAppBalanceDelta(
Expand Down

0 comments on commit 55f48d3

Please sign in to comment.