Skip to content

Commit

Permalink
Update pricefeeds for rsETH and weETH on Mainnet WETH market (#878)
Browse files Browse the repository at this point in the history
Co-authored-by: dmitriy-woof-software <dmitriy@woof.software>
  • Loading branch information
MishaShWoof and dmitriy-woof-software authored Aug 16, 2024
1 parent 071dbbf commit b1ea9b0
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 0 deletions.
6 changes: 6 additions & 0 deletions contracts/IRateProvider.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.15;

interface IRateProvider {
function getRate() external view returns (uint256);
}
97 changes: 97 additions & 0 deletions contracts/pricefeeds/RateBasedScalingPriceFeed.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.15;

import "../vendor/@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "../IPriceFeed.sol";
import "../IRateProvider.sol";

/**
* @title Scaling price feed for rate based oracles
* @notice A custom price feed that scales up or down the price received from an underlying price feed and returns the result
* @author Compound
*/
contract RateBasedScalingPriceFeed is IPriceFeed {
/** Custom errors **/
error InvalidInt256();
error BadDecimals();

/// @notice Version of the price feed
uint public constant VERSION = 1;

/// @notice Description of the price feed
string public description;

/// @notice Number of decimals for returned prices
uint8 public immutable override decimals;

/// @notice Underlying price feed where prices are fetched from
address public immutable underlyingPriceFeed;

/// @notice Whether or not the price should be upscaled
bool internal immutable shouldUpscale;

/// @notice The amount to upscale or downscale the price by
int256 internal immutable rescaleFactor;

/**
* @notice Construct a new scaling price feed
* @param underlyingPriceFeed_ The address of the underlying price feed to fetch prices from
* @param decimals_ The number of decimals for the returned prices
**/
constructor(address underlyingPriceFeed_, uint8 decimals_, uint8 underlyingDecimals_, string memory description_) {
underlyingPriceFeed = underlyingPriceFeed_;
if (decimals_ > 18) revert BadDecimals();
decimals = decimals_;
description = description_;

uint8 priceFeedDecimals = underlyingDecimals_;
// Note: Solidity does not allow setting immutables in if/else statements
shouldUpscale = priceFeedDecimals < decimals_ ? true : false;
rescaleFactor = (shouldUpscale
? signed256(10 ** (decimals_ - priceFeedDecimals))
: signed256(10 ** (priceFeedDecimals - decimals_))
);
}

/**
* @notice Price for the latest round
* @return roundId Round id from the underlying price feed
* @return answer Latest price for the asset in terms of ETH
* @return startedAt Timestamp when the round was started; passed on from underlying price feed
* @return updatedAt Timestamp when the round was last updated; passed on from underlying price feed
* @return answeredInRound Round id in which the answer was computed; passed on from underlying price feed
**/
function latestRoundData() override external view returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
) {
uint256 rate = IRateProvider(underlyingPriceFeed).getRate();
return (1, scalePrice(signed256(rate)), block.timestamp, block.timestamp, 1);
}

function signed256(uint256 n) internal pure returns (int256) {
if (n > uint256(type(int256).max)) revert InvalidInt256();
return int256(n);
}

function scalePrice(int256 price) internal view returns (int256) {
int256 scaledPrice;
if (shouldUpscale) {
scaledPrice = price * rescaleFactor;
} else {
scaledPrice = price / rescaleFactor;
}
return scaledPrice;
}

/**
* @notice Current version of the price feed
* @return The version of the price feed contract
**/
function version() external pure returns (uint256) {
return VERSION;
}
}
97 changes: 97 additions & 0 deletions contracts/pricefeeds/RsETHScalingPriceFeed.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.15;

import "../vendor/@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "../vendor/kelp/ILRTOracle.sol";
import "../IPriceFeed.sol";

