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

feat: principal-based account #102

Open
wants to merge 1 commit into
base: feat/continuous-index
Choose a base branch
from

Conversation

deluca-mike
Copy link
Collaborator

  • principal-based earner account, instead of last index
  • auto and manual migration for existing earners

@deluca-mike deluca-mike added the enhancement New feature or request label Jan 13, 2025
@deluca-mike deluca-mike self-assigned this Jan 13, 2025
Copy link

github-actions bot commented Jan 13, 2025

LCOV of commit b6e8c41 during Forge Coverage #537

Summary coverage rate:
  lines......: 98.4% (250 of 254 lines)
  functions..: 98.2% (56 of 57 functions)
  branches...: 88.0% (22 of 25 branches)

Files changed coverage rate:
                       |Lines       |Functions  |Branches    
  Filename             |Rate     Num|Rate    Num|Rate     Num
  ===========================================================
  src/WrappedMToken.sol|23.1%    225|41.2%    51|    -      0

@deluca-mike deluca-mike force-pushed the feat/continuous-index branch from 09fcec1 to 40784bf Compare January 13, 2025 22:05
@deluca-mike deluca-mike force-pushed the feat/principal-based-accounts branch from 76f82b0 to d447ba7 Compare January 13, 2025 22:06
@deluca-mike deluca-mike force-pushed the feat/continuous-index branch from 40784bf to 323b291 Compare January 14, 2025 18:59
@deluca-mike deluca-mike force-pushed the feat/principal-based-accounts branch from d447ba7 to e261b7b Compare January 14, 2025 19:03
@deluca-mike deluca-mike force-pushed the feat/continuous-index branch from 323b291 to aa920f6 Compare January 14, 2025 19:19
@deluca-mike deluca-mike force-pushed the feat/principal-based-accounts branch from e261b7b to b27e0af Compare January 14, 2025 19:19
@deluca-mike deluca-mike force-pushed the feat/continuous-index branch from aa920f6 to a021e1a Compare January 14, 2025 19:53
@deluca-mike deluca-mike force-pushed the feat/principal-based-accounts branch from b27e0af to 8c6a412 Compare January 14, 2025 19:56
src/WrappedMToken.sol Outdated Show resolved Hide resolved
@deluca-mike deluca-mike force-pushed the feat/principal-based-accounts branch from 8c6a412 to eba643f Compare January 16, 2025 18:27
unchecked {
// Increment the total earning supply and principal proportionally.
totalEarningSupply += amount_;
principalOfTotalEarningSupply += IndexingMath.getPrincipalAmountRoundedUp(amount_, currentIndex_);
Copy link
Contributor

Choose a reason for hiding this comment

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

why the was a round up in previous version? Bug? round down makes more sense

Copy link
Collaborator Author

@deluca-mike deluca-mike Jan 17, 2025

Choose a reason for hiding this comment

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

Needed to round up because individual account principals were not stored and thus could not be accumulated in a fixed way, so principalOfTotalEarningSupply needed to be rounded up (inflated) to ensure excess was conservative (deflated).

Now that each earner has a fixed computeed earning principal, totalEarningPrincipal can simply be added to or subtracted from with these individual account earning principals. It is an exact accumulation.

);

totalEarningSupply -= amount_;
totalEarningSupply = totalEarningSupply_ - UIntMath.min240(amount_, totalEarningSupply_);
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

@deluca-mike deluca-mike Jan 17, 2025

Choose a reason for hiding this comment

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

That link does not seem to work so I do not know what you are referring to (but I assume it is the uint112 principal_ = UIntMath.min112 line in _subtractEarningAmount). In any case, the UIntMath.min240 and UIntMath.min112 here just prevent underflows without the _subtractTotalEarningSupply function needing to be aware of how its callers operate or what they check. It is a safety measure, especially if this function may be used again later down the road. It is minimal gas for that safety.

But yes, technically it would be impossible to decrement totalEarningSupply or totalEarningPrincipal below 0 given any accounts' amount_ or principal_, since both those totals are already established totals.

We can remove the checks if you prefer.

@@ -205,11 +220,11 @@ interface IWrappedMToken is IMigratable, IERC20Extended {
function balanceWithYieldOf(address account) external view returns (uint256 balance);
Copy link
Contributor

Choose a reason for hiding this comment

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

balanceWithUnclaimedYieldOf or even delete this function

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will handle fucntion renames later on as:

  • it is outside the scope of this PR
  • they would be applicable and useful even if this pr is not merged
  • the rebase(s) would potentially be too tedious at this point


return;
}

