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

Yield Delegation / Rebasing Token Rewrite #2298

Merged
merged 114 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 109 commits
Commits
Show all changes
114 commits
Select commit Hold shift + click to select a range
07293d2
initial second implementation of rebasing to another account
sparrowDom Oct 30, 2024
048b03f
update to core functionality
sparrowDom Oct 30, 2024
36c599e
add gas fucntion
sparrowDom Nov 3, 2024
05d746e
simplify execute
sparrowDom Nov 3, 2024
28d83f9
simplify mint and burn
sparrowDom Nov 3, 2024
45f476b
initial version of Daniel's yield delegation implementation
sparrowDom Nov 7, 2024
9994263
prettier and linter fixes
naddison36 Nov 7, 2024
4c22467
Add slots to OUSD contract to align with existing deployments
naddison36 Nov 7, 2024
f537b63
Generated new OUSD contract diagrams
naddison36 Nov 7, 2024
5743461
Added deploy scripts for OToken upgrades
naddison36 Nov 7, 2024
68ae87e
Generated new OUSD storage diagram
naddison36 Nov 7, 2024
2dbd19e
Merge remote-tracking branch 'origin/master' into sparrowDom/rebaseEl…
naddison36 Nov 7, 2024
58afd12
some minor bug fixes
sparrowDom Nov 7, 2024
17f121c
Prettier and fix spelling in comments
naddison36 Nov 7, 2024
381d13d
Unit test fixes
naddison36 Nov 7, 2024
4ee1b4b
fix initialise function sigature and visibility of functions
sparrowDom Nov 7, 2024
9a29c45
fix some tests
sparrowDom Nov 7, 2024
47e529f
add explanation for the alternativeCreditsPerToken check
sparrowDom Nov 7, 2024
24f099b
prettier
sparrowDom Nov 7, 2024
22c1cf5
reintroduce the original check
sparrowDom Nov 7, 2024
a5269b8
explicitly call rebaseOpt out if the yield delegating account hasn't …
sparrowDom Nov 8, 2024
e64ab17
add one more test
sparrowDom Nov 8, 2024
68d14bd
add Readme of the token contract logic
sparrowDom Nov 8, 2024
ef7c4ab
force account to be rebasing on yield delegation
sparrowDom Nov 8, 2024
90ec917
add explicit whitelist of allowed states when delegating yield
sparrowDom Nov 11, 2024
2c96306
add more defensive checks where isNonRebasingAccount can not mistanki…
sparrowDom Nov 13, 2024
4bf3360
remove increase/decrease allowance
sparrowDom Nov 13, 2024
5cbcf4c
Update docs with invarients
DanielVF Nov 13, 2024
0ab7bc8
Use safecast for any downcasting (#2306)
sparrowDom Nov 13, 2024
2029659
More invarients around outer functions
DanielVF Nov 13, 2024
5951660
Format invarients
DanielVF Nov 13, 2024
8c126c8
further simplify the code when handling different rebase states
sparrowDom Nov 13, 2024
8c2a358
remove nonreentrant modifiers from the toke code as they are not needed
sparrowDom Nov 13, 2024
d7a303f
allow empty conracts to rebaseOptIn without auto migration
sparrowDom Nov 15, 2024
2b3189f
Transfer unit tests for new token implementation (#2310)
sparrowDom Nov 15, 2024
aa53ea3
More invarients
DanielVF Nov 13, 2024
727e4e9
Correct total supply docs
DanielVF Nov 15, 2024
77d69f1
Transfer unit tests for new token implementation (#2310)
sparrowDom Nov 15, 2024
0cfa352
fix test
sparrowDom Nov 15, 2024
31c8078
remove redundant state checks
sparrowDom Nov 15, 2024
511dcaa
remove overwriting the same value to alternativeCreditsPerToken
sparrowDom Nov 15, 2024
5f63650
add non zero checks in delegation functions
sparrowDom Nov 15, 2024
4e327fa
correct error
sparrowDom Nov 15, 2024
9e9c726
correct some contract view modifiers
sparrowDom Nov 15, 2024
6e1b63e
add a readable error message when allowance is exceeded
sparrowDom Nov 15, 2024
659f294
reducing 2 functions to 1
sparrowDom Nov 15, 2024
f261756
deprecate isUpgraded
sparrowDom Nov 18, 2024
54b7ebe
rename totalSupply
sparrowDom Nov 18, 2024
2b31fd6
improve initialisation checks
sparrowDom Nov 18, 2024
108f0eb
remove gas optimisation that would also allow for errorneously large …
sparrowDom Nov 18, 2024
24865d0
remove underflow checks
sparrowDom Nov 18, 2024
b728d2e
simplify code check for rebaseOptIn and add a test for it
sparrowDom Nov 18, 2024
ba625f0
remove comments
sparrowDom Nov 18, 2024
f8280d1
move the balance and credits query above the rebaseState changes
sparrowDom Nov 18, 2024
ab460b8
use _adjustGlobals function to adjust globals in yield delegation
sparrowDom Nov 18, 2024
c94cd5f
futher simplify the undelegateYield function
sparrowDom Nov 18, 2024
c3684a5
unify the variable names
sparrowDom Nov 18, 2024
9cc74cc
add comment
sparrowDom Nov 18, 2024
2987362
a couple of more places to utilise the _adjustGlobals function
sparrowDom Nov 18, 2024
6b02387
no need this being a separate variable
sparrowDom Nov 18, 2024
6556835
undo bug introduction
sparrowDom Nov 18, 2024
56b11e2
wrong use of msg.sender bug fix
sparrowDom Nov 18, 2024
bbb3f45
function doesn't need to return any values
sparrowDom Nov 19, 2024
e949129
simplify
sparrowDom Nov 19, 2024
a1d3cdc
improve syntax
sparrowDom Nov 19, 2024
504a421
add events for yield delegation
sparrowDom Nov 19, 2024
546695f
remove var init
sparrowDom Nov 19, 2024
62c1045
fix deploy file
sparrowDom Nov 19, 2024
fd45920
add tests to catch possible incorrect rebaseOptIn / rebaseOptOut attr…
sparrowDom Nov 20, 2024
3d03c7b
simplify changeSupply code
sparrowDom Nov 20, 2024
fbb11a9
add storage slot gap
sparrowDom Nov 20, 2024
6d5c745
Comments update
DanielVF Nov 20, 2024
9ded07a
Comments spelling update
DanielVF Nov 20, 2024
2c6bdc2
correct comments
sparrowDom Nov 20, 2024
9436e7c
Merge remote-tracking branch 'origin/sparrowDom/rebaseElsewhere_v2' i…
sparrowDom Nov 20, 2024
f1939db
unify variable names
sparrowDom Nov 20, 2024
57c8733
make credits calculation based of off balance for higher accuracy (in…
sparrowDom Nov 20, 2024
dc803f2
minor gas optimisation
sparrowDom Nov 20, 2024
4893186
correct storage slot amount so it totals to 200
sparrowDom Nov 21, 2024
09bde1b
Improve rebasing supply accuracy V2 (#2314)
sparrowDom Nov 21, 2024
ae51bbd
gas optimisation
sparrowDom Nov 21, 2024
f03deb3
better naming
sparrowDom Nov 21, 2024
db2044a
add a test where multiple rebaseOptIn/OptOut calls do not result in i…
sparrowDom Nov 22, 2024
4495130
add a check for zero address with governanceRebaseOptIn tx
sparrowDom Nov 23, 2024
53db807
Update on rebasing
DanielVF Nov 25, 2024
948014c
add a check for zero address with governanceRebaseOptIn tx
sparrowDom Nov 23, 2024
7d7055c
Merge remote-tracking branch 'origin/sparrowDom/rebaseElsewhere_v2' i…
sparrowDom Nov 25, 2024
01b49a3
make an exception for balance exact non rebasing accounts (StdNonReba…
sparrowDom Nov 25, 2024
e355f3d
add test for the 1e27 cpt token exception
sparrowDom Nov 25, 2024
7636c99
add a test to for creditsBalanceOf and creditsBalanceOfHighres
sparrowDom Nov 25, 2024
da68531
add nonRebasingCreditsPerToken to the test
sparrowDom Nov 25, 2024
1b9d1e3
add auto migration test and revert test for rebaseOptOut
sparrowDom Nov 25, 2024
34021fb
prettier
sparrowDom Nov 26, 2024
57cccba
add tests for missing requires in yield delegation
sparrowDom Nov 26, 2024
24bf72a
simplify code
sparrowDom Nov 26, 2024
f1b5290
revert to the previous implementation of the deprecated function
sparrowDom Nov 26, 2024
20f1bd1
add OETH upgrade deployment file
sparrowDom Nov 28, 2024
7b76cb8
change license to Business Source License
sparrowDom Nov 29, 2024
dbb3434
prettier
sparrowDom Nov 29, 2024
93545c3
on changeSupply round up in the favour of the protocol
sparrowDom Nov 29, 2024
dc854bc
round down when calculating credits from balances
sparrowDom Dec 1, 2024
63ee2ba
Revert "round down when calculating credits from balances"
sparrowDom Dec 2, 2024
296f5f4
fix typos (#2323)
sparrowDom Dec 5, 2024
74bd138
gas optimisation (#2322)
sparrowDom Dec 5, 2024
040ad7a
add missing natspec (#2321)
sparrowDom Dec 5, 2024
d9c6def
L-02 Missing Docstrings (#2319)
sparrowDom Dec 5, 2024
5e57112
Correct globals storage
DanielVF Dec 10, 2024
eb957f1
Only empty accounts can rebaseOptIn if already rebasing
DanielVF Dec 17, 2024
d285b32
gas optimisation
sparrowDom Dec 17, 2024
55efcd6
remove extra new line
sparrowDom Dec 17, 2024
412e93d
Merge remote-tracking branch 'origin/master' into sparrowDom/rebaseEl…
sparrowDom Dec 17, 2024
c980607
optimise gas when setting alternativeCreditsPerToken (#2325)
sparrowDom Dec 17, 2024
07b3e70
Certora Formal Verification for OUSD (#2329)
Roy-Certora Dec 19, 2024
18d4a51
Fix certora spec. Starting with an invalid state would result in an i…
DanielVF Jan 7, 2025
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
36 changes: 0 additions & 36 deletions contracts/contracts/echidna/EchidnaHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,42 +131,6 @@ contract EchidnaHelper is EchidnaSetup {
ousd.approve(spender, amount);
}

/**
* @notice Increase the allowance of an account to spend OUSD
* @param ownerAcc Account that owns the OUSD
* @param spenderAcc Account that is approved to spend the OUSD
* @param amount Amount to increase the allowance by
*/
function increaseAllowance(
uint8 ownerAcc,
uint8 spenderAcc,
uint256 amount
) public {
address owner = getAccount(ownerAcc);
address spender = getAccount(spenderAcc);
hevm.prank(owner);
// slither-disable-next-line unused-return
ousd.increaseAllowance(spender, amount);
}

/**
* @notice Decrease the allowance of an account to spend OUSD
* @param ownerAcc Account that owns the OUSD
* @param spenderAcc Account that is approved to spend the OUSD
* @param amount Amount to decrease the allowance by
*/
function decreaseAllowance(
uint8 ownerAcc,
uint8 spenderAcc,
uint256 amount
) public {
address owner = getAccount(ownerAcc);
address spender = getAccount(spenderAcc);
hevm.prank(owner);
// slither-disable-next-line unused-return
ousd.decreaseAllowance(spender, amount);
}

/**
* @notice Get the sum of all OUSD balances
* @return total Total balance
Expand Down
2 changes: 1 addition & 1 deletion contracts/contracts/echidna/EchidnaSetup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract EchidnaSetup is EchidnaConfig {
* @notice Deploy the OUSD contract and set up initial state
*/
constructor() {
ousd.initialize("Origin Dollar", "OUSD", ADDRESS_VAULT, 1e18);
ousd.initialize(ADDRESS_VAULT, 1e18);

// Deploy dummny contracts as users
Dummy outsider = new Dummy();
Expand Down
42 changes: 0 additions & 42 deletions contracts/contracts/echidna/EchidnaTestApproval.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,46 +94,4 @@ contract EchidnaTestApproval is EchidnaTestMintBurn {

assert(allowanceAfter2 == amount / 2);
}

/**
* @notice Increasing the allowance should raise it by the amount provided
* @param ownerAcc The account that is approving
* @param spenderAcc The account that is being approved
* @param amount The amount to approve
*/
function testIncreaseAllowance(
uint8 ownerAcc,
uint8 spenderAcc,
uint256 amount
) public {
address owner = getAccount(ownerAcc);
address spender = getAccount(spenderAcc);

uint256 allowanceBefore = ousd.allowance(owner, spender);
increaseAllowance(ownerAcc, spenderAcc, amount);
uint256 allowanceAfter = ousd.allowance(owner, spender);

assert(allowanceAfter == allowanceBefore + amount);
}

/**
* @notice Decreasing the allowance should lower it by the amount provided
* @param ownerAcc The account that is approving
* @param spenderAcc The account that is being approved
* @param amount The amount to approve
*/
function testDecreaseAllowance(
uint8 ownerAcc,
uint8 spenderAcc,
uint256 amount
) public {
address owner = getAccount(ownerAcc);
address spender = getAccount(spenderAcc);

uint256 allowanceBefore = ousd.allowance(owner, spender);
decreaseAllowance(ownerAcc, spenderAcc, amount);
uint256 allowanceAfter = ousd.allowance(owner, spender);

assert(allowanceAfter == allowanceBefore - amount);
}
}
3 changes: 2 additions & 1 deletion contracts/contracts/echidna/OUSDEchidna.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ contract OUSDEchidna is OUSD {
public
returns (bool)
{
return _isNonRebasingAccount(_account);
_autoMigrate(_account);
return alternativeCreditsPerToken[_account] > 0;
}
}
32 changes: 22 additions & 10 deletions contracts/contracts/flipper/Flipper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract Flipper is Governable {

// Settable coin addresses allow easy testing and use of mock currencies.
IERC20 immutable dai;
OUSD immutable ousd;
address immutable ousd;
IERC20 immutable usdc;
Tether immutable usdt;

Expand All @@ -37,7 +37,7 @@ contract Flipper is Governable {
require(address(_usdc) != address(0));
require(address(_usdt) != address(0));
dai = IERC20(_dai);
ousd = OUSD(_ousd);
ousd = _ousd;
usdc = IERC20(_usdc);
usdt = Tether(_usdt);
}
Expand All @@ -54,7 +54,10 @@ contract Flipper is Governable {
dai.transferFrom(msg.sender, address(this), amount),
"DAI transfer failed"
);
require(ousd.transfer(msg.sender, amount), "OUSD transfer failed");
require(
IERC20(ousd).transfer(msg.sender, amount),
"OUSD transfer failed"
);
}

/// @notice Sell OUSD for Dai
Expand All @@ -63,7 +66,7 @@ contract Flipper is Governable {
require(amount <= MAXIMUM_PER_TRADE, "Amount too large");
require(dai.transfer(msg.sender, amount), "DAI transfer failed");
require(
ousd.transferFrom(msg.sender, address(this), amount),
IERC20(ousd).transferFrom(msg.sender, address(this), amount),
"OUSD transfer failed"
);
}
Expand All @@ -77,7 +80,10 @@ contract Flipper is Governable {
usdc.transferFrom(msg.sender, address(this), amount / 1e12),
"USDC transfer failed"
);
require(ousd.transfer(msg.sender, amount), "OUSD transfer failed");
require(
IERC20(ousd).transfer(msg.sender, amount),
"OUSD transfer failed"
);
}

/// @notice Sell OUSD for USDC
Expand All @@ -89,7 +95,7 @@ contract Flipper is Governable {
"USDC transfer failed"
);
require(
ousd.transferFrom(msg.sender, address(this), amount),
IERC20(ousd).transferFrom(msg.sender, address(this), amount),
"OUSD transfer failed"
);
}
Expand All @@ -102,7 +108,10 @@ contract Flipper is Governable {
// USDT does not return a boolean and reverts,
// so no need for a require.
usdt.transferFrom(msg.sender, address(this), amount / 1e12);
require(ousd.transfer(msg.sender, amount), "OUSD transfer failed");
require(
IERC20(ousd).transfer(msg.sender, amount),
"OUSD transfer failed"
);
}

/// @notice Sell OUSD for USDT
Expand All @@ -113,7 +122,7 @@ contract Flipper is Governable {
// so no need for a require.
usdt.transfer(msg.sender, amount / 1e12);
require(
ousd.transferFrom(msg.sender, address(this), amount),
IERC20(ousd).transferFrom(msg.sender, address(this), amount),
"OUSD transfer failed"
);
}
Expand All @@ -125,7 +134,7 @@ contract Flipper is Governable {
/// @dev Opting into yield reduces the gas cost per transfer by about 4K, since
/// ousd needs to do less accounting and one less storage write.
function rebaseOptIn() external onlyGovernor nonReentrant {
ousd.rebaseOptIn();
OUSD(ousd).rebaseOptIn();
}

/// @notice Owner function to withdraw a specific amount of a token
Expand All @@ -142,7 +151,10 @@ contract Flipper is Governable {
/// again by transferring assets to the contract.
function withdrawAll() external onlyGovernor nonReentrant {
IERC20(dai).safeTransfer(_governor(), dai.balanceOf(address(this)));
IERC20(ousd).safeTransfer(_governor(), ousd.balanceOf(address(this)));
IERC20(ousd).safeTransfer(
_governor(),
IERC20(ousd).balanceOf(address(this))
);
IERC20(address(usdt)).safeTransfer(
_governor(),
usdt.balanceOf(address(this))
Expand Down
4 changes: 2 additions & 2 deletions contracts/contracts/mocks/MockNonRebasing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ contract MockNonRebasing {
oUSD.transferFrom(_from, _to, _value);
}

function increaseAllowance(address _spender, uint256 _addedValue) public {
oUSD.increaseAllowance(_spender, _addedValue);
function approve(address _spender, uint256 _addedValue) public {
oUSD.approve(_spender, _addedValue);
}

function mintOusd(
Expand Down
25 changes: 25 additions & 0 deletions contracts/contracts/mocks/TestUpgradedOUSD.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../token/OUSD.sol";

// used to alter internal state of OUSD contract
contract TestUpgradedOUSD is OUSD {
constructor() OUSD() {}

function overwriteCreditBalances(address _account, uint256 _creditBalance)
public
{
creditBalances[_account] = _creditBalance;
}

function overwriteAlternativeCPT(address _account, uint256 _acpt) public {
alternativeCreditsPerToken[_account] = _acpt;
}

function overwriteRebaseState(address _account, RebaseOptions _rebaseOption)
public
{
rebaseState[_account] = _rebaseOption;
}
}
10 changes: 10 additions & 0 deletions contracts/contracts/token/OETH.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,15 @@ import { OUSD } from "./OUSD.sol";
* @author Origin Protocol Inc
*/
contract OETH is OUSD {
function symbol() external pure override returns (string memory) {
return "OETH";
}

function name() external pure override returns (string memory) {
return "Origin Ether";
}

function decimals() external pure override returns (uint8) {
return 18;
}
}
19 changes: 10 additions & 9 deletions contracts/contracts/token/OETHBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
pragma solidity ^0.8.0;

import { OUSD } from "./OUSD.sol";
import { InitializableERC20Detailed } from "../utils/InitializableERC20Detailed.sol";

/**
* @title OETH Token Contract
* @author Origin Protocol Inc
*/
contract OETHBase is OUSD {
/**
* @dev OETHb is already intialized on Base. So `initialize`
* cannot be used again. And the `name` and `symbol`
* methods aren't `virtual`. That's the reason this
* function exists.
*/
function initialize2() external onlyGovernor {
InitializableERC20Detailed._initialize("Super OETH", "superOETHb", 18);
function symbol() external pure override returns (string memory) {
return "superOETHb";
}

function name() external pure override returns (string memory) {
return "Super OETH";
}

function decimals() external pure override returns (uint8) {
return 18;
}
}
Loading
Loading