Skip to content

Commit

Permalink
settle(address) => settle() & settleFor(address)
Browse files Browse the repository at this point in the history
  • Loading branch information
gretzke committed Jul 13, 2024
1 parent e3e0ac1 commit 618cf35
Show file tree
Hide file tree
Showing 53 changed files with 97 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
146680
146240
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
320861
320421
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
275837
275397
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137004
136557
Original file line number Diff line number Diff line change
@@ -1 +1 @@
294813
294373
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 1 token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
104784
104630
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 2 tokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
144773
144311
2 changes: 1 addition & 1 deletion .forge-snapshots/erc20 collect protocol fees.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
57224
57246
2 changes: 1 addition & 1 deletion .forge-snapshots/extsload getFeeGrowthGlobals.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
704
726
2 changes: 1 addition & 1 deletion .forge-snapshots/extsload getFeeGrowthInside.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
375
397
2 changes: 1 addition & 1 deletion .forge-snapshots/extsload getLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
375
397
2 changes: 1 addition & 1 deletion .forge-snapshots/extsload getPositionInfo.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
874
896
2 changes: 1 addition & 1 deletion .forge-snapshots/extsload getPositionLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
375
397
2 changes: 1 addition & 1 deletion .forge-snapshots/extsload getSlot0.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
375
397
2 changes: 1 addition & 1 deletion .forge-snapshots/extsload getTickBitmap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
375
397
2 changes: 1 addition & 1 deletion .forge-snapshots/extsload getTickFeeGrowthOutside.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
704
726
2 changes: 1 addition & 1 deletion .forge-snapshots/extsload getTickInfo.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
874
896
2 changes: 1 addition & 1 deletion .forge-snapshots/extsload getTickLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
375
397
2 changes: 1 addition & 1 deletion .forge-snapshots/getReserves.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1379
1401
2 changes: 1 addition & 1 deletion .forge-snapshots/initialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
59576
59620
2 changes: 1 addition & 1 deletion .forge-snapshots/native collect protocol fees.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
59588
59610
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23872
23983
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
176325
176501
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131597
131773
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
113398
113539
Original file line number Diff line number Diff line change
@@ -1 +1 @@
98847
98627
2 changes: 1 addition & 1 deletion .forge-snapshots/simple addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
161327
161107
Original file line number Diff line number Diff line change
@@ -1 +1 @@
92503
92591
2 changes: 1 addition & 1 deletion .forge-snapshots/simple removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
84560
84648
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap with native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
109811
109547
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124651
124409
2 changes: 1 addition & 1 deletion .forge-snapshots/swap CA custom curve + swap noop.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127470
127008
2 changes: 1 addition & 1 deletion .forge-snapshots/swap CA fee on unspecified.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
172876
172722
Original file line number Diff line number Diff line change
@@ -1 +1 @@
106750
106574
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
117843
117689
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn 6909 for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129595
129771
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn native 6909 for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
118816
118992
2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint native output as 6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
140726
140594
2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint output as 6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
156530
156398
Original file line number Diff line number Diff line change
@@ -1 +1 @@
208706
208310
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
140624
140470
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133294
133140
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with lp fee and protocol fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170427
170317
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with return dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
146855
146701
2 changes: 1 addition & 1 deletion .forge-snapshots/update dynamic fee in before swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149113
149003
37 changes: 23 additions & 14 deletions src/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -278,21 +278,13 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
}

/// @inheritdoc IPoolManager
function settle(address recipient) external payable onlyWhenUnlocked returns (uint256 paid) {
Currency currency = CurrencyReserves.getSyncedCurrency();
// If not previously synced, expects native currency to be settled because currency.isNative() == address(0)
if (currency.isNative()) {
paid = msg.value;
} else {
if (msg.value > 0) NonZeroNativeValue.selector.revertWith();
// Reserves are guaranteed to be set, because currency and reserves are always set together
uint256 reservesBefore = CurrencyReserves.getSyncedReserves();
uint256 reservesNow = currency.balanceOfSelf();
paid = reservesNow - reservesBefore;
CurrencyReserves.reset();
}
function settle() external payable onlyWhenUnlocked returns (uint256 paid) {
return _settle(msg.sender);
}

_accountDelta(currency, paid.toInt128(), recipient);
/// @inheritdoc IPoolManager
function settleFor(address recipient) external payable onlyWhenUnlocked returns (uint256 paid) {
return _settle(recipient);
}

/// @inheritdoc IPoolManager
Expand Down Expand Up @@ -320,6 +312,23 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
_pools[id].setLPFee(newDynamicLPFee);
}

