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

Mature positions aren't effected by negative interest #584

Closed
jalextowle opened this issue Sep 22, 2023 · 2 comments
Closed

Mature positions aren't effected by negative interest #584

jalextowle opened this issue Sep 22, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@jalextowle
Copy link
Contributor

Problem

While working on #537, I noticed that negative interest isn't being applied correctly to shorts and longs in _applyCheckpoint. The current implementation contains this logic:


        // Close out the short positions with a maturity time equal to the latest checkpoint.
        // This ensures that shorts don't continue to collect free variable interest and
        // ensures that LP's can withdraw the proceeds of their side of the trade.
        uint256 maturedShortsAmount = _totalSupply[
            AssetId.encodeAssetId(AssetId.AssetIdPrefix.Short, _checkpointTime)
        ];
        if (maturedShortsAmount > 0) {
            uint256 shareProceeds = maturedShortsAmount.divDown(_sharePrice);
            uint256 flatFee = shareProceeds.mulDown(_flatFee);
            uint256 govFee = flatFee.mulDown(_governanceFee);

            // Add accrued governance fees to the totalGovernanceFeesAccrued in terms of shares
            _governanceFeesAccrued += govFee;

            // Increase shareProceeds by the flatFeeCharged, and less the govFee from the amount as it doesn't count
            // towards reserves. shareProceeds will only be used to update reserves, so its fine to take fees here.
            shareProceeds += flatFee - govFee;

            // Closing out shorts first helps with netting by ensuring the LP funds
            // that were netted with longs are back in the shareReserves before we
            // close out the longs.
            _applyCloseShort(
                maturedShortsAmount,
                0,
                shareProceeds,
                0,
                _checkpointTime,
                _sharePrice
            );
        }

        // Close out the long positions with a maturity time equal to the latest checkpoint.
        uint256 maturedLongsAmount = _totalSupply[
            AssetId.encodeAssetId(AssetId.AssetIdPrefix.Long, _checkpointTime)
        ];
        if (maturedLongsAmount > 0) {
            uint256 shareProceeds = maturedLongsAmount.divDown(_sharePrice);
            uint256 flatFee = shareProceeds.mulDown(_flatFee);
            uint256 govFee = flatFee.mulDown(_governanceFee);

            // Add accrued governance fees to the totalGovernanceFeesAccrued in terms of shares
            _governanceFeesAccrued += govFee;

            // Reduce shareProceeds by the flatFeeCharged, and less the govFee from the amount as it doesn't count
            // towards reserves. shareProceeds will only be used to update reserves, so its fine to take fees here.
            shareProceeds -= flatFee - govFee;

            _applyCloseLong(
                maturedLongsAmount,
                0,
                shareProceeds,
                0,
                _checkpointTime,
                _sharePrice
            );
        }

We normally adjust the share proceeds and reserves deltas to account for negative interest in the calculation functions. Since the logic is simpler in _applyCheckpoint, we just included the logic inline in _applyCheckpoint, which results in this issue.

Solution

We should apply the negative interest logic. Aside from this, we should refactor the code so that we have calculation methods.

@jalextowle jalextowle added the bug Something isn't working label Sep 22, 2023
@jrhea
Copy link
Contributor

jrhea commented Nov 6, 2023

Pretty sure this was resolved in #593

@jalextowle
Copy link
Contributor Author

Closed by #593.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants