Skip to content

Commit

Permalink
feat: Add to in parameter for settleAndRefund
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefMist committed Mar 20, 2024
1 parent 095503b commit 7cf771c
Show file tree
Hide file tree
Showing 46 changed files with 68 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
92245
92310
Original file line number Diff line number Diff line change
@@ -1 +1 @@
67437
67502
Original file line number Diff line number Diff line change
@@ -1 +1 @@
150992
151057
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
68616
68681
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
53952
53973
Original file line number Diff line number Diff line change
@@ -1 +1 @@
968047
968068
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121561
121582
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337237
337258
Original file line number Diff line number Diff line change
@@ -1 +1 @@
56298
56319
Original file line number Diff line number Diff line change
@@ -1 +1 @@
93040
93083
Original file line number Diff line number Diff line change
@@ -1 +1 @@
95025
95068
Original file line number Diff line number Diff line change
@@ -1 +1 @@
74469
74512
Original file line number Diff line number Diff line change
@@ -1 +1 @@
318945
318966
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testNoOpGas_Burn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
41876
41941
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19493
19558
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testNoOpGas_Mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
37345
37410
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testNoOpGas_Swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22699
22764
Original file line number Diff line number Diff line change
@@ -1 +1 @@
348452
348473
Original file line number Diff line number Diff line change
@@ -1 +1 @@
61000
61021
Original file line number Diff line number Diff line change
@@ -1 +1 @@
241390
241433
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
84138
84159
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
53445
53488
Original file line number Diff line number Diff line change
@@ -1 +1 @@
43322
43387
Original file line number Diff line number Diff line change
@@ -1 +1 @@
56445
56488
Original file line number Diff line number Diff line change
@@ -1 +1 @@
104554
104597
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25044269
25044312
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
36336
36401
Original file line number Diff line number Diff line change
@@ -1 +1 @@
103030
103140
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
41986
42051
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
36339
36404
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19448
19513
Original file line number Diff line number Diff line change
@@ -1 +1 @@
27615
27680
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#testNoOp_gas_Swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21897
21962
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#Vault.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7016
7018
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122519
122540
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
158811
158832
Original file line number Diff line number Diff line change
@@ -1 +1 @@
46974
47017
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#lockSettledWhenSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
46973
47016
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#testLock_NoOp.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11532
11597
Original file line number Diff line number Diff line change
@@ -1 +1 @@
75609
79085
Original file line number Diff line number Diff line change
@@ -1 +1 @@
33010
36485
9 changes: 7 additions & 2 deletions src/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ contract Vault is IVault, VaultToken, Ownable {
SettlementGuard.accountDelta(msg.sender, currency, -(paid.toInt128()));
}

