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

Middleware protect #152

Open
wants to merge 54 commits into
base: middleware-remove
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
128aacf
create outline
Jun1on Jul 15, 2024
6801684
add liquidity protection
Jun1on Jul 15, 2024
5e6c2ae
add quoter check in beforeswap
Jun1on Jul 15, 2024
9f6788e
increase foundry gas_limit
Jun1on Jul 16, 2024
e3ad3c7
forge fmt
Jun1on Jul 16, 2024
d3e40ff
add function to handle errors
Jun1on Jul 16, 2024
052f21d
enforce implementation address mined
Jun1on Jul 16, 2024
71962b1
fix error bug
Jun1on Jul 17, 2024
9a60e41
bring back random function
Jun1on Jul 17, 2024
daf74ce
Recipient mapping! (#246)
hensha256 Aug 2, 2024
fdf28e4
allow batching sig based approvals through p2 forwarder (#238)
snreynolds Aug 2, 2024
ebf7a4d
Take portion (#250)
hensha256 Aug 2, 2024
30863cc
Pay with balance, and use delta as amount in (#249)
hensha256 Aug 2, 2024
a9e463d
Settle pair (#242)
dianakocsis Aug 2, 2024
e764aec
posm: add staking through subscribers (#229)
snreynolds Aug 2, 2024
912536c
make msgSender public (#253)
hensha256 Aug 2, 2024
eee5a0e
posm: CLEAR_OR_TAKE (#252)
saucepoint Aug 2, 2024
6fe5428
TAKE_PAIR (#254)
dianakocsis Aug 3, 2024
1f28ac2
ERC721Permit (#210)
saucepoint Aug 3, 2024
2f15bb2
Take (#257)
snreynolds Aug 3, 2024
86b5ea3
multicall: bubble up revert reason (#236)
saucepoint Aug 4, 2024
d65f158
Optimise permit hashing (#260)
hensha256 Aug 4, 2024
0c956bf
Replace OZ EIP712 (#256)
saucepoint Aug 4, 2024
60a983e
Take command in router (#261)
hensha256 Aug 4, 2024
dfa1865
posm: Rename File Collisions (#263)
saucepoint Aug 4, 2024
2d07bd4
Align constants with UR (#267)
hensha256 Aug 4, 2024
e2d2508
One BPS library (#268)
hensha256 Aug 4, 2024
eb0cf58
slippage params routing (#264)
hensha256 Aug 4, 2024
41bbc7d
add liquidity view (#270)
snreynolds Aug 4, 2024
ea5f9ec
add bytes, clean up compiliation (#269)
snreynolds Aug 4, 2024
f402aa7
actions with no unlock (#231)
hensha256 Aug 5, 2024
d1f9005
ERC721Permit - PermitForAll (#271)
saucepoint Aug 5, 2024
3b93674
Wrap reverts thrown by subscribers (#273)
gretzke Aug 5, 2024
df47aa9
Some cleanup (#276)
snreynolds Aug 5, 2024
20718d5
Make PositionManager.transferFrom virtual (#278)
brockmiller Aug 5, 2024
cf4e2ad
Use custom revert (#277)
snreynolds Aug 5, 2024
af688af
add mint position event (#279)
snreynolds Aug 5, 2024
bf3b8ad
Provide feesAccrued to subscriber.notifyModifyLiquidity (#282)
saucepoint Aug 6, 2024
17f1a49
OZ: posm - restore permissioning on increase (#290)
saucepoint Aug 7, 2024
7cad2f6
fix: slippage checks (#285)
snreynolds Aug 8, 2024
b890da6
nit: make multicall external (#292)
snreynolds Aug 8, 2024
4d56687
OZ: Remove contract balance swap input (#286)
hensha256 Aug 8, 2024
469f856
move sub unsub (#287)
snreynolds Aug 8, 2024
5ad4439
add view quoter
Jun1on Aug 8, 2024
656afa4
Merge branch 'middleware-remove' into middleware-protect
Jun1on Aug 9, 2024
05ad967
uninheret from middleware-remove
Jun1on Aug 13, 2024
54c12c8
add gas tests with revert quoter
Jun1on Aug 13, 2024
4ad5a3f
Merge branch 'view-quoter' into middleware-protect
Jun1on Aug 13, 2024
8139431
forge test --isolate
Jun1on Aug 13, 2024
81e9692
use view quoter
Jun1on Aug 13, 2024
3f69c05
optimize quoter
Jun1on Aug 13, 2024
6632806
update docs
Jun1on Aug 14, 2024
cea56de
use tstore
Jun1on Aug 15, 2024
5eec68a
update gas snapshots
Jun1on Aug 15, 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
Prev Previous commit
Next Next commit
Align constants with UR (#267)
* Align constants with UR

* rename

* another rename
  • Loading branch information
hensha256 authored Aug 4, 2024
commit 2d07bd4eea612de8147010a7fc215decd632c826
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127690
127708
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127828
127846
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_decrease_take_take.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116639
116603
Original file line number Diff line number Diff line change
@@ -1 +1 @@
370975
370951
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_Bytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8199
8165
6 changes: 3 additions & 3 deletions src/base/BaseActionsRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {SafeCallback} from "./SafeCallback.sol";
import {CalldataDecoder} from "../libraries/CalldataDecoder.sol";
import {Actions} from "../libraries/Actions.sol";
import {Constants} from "../libraries/Constants.sol";
import {ActionConstants} from "../libraries/ActionConstants.sol";

/// @notice Abstract contract for performing a combination of actions on Uniswap v4.
/// @dev Suggested uint256 action values are defined in Actions.sol, however any definition can be used
Expand Down Expand Up @@ -57,9 +57,9 @@ abstract contract BaseActionsRouter is SafeCallback {

/// @notice Calculates the address for a action
function _mapRecipient(address recipient) internal view returns (address) {
if (recipient == Constants.MSG_SENDER) {
if (recipient == ActionConstants.MSG_SENDER) {
return msgSender();
} else if (recipient == Constants.ADDRESS_THIS) {
} else if (recipient == ActionConstants.ADDRESS_THIS) {
return address(this);
} else {
return recipient;
Expand Down
12 changes: 6 additions & 6 deletions src/base/DeltaResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {TransientStateLibrary} from "@uniswap/v4-core/src/libraries/TransientSta
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {ImmutableState} from "./ImmutableState.sol";
import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol";
import {Constants} from "../libraries/Constants.sol";
import {ActionConstants} from "../libraries/ActionConstants.sol";

/// @notice Abstract contract used to sync, send, and settle funds to the pool manager
/// @dev Note that sync() is called before any erc-20 transfer in `settle`.
Expand Down Expand Up @@ -71,17 +71,17 @@ abstract contract DeltaResolver is ImmutableState {

/// @notice Calculates the amount for a settle action
function _mapSettleAmount(uint256 amount, Currency currency) internal view returns (uint256) {
if (amount == Constants.CONTRACT_BALANCE) {
if (amount == ActionConstants.CONTRACT_BALANCE) {
return currency.balanceOfSelf();
} else if (amount == Constants.OPEN_DELTA) {
} else if (amount == ActionConstants.OPEN_DELTA) {
return _getFullDebt(currency);
}
return amount;
}

/// @notice Calculates the amount for a take action
function _mapTakeAmount(uint256 amount, Currency currency) internal view returns (uint256) {
if (amount == Constants.OPEN_DELTA) {
if (amount == ActionConstants.OPEN_DELTA) {
return _getFullCredit(currency);
}
return amount;
Expand All @@ -93,9 +93,9 @@ abstract contract DeltaResolver is ImmutableState {
/// For example USDC-v2->DAI-v4->USDT. This intermediate DAI amount could be swapped using CONTRACT_BALANCE
/// or settled using CONTRACT_BALANCE then swapped using OPEN_DELTA.
function _mapInputAmount(uint128 amount, Currency currency) internal view returns (uint128) {
if (amount == Constants.CONTRACT_BALANCE) {
if (amount == ActionConstants.CONTRACT_BALANCE) {
return currency.balanceOfSelf().toUint128();
} else if (amount == Constants.OPEN_DELTA) {
} else if (amount == ActionConstants.OPEN_DELTA) {
return _getFullCredit(currency).toUint128();
}
return amount;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.19;

library Constants {
library ActionConstants {
/// @notice used to signal that an action should use the input value of the open delta on the pool manager
/// or of the balance that the contract holds
uint128 internal constant CONTRACT_BALANCE = 0;
uint128 internal constant OPEN_DELTA = 1;
uint128 internal constant OPEN_DELTA = 0;
/// This value is equivalent to 1<<255, i.e. a singular 1 in the most significant bit.
uint256 internal constant CONTRACT_BALANCE = 0x8000000000000000000000000000000000000000000000000000000000000000;

/// @notice used to signal that the recipient of an action should be the msgSender of address(this)
address internal constant MSG_SENDER = address(1);
Expand Down
6 changes: 3 additions & 3 deletions test/BaseActionsRouter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.24;
import {MockBaseActionsRouter} from "./mocks/MockBaseActionsRouter.sol";
import {Planner, Plan} from "./shared/Planner.sol";
import {Actions} from "../src/libraries/Actions.sol";
import {Constants} from "../src/libraries/Constants.sol";
import {ActionConstants} from "../src/libraries/ActionConstants.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {Test} from "forge-std/Test.sol";
import {Deployers} from "@uniswap/v4-core/test/utils/Deployers.sol";
Expand Down Expand Up @@ -141,9 +141,9 @@ contract BaseActionsRouterTest is Test, Deployers, GasSnapshot {

function test_fuzz_mapRecipient(address recipient) public view {
address mappedRecipient = router.mapRecipient(recipient);
if (recipient == Constants.MSG_SENDER) {
if (recipient == ActionConstants.MSG_SENDER) {
assertEq(mappedRecipient, address(0xdeadbeef));
} else if (recipient == Constants.ADDRESS_THIS) {
} else if (recipient == ActionConstants.ADDRESS_THIS) {
assertEq(mappedRecipient, address(router));
} else {
assertEq(mappedRecipient, recipient);
Expand Down
22 changes: 16 additions & 6 deletions test/position-managers/Execute.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {IERC20} from "forge-std/interfaces/IERC20.sol";
import {IPositionManager} from "../../src/interfaces/IPositionManager.sol";
import {PositionManager} from "../../src/PositionManager.sol";
import {PositionConfig} from "../../src/libraries/PositionConfig.sol";
import {Constants} from "../../src/libraries/Constants.sol";
import {ActionConstants} from "../../src/libraries/ActionConstants.sol";
import {Actions} from "../../src/libraries/Actions.sol";

import {LiquidityFuzzers} from "../shared/fuzz/LiquidityFuzzers.sol";
Expand Down Expand Up @@ -68,7 +68,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers {
initialLiquidity = bound(initialLiquidity, 1e18, 1000e18);
liquidityToAdd = bound(liquidityToAdd, 1e18, 1000e18);
uint256 tokenId = lpm.nextTokenId();
mint(config, initialLiquidity, Constants.MSG_SENDER, ZERO_BYTES);
mint(config, initialLiquidity, ActionConstants.MSG_SENDER, ZERO_BYTES);

increaseLiquidity(tokenId, config, liquidityToAdd, ZERO_BYTES);

Expand All @@ -88,7 +88,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers {
liquidityToAdd = bound(liquidityToAdd, 1e18, 1000e18);
liquidityToAdd2 = bound(liquidityToAdd2, 1e18, 1000e18);
uint256 tokenId = lpm.nextTokenId();
mint(config, initialLiquidity, Constants.MSG_SENDER, ZERO_BYTES);
mint(config, initialLiquidity, ActionConstants.MSG_SENDER, ZERO_BYTES);

Plan memory planner = Planner.init();

Expand Down Expand Up @@ -155,7 +155,12 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers {
planner.add(
Actions.MINT_POSITION,
abi.encode(
config, initialLiquidity, MAX_SLIPPAGE_INCREASE, MAX_SLIPPAGE_INCREASE, Constants.MSG_SENDER, ZERO_BYTES
config,
initialLiquidity,
MAX_SLIPPAGE_INCREASE,
MAX_SLIPPAGE_INCREASE,
ActionConstants.MSG_SENDER,
ZERO_BYTES
)
);
planner.add(
Expand All @@ -179,7 +184,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers {

// mint a position on range [-300, 300]
uint256 tokenId = lpm.nextTokenId();
mint(config, initialLiquidity, Constants.MSG_SENDER, ZERO_BYTES);
mint(config, initialLiquidity, ActionConstants.MSG_SENDER, ZERO_BYTES);
BalanceDelta delta = getLastDelta();

// we'll burn and mint a new position on [-60, 60]; calculate the liquidity units for the new range
Expand Down Expand Up @@ -207,7 +212,12 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers {
planner.add(
Actions.MINT_POSITION,
abi.encode(
newConfig, newLiquidity, MAX_SLIPPAGE_INCREASE, MAX_SLIPPAGE_INCREASE, Constants.MSG_SENDER, ZERO_BYTES
newConfig,
newLiquidity,
MAX_SLIPPAGE_INCREASE,
MAX_SLIPPAGE_INCREASE,
ActionConstants.MSG_SENDER,
ZERO_BYTES
)
);
bytes memory calls = planner.finalizeModifyLiquidityWithClose(config.poolKey);
Expand Down
26 changes: 13 additions & 13 deletions test/position-managers/IncreaseLiquidity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {Actions} from "../../src/libraries/Actions.sol";
import {Planner, Plan} from "../shared/Planner.sol";
import {FeeMath} from "../shared/FeeMath.sol";
import {PosmTestSetup} from "../shared/PosmTestSetup.sol";
import {Constants} from "../../src/libraries/Constants.sol";
import {ActionConstants} from "../../src/libraries/ActionConstants.sol";

contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {
using FixedPointMathLib for uint256;
Expand Down Expand Up @@ -492,7 +492,7 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {
function test_increaseLiquidity_slippage_revertAmount0() public {
// increasing liquidity with strict slippage parameters (amount0) will revert
uint256 tokenId = lpm.nextTokenId();
mint(config, 100e18, Constants.MSG_SENDER, ZERO_BYTES);
mint(config, 100e18, ActionConstants.MSG_SENDER, ZERO_BYTES);

// revert since amount0Max is too low
bytes memory calls = getIncreaseEncoded(tokenId, config, 100e18, 1 wei, type(uint128).max, ZERO_BYTES);
Expand All @@ -503,7 +503,7 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {
function test_increaseLiquidity_slippage_revertAmount1() public {
// increasing liquidity with strict slippage parameters (amount1) will revert
uint256 tokenId = lpm.nextTokenId();
mint(config, 100e18, Constants.MSG_SENDER, ZERO_BYTES);
mint(config, 100e18, ActionConstants.MSG_SENDER, ZERO_BYTES);

// revert since amount1Max is too low
bytes memory calls = getIncreaseEncoded(tokenId, config, 100e18, type(uint128).max, 1 wei, ZERO_BYTES);
Expand All @@ -514,7 +514,7 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {
function test_increaseLiquidity_slippage_exactDoesNotRevert() public {
// increasing liquidity with perfect slippage parameters does not revert
uint256 tokenId = lpm.nextTokenId();
mint(config, 100e18, Constants.MSG_SENDER, ZERO_BYTES);
mint(config, 100e18, ActionConstants.MSG_SENDER, ZERO_BYTES);

uint128 newLiquidity = 10e18;
(uint256 amount0, uint256 amount1) = LiquidityAmounts.getAmountsForLiquidity(
Expand All @@ -539,7 +539,7 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {
function test_increaseLiquidity_slippage_revert_swap() public {
// increasing liquidity with perfect slippage parameters does not revert
uint256 tokenId = lpm.nextTokenId();
mint(config, 100e18, Constants.MSG_SENDER, ZERO_BYTES);
mint(config, 100e18, ActionConstants.MSG_SENDER, ZERO_BYTES);

uint128 newLiquidity = 10e18;
(uint256 amount0, uint256 amount1) = LiquidityAmounts.getAmountsForLiquidity(
Expand Down Expand Up @@ -567,8 +567,8 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {
Actions.MINT_POSITION,
abi.encode(config, liquidityAlice, MAX_SLIPPAGE_INCREASE, MAX_SLIPPAGE_INCREASE, alice, ZERO_BYTES)
);
planner.add(Actions.SETTLE, abi.encode(currency0, Constants.OPEN_DELTA, false));
planner.add(Actions.SETTLE, abi.encode(currency1, Constants.OPEN_DELTA, false));
planner.add(Actions.SETTLE, abi.encode(currency0, ActionConstants.OPEN_DELTA, false));
planner.add(Actions.SETTLE, abi.encode(currency1, ActionConstants.OPEN_DELTA, false));
// this test sweeps to the test contract, even though Alice is the caller of the transaction
planner.add(Actions.SWEEP, abi.encode(currency0, address(this)));
planner.add(Actions.SWEEP, abi.encode(currency1, address(this)));
Expand Down Expand Up @@ -609,10 +609,10 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {
Actions.MINT_POSITION,
abi.encode(config, liquidityAlice, MAX_SLIPPAGE_INCREASE, MAX_SLIPPAGE_INCREASE, alice, ZERO_BYTES)
);
planner.add(Actions.SETTLE, abi.encode(currency0, Constants.OPEN_DELTA, false));
planner.add(Actions.SETTLE, abi.encode(currency1, Constants.OPEN_DELTA, false));
planner.add(Actions.SWEEP, abi.encode(currency0, Constants.MSG_SENDER));
planner.add(Actions.SWEEP, abi.encode(currency1, Constants.MSG_SENDER));
planner.add(Actions.SETTLE, abi.encode(currency0, ActionConstants.OPEN_DELTA, false));
planner.add(Actions.SETTLE, abi.encode(currency1, ActionConstants.OPEN_DELTA, false));
planner.add(Actions.SWEEP, abi.encode(currency0, ActionConstants.MSG_SENDER));
planner.add(Actions.SWEEP, abi.encode(currency1, ActionConstants.MSG_SENDER));

uint256 balanceBefore0 = currency0.balanceOf(alice);
uint256 balanceBefore1 = currency1.balanceOf(alice);
Expand Down Expand Up @@ -656,8 +656,8 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {
Actions.INCREASE_LIQUIDITY,
abi.encode(tokenIdAlice, config, liquidityAlice, MAX_SLIPPAGE_INCREASE, MAX_SLIPPAGE_INCREASE, ZERO_BYTES)
);
planner.add(Actions.SETTLE, abi.encode(currency0, Constants.OPEN_DELTA, false));
planner.add(Actions.SETTLE, abi.encode(currency1, Constants.OPEN_DELTA, false));
planner.add(Actions.SETTLE, abi.encode(currency0, ActionConstants.OPEN_DELTA, false));
planner.add(Actions.SETTLE, abi.encode(currency1, ActionConstants.OPEN_DELTA, false));
// this test sweeps to the test contract, even though Alice is the caller of the transaction
planner.add(Actions.SWEEP, abi.encode(currency0, address(this)));
planner.add(Actions.SWEEP, abi.encode(currency1, address(this)));
Expand Down
Loading
Loading