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

Upgrade: Notional v3 [Contract Changes Only Diff] #108

Closed
wants to merge 27 commits into from

Conversation

jeffywu
Copy link
Contributor

@jeffywu jeffywu commented Mar 26, 2023

No description provided.

This commit updates the pure math libraries used in Notional V3. Most changes are relatively straightforward to review. The SafeInt256#negChange method is used extensively to detect changes in debt balances in Notional V3. Tests for this method can be seen in tests/internal/math/test_neg_change.py.
- The “Reserve” address is split into two separate addresses, the FEE_RESERVE and the SETTLEMENT_RESERVE. Note that methods guarded by requireValidAccount will fail if either of these constants are used as accounts.
- Beacon addresses in Deployments.sol are temporary and may change in various testing environments. The deployments.py file will print out the proper addresses and they may need to be updated.
- The NOTIONAL_V2_FINAL_SETTLEMENT constant has not yet been set, it will be set prior to actual deployment to mark the final cutover timestamp from Notional V2.
- Some storage slot enums are marked as deprecated, all other new storage slot enums are appended.
- Storage structs may have variables marked as deprecated, but no existing storage structs will have any variables removed. Only structs with names that end in “Storage” are used in storage, other structs are only used in memory.
- Removes interest rate calculation logic from Market.sol and adds a new file, InterestRateCurve.sol to implement interest rate calculation logic.
- New views are added to inspect the interest rate parameters.
- CalculationViews, TradingAction, nTokenMintAction are updated to point to the new respective functions.
- InitializeMarketsAction is updated to use the new interest rate curve calculation.
- Anchor rates are deprecated in nTokenHandler.sol, they are no longer relevant in the new AMM curve
Adds a variable rate lending and borrowing market called "Prime Cash". Deprecates Asset Rate calculations used in Notional V2 to convert cToken denominations into underlying. DeprecatedAssetRate calculations are only used in places that require backwards compatibility.
Some system accounts like the FEE_RESERVE and the nToken can only hold positive cash balances. Getting and setting these balances is simplified in this commit.

For all other accounts, the getters and setters for balance storage are updated. _setBalanceStorage will convert the “signed prime supply value” stored on the stack and in memory to the proper prime cash storage value via convertToStorageNonSettlementNonVault.

Methods that use the balance handler getters are updated. Not all updates to balance setters are in this commit.
This larger commit makes a number of changes to how Notional handles tokens. A summary of the changes is as follows:

- Notional V3 will no longer have the notion of “Asset Tokens” for newly listed currencies. Existing currencies that already have “asset tokens” will remain for backwards compatibility reasons. All of these tokens are Compound V2 tokens, therefore the AaveHandler and aTokenAggregator are removed.
- Generic Token now supports redemption to WETH.
- Token Handler will manage deposits and withdraws of underlying tokens. During deposit and withdraw, total prime supply and total underlying value prime cash factors are updated. Stored token balances are also updated to ensure that we have internal tracking for token balances.
- Deposits and withdrawals are also simplified in Balance Handler to match the Token Handler changes.
- AccountAction, BatchAction, and Liquidation methods are updated to use the new deposit and withdraw functionality.
- Views are updated to reflect the depreciation of asset tokens
A breaking change to the nToken to mint calculation. Previously, this calculation accepted values in asset tokens, which are no longer accepted. It now takes intputs in underlying tokens. It's unclear if this may break integrations and may need to change.
 - Liquidity Token settlement is moved into Initialize Markets when the nToken is settled. No other accounts are allowed to hold liquidity tokens. This greatly simplifies the logic in SettlePortfolioAssets.
 - The general logic of SettleBitmapAssets and SettlePortfolioAssets stays the same, however, now settled positive and negative balances are tracked separately and they are stored in finalizeSettleAmounts.
 - Prior to settlement, total outstanding fCash debt is tracked in nTokenRedeemAction, AccountContextHandler, and BitmapAssetsHandler.
 - Removes SettledCashDebt from TradingAction, it is no longer required.
Adds a boolean flag on account context to allow accounts to borrow prime cash. Removes the check sufficient cash method in Batch Action. Accounts may still incur prime debt due to fCash settlement or liquidation, the allowPrimeBorrow flag exists mainly as a safety check for UX.
Changes the ETH exchange rate buffer scale above 150 units so that it scales up to 1200%. This imposes a soft debt cap on a currency. The aim is to allow more capital efficient borrowing for soft capped currencies via leveraged vaults where hard debt caps are enforced.
Adds prime supply cap cap checks to deposits in Account Action and Batch Action. Supply caps are not enforced during withdrawals or liquidations.
During cross currency liquidation, accounts will be forced to borrow against fCash and nToken collateral in order to transfer exchange rate risk into interest rate risk.
Liquidity tokens are formally eliminated from being held in portfolios. They were never allowed to be held there in Notional V2 and this commit eliminates the unused code paths in valuation and liquidation.
Adds an event emission library for Notional V3 Transfer events.
Refactors nToken mint and redeem code to emit proper Transfer events. nTokenRedeem has quite a few lines of changes, but these are primarily to deal with stack size issues. There are no intended business logic changes.

The erroneous “hasResidual” boolean is removed in favor of checking the newifCashAssets array length. This boolean was not set properly in prior code and required a hot fix.
Adds proper event emissions during liquidation.
Adds proper event emissions to trading action.
A significant re-write of the leveraged vaults module. The leveraged vaults module is relatively isolated from the rest of Notional. It primarily affects the fCash and prime cash markets via lending and borrowing.
Moves the storeAssetsAndUpdateContext method from Batch Action into Settle Assets External. No logical changes are intended.
Moves updateIncentiveEmissionRate from GovernanceAction to Treasury Action. No logical changes are intended.
Moves sweepCashIntoMarkets to nToken Mint Action. No logical changes are intended.
Moves code that updates assets in portfolios to use the Settle Assets External library. No logical changes are intended.
Refactors ERC1155 action to use the event emitter code for encoding and decoding ids. The logic is intended to be effectively the same, with the additional support of leveraged vault ERC1155 ids.
Updates governance action with new parameters to set. updateGlobalTransferOperator and setSecondaryIncentiveRewarder are currently unused and there are no immediate plans to activate them so they are removed to stay below the bytecode size limit.
Adds new upgradeable beacon proxies. ERC4626 method signatures are available but stateful methods (mint, redeem, deposit, withdraw) are not yet implemented.
Allows governance to set targets for various money market holdings in Notional V3 and execute a proportional rebalancing strategy. Two example Prime Cash Holdings oracles for Compound V2 and underlying tokens are given.
Adds a new contract that will migrate Notional V2 to Notional V3. The migration algorithm is described in V3Environment.py.
@jeffywu jeffywu closed this Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant