-
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
0c924e1
a12fab0
157c5f5
3b570d0
8852b35
7a84bc1
c921baf
19ecc15
e7bf244
acbc781
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 | ||
179611 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
311254 | ||
311574 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
170145 | ||
167865 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
410336 | ||
410526 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
23287 | ||
23766 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
133892 | ||
135054 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
142717 | ||
143286 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
289683 | ||
297444 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
127065 | ||
128227 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
968475 | ||
971323 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
327787 | ||
330068 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
337511 | ||
337831 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
140062 | ||
140319 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
304550 | ||
304870 |
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,21 @@ library BinPool { | |
bytes32 binReserves = self.reserveOfBin[id]; | ||
uint256 supply = self.shareOfBin[id]; | ||
|
||
_subShare(self, params.from, id, params.salt, amountToBurn); | ||
amountToBurn = _subShare(self, params.from, id, params.salt, amountToBurn); | ||
|
||
bytes32 amountsOutFromBin = binReserves.getAmountOutOfBin(amountToBurn, supply); | ||
if (supply == amountToBurn) feeAmountToProtocol = binReserves.getAmountOutOfBin(MINIMUM_SHARE, supply); | ||
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 to document in code probably on why 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. also can you have look at this implementation? moved the code a bit
|
||
|
||
if (amountsOutFromBin == 0) revert BinPool__ZeroAmountsOut(id); | ||
|
||
binReserves = binReserves.sub(amountsOutFromBin); | ||
|
||
if (supply == amountToBurn) _removeBinIdToTree(self, id); | ||
if (supply == amountToBurn) { | ||
_removeBinIdToTree(self, id); | ||
|
||
/// @notice withdraw all the liquidity from the bin, the locked up share will be withdrawn as protocol fee | ||
amountsOutFromBin.sub(feeAmountToProtocol); | ||
} | ||
|
||
self.reserveOfBin[id] = binReserves; | ||
amounts[i] = amountsOutFromBin; | ||
|
@@ -385,12 +395,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 +471,33 @@ 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 { | ||
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; | ||
} | ||
|
||
|
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.