Skip to content

Commit

Permalink
Rename to alternativeCreditsPerToken
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielVF committed Nov 6, 2024
1 parent 85a009b commit e579a91
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# YOLO Spike, OUSD Token
# OUSD Token: 4.0

We are revamping the our rebasing token contract.

The primary objective is to allow delegated yield. Delegated yield an account to seemlessly transfer all earned yield to another account.

Secondarily, we'd like to fix the tiny rounding issues around transfers and between local account information and global tracking slots.


## How old OUSD works.

OUSD is a rebasing token. It's mission in life is to be able to distribute increases in backing assets on to users by having user's balances go up each time the token rebases.

**`_rebasingCreditsPerToken`** is a global variable that converts between "credits" stored on a account, and the actual balance of the account. This allows this single variable to be updated and all "rebasing" users have their account balance change proportionally. Counterintuitively, this is not a multiplier on users credits, but a divider. So it's `user balance = user credits / _rebasingCreditsPerToken`. Because it's a divider, OUSD will slowly lose resolution over very long timeframes, as opposed to abruptly stoping working suddenly once enough yield has been earned.

**_creditBalances[account]** This account mapping stores the internal credits for each account.

**nonRebasingCreditsPerToken[account]** This account mapping stores an alternative conversion factor for the value used in creditBalances.

## Default / Rebasing Account



In which I take a swing at a balance centric approach to see if it results in simpiler code.
32 changes: 18 additions & 14 deletions src/token/OUSD.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ contract OUSD is Governable {
uint256 private _rebasingCredits; // Sum of all rebasing credits (_creditBalances for rebasing accounts)
uint256 private _rebasingCreditsPerToken;
uint256 public nonRebasingSupply; // All nonrebasing balances
mapping(address => uint256) public nonRebasingCreditsPerToken;
mapping(address => uint256) private alternativeCreditsPerToken;
mapping(address => RebaseOptions) public rebaseState;
mapping(address => uint256) public isUpgraded;
mapping(address => address) public yieldTo;
Expand Down Expand Up @@ -191,6 +191,10 @@ contract OUSD is Governable {
);
}

function nonRebasingCreditsPerToken(address _account) external view returns (uint256) {
return alternativeCreditsPerToken[_account];
}

/**
* @dev Transfer tokens to a specified address.
* @param _to the address to transfer to.
Expand Down Expand Up @@ -273,7 +277,7 @@ contract OUSD is Governable {

_creditBalances[account] = newBalance;
_creditBalances[target] = targetNewCredits;
nonRebasingCreditsPerToken[account] = 1e18;
alternativeCreditsPerToken[account] = 1e18;

} else if (state == RebaseOptions.YieldDelegationTarget) {
uint256 newCredits = _balanceToRebasingCredits(newBalance + _creditBalances[yieldFrom[account]]);
Expand All @@ -282,7 +286,7 @@ contract OUSD is Governable {

} else if(_isNonRebasingAccount(account)){
nonRebasingSupplyDiff = balanceChange;
nonRebasingCreditsPerToken[account] = 1e18;
alternativeCreditsPerToken[account] = 1e18;
_creditBalances[account] = newBalance;

} else {
Expand Down Expand Up @@ -412,8 +416,8 @@ contract OUSD is Governable {
view
returns (uint256)
{
if (nonRebasingCreditsPerToken[_account] != 0) {
return nonRebasingCreditsPerToken[_account];
if (alternativeCreditsPerToken[_account] != 0) {
return alternativeCreditsPerToken[_account];
} else {
return _rebasingCreditsPerToken;
}
Expand All @@ -426,10 +430,10 @@ contract OUSD is Governable {
*/
function _isNonRebasingAccount(address _account) internal returns (bool) {
bool isContract = _account.code.length > 0;
if (isContract && rebaseState[_account] == RebaseOptions.NotSet && nonRebasingCreditsPerToken[_account] != 0) {
if (isContract && rebaseState[_account] == RebaseOptions.NotSet && alternativeCreditsPerToken[_account] != 0) {
_rebaseOptOut(msg.sender);
}
return nonRebasingCreditsPerToken[_account] > 0;
return alternativeCreditsPerToken[_account] > 0;
}

function _balanceToRebasingCredits(uint256 balance) internal view returns (uint256) {
Expand Down Expand Up @@ -465,14 +469,14 @@ contract OUSD is Governable {
}

function _rebaseOptIn(address _account) internal {
require(nonRebasingCreditsPerToken[_account] != 0, "Account has not opted out");
require(alternativeCreditsPerToken[_account] != 0, "Account has not opted out");
require(rebaseState[msg.sender] != RebaseOptions.YieldDelegationTarget, "Cannot opt in while yield delegating");

uint256 balance = balanceOf(msg.sender);

// Account
rebaseState[msg.sender] = RebaseOptions.OptIn;
nonRebasingCreditsPerToken[msg.sender] = 0;
alternativeCreditsPerToken[msg.sender] = 0;
_creditBalances[msg.sender] = _balanceToRebasingCredits(balance);

// Globals
Expand All @@ -487,15 +491,15 @@ contract OUSD is Governable {
}

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

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

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

// Globals
Expand Down Expand Up @@ -569,7 +573,7 @@ contract OUSD is Governable {

// Local
_creditBalances[from] = balance;
nonRebasingCreditsPerToken[from] = 1e18;
alternativeCreditsPerToken[from] = 1e18;
_creditBalances[to] += credits;

// Global
Expand All @@ -594,9 +598,9 @@ contract OUSD is Governable {

// Local
_creditBalances[from] = fromBalance;
nonRebasingCreditsPerToken[from] = 1e18;
alternativeCreditsPerToken[from] = 1e18;
_creditBalances[to] = toNewCredits;
nonRebasingCreditsPerToken[to] = 0; // Should be not be needed
alternativeCreditsPerToken[to] = 0; // Should be not be needed

// Global
nonRebasingSupply += fromBalance;
Expand Down

0 comments on commit e579a91

Please sign in to comment.