@@ -31,6 +31,17 @@ contract OUSD is Initializable, InitializableERC20Detailed, Governable {
3131 uint256 rebasingCreditsPerToken
3232 );
3333
34+ event RebasingDisabled (
35+ address indexed account ,
36+ uint256 balance ,
37+ uint256 rebasingCreditsPerToken
38+ );
39+ event RebasingEnabled (
40+ address indexed account ,
41+ uint256 balance ,
42+ uint256 rebasingCreditsPerToken
43+ );
44+
3445 enum RebaseOptions {
3546 NotSet,
3647 OptOut,
@@ -48,7 +59,7 @@ contract OUSD is Initializable, InitializableERC20Detailed, Governable {
4859 // do not receive yield unless they explicitly opt in)
4960 uint256 public nonRebasingSupply;
5061 mapping (address => uint256 ) public nonRebasingCreditsPerToken;
51- mapping (address => RebaseOptions) public rebaseState;
62+ mapping (address => RebaseOptions) public rebaseState; // User OptIn/OptOut
5263 mapping (address => uint256 ) public isUpgraded;
5364
5465 uint256 private constant RESOLUTION_INCREASE = 1e9 ;
@@ -471,29 +482,34 @@ contract OUSD is Initializable, InitializableERC20Detailed, Governable {
471482 return ; // Account already is non-rebasing
472483 }
473484 if (_creditBalances[_account] == 0 ) {
474- // Since there is no existing balance, we can directly set to
485+ // Since there is no existing balance, we can directly set it to
475486 // high resolution, and do not have to do any other bookkeeping
476487 nonRebasingCreditsPerToken[_account] = 1e27 ;
477- return ;
488+ } else {
489+ // This does not change, but if it did, we would want to
490+ // use the value before changes.
491+ uint256 oldCredits = _creditBalances[_account];
492+
493+ // Atomicly update account information:
494+ // It is important that balanceOf not be called inside updating
495+ // account data, since it will give wrong answers if it does
496+ // not have all an account's data in a consistent state.
497+ //
498+ // By setting a per account nonRebasingCreditsPerToken,
499+ // this account will no longer follow with the global
500+ // rebasing credits per token and will become non-rebasing.
501+ nonRebasingCreditsPerToken[_account] = _rebasingCreditsPerToken;
502+
503+ // Update global totals
504+ nonRebasingSupply = nonRebasingSupply.add (balanceOf (_account));
505+ _rebasingCredits = _rebasingCredits.sub (oldCredits);
478506 }
479507
480- // This does not change, but if it did, we would want to
481- // use the value before changes.
482- uint256 oldCredits = _creditBalances[_account];
483-
484- // Atomicly update account information:
485- // It is important that balanceOf not be called inside updating
486- // account data, since it will give wrong answers if it does
487- // not have all an account's data in a consistent state.
488- //
489- // By setting a per account nonRebasingCreditsPerToken,
490- // this account will no longer follow with the global
491- // rebasing credits per token and will become non-rebasing.
492- nonRebasingCreditsPerToken[_account] = _rebasingCreditsPerToken;
493-
494- // Update global totals
495- nonRebasingSupply = nonRebasingSupply.add (balanceOf (_account));
496- _rebasingCredits = _rebasingCredits.sub (oldCredits);
508+ emit RebasingDisabled (
509+ _account,
510+ balanceOf (_account),
511+ _rebasingCreditsPerToken
512+ );
497513 }
498514
499515 /**
@@ -504,10 +520,12 @@ contract OUSD is Initializable, InitializableERC20Detailed, Governable {
504520 function rebaseOptIn () public nonReentrant {
505521 require (_isNonRebasingAccount (msg .sender ), "Account has not opted out " );
506522
523+ // Precalculate old balance so that no partial
524+ // account changes will affect it
507525 uint256 oldBalance = balanceOf (msg .sender );
508526
509527 // Precalculate new credits, so that we avoid internal calls when
510- // atomicly updating account.
528+ // atomically updating account.
511529 // Convert balance into the same amount at the current exchange rate
512530 uint256 newCreditBalance = _creditBalances[msg .sender ]
513531 .mul (_rebasingCreditsPerToken)
@@ -519,7 +537,7 @@ contract OUSD is Initializable, InitializableERC20Detailed, Governable {
519537 delete nonRebasingCreditsPerToken[msg .sender ];
520538 // New credits
521539 _creditBalances[msg .sender ] = newCreditBalance;
522- // Mark explicitly opted out of rebasing
540+ // Mark explicitly opted in to rebasing
523541 rebaseState[msg .sender ] = RebaseOptions.OptIn;
524542
525543 // Update global totals:
@@ -528,6 +546,8 @@ contract OUSD is Initializable, InitializableERC20Detailed, Governable {
528546 // Increase rebasing credits, totalSupply remains unchanged so no
529547 // adjustment necessary
530548 _rebasingCredits = _rebasingCredits.add (_creditBalances[msg .sender ]);
549+
550+ emit RebasingEnabled (msg .sender , oldBalance, _rebasingCreditsPerToken);
531551 }
532552
533553 /**
0 commit comments