senderAccountInfo_.isEarning
// TODO: Don't touch globals if both are earning or not earning.
Copy link
Contributor

Choose a reason for hiding this comment

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

optimization mentioned in TODO?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In a future PR, if needed at all. May depend on other PRs as well.

Copy link
Contributor

@toninorair toninorair left a comment

Choose a reason for hiding this comment

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

great work, things to discuss - simplifying migration strategy / code

by looping over all wM accounts in Migrator contract etc

senderAccountInfo_.balance = senderBalance_ - amount_;
recipientAccountInfo_.balance += amount_;
}
if (balance_ < amount_) revert InsufficientBalance(sender_, balance_, amount_);
Copy link
Member

Choose a reason for hiding this comment

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

Is this check still relevant?
We are performing no operation once we enter this condition.

Copy link
Collaborator Author

@deluca-mike deluca-mike Jan 17, 2025

Choose a reason for hiding this comment

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

Yes, because the event is emitted, and it may break indexers. Plus, you should not be able to transfer yourself a billion dollars if you don't have it.

@deluca-mike deluca-mike force-pushed the feat/continuous-index branch from a021e1a to b682915 Compare January 17, 2025 15:58
@deluca-mike deluca-mike force-pushed the feat/principal-based-accounts branch from eba643f to 3d3bde5 Compare January 17, 2025 15:59
@deluca-mike deluca-mike force-pushed the feat/continuous-index branch from b682915 to dfc2f33 Compare January 17, 2025 19:10
@deluca-mike
Copy link
Collaborator Author

deluca-mike commented Jan 17, 2025

great work, things to discuss - simplifying migration strategy / code by looping over all wM accounts in Migrator contract etc

While it could work, it would mean that the earners list cannot change from the time the Migrator is deployed to the time it is used, which is not that trivial.

First the new implementation and migrator are deployed, then the proposals are created to enable the migrator, then they are passed, then they are executed, then the migration can be performed. In that time, other proposals to modify the earners list may come in, which can create gaps in that batch account migration.

The "as needed" migration is impossible to break.

However, if we did do batch migration (and could guarantee completeness), it would:

  • remove the account migration code from the implementation and interface
  • remove the "ugly" EarningState enums
  • make future PRs and abstracted branches have a smaller diff

Its worth noting that trying to migrate a non-earner results in a no-op, so it is fine. But skipping over an earner and not migrating them is a critical error, and the MIgrate contract process only happens once and cannot be repeated.

@deluca-mike deluca-mike force-pushed the feat/principal-based-accounts branch from 3d3bde5 to b452ee9 Compare January 17, 2025 19:29
@deluca-mike deluca-mike force-pushed the feat/continuous-index branch from dfc2f33 to 6f20cbe Compare January 17, 2025 19:39
@deluca-mike deluca-mike force-pushed the feat/principal-based-accounts branch from b452ee9 to 0e76045 Compare January 17, 2025 19:39
@deluca-mike deluca-mike force-pushed the feat/continuous-index branch from 6f20cbe to 7383901 Compare January 17, 2025 19:46
@deluca-mike deluca-mike force-pushed the feat/principal-based-accounts branch from 0e76045 to 8f65b68 Compare January 17, 2025 19:46
@deluca-mike deluca-mike force-pushed the feat/continuous-index branch from 7383901 to bef592e Compare January 20, 2025 17:15
@deluca-mike deluca-mike force-pushed the feat/principal-based-accounts branch from 8f65b68 to 7813bef Compare January 20, 2025 17:15
@deluca-mike deluca-mike force-pushed the feat/continuous-index branch from bef592e to 4b1b4b0 Compare January 21, 2025 18:57
@deluca-mike deluca-mike force-pushed the feat/principal-based-accounts branch from 7813bef to 2a4be08 Compare January 21, 2025 18:57
@deluca-mike deluca-mike force-pushed the feat/continuous-index branch from 4b1b4b0 to 68dc8a4 Compare January 21, 2025 19:02
@deluca-mike deluca-mike force-pushed the feat/principal-based-accounts branch from 2a4be08 to 04502d8 Compare January 21, 2025 19:02
@deluca-mike deluca-mike force-pushed the feat/continuous-index branch from 68dc8a4 to f048807 Compare January 21, 2025 20:44
- principal-based earner account, instead of last index
- auto and manual migration for existing earners
@deluca-mike deluca-mike force-pushed the feat/principal-based-accounts branch from 04502d8 to b6e8c41 Compare January 21, 2025 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants