-
Notifications
You must be signed in to change notification settings - Fork 111
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
Revenue Facet #1182
Merged
Merged
Revenue Facet #1182
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
823ca6b
facet: pull out version-compatible code into library
tbrent 1b85067
revenue facet for revenues across ecosystem
tbrent e795a0a
test revenue facet
tbrent fcff066
scripts
tbrent b343780
mainnet address
tbrent df4ebf1
add UoA to return values
tbrent 5e6878e
use ERC7201 for facet storage
tbrent ea71861
make private
tbrent ed681b9
tweak RevenueFacet
tbrent 5db8f8e
whoops, decimals
tbrent f78f409
polish
tbrent 7c1c08c
test
tbrent f9d2899
new addresses
tbrent 31099a6
fix comments
tbrent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// SPDX-License-Identifier: BlueOak-1.0.0 | ||
pragma solidity 0.8.19; | ||
|
||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import "../../interfaces/IAssetRegistry.sol"; | ||
import "../../interfaces/IBackingManager.sol"; | ||
import "../../interfaces/IBasketHandler.sol"; | ||
import "../../interfaces/IRToken.sol"; | ||
import "../lib/FacetLib.sol"; | ||
|
||
/** | ||
* @title AuctionsFacet | ||
* @notice Single-function facet to return all revenues accumulating across RTokens | ||
* @custom:static-call - Use ethers callStatic() to get result after update; do not execute | ||
*/ | ||
// slither-disable-start | ||
contract RevenueFacet { | ||
struct Revenue { | ||
IRToken rToken; | ||
IERC20 erc20; | ||
uint256 surplus; | ||
} | ||
|
||
// normally we don't let facets use storage, but this function requires it | ||
// we don't yet have a full solution for sharding storage across facets | ||
tbrent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// so for now we'll just have to live with a janky hardcoded gap. | ||
uint256[300] private __gap; | ||
Revenue[] _revenues; // empty at-rest | ||
|
||
/// Return revenues across multiple RTokens | ||
function revenues(IRToken[] memory rTokens) | ||
external | ||
returns (Revenue[] memory rTokenRevenues, Revenue[] memory rsrRevenues) | ||
{ | ||
for (uint256 i = 0; i < rTokens.length; ++i) { | ||
IMain main = rTokens[i].main(); | ||
IERC20[] memory erc20s = main.assetRegistry().erc20s(); | ||
|
||
// Forward ALL revenue | ||
FacetLib.forwardRevenue(main.backingManager(), erc20s); | ||
|
||
IRevenueTrader rTokenTrader = main.rTokenTrader(); | ||
IRevenueTrader rsrTrader = main.rsrTrader(); | ||
for (uint256 j = 0; j < erc20s.length; ++j) { | ||
IERC20 erc20 = erc20s[j]; | ||
|
||
// RTokenTrader -- Settle first if possible so have full available balances | ||
ITrade trade = rTokenTrader.trades(erc20); | ||
if (address(trade) != address(0) && trade.canSettle()) { | ||
FacetLib.settleTrade(rTokenTrader, erc20); | ||
} | ||
_revenues.push(Revenue(rTokens[i], erc20, erc20.balanceOf(address(rTokenTrader)))); | ||
|
||
// RSRTrader -- Settle first if possible so have full available balances | ||
trade = rsrTrader.trades(erc20); | ||
if (address(trade) != address(0) && trade.canSettle()) { | ||
FacetLib.settleTrade(rsrTrader, erc20); | ||
} | ||
_revenues.push(Revenue(rTokens[i], erc20, erc20.balanceOf(address(rsrTrader)))); | ||
} | ||
} | ||
|
||
// Empty storage queue in reverse order, we know evens are RSR revenues and odds are RToken | ||
rTokenRevenues = new Revenue[](_revenues.length / 2); | ||
rsrRevenues = new Revenue[](_revenues.length / 2); | ||
for (uint256 i = _revenues.length; i > 0; --i) { | ||
if (i % 2 == 0) rsrRevenues[(i - 1) / 2] = _revenues[i - 1]; | ||
else rTokenRevenues[(i - 1) / 2] = _revenues[i - 1]; | ||
_revenues.pop(); | ||
} | ||
assert(_revenues.length == 0); | ||
} | ||
} | ||
// slither-disable-end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// SPDX-License-Identifier: BlueOak-1.0.0 | ||
pragma solidity 0.8.19; | ||
|
||
import "@openzeppelin/contracts/utils/Address.sol"; | ||
import "../../interfaces/IBackingManager.sol"; | ||
import "../../interfaces/IRevenueTrader.sol"; | ||
import "../../interfaces/ITrade.sol"; | ||
import "../../interfaces/ITrading.sol"; | ||
import "../../plugins/trading/DutchTrade.sol"; | ||
import "../../plugins/trading/GnosisTrade.sol"; | ||
import "../../libraries/Fixed.sol"; | ||
|
||
library FacetLib { | ||
using Address for address; | ||
using FixLib for uint192; | ||
|
||
function getSellAmount(ITrade trade) internal view returns (uint256) { | ||
if (trade.KIND() == TradeKind.DUTCH_AUCTION) { | ||
return | ||
DutchTrade(address(trade)).sellAmount().shiftl_toUint( | ||
int8(trade.sell().decimals()) | ||
); | ||
} else if (trade.KIND() == TradeKind.BATCH_AUCTION) { | ||
return GnosisTrade(address(trade)).initBal(); | ||
} else { | ||
revert("invalid trade type"); | ||
} | ||
} | ||
|
||
function settleTrade(ITrading trader, IERC20 toSettle) internal { | ||
bytes1 majorVersion = bytes(trader.version())[0]; | ||
if (majorVersion == bytes1("3")) { | ||
// Settle auctions | ||
trader.settleTrade(toSettle); | ||
} else if (majorVersion == bytes1("2") || majorVersion == bytes1("1")) { | ||
address(trader).functionCall(abi.encodeWithSignature("settleTrade(address)", toSettle)); | ||
} else { | ||
_revertUnrecognizedVersion(); | ||
} | ||
} | ||
|
||
function forwardRevenue(IBackingManager bm, IERC20[] memory toStart) internal { | ||
bytes1 majorVersion = bytes(bm.version())[0]; | ||
// Need to use try-catch here in order to still show revenueOverview when basket not ready | ||
if (majorVersion == bytes1("3")) { | ||
// solhint-disable-next-line no-empty-blocks | ||
try bm.forwardRevenue(toStart) {} catch {} | ||
} else if (majorVersion == bytes1("2") || majorVersion == bytes1("1")) { | ||
// solhint-disable-next-line avoid-low-level-calls | ||
(bool success, ) = address(bm).call{ value: 0 }( | ||
abi.encodeWithSignature("manageTokens(address[])", toStart) | ||
); | ||
success = success; // hush warning | ||
} else { | ||
_revertUnrecognizedVersion(); | ||
} | ||
} | ||
|
||
function runRevenueAuctions( | ||
IRevenueTrader revenueTrader, | ||
IERC20[] memory toStart, | ||
TradeKind[] memory kinds | ||
) internal { | ||
bytes1 majorVersion = bytes(revenueTrader.version())[0]; | ||
|
||
if (majorVersion == bytes1("3")) { | ||
revenueTrader.manageTokens(toStart, kinds); | ||
} else if (majorVersion == bytes1("2") || majorVersion == bytes1("1")) { | ||
for (uint256 i = 0; i < toStart.length; ++i) { | ||
address(revenueTrader).functionCall( | ||
abi.encodeWithSignature("manageToken(address)", toStart[i]) | ||
); | ||
} | ||
} else { | ||
_revertUnrecognizedVersion(); | ||
} | ||
} | ||
|
||
function rebalance(IBackingManager bm, TradeKind kind) internal { | ||
bytes1 majorVersion = bytes(bm.version())[0]; | ||
|
||
if (majorVersion == bytes1("3")) { | ||
// solhint-disable-next-line no-empty-blocks | ||
try bm.rebalance(kind) {} catch {} | ||
} else if (majorVersion == bytes1("2") || majorVersion == bytes1("1")) { | ||
IERC20[] memory emptyERC20s = new IERC20[](0); | ||
// solhint-disable-next-line avoid-low-level-calls | ||
(bool success, ) = address(bm).call{ value: 0 }( | ||
abi.encodeWithSignature("manageTokens(address[])", emptyERC20s) | ||
); | ||
success = success; // hush warning | ||
} else { | ||
_revertUnrecognizedVersion(); | ||
} | ||
} | ||
|
||
function _revertUnrecognizedVersion() internal pure { | ||
revert("unrecognized version"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably need to rename to RevenueFacet?