function settleAndRefund(Currency currency) external payable isLocked returns (uint256 paid, uint256 refund) {
function settleAndRefund(Currency currency, address to)
external
payable
isLocked
returns (uint256 paid, uint256 refund)
{
paid = settle(currency);

int256 afterCurrencyDelta = SettlementGuard.getCurrencyDelta(msg.sender, currency);
Expand All @@ -138,7 +143,7 @@ contract Vault is IVault, VaultToken, Ownable {
/// thus refund msg.sender and update accountDelta
SettlementGuard.accountDelta(msg.sender, currency, refund.toInt128());
reservesOfVault[currency] -= refund;
currency.transfer(msg.sender, refund);
currency.transfer(to, refund);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/interfaces/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ interface IVault is IVaultToken {
function settle(Currency token) external payable returns (uint256 paid);

/// @notice Called by the user to pay what is owed. If the payment is more than the debt, the surplus is refunded
function settleAndRefund(Currency token) external payable returns (uint256 paid, uint256 refund);
/// @param currency The currency to settle
/// @param to The address to refund the surplus to
function settleAndRefund(Currency currency, address to) external payable returns (uint256 paid, uint256 refund);

/// @notice move the delta from target to the msg.sender, only payment delta can be moved
/// @param currency The currency to settle
Expand Down
6 changes: 4 additions & 2 deletions test/vault/FakePoolManagerRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ contract FakePoolManagerRouter {
vault.settle{value: 5 ether}(CurrencyLibrary.NATIVE);
vault.take(CurrencyLibrary.NATIVE, address(this), 5 ether);
} else if (data[0] == 0x18) {
vault.settleAndRefund(poolKey.currency0);
vault.settleAndRefund(poolKey.currency1);
// call this method via vault.lock(abi.encodePacked(hex"18", alice));
address to = address(uint160(uint256(bytes32(data[1:0x15]) >> 96)));
vault.settleAndRefund(poolKey.currency0, to);
vault.settleAndRefund(poolKey.currency1, to);
}

return "";
Expand Down
18 changes: 11 additions & 7 deletions test/vault/Vault.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -174,35 +174,39 @@ contract VaultTest is Test, GasSnapshot {
}

function testSettleAndRefund_WithErc20Transfer() public {
address alice = makeAddr("alice");

// simulate someone transferred token to vault
currency0.transfer(address(vault), 10 ether);
assertEq(IERC20(Currency.unwrap(currency0)).balanceOf(address(fakePoolManagerRouter)), 0 ether);
assertEq(IERC20(Currency.unwrap(currency1)).balanceOf(address(fakePoolManagerRouter)), 0 ether);
assertEq(IERC20(Currency.unwrap(currency0)).balanceOf(address(alice)), 0 ether);

// settle and refund
vm.prank(address(fakePoolManagerRouter));
snapStart("VaultTest#testSettleAndRefund_WithErc20Transfer");
vault.lock(hex"18");
vault.lock(abi.encodePacked(hex"18", alice));
snapEnd();

// verify
assertEq(IERC20(Currency.unwrap(currency0)).balanceOf(address(fakePoolManagerRouter)), 10 ether);
assertEq(IERC20(Currency.unwrap(currency1)).balanceOf(address(fakePoolManagerRouter)), 0 ether);
assertEq(IERC20(Currency.unwrap(currency0)).balanceOf(address(fakePoolManagerRouter)), 0 ether);
assertEq(IERC20(Currency.unwrap(currency0)).balanceOf(address(alice)), 10 ether);
}

function testSettleAndRefund_WithoutErc20Transfer() public {
address alice = makeAddr("alice");

assertEq(IERC20(Currency.unwrap(currency0)).balanceOf(address(fakePoolManagerRouter)), 0 ether);
assertEq(IERC20(Currency.unwrap(currency1)).balanceOf(address(fakePoolManagerRouter)), 0 ether);
assertEq(IERC20(Currency.unwrap(currency0)).balanceOf(address(alice)), 0 ether);

// settleAndRefund works even if there's no excess currency
vm.prank(address(fakePoolManagerRouter));
snapStart("VaultTest#testSettleAndRefund_WithoutErc20Transfer");
vault.lock(hex"18");
vault.lock(abi.encodePacked(hex"18", alice));
snapEnd();

// verify
assertEq(IERC20(Currency.unwrap(currency0)).balanceOf(address(fakePoolManagerRouter)), 0 ether);
assertEq(IERC20(Currency.unwrap(currency1)).balanceOf(address(fakePoolManagerRouter)), 0 ether);
assertEq(IERC20(Currency.unwrap(currency0)).balanceOf(address(alice)), 0 ether);
}

function testNotCorrectPoolManager() public {
Expand Down
4 changes: 2 additions & 2 deletions test/vault/VaultInvariant.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ contract VaultPoolManager is Test {
token0.transfer(address(vault), action.amt0);
token1.transfer(address(vault), action.amt1);

vault.settleAndRefund(currency0);
vault.settleAndRefund(currency1);
vault.settleAndRefund(currency0, address(this));
vault.settleAndRefund(currency1, address(this));
} else if (action.actionType == ActionType.SettleFor) {
// hook cash out the fee ahead
BalanceDelta delta = toBalanceDelta(int128(action.amt0), int128(action.amt1));
Expand Down

0 comments on commit 7cf771c

Please sign in to comment.