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

Support arbitrary bytes on test routers #343

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
968c345
overload PoolSwapTest.swap() with support for arbitrary bytes
saucepoint Aug 25, 2023
870b588
forge fmt
saucepoint Aug 25, 2023
8e65fdf
consolidate logic to one function
saucepoint Aug 25, 2023
bb5ae64
overload routertest.modifyPosition() with support for arbitrary bytes
saucepoint Aug 25, 2023
c304834
overload PoolDonateTest.donate() with support for arbitrary bytes
saucepoint Aug 25, 2023
b45318b
overload test helper with support for arbitrary bytes
saucepoint Aug 25, 2023
bb48e13
define storage variables to verify arbitrary calldata
saucepoint Aug 25, 2023
1d53214
test that arbitrary calldata provided through routers are being captu…
saucepoint Aug 25, 2023
9a67aaa
forge fmt
saucepoint Aug 25, 2023
8c65e81
resolve conflicts
saucepoint Sep 8, 2023
1347893
forge snapshots
saucepoint Sep 8, 2023
f3623dc
Merge branch 'main' into router-bytes
saucepoint Sep 12, 2023
e04f2c2
remove overloaded modifyPosition(); consolidate PoolModifyPositionTes…
saucepoint Sep 12, 2023
9f08118
remove overloaded swapRouter.swap() and consolidate to signature with…
saucepoint Sep 12, 2023
1346029
Merge pull request #1 from saucepoint/consolidate
saucepoint Sep 19, 2023
fdef1fa
updated hardhart tests with the new router function signatures; updat…
saucepoint Sep 19, 2023
298e7bb
function visibility
saucepoint Sep 21, 2023
8b66c7b
remove overloaded PoolDonateTest.donate(), updated tests with new fun…
saucepoint Sep 21, 2023
40122d5
provide empty bytes on donate; yarn prettier
saucepoint Sep 26, 2023
079693d
recalc gas snapshots
saucepoint Sep 26, 2023
ba7bdd7
misc
saucepoint Sep 26, 2023
9d8ce09
forge fmt
saucepoint Sep 26, 2023
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
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 @@
94397
96005
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 @@
151710
153358
2 changes: 1 addition & 1 deletion .forge-snapshots/mint with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
300485
307152
2 changes: 1 addition & 1 deletion .forge-snapshots/mint with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
273557
275102
2 changes: 1 addition & 1 deletion .forge-snapshots/mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
292221
293760
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
48050
49720
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123316
124982
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
108023
109692
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 @@
89869
91502
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
48025
49691
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
48050
49720
61 changes: 37 additions & 24 deletions contracts/test/MockHooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,48 @@ contract MockHooks is IHooks, IHookFeeManager {
using PoolIdLibrary for PoolKey;
using Hooks for IHooks;

bytes public beforeInitializeData;
bytes public afterInitializeData;
bytes public beforeModifyPositionData;
bytes public afterModifyPositionData;
bytes public beforeSwapData;
bytes public afterSwapData;
bytes public beforeDonateData;
bytes public afterDonateData;

mapping(bytes4 => bytes4) public returnValues;

mapping(PoolId => uint16) public swapFees;

mapping(PoolId => uint16) public withdrawFees;

function beforeInitialize(address, PoolKey calldata, uint160, bytes calldata)
function beforeInitialize(address, PoolKey calldata, uint160, bytes calldata hookData)
external
view
override
returns (bytes4)
{
beforeInitializeData = hookData;
bytes4 selector = MockHooks.beforeInitialize.selector;
return returnValues[selector] == bytes4(0) ? selector : returnValues[selector];
}

function afterInitialize(address, PoolKey calldata, uint160, int24, bytes calldata)
function afterInitialize(address, PoolKey calldata, uint160, int24, bytes calldata hookData)
external
view
override
returns (bytes4)
{
afterInitializeData = hookData;
bytes4 selector = MockHooks.afterInitialize.selector;
return returnValues[selector] == bytes4(0) ? selector : returnValues[selector];
}

function beforeModifyPosition(address, PoolKey calldata, IPoolManager.ModifyPositionParams calldata, bytes calldata)
external
view
override
returns (bytes4)
{
function beforeModifyPosition(
address,
PoolKey calldata,
IPoolManager.ModifyPositionParams calldata,
bytes calldata hookData
) external override returns (bytes4) {
beforeModifyPositionData = hookData;
bytes4 selector = MockHooks.beforeModifyPosition.selector;
return returnValues[selector] == bytes4(0) ? selector : returnValues[selector];
}
Expand All @@ -54,48 +64,51 @@ contract MockHooks is IHooks, IHookFeeManager {
PoolKey calldata,
IPoolManager.ModifyPositionParams calldata,
BalanceDelta,
bytes calldata
) external view override returns (bytes4) {
bytes calldata hookData
) external override returns (bytes4) {
afterModifyPositionData = hookData;
bytes4 selector = MockHooks.afterModifyPosition.selector;
return returnValues[selector] == bytes4(0) ? selector : returnValues[selector];
}

function beforeSwap(address, PoolKey calldata, IPoolManager.SwapParams calldata, bytes calldata)
function beforeSwap(address, PoolKey calldata, IPoolManager.SwapParams calldata, bytes calldata hookData)
external
view
override
returns (bytes4)
{
beforeSwapData = hookData;
bytes4 selector = MockHooks.beforeSwap.selector;
return returnValues[selector] == bytes4(0) ? selector : returnValues[selector];
}

function afterSwap(address, PoolKey calldata, IPoolManager.SwapParams calldata, BalanceDelta, bytes calldata)
external
view
override
returns (bytes4)
{
function afterSwap(
address,
PoolKey calldata,
IPoolManager.SwapParams calldata,
BalanceDelta,
bytes calldata hookData
) external override returns (bytes4) {
afterSwapData = hookData;
bytes4 selector = MockHooks.afterSwap.selector;
return returnValues[selector] == bytes4(0) ? selector : returnValues[selector];
}

function beforeDonate(address, PoolKey calldata, uint256, uint256, bytes calldata)
function beforeDonate(address, PoolKey calldata, uint256, uint256, bytes calldata hookData)
external
view
override
returns (bytes4)
{
beforeDonateData = hookData;
bytes4 selector = MockHooks.beforeDonate.selector;
return returnValues[selector] == bytes4(0) ? selector : returnValues[selector];
}

function afterDonate(address, PoolKey calldata, uint256, uint256, bytes calldata)
function afterDonate(address, PoolKey calldata, uint256, uint256, bytes calldata hookData)
external
view
override
returns (bytes4)
{
afterDonateData = hookData;
bytes4 selector = MockHooks.afterDonate.selector;
return returnValues[selector] == bytes4(0) ? selector : returnValues[selector];
}
Expand Down
9 changes: 6 additions & 3 deletions contracts/test/PoolDonateTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ contract PoolDonateTest is ILockCallback {
PoolKey key;
uint256 amount0;
uint256 amount1;
bytes hookData;
}

function donate(PoolKey memory key, uint256 amount0, uint256 amount1)
function donate(PoolKey memory key, uint256 amount0, uint256 amount1, bytes memory hookData)
external
payable
returns (BalanceDelta delta)
{
delta = abi.decode(manager.lock(abi.encode(CallbackData(msg.sender, key, amount0, amount1))), (BalanceDelta));
delta = abi.decode(
manager.lock(abi.encode(CallbackData(msg.sender, key, amount0, amount1, hookData))), (BalanceDelta)
);

uint256 ethBalance = address(this).balance;
if (ethBalance > 0) {
Expand All @@ -44,7 +47,7 @@ contract PoolDonateTest is ILockCallback {

CallbackData memory data = abi.decode(rawData, (CallbackData));

BalanceDelta delta = manager.donate(data.key, data.amount0, data.amount1, new bytes(0));
BalanceDelta delta = manager.donate(data.key, data.amount0, data.amount1, data.hookData);

if (delta.amount0() > 0) {
if (data.key.currency0.isNative()) {
Expand Down
7 changes: 4 additions & 3 deletions contracts/test/PoolModifyPositionTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ contract PoolModifyPositionTest is ILockCallback {
address sender;
PoolKey key;
IPoolManager.ModifyPositionParams params;
bytes hookData;
}

function modifyPosition(PoolKey memory key, IPoolManager.ModifyPositionParams memory params)
function modifyPosition(PoolKey memory key, IPoolManager.ModifyPositionParams memory params, bytes memory hookData)
external
payable
returns (BalanceDelta delta)
{
delta = abi.decode(manager.lock(abi.encode(CallbackData(msg.sender, key, params))), (BalanceDelta));
delta = abi.decode(manager.lock(abi.encode(CallbackData(msg.sender, key, params, hookData))), (BalanceDelta));

uint256 ethBalance = address(this).balance;
if (ethBalance > 0) {
Expand All @@ -42,7 +43,7 @@ contract PoolModifyPositionTest is ILockCallback {

CallbackData memory data = abi.decode(rawData, (CallbackData));

BalanceDelta delta = manager.modifyPosition(data.key, data.params, new bytes(0));
BalanceDelta delta = manager.modifyPosition(data.key, data.params, data.hookData);

if (delta.amount0() > 0) {
if (data.key.currency0.isNative()) {
Expand Down
23 changes: 12 additions & 11 deletions contracts/test/PoolSwapTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,34 @@ contract PoolSwapTest is ILockCallback {
TestSettings testSettings;
PoolKey key;
IPoolManager.SwapParams params;
bytes hookData;
}

struct TestSettings {
bool withdrawTokens;
bool settleUsingTransfer;
}

function swap(PoolKey memory key, IPoolManager.SwapParams memory params, TestSettings memory testSettings)
external
payable
returns (BalanceDelta delta)
{
delta =
abi.decode(manager.lock(abi.encode(CallbackData(msg.sender, testSettings, key, params))), (BalanceDelta));
function swap(
PoolKey memory key,
IPoolManager.SwapParams memory params,
TestSettings memory testSettings,
bytes memory hookData
) external payable returns (BalanceDelta delta) {
delta = abi.decode(
manager.lock(abi.encode(CallbackData(msg.sender, testSettings, key, params, hookData))), (BalanceDelta)
);

uint256 ethBalance = address(this).balance;
if (ethBalance > 0) {
CurrencyLibrary.NATIVE.transfer(msg.sender, ethBalance);
}
if (ethBalance > 0) CurrencyLibrary.NATIVE.transfer(msg.sender, ethBalance);
}

function lockAcquired(bytes calldata rawData) external returns (bytes memory) {
require(msg.sender == address(manager));

CallbackData memory data = abi.decode(rawData, (CallbackData));

BalanceDelta delta = manager.swap(data.key, data.params, new bytes(0));
BalanceDelta delta = manager.swap(data.key, data.params, data.hookData);

if (data.params.zeroForOne) {
if (delta.amount0() > 0) {
Expand Down
33 changes: 22 additions & 11 deletions test/PoolManager.gas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ describe('PoolManager gas tests', () => {
{
withdrawTokens: true,
settleUsingTransfer: true,
}
},
'0x00'
)
}
const swapToHigherPrice: SwapToPriceFunction = (sqrtPriceX96, to) => {
Expand All @@ -104,7 +105,8 @@ describe('PoolManager gas tests', () => {
{
withdrawTokens: true,
settleUsingTransfer: true,
}
},
'0x00'
)
}
const swapToLowerPrice: SwapToPriceFunction = (sqrtPriceX96, to) => {
Expand All @@ -118,18 +120,23 @@ describe('PoolManager gas tests', () => {
{
withdrawTokens: true,
settleUsingTransfer: true,
}
},
'0x00'
)
}
const modifyPosition: ModifyPositionFunction = (tickLower, tickUpper, liquidityDelta) => {
return modifyPositionTest.modifyPosition(poolKey, {
tickLower,
tickUpper,
liquidityDelta,
})
return modifyPositionTest.modifyPosition(
poolKey,
{
tickLower,
tickUpper,
liquidityDelta,
},
'0x00'
)
}
const donate: DonateFunction = (amount0, amount1) => {
return donateTest.donate(poolKey, amount0, amount1)
return donateTest.donate(poolKey, amount0, amount1, '0x00')
}
const getSlot0 = async () => {
return await manager.getSlot0(getPoolId(poolKey))
Expand Down Expand Up @@ -373,6 +380,7 @@ describe('PoolManager gas tests', () => {
withdrawTokens: true,
settleUsingTransfer: true,
},
'0x00',
{
value: amount,
}
Expand All @@ -389,7 +397,8 @@ describe('PoolManager gas tests', () => {
{
withdrawTokens: true,
settleUsingTransfer: true,
}
},
'0x00'
)
}
const swapToLowerPrice: SwapToPriceFunction = (sqrtPriceX96, to) => {
Expand All @@ -404,6 +413,7 @@ describe('PoolManager gas tests', () => {
withdrawTokens: true,
settleUsingTransfer: true,
},
'0x00',
{
value: MaxUint128,
}
Expand All @@ -417,11 +427,12 @@ describe('PoolManager gas tests', () => {
tickUpper,
liquidityDelta,
},
'0x00',
{ value: liquidityDelta }
)
}
const donate: DonateFunction = (amount0, amount1) => {
return donateTest.donate(poolKey, amount0, amount1, { value: amount0 })
return donateTest.donate(poolKey, amount0, amount1, '0x00', { value: amount0 })
}
const getSlot0 = async () => {
return await manager.getSlot0(getPoolId(poolKey))
Expand Down
Loading