Skip to content
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

Add transfer support #218

Merged
merged 30 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d59846b
move to a base actions router
snreynolds Jul 27, 2024
46f47b2
use one Planner file
snreynolds Jul 27, 2024
b198c30
msgSender
snreynolds Jul 27, 2024
3482b23
add transfer
snreynolds Jul 27, 2024
2cee496
add transfer test
snreynolds Jul 27, 2024
bf369e6
merge main
snreynolds Jul 29, 2024
494f7d6
use delta saving hook to get deltas
snreynolds Jul 29, 2024
0449cac
remove return values
snreynolds Jul 29, 2024
47bbc6d
add burn comment
snreynolds Jul 29, 2024
7c911cb
make gas snapshots more accurate, remove hook
snreynolds Jul 29, 2024
5f26936
merge main
snreynolds Jul 29, 2024
03cf453
move to 1 planner, fix merge conf
snreynolds Jul 29, 2024
2aba16d
sweep currency, pr comments
snreynolds Jul 30, 2024
486617e
merge updates from use-actions-router
snreynolds Jul 30, 2024
b3aa874
rename, add liquidityDelta return param
snreynolds Jul 30, 2024
6885931
rename
snreynolds Jul 30, 2024
a60f16d
comment
snreynolds Jul 30, 2024
7ac7043
gas check
snreynolds Jul 30, 2024
64897c3
add gas test, using uint256
snreynolds Jul 30, 2024
b0b1b73
gas check, using 0
snreynolds Jul 30, 2024
bdfc04d
comments
snreynolds Jul 30, 2024
767605d
Merge branch 'main' into use-actions-router
snreynolds Jul 30, 2024
68e3a66
remove SafeCallback
snreynolds Jul 30, 2024
9d403c3
merge actions router
snreynolds Jul 30, 2024
0778130
remove FULL_DELTA
snreynolds Jul 30, 2024
9087916
merge main
snreynolds Jul 30, 2024
03e978f
move helpers to delta resolver
snreynolds Jul 30, 2024
dedf310
remove import
snreynolds Jul 30, 2024
428db01
increase liq with sttle with balance test
hensha256 Jul 31, 2024
858707e
Merge branch 'main' into add-transfer-support
hensha256 Jul 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148697
148651
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133879
133778
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
368776
368730
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
336858
336757
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_nativeWithSweep.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
343796
346786
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickLower.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
311458
311412
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickUpper.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
312100
312054
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
237682
237636
Original file line number Diff line number Diff line change
@@ -1 +1 @@
317476
317430
Original file line number Diff line number Diff line change
@@ -1 +1 @@
413266
413220
22 changes: 6 additions & 16 deletions src/PositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ contract PositionManager is
return _burn(params);
} else if (action == Actions.SETTLE_WITH_BALANCE) {
return _settleWithBalance(params);
} else if (action == Actions.SWEEP_ERC20_TO) {
_sweepERC20To(params);
} else if (action == Actions.SWEEP) {
_sweep(params);
} else {
revert UnsupportedAction(action);
}
Expand Down Expand Up @@ -158,9 +158,6 @@ contract PositionManager is
address caller = _msgSender();
if (currencyDelta < 0) {
_settle(currency, caller, uint256(-currencyDelta));

// if there are native tokens left over after settling, return to locker
if (currency.isNative()) _sweepNativeToken(caller);
hensha256 marked this conversation as resolved.
Show resolved Hide resolved
} else if (currencyDelta > 0) {
_take(currency, caller, uint256(currencyDelta));
}
Expand All @@ -171,7 +168,6 @@ contract PositionManager is
/// @param params is an encoding of Currency, uint256 amount
/// @dev if amount == FULL_DELTA, it settles the full negative delta
/// @dev uses this addresses balance to settle a negative delta
/// @dev Should not be called for NATIVE settling bc does not sweep.
function _settleWithBalance(bytes memory params) internal returns (bytes memory) {
(Currency currency, uint256 amount) = abi.decode(params, (Currency, uint256));

Expand Down Expand Up @@ -230,18 +226,12 @@ contract PositionManager is
liquidity = poolManager.getPositionLiquidity(config.poolKey.toId(), positionId);
}

/// @dev Send excess native tokens back to the recipient (locker)
/// @param recipient the receiver of the excess native tokens. Should be the caller, the one that sent the native tokens
function _sweepNativeToken(address recipient) internal {
uint256 nativeBalance = address(this).balance;
if (nativeBalance > 0) recipient.safeTransferETH(nativeBalance);
}

