Skip to content

Commit

Permalink
solidity impl
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrent committed Oct 16, 2023
1 parent b0b3cff commit 42a119b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
29 changes: 18 additions & 11 deletions contracts/plugins/assets/RTokenAsset.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ contract RTokenAsset is IAsset, VersionedAsset, IRTokenOracle {

// Component addresses are not mutable in protocol, so it's safe to cache these
IMain public immutable main;
IBasketHandler public immutable basketHandler;
IAssetRegistry public immutable assetRegistry;
IBackingManager public immutable backingManager;
IBasketHandler public immutable basketHandler;
IFurnace public immutable furnace;
IERC20 public immutable rsr;
IStRSR public immutable stRSR;

IERC20Metadata public immutable erc20;
IERC20Metadata public immutable erc20; // The RToken

uint8 public immutable erc20Decimals;

Expand All @@ -39,10 +41,12 @@ contract RTokenAsset is IAsset, VersionedAsset, IRTokenOracle {
require(maxTradeVolume_ > 0, "invalid max trade volume");

main = erc20_.main();
basketHandler = main.basketHandler();
assetRegistry = main.assetRegistry();
backingManager = main.backingManager();
basketHandler = main.basketHandler();
furnace = main.furnace();
rsr = main.rsr();
stRSR = main.stRSR();

erc20 = IERC20Metadata(address(erc20_));
erc20Decimals = erc20_.decimals();
Expand Down Expand Up @@ -79,18 +83,15 @@ contract RTokenAsset is IAsset, VersionedAsset, IRTokenOracle {
assert(low <= high); // not obviously true
}

// solhint-disable no-empty-blocks
function refresh() public virtual override {
// No need to save lastPrice; can piggyback off the backing collateral's saved prices

if (msg.sender != address(assetRegistry)) assetRegistry.refresh();
furnace.melt();

cachedOracleData.cachedAtTime = 0; // force oracle refresh
cachedOracleData.cachedAtTime = 0; // clear oracle cache
}

// solhint-enable no-empty-blocks

/// Should not revert
/// @dev See `tryPrice` caveat about possible compounding error in calculating price
/// @return {UoA/tok} The lower end of the price estimate
Expand Down Expand Up @@ -130,10 +131,13 @@ contract RTokenAsset is IAsset, VersionedAsset, IRTokenOracle {

// solhint-enable no-empty-blocks

/// Force an update to the cache, including refreshing underlying assets
/// @dev Can revert if RToken is unpriced
function forceUpdatePrice() external {
_updateCachedPrice();
}

/// @dev Can revert if RToken is unpriced
function latestPrice() external returns (uint192 rTokenPrice, uint256 updatedAt) {
// Situations that require an update, from most common to least common.
if (
Expand All @@ -152,6 +156,9 @@ contract RTokenAsset is IAsset, VersionedAsset, IRTokenOracle {

// Update Oracle Data
function _updateCachedPrice() internal {
assetRegistry.refresh();
furnace.melt();

(uint192 low, uint192 high) = price();

require(low != 0 && high != FIX_MAX, "invalid price");
Expand Down Expand Up @@ -183,12 +190,12 @@ contract RTokenAsset is IAsset, VersionedAsset, IRTokenOracle {
TradingContext memory ctx;

ctx.basketsHeld = basketsHeld;
ctx.ar = assetRegistry;
ctx.bm = backingManager;
ctx.bh = basketHandler;
ctx.ar = assetRegistry;
ctx.stRSR = main.stRSR();
ctx.rsr = main.rsr();
ctx.rToken = main.rToken();
ctx.rsr = rsr;
ctx.rToken = IRToken(address(erc20));
ctx.stRSR = stRSR;
ctx.minTradeVolume = backingManager.minTradeVolume();
ctx.maxTradeSlippage = backingManager.maxTradeSlippage();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: BlueOak-1.0.0
pragma solidity 0.8.19;

import "../RTokenAsset.sol";
import "./CurveStableMetapoolCollateral.sol";

/**
Expand Down Expand Up @@ -42,6 +43,11 @@ contract CurveStableRTokenMetapoolCollateral is CurveStableMetapoolCollateral {
pairedAssetRegistry = IRToken(address(pairedToken)).main().assetRegistry();
}

function refresh() public override {
pairedAssetRegistry.refresh(); // refresh all registered assets
super.refresh(); // already handles all necessary default checks
}

/// Can revert, used by `_anyDepeggedOutsidePool()`
/// Should not return FIX_MAX for low
/// @return lowPaired {UoA/pairedTok} The low price estimate of the paired token
Expand Down

0 comments on commit 42a119b

Please sign in to comment.