Skip to content

Commit

Permalink
Dedup code around auto rebasing opt in
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielVF committed Nov 4, 2024
1 parent 0b8ae18 commit ecd2caa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 51 deletions.
36 changes: 18 additions & 18 deletions snapshots/OUSDTest.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
{
"Transfer Attacker -> Attacker": "24166",
"Transfer Attacker -> Collector": "59816",
"Transfer Attacker -> Matt": "57833",
"Transfer Attacker -> NonRebasing": "62696",
"Transfer Attacker -> Pool": "63955",
"Transfer Collector -> Attacker": "56948",
"Transfer Attacker -> Collector": "59836",
"Transfer Attacker -> Matt": "57873",
"Transfer Attacker -> NonRebasing": "62736",
"Transfer Attacker -> Pool": "63975",
"Transfer Collector -> Attacker": "56968",
"Transfer Collector -> Collector": "24166",
"Transfer Collector -> Matt": "56948",
"Transfer Collector -> NonRebasing": "67179",
"Transfer Collector -> Matt": "56968",
"Transfer Collector -> NonRebasing": "67199",
"Transfer Collector -> Pool": "52838",
"Transfer Matt -> Attacker": "52465",
"Transfer Matt -> Collector": "54448",
"Transfer Matt -> Attacker": "52505",
"Transfer Matt -> Collector": "54468",
"Transfer Matt -> Matt": "24166",
"Transfer Matt -> NonRebasing": "62696",
"Transfer Matt -> Pool": "63955",
"Transfer NonRebasing -> Attacker": "62696",
"Transfer NonRebasing -> Collector": "64679",
"Transfer NonRebasing -> Matt": "62696",
"Transfer Matt -> NonRebasing": "62736",
"Transfer Matt -> Pool": "63975",
"Transfer NonRebasing -> Attacker": "62736",
"Transfer NonRebasing -> Collector": "64699",
"Transfer NonRebasing -> Matt": "62736",
"Transfer NonRebasing -> NonRebasing": "24166",
"Transfer NonRebasing -> Pool": "74186",
"Transfer Pool -> Attacker": "66455",
"Transfer NonRebasing -> Pool": "74206",
"Transfer Pool -> Attacker": "66475",
"Transfer Pool -> Collector": "52838",
"Transfer Pool -> Matt": "66455",
"Transfer Pool -> NonRebasing": "76686",
"Transfer Pool -> Matt": "66475",
"Transfer Pool -> NonRebasing": "76706",
"Transfer Pool -> Pool": "24166"
}
47 changes: 14 additions & 33 deletions src/token/OUSD.sol
Original file line number Diff line number Diff line change
Expand Up @@ -433,35 +433,12 @@ contract OUSD is Governable {
*/
function _isNonRebasingAccount(address _account) internal returns (bool) {
bool isContract = _account.code.length > 0;
if (isContract && rebaseState[_account] == RebaseOptions.NotSet) {
_ensureNonRebasingMigration(_account);
if (isContract && rebaseState[_account] == RebaseOptions.NotSet && nonRebasingCreditsPerToken[_account] != 0) {
_rebaseOptOut(msg.sender);
}
return nonRebasingCreditsPerToken[_account] > 0;
}

/**
* @dev If this is the first time a contract has been seen, migrate it to
* non-rebasing accounting.
*/
function _ensureNonRebasingMigration(address _account) internal {
if (nonRebasingCreditsPerToken[_account] != 0) {
return; // Account is already non-rebasing
}
uint256 oldCredits = _creditBalances[_account];
uint256 balance = balanceOf(_account);

// Account
rebaseState[_account] = RebaseOptions.OptOut;
_creditBalances[_account] = balance;
nonRebasingCreditsPerToken[_account] = 1e18;

// Global
nonRebasingSupply += balance;
_rebasingCredits -= oldCredits;

emit AccountRebasingDisabled(_account);
}

function _balanceToRebasingCredits(uint256 balance) internal view returns (uint256) {
// Rounds up, because we need to ensure that accounts allways have
// at least the balance that they should have.
Expand Down Expand Up @@ -513,22 +490,26 @@ contract OUSD is Governable {
}

function rebaseOptOut() public nonReentrant {
require(!_isNonRebasingAccount(msg.sender), "Account has not opted in");
require(rebaseState[msg.sender] != RebaseOptions.YieldDelegationSource, "Cannot opt out while receiving yield");
_rebaseOptOut(msg.sender);
}

function _rebaseOptOut(address _account) internal {
require(!_isNonRebasingAccount(_account), "Account has not opted in");
require(rebaseState[_account] != RebaseOptions.YieldDelegationSource, "Cannot opt out while receiving yield");

uint256 oldCredits = _creditBalances[msg.sender];
uint256 balance = balanceOf(msg.sender);
uint256 oldCredits = _creditBalances[_account];
uint256 balance = balanceOf(_account);

// Account
rebaseState[msg.sender] = RebaseOptions.OptOut;
nonRebasingCreditsPerToken[msg.sender] = 1e18;
_creditBalances[msg.sender] = balance;
rebaseState[_account] = RebaseOptions.OptOut;
nonRebasingCreditsPerToken[_account] = 1e18;
_creditBalances[_account] = balance;

// Globals
nonRebasingSupply += balance;
_rebasingCredits -= oldCredits;

emit AccountRebasingDisabled(msg.sender);
emit AccountRebasingDisabled(_account);
}

/**
Expand Down

0 comments on commit ecd2caa

Please sign in to comment.