/// @notice Sweeps the entire contract balance of specified currency to the recipient
/// @param params an encoding of Currency, address
function _sweepERC20To(bytes calldata params) internal {
function _sweep(bytes calldata params) internal {
(Currency currency, address to) = abi.decode(params, (Currency, address));
uint256 tokenBalance = ERC20(Currency.unwrap(currency)).balanceOf(address(this));
if (tokenBalance > 0) currency.transfer(to, tokenBalance);
uint256 balance = currency.balanceOfSelf();
if (balance > 0) currency.transfer(to, balance);
}

// implementation of abstract function DeltaResolver._pay
Expand Down
1 change: 0 additions & 1 deletion src/interfaces/IPositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
interface IPositionManager {
error NotApproved(address caller);
error DeadlinePassed();
error NothingToSettle();
error IncorrectUseOfTake();
error IncorrectUseOfSettle();
error IncorrectPositionConfigForTokenId(uint256 tokenId);
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Actions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ library Actions {

// settle using the balance contract
uint256 constant SETTLE_WITH_BALANCE = 0x24;
uint256 constant SWEEP_ERC20_TO = 0x25;
uint256 constant SWEEP = 0x25;
}
8 changes: 7 additions & 1 deletion test/position-managers/Gas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,13 @@ contract GasTest is Test, PosmTestSetup, GasSnapshot {

function test_gas_mint_native_excess() public {
uint256 liquidityToAdd = 10_000 ether;
bytes memory calls = getMintEncoded(configNative, liquidityToAdd, address(this), ZERO_BYTES);

Plan memory planner = Planner.init();
planner.add(Actions.MINT_POSITION, abi.encode(configNative, liquidityToAdd, address(this), ZERO_BYTES));
planner.add(Actions.CLOSE_CURRENCY, abi.encode(nativeKey.currency0));
planner.add(Actions.CLOSE_CURRENCY, abi.encode(nativeKey.currency1));
planner.add(Actions.SWEEP, abi.encode(CurrencyLibrary.NATIVE, address(this)));
bytes memory calls = planner.encode();

(uint256 amount0,) = LiquidityAmounts.getAmountsForLiquidity(
SQRT_PRICE_1_1,
Expand Down
6 changes: 3 additions & 3 deletions test/position-managers/IncreaseLiquidity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {
planner.add(Actions.MINT_POSITION, abi.encode(config, liquidityAlice, alice, ZERO_BYTES));
planner.add(Actions.SETTLE_WITH_BALANCE, abi.encode(currency0, type(uint256).max));
planner.add(Actions.SETTLE_WITH_BALANCE, abi.encode(currency1, type(uint256).max));
planner.add(Actions.SWEEP_ERC20_TO, abi.encode(currency0, address(this)));
planner.add(Actions.SWEEP_ERC20_TO, abi.encode(currency1, address(this)));
planner.add(Actions.SWEEP, abi.encode(currency0, address(this)));
planner.add(Actions.SWEEP, abi.encode(currency1, address(this)));

uint256 balanceBefore0 = currency0.balanceOf(address(this));
uint256 balanceBefore1 = currency1.balanceOf(address(this));
Expand All @@ -370,6 +370,6 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {
assertEq(IERC20(Currency.unwrap(currency1)).balanceOf(address(lpm)), 0);

assertEq(currency0.balanceOf(address(this)), balanceBefore0 - amount0);
hensha256 marked this conversation as resolved.
Show resolved Hide resolved
assertEq(currency1.balanceOf(address(this)), balanceBefore0 - amount0);
assertEq(currency1.balanceOf(address(this)), balanceBefore1 - amount1);
}
}
19 changes: 17 additions & 2 deletions test/position-managers/NativeToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,15 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers {
uint256 balance1Before = currency1.balanceOfSelf();

uint256 tokenId = lpm.nextTokenId();
bytes memory calls = getMintEncoded(config, liquidityToAdd, address(this), ZERO_BYTES);

Plan memory planner = Planner.init();
planner.add(Actions.MINT_POSITION, abi.encode(config, liquidityToAdd, address(this), ZERO_BYTES));
planner.add(Actions.CLOSE_CURRENCY, abi.encode(nativeKey.currency0));
planner.add(Actions.CLOSE_CURRENCY, abi.encode(nativeKey.currency1));
// sweep the excess eth
planner.add(Actions.SWEEP, abi.encode(currency0, address(this)));

bytes memory calls = planner.encode();

(uint256 amount0,) = LiquidityAmounts.getAmountsForLiquidity(
SQRT_PRICE_1_1,
Expand Down Expand Up @@ -293,7 +301,14 @@ contract PositionManagerTest is Test, PosmTestSetup, LiquidityFuzzers {
uint128(liquidityToAdd)
);

bytes memory calls = getIncreaseEncoded(tokenId, config, liquidityToAdd, ZERO_BYTES); // double the liquidity
Plan memory planner = Planner.init();
planner.add(Actions.INCREASE_LIQUIDITY, abi.encode(tokenId, config, liquidityToAdd, ZERO_BYTES));
planner.add(Actions.CLOSE_CURRENCY, abi.encode(nativeKey.currency0));
planner.add(Actions.CLOSE_CURRENCY, abi.encode(nativeKey.currency1));
// sweep the excess eth
planner.add(Actions.SWEEP, abi.encode(currency0, address(this)));
bytes memory calls = planner.encode();

bytes[] memory result = lpm.modifyLiquidities{value: amount0 * 2}(calls, _deadline); // overpay on increase liquidity
BalanceDelta delta = abi.decode(result[0], (BalanceDelta));

Expand Down