-
Notifications
You must be signed in to change notification settings - Fork 47
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
POC/ hexen r7 option 1 optimization #214
base: main
Are you sure you want to change the base?
Changes from 6 commits
a761527
8f2cdda
aa9ae77
87906ea
3d0d39a
a03e4f9
24212b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
178130 | ||
178922 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
311254 | ||
311556 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
170145 | ||
170937 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
410336 | ||
410638 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
23287 | ||
23612 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
133892 | ||
134526 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
142717 | ||
143512 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
289683 | ||
295386 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
127065 | ||
127698 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
968475 | ||
971193 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
327787 | ||
330505 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
337511 | ||
337813 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
140062 | ||
140364 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
304550 | ||
304852 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ library BinPool { | |
error BinPool__NoLiquidityToReceiveFees(); | ||
/// @dev if swap exactIn, x for y, unspecifiedToken = token y. if swap x for exact out y, unspecified token is x | ||
error BinPool__InsufficientAmountUnSpecified(); | ||
error BinPool__UserPositionBelowMinimumShare(uint256 balanceShare); | ||
|
||
/// @dev The state of a pool | ||
struct State { | ||
|
@@ -66,6 +67,9 @@ library BinPool { | |
mapping(bytes32 => bytes32) level2; | ||
} | ||
|
||
/// @dev when liquidity is removed, ensure there is either greater than min_share or 0 liquidity left | ||
uint256 constant MINIMUM_SHARE = 1e9; | ||
|
||
function initialize(State storage self, uint24 activeId, uint24 protocolFee, uint24 lpFee) internal { | ||
/// An initialized pool will not have activeId: 0 | ||
if (self.slot0.activeId() != 0) revert PoolAlreadyInitialized(); | ||
|
@@ -313,6 +317,12 @@ library BinPool { | |
bytes32 binReserves = self.reserveOfBin[id]; | ||
uint256 supply = self.shareOfBin[id]; | ||
|
||
uint256 userTotalShare = self.positions.get(params.from, id, params.salt).share; | ||
/// @dev Remove all shares if the remaining shares fall below the minimum threshold | ||
if (amountToBurn > userTotalShare - MINIMUM_SHARE) { | ||
amountToBurn = userTotalShare; | ||
} | ||
|
||
_subShare(self, params.from, id, params.salt, amountToBurn); | ||
|
||
bytes32 amountsOutFromBin = binReserves.getAmountOutOfBin(amountToBurn, supply); | ||
|
@@ -464,12 +474,24 @@ library BinPool { | |
function _subShare(State storage self, address owner, uint24 binId, bytes32 salt, uint256 shares) internal { | ||
self.positions.get(owner, binId, salt).subShare(shares); | ||
self.shareOfBin[binId] -= shares; | ||
|
||
/// @dev Ensure bin user's position share is either 0 or greater than minimum share | ||
uint256 userPositionShare = self.positions.get(owner, binId, salt).share; | ||
if (userPositionShare > 0 && userPositionShare < MINIMUM_SHARE) { | ||
revert BinPool__UserPositionBelowMinimumShare(userPositionShare); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be better to add this part in BinPosition::addShare, BinPosition::subShare:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok , this is just a POC. will refactor logic if decide to use this. |
||
} | ||
|
||
/// @notice Add share to user's position and update total share supply of bin | ||
function _addShare(State storage self, address owner, uint24 binId, bytes32 salt, uint256 shares) internal { | ||
self.positions.get(owner, binId, salt).addShare(shares); | ||
self.shareOfBin[binId] += shares; | ||
|
||
/// @dev Ensure bin user's position share is either 0 or greater than minimum share | ||
uint256 userPositionShare = self.positions.get(owner, binId, salt).share; | ||
if (userPositionShare < MINIMUM_SHARE) { | ||
revert BinPool__UserPositionBelowMinimumShare(userPositionShare); | ||
} | ||
} | ||
|
||
/// @notice Enable bin id for a pool | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will be sufficient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no , if removed all , userPositionShare will be 0 now , will get revert error