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

fix: consider protocol loss when calculating shares to revoke and use 512-bit mulDivs in CollateralTracker #23

Merged
merged 2 commits into from
Nov 30, 2023

Conversation

dyedm1
Copy link
Member

@dyedm1 dyedm1 commented Nov 29, 2023

No description provided.

// subtract delegatee balance from N since it was already transferred to the delegator
_mint(
delegator,
Math.mulDiv(assets, totalSupply - delegateeBalance, totalAssets() - assets) -
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I had to check. The expression on the liquidation branch is different, but it turns out it's the exact same value:

From the liquidation branch:

            // must mint the correct amount of shares (∆) so that the redeemable value is exactly equal to A, the requested assets
            // tA: total assets in vault (ie. totalAssets())
            // tS: total supply of shares (ie. totalSupply)
            // ∆: desired number of shares to be minted
            // A: requested assets
            // B: transferred shares from liquidatee (ie. delegateeBalance)
            //
            // Redeemable value = assets === delegatorShares * totalAssets() / newTotalSupply
            //
            // A = (B + ∆) * tA / (tS + ∆)
            // ∆ * (tA - A) = A * tS - B * tA
            // ∆ = (A*tS - B*tA)/(tA-A)

            uint256 tA = totalAssets();
            unchecked {
                // mint shares to complete to requested amount
                _mint(
                    delegator,
                    Math.mulDiv(assets, totalSupply, tA - assets) -
                        Math.mulDiv(delegateeBalance, tA, tA - assets)
                );
            }

So the final expressions are:

Math.mulDiv(assets, totalSupply - delegateeBalance, totalAssets() - assets) -delegateeBalance)

versus

Math.mulDiv(assets, totalSupply, tA - assets) - Math.mulDiv(delegateeBalance, tA, tA - assets)

It's the exact same final expression once we expand yours.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep. I did it that way to save a mulDiv.

@dyedm1 dyedm1 merged commit 02cd20d into main Nov 30, 2023
5 checks passed
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