-
Notifications
You must be signed in to change notification settings - Fork 45
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]: example if we remove share to 0 if only lockup shares are left #217
Draft
chefburger
wants to merge
10
commits into
main
Choose a base branch
from
feat/hexen-r7-option-2-optimise-beta
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0c924e1
feat: example of option 2
ChefMist a12fab0
feat: fix all existing test cases with min share burned
ChefMist 157c5f5
test: fix remaining tests
ChefMist 3b570d0
feat: clean up more test
ChefMist 8852b35
test: format test code
ChefMist 7a84bc1
Merge branch 'main' into feat/hexen-r7-option-2
ChefMist c921baf
feat: include updated gas cost
ChefMist 19ecc15
feat: update IBinPoolManager doc per PR
ChefMist e7bf244
feat: example if we remove share to 0 if only lockup shares are left
chefburger acbc781
fix: adjust per comments
chefburger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
178130 | ||
233208 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
311254 | ||
311574 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
170145 | ||
221462 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
410336 | ||
410526 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
23287 | ||
23645 |
2 changes: 1 addition & 1 deletion
2
.forge-snapshots/BinPoolManagerTest#testBurnNativeCurrency.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
133892 | ||
183557 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
142717 | ||
143406 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
289683 | ||
330965 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
127065 | ||
175023 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
968475 | ||
971323 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
327787 | ||
330068 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
337511 | ||
337831 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
140062 | ||
140319 |
2 changes: 1 addition & 1 deletion
2
.forge-snapshots/BinPoolManagerTest#testMintNativeCurrency.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
304550 | ||
304870 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,10 @@ library BinPool { | |
mapping(bytes32 => bytes32) level2; | ||
} | ||
|
||
/// @dev when a bin has supply for the first time, 1e3 share will be locked up | ||
/// this is to prevent share inflation attack on BinPool type | ||
uint256 constant MINIMUM_SHARE = 1e3; | ||
|
||
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(); | ||
|
@@ -294,7 +298,7 @@ library BinPool { | |
/// @return result the delta of the token balance of the pool | ||
function burn(State storage self, BurnParams memory params) | ||
internal | ||
returns (BalanceDelta result, uint256[] memory ids, bytes32[] memory amounts) | ||
returns (BalanceDelta result, uint256[] memory ids, bytes32[] memory amounts, bytes32 feeAmountToProtocol) | ||
{ | ||
ids = params.ids; | ||
uint256 idsLength = ids.length; | ||
|
@@ -313,15 +317,26 @@ library BinPool { | |
bytes32 binReserves = self.reserveOfBin[id]; | ||
uint256 supply = self.shareOfBin[id]; | ||
|
||
_subShare(self, params.from, id, params.salt, amountToBurn); | ||
|
||
bytes32 amountsOutFromBin = binReserves.getAmountOutOfBin(amountToBurn, supply); | ||
|
||
if (amountsOutFromBin == 0) revert BinPool__ZeroAmountsOut(id); | ||
|
||
binReserves = binReserves.sub(amountsOutFromBin); | ||
|
||
if (supply == amountToBurn) _removeBinIdToTree(self, id); | ||
/// @notice if user is last lp for this bin, amountToBurn would be amountToBurn + MINIMUM_SHARE | ||
amountToBurn = _subShare(self, params.from, id, params.salt, amountToBurn); | ||
|
||
bytes32 amountsOutFromBin; | ||
/// @dev if user is last lp for this bin, supply == amountToBurn == amountsToBurn[i] + MINIMUM_SHARE | ||
/// then we will remove all the liqudity from the bin where MINIMUM_SHARE will be withdrawn as protocol fee | ||
/// to prevent MINIMUM_SHARE from being locked up in the bin which cause extra gas cost when swap | ||
if (supply == amountToBurn) { | ||
_removeBinIdToTree(self, id); | ||
|
||
/// @notice withdraw all the liquidity from the bin, the locked up share will be withdrawn as protocol fee | ||
/// @dev we can't use "binReserves.getAmountOutOfBin(MINIMUM_SHARE, supply)" to calc the fee as it will round down to 0 | ||
amountsOutFromBin = binReserves.getAmountOutOfBin(amountToBurn - MINIMUM_SHARE, supply); | ||
feeAmountToProtocol = feeAmountToProtocol.add(binReserves.sub(amountsOutFromBin)); | ||
binReserves = 0; | ||
} else { | ||
amountsOutFromBin = binReserves.getAmountOutOfBin(amountToBurn, supply); | ||
binReserves = binReserves.sub(amountsOutFromBin); | ||
} | ||
if (amountsOutFromBin.add(feeAmountToProtocol) == 0) revert BinPool__ZeroAmountsOut(id); | ||
|
||
self.reserveOfBin[id] = binReserves; | ||
amounts[i] = amountsOutFromBin; | ||
|
@@ -385,12 +400,12 @@ library BinPool { | |
amountsLeft = amountsLeft.sub(amountsIn); | ||
feeAmountToProtocol = feeAmountToProtocol.add(binFeeAmt); | ||
|
||
shares = _addShare(self, params.to, id, params.salt, shares); | ||
|
||
arrays.ids[i] = id; | ||
arrays.amounts[i] = amountsInToBin; | ||
arrays.liquidityMinted[i] = shares; | ||
|
||
_addShare(self, params.to, id, params.salt, shares); | ||
|
||
compositionFeeAmount = compositionFeeAmount.add(binCompositionFee); | ||
|
||
unchecked { | ||
|
@@ -461,14 +476,35 @@ library BinPool { | |
} | ||
|
||
/// @notice Subtract share from user's position and update total share supply of bin | ||
function _subShare(State storage self, address owner, uint24 binId, bytes32 salt, uint256 shares) internal { | ||
/// @param shares - amount of share to deduct specified by user | ||
/// @return amountsToBurn amount of share burned, nornally equals to shares except when bin is left with MINIMUM_SHARE, amountsToBurn will be share + MINIMUM_SHARE | ||
function _subShare(State storage self, address owner, uint24 binId, bytes32 salt, uint256 shares) | ||
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. need comment on this -- so other devs can understand the logic below easily 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. eg.
|
||
internal | ||
returns (uint256 amountsToBurn) | ||
{ | ||
self.positions.get(owner, binId, salt).subShare(shares); | ||
self.shareOfBin[binId] -= shares; | ||
amountsToBurn = shares; | ||
if (self.shareOfBin[binId] - shares == MINIMUM_SHARE) { | ||
amountsToBurn += MINIMUM_SHARE; | ||
} | ||
self.shareOfBin[binId] -= amountsToBurn; | ||
} | ||
|
||
/// @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); | ||
/// @dev if bin is empty, deduct MINIMUM_SHARE from shares | ||
/// @return userShareAdded The amount of share added to user's position | ||
function _addShare(State storage self, address owner, uint24 binId, bytes32 salt, uint256 shares) | ||
internal | ||
returns (uint256 userShareAdded) | ||
{ | ||
userShareAdded = shares; | ||
if (self.shareOfBin[binId] == 0) { | ||
/// @dev Only for first liquidity provider for the bin, deduct MINIMUM_SHARE, expected to underflow | ||
/// if shares < MINIMUM_SHARE for first mint | ||
userShareAdded = shares - MINIMUM_SHARE; | ||
} | ||
|
||
self.positions.get(owner, binId, salt).addShare(userShareAdded); | ||
self.shareOfBin[binId] += shares; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.