/**
* @title Scaling price feed for rsETH
* @notice A custom price feed that scales up or down the price received from an underlying Kelp price feed and returns the result
* @author Compound
*/
contract RsETHScalingPriceFeed is IPriceFeed {
/** Custom errors **/
error InvalidInt256();
error BadDecimals();

/// @notice Version of the price feed
uint public constant VERSION = 1;

/// @notice Description of the price feed
string public description;

/// @notice Number of decimals for returned prices
uint8 public immutable override decimals;

/// @notice Underlying Kelp price feed where prices are fetched from
address public immutable underlyingPriceFeed;

/// @notice Whether or not the price should be upscaled
bool internal immutable shouldUpscale;

/// @notice The amount to upscale or downscale the price by
int256 internal immutable rescaleFactor;

/**
* @notice Construct a new scaling price feed
* @param underlyingPriceFeed_ The address of the underlying price feed to fetch prices from
* @param decimals_ The number of decimals for the returned prices
**/
constructor(address underlyingPriceFeed_, uint8 decimals_, string memory description_) {
underlyingPriceFeed = underlyingPriceFeed_;
if (decimals_ > 18) revert BadDecimals();
decimals = decimals_;
description = description_;

uint8 underlyingPriceFeedDecimals = 18;
// Note: Solidity does not allow setting immutables in if/else statements
shouldUpscale = underlyingPriceFeedDecimals < decimals_ ? true : false;
rescaleFactor = (shouldUpscale
? signed256(10 ** (decimals_ - underlyingPriceFeedDecimals))
: signed256(10 ** (underlyingPriceFeedDecimals - decimals_))
);
}

/**
* @notice Price for the latest round
* @return roundId Round id from the underlying price feed
* @return answer Latest price for the asset in terms of ETH
* @return startedAt Timestamp when the round was started; passed on from underlying price feed
* @return updatedAt Timestamp when the round was last updated; passed on from underlying price feed
* @return answeredInRound Round id in which the answer was computed; passed on from underlying price feed
**/
function latestRoundData() override external view returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
) {
int256 price = signed256(ILRTOracle(underlyingPriceFeed).rsETHPrice());
return (1, scalePrice(price), block.timestamp, block.timestamp, 1);
}

function signed256(uint256 n) internal pure returns (int256) {
if (n > uint256(type(int256).max)) revert InvalidInt256();
return int256(n);
}

function scalePrice(int256 price) internal view returns (int256) {
int256 scaledPrice;
if (shouldUpscale) {
scaledPrice = price * rescaleFactor;
} else {
scaledPrice = price / rescaleFactor;
}
return scaledPrice;
}

/**
* @notice Current version of the price feed
* @return The version of the price feed contract
**/
function version() external pure returns (uint256) {
return VERSION;
}
}
6 changes: 6 additions & 0 deletions contracts/vendor/kelp/ILRTOracle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.15;

