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

feat: Add anti-sniping hook contract #20

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Conversation

ChefCupcake
Copy link

No description provided.

liqParams.tickUpper = params.tickUpper;
liqParams.salt = params.salt;
positionKeyToLiquidityParams[positionKey] = liqParams;
if (positionCreationBlock[poolId][positionKey] != 0) revert PositionAlreadyExists();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So ppl can not add liquidity into an existing position ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can, but it will prevent not be sniping attack by MEV in swap and donate

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how @ChefCupcake ? doesn't this condition blocks user from adding liquidity to the same position?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and what happens if user add liquidity via CLPositionManager?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in raw project it is not allow add, only mint, but i will try to open the restrict, and test if it is ok

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea same question here, I don't think users can still add liquidity to the same position

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, i think the raw project not allow add reason is the hook need to remember the timestamp when the liquidity was created, otherwise the timestamp will be last added, then it will conflict with the lock duration

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChefCupcake does this means user cannot increase liquidity for the same tokenId? (yes or no)?

if the answer is yes, please prep a doc on some suggestions on how we can fix this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the answer is yes, if user increase liquidity, then the timestamp should the last action, the firstBlockFeesToken0 and firstBlockFeesToken1 will be lost, beside as the hook doesn't allow partial remove liquidity, so it will extend the lock duration

returns (bytes4, BeforeSwapDelta, uint24)
{
PoolId poolId = key.toId();
collectLastBlockInfo(poolId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can u add some test cases so that we can compare the gas snapshot ? I am a bit curious that how many gas overhead will this function bring

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, will add testcase later

src/pool-cl/anti-sniping/CLAntiSniping.sol Outdated Show resolved Hide resolved
src/pool-cl/anti-sniping/CLAntiSniping.sol Show resolved Hide resolved
liqParams.tickUpper = params.tickUpper;
liqParams.salt = params.salt;
positionKeyToLiquidityParams[positionKey] = liqParams;
if (positionCreationBlock[poolId][positionKey] != 0) revert PositionAlreadyExists();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how @ChefCupcake ? doesn't this condition blocks user from adding liquidity to the same position?

liqParams.tickUpper = params.tickUpper;
liqParams.salt = params.salt;
positionKeyToLiquidityParams[positionKey] = liqParams;
if (positionCreationBlock[poolId][positionKey] != 0) revert PositionAlreadyExists();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and what happens if user add liquidity via CLPositionManager?

// If the pool is empty, the fees are not donated and are returned to the sender
hookDelta = BalanceDeltaLibrary.ZERO_DELTA;
}
return (this.afterRemoveLiquidity.selector, hookDelta);
Copy link
Collaborator

@ChefMist ChefMist Nov 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason why we are not doing the Cleanup stored data for the position?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question here
maybe it's fine to not clean up if we are assuming that all users will only use position manager to add/removeLiq (position keys will always be different due to the fact that salt=tokenId)
but what if the user is not using position manager?
also as a plus, delete can save gas costs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because only mint and remove all liquidity are allowed, so tokenId will not be repeat in this scenario for extra gas costs,but yes i can add delete in this func

@chef-omelette chef-omelette self-requested a review November 28, 2024 06:36

uint24 constant FEE = 3000;
uint128 constant POSITION_LOCK_DURATION = 1000;
uint128 constant SAME_BLOCK_POSITIONS_LIMIT = 5;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is 5 enough in real world scenario? can you add test cases where different settings applied and let's see the gas snapshot comparison?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I added a testcase to show that no matter the SAME_BLOCK_POSITIONS_LIMIT is, the gas won't cost more because it will delete each time

mapping(PoolId => mapping(bytes32 => uint256)) public positionCreationBlock;

/// @notice The duration (in blocks) for which a position must remain locked before it can be removed.
uint128 public positionLockDuration;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can make this immutable

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, change to immutable

src/pool-cl/anti-sniping/CLAntiSniping.sol Outdated Show resolved Hide resolved
Copy link
Collaborator

@ChefMist ChefMist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add gas snapshot for add, remove liquidity, donate and swap operation?

liqParams.tickLower = params.tickLower;
liqParams.tickUpper = params.tickUpper;
liqParams.salt = params.salt;
positionKeyToLiquidityParams[positionKey] = liqParams;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

liqParams is storage , do not need this line.
positionKeyToLiquidityParams[positionKey] = liqParams;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants