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

Cleanup OUSD rebasing/nonrebasing accounting changes #1239

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions contracts/contracts/token/OUSD.sol
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,16 @@ contract OUSD is Initializable, InitializableERC20Detailed, Governable {
// high resolution, and do not have to do any other bookkeeping
nonRebasingCreditsPerToken[_account] = 1e27;
} else {
// Migrate an existing account:

// Migrate the existing account:
// 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. This
// isn't a problem in the current implimentation, since we only
DanielVF marked this conversation as resolved.
Show resolved Hide resolved
// need to update nonRebasingCreditsPerToken.
// Set fixed credits per token for this account
nonRebasingCreditsPerToken[_account] = _rebasingCreditsPerToken;

// Update global totals:
// Update non rebasing supply
nonRebasingSupply = nonRebasingSupply.add(balanceOf(_account));
// Update credit tallies
Expand All @@ -495,24 +501,29 @@ 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.
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];
}

/**
Expand All @@ -521,17 +532,19 @@ 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));
// Atomicly update this account
// Important that no internal calls happen during this.
// Set fixed credits per token
nonRebasingCreditsPerToken[msg.sender] = _rebasingCreditsPerToken;
// Mark explicitly opted out of rebasing
rebaseState[msg.sender] = RebaseOptions.OptOut;

// Update global totals:
// Increase non rebasing supply
nonRebasingSupply = nonRebasingSupply.add(balanceOf(msg.sender));
// Decrease rebasing credits, total supply remains unchanged so no
// adjustment necessary
_rebasingCredits = _rebasingCredits.sub(_creditBalances[msg.sender]);

// Mark explicitly opted out of rebasing
rebaseState[msg.sender] = RebaseOptions.OptOut;
}

/**
Expand Down