interface ILRTOracle {
function rsETHPrice() external view returns (uint256);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { DeploymentManager, migration } from '../../../../plugins/deployment_manager';
import { proposal } from '../../../../src/deploy';

import { expect } from 'chai';
import { ethers } from 'ethers';

const RSETH_ADDRESS = '0xA1290d69c65A6Fe4DF752f95823fae25cB99e5A7';
const RSETH_PRICEFEED_ADDRESS = '0x349A73444b1a310BAe67ef67973022020d70020d';
const WEETH_ADDRESS = '0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee';
const WEETH_PRICEFEED_ADDRESS = '0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee';

let newRsETHPriceFeed: string;
let newWeETHPriceFeed: string;

export default migration('1719835184_update_rseth_and_weth_pricefeed', {
async prepare(deploymentManager: DeploymentManager) {
const _rsETHPriceFeed = await deploymentManager.deploy(
'rsETH:priceFeed',
'pricefeeds/RsETHScalingPriceFeed.sol',
[RSETH_PRICEFEED_ADDRESS, 8, 'rsETH / ETH exchange rate'],
true
);

const _weETHPriceFeed = await deploymentManager.deploy(
'weETH:priceFeed',
'pricefeeds/RateBasedScalingPriceFeed.sol',
[WEETH_PRICEFEED_ADDRESS, 8, 18, 'weETH / ETH exchange rate'],
true
);
return { rsETHPriceFeed: _rsETHPriceFeed.address, weETHPriceFeed: _weETHPriceFeed.address };
},

async enact(deploymentManager: DeploymentManager, _, { rsETHPriceFeed, weETHPriceFeed }) {
const trace = deploymentManager.tracer();

const {
governor,
comet,
configurator,
cometAdmin,
} = await deploymentManager.getContracts();

newRsETHPriceFeed = rsETHPriceFeed;
newWeETHPriceFeed = weETHPriceFeed;
const actions = [
// 1. Update the price feed for rsETH
{
contract: configurator,
signature: 'updateAssetPriceFeed(address,address,address)',
args: [comet.address, RSETH_ADDRESS, rsETHPriceFeed],
},
// 2. Update the price feed for weETH
{
contract: configurator,
signature: 'updateAssetPriceFeed(address,address,address)',
args: [comet.address, WEETH_ADDRESS, weETHPriceFeed],
},
// 3. Deploy and upgrade to a new version of Comet
{
contract: cometAdmin,
signature: 'deployAndUpgradeTo(address,address)',
args: [configurator.address, comet.address],
},
];
const description = '# Update rsETH and weETH price feeds in cWETHv3 on Mainnet\n\n## Proposal summary\n\nThis proposal updates existing price feeds for rsETH and weETH collaterals in the WETH market on Mainnet from market rates to exchange rates. If exchange rate oracles are implemented, Gauntlet can recommend more capital efficient parameters as the asset remains insulated from market movements, although this exposes it to tail-end risks. The exchange rate based risk parameters could facilitate higher caps and Liquidation Factors along with more conservative Liquidation Penalties.\n\nFurther detailed information can be found on the corresponding [proposal pull request](https://github.com/compound-finance/comet/pull/878), [forum discussion for rsETH](https://www.comp.xyz/t/add-rseth-market-on-ethereum-mainnet/5118) and [forum discussion for weETH](https://www.comp.xyz/t/add-weeth-market-on-ethereum/5179).\n\n\n## Proposal Actions\n\nThe first proposal action updates rsETH price feed from market rate to exchange rate.\n\nThe second proposal action updates weETH price feed from market rate to exchange rate.\n\nThe third action deploys and upgrades Comet to a new version.';
const txn = await deploymentManager.retry(
async () => trace((await governor.propose(...await proposal(actions, description))))
);

const event = txn.events.find(event => event.event === 'ProposalCreated');
const [proposalId] = event.args;

trace(`Created proposal ${proposalId}.`);
},

async enacted(): Promise<boolean> {
return true;
},

async verify(deploymentManager: DeploymentManager) {
const {
comet,
configurator
} = await deploymentManager.getContracts();

const rsETH = new ethers.Contract(RSETH_ADDRESS, [
'function symbol() view returns (string)',
], deploymentManager.hre.ethers.provider);

const weETH = new ethers.Contract(WEETH_ADDRESS, [
'function symbol() view returns (string)',
], deploymentManager.hre.ethers.provider);

expect(await rsETH.symbol()).to.eq('rsETH');
const rsETHId = await configurator.getAssetIndex(comet.address, RSETH_ADDRESS);
expect(await weETH.symbol()).to.eq('weETH');
const weETHId = await configurator.getAssetIndex(comet.address, WEETH_ADDRESS);
const configuration = await configurator.getConfiguration(comet.address);
expect(configuration.assetConfigs[rsETHId].priceFeed).to.eq(newRsETHPriceFeed);
expect(configuration.assetConfigs[weETHId].priceFeed).to.eq(newWeETHPriceFeed);
},
});

0 comments on commit b1ea9b0

Please sign in to comment.