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

[POC]: example if we remove share to 0 if only lockup shares are left #217

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

chefburger
Copy link
Collaborator

No description provided.

@chefburger
Copy link
Collaborator Author

will further polish the tests once picked


bytes32 amountsOutFromBin = binReserves.getAmountOutOfBin(amountToBurn, supply);
if (supply == amountToBurn) feeAmountToProtocol = binReserves.getAmountOutOfBin(MINIMUM_SHARE, supply);
Copy link
Collaborator

@ChefMist ChefMist Nov 13, 2024

Choose a reason for hiding this comment

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

need to document in code probably on why supply == amountToBurn we'll do this

@@ -465,9 +471,16 @@ 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)
Copy link
Collaborator

@ChefMist ChefMist Nov 13, 2024

Choose a reason for hiding this comment

The 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

Copy link
Collaborator

Choose a reason for hiding this comment

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

eg.

    /// @notice Subtract share from user's position and update total share supply of bin
    /// @param shares - amount of share to deduct 
    /// @return amountsToBurn amount of share burned, if bin is left with MINIMUM_SHARE, amountsToBurn will be share + MINIMUM_SHARE

@@ -317,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);
Copy link
Collaborator

@ChefMist ChefMist Nov 13, 2024

Choose a reason for hiding this comment

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

// 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 = binReserves.getAmountOutOfBin(amountToBurn, supply);
if (supply == amountToBurn) feeAmountToProtocol = binReserves.getAmountOutOfBin(MINIMUM_SHARE, supply);
Copy link
Collaborator

@ChefMist ChefMist Nov 13, 2024

Choose a reason for hiding this comment

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

also feeAmountToProtocol should be += as we are in the loop. so feeAmountToProtocol is being overwritten if we loop twice with such cases.

can you have look at this implementation? moved the code a bit

    /// @notice Burn user's share and withdraw tokens form the pool.
    /// @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, bytes32 feeAmountToProtocol)
    {
        ids = params.ids;
        uint256 idsLength = ids.length;
        uint256[] memory amountsToBurn = params.amountsToBurn;

        if (idsLength == 0 || idsLength != amountsToBurn.length) revert BinPool__InvalidBurnInput();

        bytes32 amountsOut;
        amounts = new bytes32[](idsLength);
        for (uint256 i; i < idsLength;) {
            uint24 id = ids[i].safe24();
            uint256 amountToBurn = amountsToBurn[i];

            if (amountToBurn == 0) revert BinPool__BurnZeroAmount(id);

            bytes32 binReserves = self.reserveOfBin[id];
            uint256 supply = self.shareOfBin[id];

            // 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 = binReserves.getAmountOutOfBin(amountToBurn, supply);
            if (amountsOutFromBin == 0) revert BinPool__ZeroAmountsOut(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
                feeAmountToProtocol = feeAmountToProtocol.add(binReserves.getAmountOutOfBin(MINIMUM_SHARE, supply));
                amountsOutFromBin.sub(feeAmountToProtocol);
            }

            binReserves = binReserves.sub(amountsOutFromBin);

            self.reserveOfBin[id] = binReserves;
            amounts[i] = amountsOutFromBin;
            amountsOut = amountsOut.add(amountsOutFromBin);

            unchecked {
                ++i;
            }
        }

        result = toBalanceDelta(amountsOut.decodeX().safeInt128(), amountsOut.decodeY().safeInt128());
    }

Copy link

github-actions bot commented Nov 13, 2024

