-
Notifications
You must be signed in to change notification settings - Fork 93
Cleanup OUSD rebasing/nonrebasing accounting changes #1239
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
Open
DanielVF
wants to merge
14
commits into
master
Choose a base branch
from
DanielVF/ousd-rebasing-migration-tidy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
92612a2
Added comments to warn us about doing a split accounting update
DanielVF 838d6a7
Reorder and clarify rebaseOptOut and rebaseOptIn
DanielVF 134f0a3
Merge
DanielVF f5a55a3
Use perfect rebasing
DanielVF 1b29fba
Switch back to old verison, since it will also be perfectly accurate
DanielVF ab9da6a
Follow normal fast exit style, rather than nested ifs
DanielVF 958070b
Use before change balance, as before
DanielVF b202146
Merge branch 'master' into DanielVF/ousd-rebasing-migration-tidy
DanielVF 3ee5461
Add events for changes in rebase state
DanielVF b8d7a9b
More comments updates
DanielVF 8526238
Merge branch 'master' into DanielVF/ousd-rebasing-migration-tidy
DanielVF 2f63c44
Add balance match require to no rebase migration.
DanielVF 5b8856c
Merge branch 'master' into DanielVF/ousd-rebasing-migration-tidy
DanielVF 063e12a
Add governance opt-in
DanielVF File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -457,7 +457,7 @@ contract OUSD is Initializable, InitializableERC20Detailed, Governable { | |
| function _isNonRebasingAccount(address _account) internal returns (bool) { | ||
| bool isContract = Address.isContract(_account); | ||
| if (isContract && rebaseState[_account] == RebaseOptions.NotSet) { | ||
| _ensureRebasingMigration(_account); | ||
| _ensureMigrationToNonRebasing(_account); | ||
| } | ||
| return nonRebasingCreditsPerToken[_account] > 0; | ||
| } | ||
|
|
@@ -466,24 +466,33 @@ contract OUSD is Initializable, InitializableERC20Detailed, Governable { | |
| * @dev Ensures internal account for rebasing and non-rebasing credits and | ||
| * supply is updated following deployment of frozen yield change. | ||
| */ | ||
| function _ensureRebasingMigration(address _account) internal { | ||
| if (nonRebasingCreditsPerToken[_account] == 0) { | ||
| if (_creditBalances[_account] == 0) { | ||
| // Since there is no existing balance, we can directly set to | ||
| // high resolution, and do not have to do any other bookkeeping | ||
| nonRebasingCreditsPerToken[_account] = 1e27; | ||
| } else { | ||
| // Migrate an existing account: | ||
|
|
||
| // Set fixed credits per token for this account | ||
| nonRebasingCreditsPerToken[_account] = _rebasingCreditsPerToken; | ||
| // Update non rebasing supply | ||
| nonRebasingSupply = nonRebasingSupply.add(balanceOf(_account)); | ||
| // Update credit tallies | ||
| _rebasingCredits = _rebasingCredits.sub( | ||
| _creditBalances[_account] | ||
| ); | ||
| } | ||
| function _ensureMigrationToNonRebasing(address _account) internal { | ||
| if (nonRebasingCreditsPerToken[_account] != 0) { | ||
| return; // Account already is non-rebasing | ||
| } | ||
| if (_creditBalances[_account] == 0) { | ||
| // Since there is no existing balance, we can directly set to | ||
DanielVF marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // high resolution, and do not have to do any other bookkeeping | ||
| nonRebasingCreditsPerToken[_account] = 1e27; | ||
| } else { | ||
| // Get old values, so we can use them unaffected by changes | ||
| uint256 oldBalance = balanceOf(_account); | ||
| uint256 oldCredits = _creditBalances[_account]; | ||
|
|
||
| // Atomicly update account information: | ||
| // It is important that balanceOf not be called inside updating | ||
| // account data, since it will give wrong answers if it does | ||
| // not have all an account's data in a consistent state. | ||
| nonRebasingCreditsPerToken[_account] = 1e27; | ||
| // difference between the 1e18 balance and the new 1e27 resolution | ||
| _creditBalances[_account] = oldBalance * 1e9; | ||
DanielVF marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Verify perfect acccount accounting update | ||
| require(oldBalance == balanceOf(_account), "Balances do not match"); | ||
|
||
|
|
||
| // Update global totals: | ||
| nonRebasingSupply = nonRebasingSupply.add(oldBalance); | ||
| _rebasingCredits = _rebasingCredits.sub(oldCredits); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -495,24 +504,28 @@ contract OUSD is Initializable, InitializableERC20Detailed, Governable { | |
| function rebaseOptIn() public nonReentrant { | ||
| require(_isNonRebasingAccount(msg.sender), "Account has not opted out"); | ||
|
|
||
| // Precalculate new credits, so that we avoid internal calls when | ||
| // atomicly updating account. | ||
| // Convert balance into the same amount at the current exchange rate | ||
| uint256 newCreditBalance = _creditBalances[msg.sender] | ||
| .mul(_rebasingCreditsPerToken) | ||
| .div(_creditsPerToken(msg.sender)); | ||
|
|
||
| // Decreasing non rebasing supply | ||
| nonRebasingSupply = nonRebasingSupply.sub(balanceOf(msg.sender)); | ||
|
|
||
| // Atomicly update this account: | ||
| // Important that no internal calls happen during this. | ||
| // Remove pinned fixed credits per token | ||
| delete nonRebasingCreditsPerToken[msg.sender]; | ||
| // New credits | ||
| _creditBalances[msg.sender] = newCreditBalance; | ||
| // Mark explicitly opted out of rebasing | ||
DanielVF marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| rebaseState[msg.sender] = RebaseOptions.OptIn; | ||
|
|
||
| // Update global totals: | ||
| // Decrease non rebasing supply | ||
| nonRebasingSupply = nonRebasingSupply.sub(balanceOf(msg.sender)); | ||
| // Increase rebasing credits, totalSupply remains unchanged so no | ||
| // adjustment necessary | ||
| _rebasingCredits = _rebasingCredits.add(_creditBalances[msg.sender]); | ||
DanielVF marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| rebaseState[msg.sender] = RebaseOptions.OptIn; | ||
|
|
||
| // Delete any fixed credits per token | ||
| delete nonRebasingCreditsPerToken[msg.sender]; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -521,16 +534,8 @@ contract OUSD is Initializable, InitializableERC20Detailed, Governable { | |
| function rebaseOptOut() public nonReentrant { | ||
| require(!_isNonRebasingAccount(msg.sender), "Account has not opted in"); | ||
|
|
||
| // Increase non rebasing supply | ||
| nonRebasingSupply = nonRebasingSupply.add(balanceOf(msg.sender)); | ||
| // Set fixed credits per token | ||
| nonRebasingCreditsPerToken[msg.sender] = _rebasingCreditsPerToken; | ||
|
|
||
| // Decrease rebasing credits, total supply remains unchanged so no | ||
| // adjustment necessary | ||
| _rebasingCredits = _rebasingCredits.sub(_creditBalances[msg.sender]); | ||
| _ensureMigrationToNonRebasing(msg.sender); | ||
|
|
||
| // Mark explicitly opted out of rebasing | ||
| rebaseState[msg.sender] = RebaseOptions.OptOut; | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.