function _settle(address recipient) internal returns (uint256 paid) {
Currency currency = CurrencyReserves.getSyncedCurrency();
// If not previously synced, expects native currency to be settled because currency.isNative() == address(0)
if (currency.isNative()) {
paid = msg.value;
} else {
if (msg.value > 0) NonZeroNativeValue.selector.revertWith();
// Reserves are guaranteed to be set, because currency and reserves are always set together
uint256 reservesBefore = CurrencyReserves.getSyncedReserves();
uint256 reservesNow = currency.balanceOfSelf();
paid = reservesNow - reservesBefore;
CurrencyReserves.reset();
}

_accountDelta(currency, paid.toInt128(), recipient);
}

/// @notice Adds a balance delta in a currency for a target address
function _accountDelta(Currency currency, int128 delta, address target) internal {
if (delta == 0) return;
Expand Down
5 changes: 4 additions & 1 deletion src/interfaces/IPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,11 @@ interface IPoolManager is IProtocolFees, IERC6909Claims, IExtsload, IExttload {
function burn(address from, uint256 id, uint256 amount) external;

/// @notice Called by the user to pay what is owed
function settle() external payable returns (uint256 paid);

/// @notice Called by the user to pay on behalf of another address
/// @param recipient The address to credit for the payment
function settle(address recipient) external payable returns (uint256 paid);
function settleFor(address recipient) external payable returns (uint256 paid);

/// @notice Updates the pools lp fees for the a pool that has enabled dynamic lp fees.
function updateDynamicLPFee(PoolKey memory key, uint24 newDynamicLPFee) external;
Expand Down
27 changes: 17 additions & 10 deletions src/test/ActionsRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import {TransientStateLibrary} from "../libraries/TransientStateLibrary.sol";
enum Actions {
SETTLE,
SETTLE_NATIVE,
SETTLE_FOR,
TAKE,
TAKE_FROM,
PRANK_TAKE_FROM,
SYNC,
MINT,
ASSERT_BALANCE_EQUALS,
Expand Down Expand Up @@ -53,13 +54,15 @@ contract ActionsRouter is IUnlockCallback, Test {
Actions action = actions[i];
bytes memory param = params[i];
if (action == Actions.SETTLE) {
_settle(param);
_settle();
} else if (action == Actions.SETTLE_NATIVE) {
_settleNative(param);
} else if (action == Actions.SETTLE_FOR) {
_settleFor(param);
} else if (action == Actions.TAKE) {
_take(param);
} else if (action == Actions.TAKE_FROM) {
_takeFrom(param);
} else if (action == Actions.PRANK_TAKE_FROM) {
_prankTakeFrom(param);
} else if (action == Actions.SYNC) {
_sync(param);
} else if (action == Actions.MINT) {
Expand All @@ -81,22 +84,26 @@ contract ActionsRouter is IUnlockCallback, Test {
manager.unlock(abi.encode(actions, params));
}

function _settle(bytes memory params) internal {
address recipient = abi.decode(params, (address));
manager.settle(recipient);
function _settle() internal {
manager.settle();
}

function _settleNative(bytes memory params) internal {
(uint256 amount, address recipient) = abi.decode(params, (uint256, address));
manager.settle{value: amount}(recipient);
uint256 amount = abi.decode(params, (uint256));
manager.settle{value: amount}();
}

function _settleFor(bytes memory params) internal {
address recipient = abi.decode(params, (address));
manager.settleFor(recipient);
}

function _take(bytes memory params) internal {
(Currency currency, address recipient, int128 amount) = abi.decode(params, (Currency, address, int128));
manager.take(currency, recipient, uint128(amount));
}

function _takeFrom(bytes memory params) internal {
function _prankTakeFrom(bytes memory params) internal {
(Currency currency, address from, address recipient, uint256 amount) =
abi.decode(params, (Currency, address, address, uint256));
vm.prank(from);
Expand Down
2 changes: 1 addition & 1 deletion src/test/DeltaReturningHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ contract DeltaReturningHook is BaseTestHooks {
} else {
uint256 amount = uint256(-int256(delta));
if (currency.isNative()) {
manager.settle{value: amount}(address(this));
manager.settle{value: amount}();
} else {
currency.settle(manager, address(this), amount, false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/PoolSettleTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract PoolSettleTest is PoolTestBase {
function unlockCallback(bytes calldata) external returns (bytes memory) {
require(msg.sender == address(manager));

manager.settle{value: address(this).balance}(address(this));
manager.settle{value: address(this).balance}();

return abi.encode(0);
}
Expand Down
2 changes: 1 addition & 1 deletion test/PoolManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot {

function test_settle_failsIfLocked() public {
vm.expectRevert(IPoolManager.ManagerLocked.selector);
manager.settle(address(this));
manager.settle();
}

function test_settle_revertsSendingNativeWithToken() public noIsolate {
Expand Down
Loading

0 comments on commit 618cf35

Please sign in to comment.