File % Lines % Statements % Branches % Funcs
src/Extsload.sol 0.00% (0/15) 0.00% (0/16) 0.00% (0/1) 100.00% (2/2)
src/Owner.sol 0.00% (0/2) 0.00% (0/2) 100.00% (0/0) 100.00% (2/2)
src/ProtocolFees.sol 86.96% (20/23) 90.32% (28/31) 100.00% (5/5) 100.00% (5/5)
src/Vault.sol 95.00% (57/60) 94.05% (79/84) 91.67% (11/12) 90.48% (19/21)
src/VaultToken.sol 85.19% (23/27) 82.86% (29/35) 100.00% (4/4) 87.50% (7/8)
src/base/Ownable.sol 81.82% (9/11) 83.33% (10/12) 50.00% (1/2) 100.00% (6/6)
src/base/Pausable.sol 75.00% (6/8) 75.00% (6/8) 100.00% (1/1) 100.00% (5/5)
src/base/PausableRole.sol 100.00% (5/5) 100.00% (9/9) 100.00% (1/1) 100.00% (3/3)
src/base/PoolManagerOwnable2Step.sol 100.00% (4/4) 100.00% (4/4) 100.00% (1/1) 100.00% (3/3)
src/libraries/BipsLibrary.sol 100.00% (2/2) 100.00% (4/4) 100.00% (1/1) 100.00% (1/1)
src/libraries/CustomRevert.sol 7.69% (1/13) 7.69% (1/13) 100.00% (0/0) 100.00% (1/1)
src/libraries/Hooks.sol 77.27% (17/22) 84.21% (32/38) 83.33% (5/6) 100.00% (5/5)
src/libraries/LPFeeLibrary.sol 77.78% (7/9) 86.67% (13/15) 100.00% (1/1) 100.00% (6/6)
src/libraries/ParseBytes.sol 0.00% (0/3) 0.00% (0/3) 100.00% (0/0) 100.00% (3/3)
src/libraries/ProtocolFeeLibrary.sol 22.22% (2/9) 22.22% (2/9) 100.00% (0/0) 100.00% (4/4)
src/libraries/SafeCast.sol 83.33% (10/12) 61.11% (11/18) 0.00% (0/5) 100.00% (6/6)
src/libraries/SettlementGuard.sol 56.25% (9/16) 68.18% (15/22) 100.00% (4/4) 100.00% (5/5)
src/libraries/VaultAppDeltaSettlement.sol 100.00% (3/3) 100.00% (3/3) 50.00% (1/2) 100.00% (1/1)
src/libraries/VaultReserve.sol 0.00% (0/4) 0.00% (0/4) 100.00% (0/0) 100.00% (2/2)
src/libraries/math/Encoded.sol 0.00% (0/8) 0.00% (0/9) 100.00% (0/0) 85.71% (6/7)
src/libraries/math/ParametersHelper.sol 100.00% (3/3) 100.00% (3/3) 100.00% (1/1) 100.00% (2/2)
src/libraries/math/UnsafeMath.sol 0.00% (0/2) 0.00% (0/2) 100.00% (0/0) 100.00% (2/2)
src/pool-bin/BinPoolManager.sol 97.80% (89/91) 97.27% (107/110) 81.82% (9/11) 92.86% (13/14)
src/pool-bin/BinPoolManagerOwner.sol 100.00% (12/12) 100.00% (13/13) 100.00% (1/1) 100.00% (8/8)
src/pool-bin/libraries/BinHelper.sol 89.66% (78/87) 91.60% (109/119) 75.00% (12/16) 100.00% (9/9)
src/pool-bin/libraries/BinHooks.sol 90.54% (67/74) 91.67% (77/84) 72.73% (16/22) 100.00% (11/11)
src/pool-bin/libraries/BinPool.sol 96.79% (151/156) 96.79% (181/187) 93.75% (30/32) 100.00% (16/16)
src/pool-bin/libraries/BinPoolParametersHelper.sol 100.00% (2/2) 100.00% (3/3) 100.00% (0/0) 100.00% (2/2)
src/pool-bin/libraries/BinPosition.sol 57.14% (4/7) 62.50% (5/8) 100.00% (0/0) 100.00% (4/4)
src/pool-bin/libraries/FeeHelper.sol 100.00% (8/8) 100.00% (12/12) 100.00% (0/0) 100.00% (3/3)
src/pool-bin/libraries/PriceHelper.sol 100.00% (10/10) 100.00% (21/21) 100.00% (0/0) 100.00% (6/6)
src/pool-bin/libraries/math/BitMath.sol 8.77% (5/57) 12.90% (8/62) 0.00% (0/16) 100.00% (4/4)
src/pool-bin/libraries/math/LiquidityConfigurations.sol 84.62% (11/13) 89.47% (17/19) 100.00% (1/1) 100.00% (3/3)
src/pool-bin/libraries/math/PackedUint128Math.sol 72.50% (29/40) 79.69% (51/64) 100.00% (10/10) 86.67% (13/15)
src/pool-bin/libraries/math/SafeCast.sol 94.29% (33/35) 49.25% (33/67) 0.00% (0/32) 100.00% (33/33)
src/pool-bin/libraries/math/TreeMath.sol 100.00% (82/82) 100.00% (101/101) 100.00% (16/16) 100.00% (7/7)
src/pool-bin/libraries/math/Uint128x128Math.sol 34.72% (25/72) 32.00% (32/100) 20.69% (6/29) 100.00% (2/2)
src/pool-bin/libraries/math/Uint256x256Math.sol 78.05% (32/41) 84.21% (48/57) 100.00% (9/9) 100.00% (8/8)
src/pool-bin/types/BinSlot0.sol 0.00% (0/6) 0.00% (0/6) 100.00% (0/0) 100.00% (6/6)
src/pool-cl/CLPoolManager.sol 96.92% (63/65) 93.10% (81/87) 75.00% (6/8) 85.71% (12/14)
src/pool-cl/CLPoolManagerOwner.sol 100.00% (9/9) 100.00% (9/9) 100.00% (0/0) 100.00% (6/6)
src/pool-cl/libraries/BitMath.sol 18.18% (2/11) 18.18% (2/11) 0.00% (0/4) 100.00% (2/2)
src/pool-cl/libraries/CLHooks.sol 91.30% (63/69) 93.75% (75/80) 78.26% (18/23) 100.00% (9/9)
src/pool-cl/libraries/CLPool.sol 93.58% (102/109) 94.02% (110/117) 91.67% (33/36) 100.00% (8/8)
src/pool-cl/libraries/CLPoolGetters.sol 100.00% (3/3) 100.00% (3/3) 100.00% (0/0) 100.00% (3/3)
src/pool-cl/libraries/CLPoolParametersHelper.sol 100.00% (2/2) 100.00% (3/3) 100.00% (0/0) 100.00% (2/2)
src/pool-cl/libraries/CLPosition.sol 81.25% (13/16) 84.21% (16/19) 100.00% (4/4) 100.00% (3/3)
src/pool-cl/libraries/FullMath.sol 68.97% (20/29) 72.73% (24/33) 33.33% (2/6) 100.00% (2/2)
src/pool-cl/libraries/LiquidityMath.sol 0.00% (0/4) 0.00% (0/4) 0.00% (0/1) 100.00% (1/1)
src/pool-cl/libraries/SqrtPriceMath.sol 60.42% (29/48) 69.84% (44/63) 58.33% (7/12) 100.00% (9/9)
src/pool-cl/libraries/SwapMath.sol 80.77% (21/26) 81.48% (22/27) 100.00% (6/6) 100.00% (2/2)
src/pool-cl/libraries/Tick.sol 79.17% (38/48) 81.48% (44/54) 100.00% (10/10) 100.00% (6/6)
src/pool-cl/libraries/TickBitmap.sol 40.00% (12/30) 50.00% (18/36) 66.67% (2/3) 100.00% (4/4)
src/pool-cl/libraries/TickMath.sol 30.00% (36/120) 48.80% (81/166) 95.83% (23/24) 100.00% (4/4)
src/pool-cl/types/CLSlot0.sol 0.00% (0/8) 0.00% (0/8) 100.00% (0/0) 100.00% (8/8)
src/types/BalanceDelta.sol 0.00% (0/2) 0.00% (0/2) 100.00% (0/0) 100.00% (2/2)
src/types/BeforeSwapDelta.sol 0.00% (0/2) 0.00% (0/2) 100.00% (0/0) 100.00% (2/2)
src/types/Currency.sol 60.87% (14/23) 70.00% (21/30) 75.00% (6/8) 100.00% (6/6)
src/types/PoolId.sol 0.00% (0/1) 0.00% (0/1) 100.00% (0/0) 100.00% (1/1)

@chefburger chefburger force-pushed the feat/hexen-r7-option-2-optimise-beta branch from 91ed183 to acbc781 Compare November 13, 2024 08:51
@chefburger chefburger changed the title feat: example if we remove share to 0 if only lockup shares are left [POC]: example if we remove share to 0 if only lockup shares are left Nov 14, 2024
@chefburger chefburger marked this pull request as draft November 14, 2024 07:29
Base automatically changed from feat/hexen-r7-option-2 to main November 14, 2024 07:32
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.

2 participants