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

On-chain add weETH collateral to weth mainnet market #21

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
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);
}
179 changes: 179 additions & 0 deletions deployments/mainnet/weth/migrations/1718968177_add_weeth_collateral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import { expect } from 'chai';
import { DeploymentManager } from '../../../../plugins/deployment_manager/DeploymentManager';
import { migration } from '../../../../plugins/deployment_manager/Migration';
import { exp, proposal } from '../../../../src/deploy';

const WEETH_ADDRESS = '0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee';
const WEETH_PRICE_FEED_ADDRESS = '0x5c9C449BbC9a6075A2c061dF312a35fd1E05fF22';

export default migration('1718968177_add_weeth_collateral', {
async prepare(deploymentManager: DeploymentManager) {
const _weETHScalingPriceFeed = await deploymentManager.deploy(
'weETH:priceFeed',
'pricefeeds/ScalingPriceFeed.sol',
[
WEETH_PRICE_FEED_ADDRESS, // weETH / ETH price feed
8 // decimals
]
);
return { weETHScalingPriceFeed: _weETHScalingPriceFeed.address };
},

async enact(deploymentManager: DeploymentManager, _, { weETHScalingPriceFeed }) {

const trace = deploymentManager.tracer();

const weETH = await deploymentManager.existing(
'weETH',
WEETH_ADDRESS,
'mainnet',
'contracts/ERC20.sol:ERC20'
);
const weEthPricefeed = await deploymentManager.existing(
'weETH:priceFeed',
weETHScalingPriceFeed,
'mainnet'
);

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

// https://www.comp.xyz/t/add-weeth-market-on-ethereum/5179/3
const weETHAssetConfig = {
asset: weETH.address,
priceFeed: weEthPricefeed.address,
decimals: await weETH.decimals(),
borrowCollateralFactor: exp(0.82, 18),
liquidateCollateralFactor: exp(0.87, 18),
liquidationFactor: exp(0.92, 18),
supplyCap: exp(22_500, 18),
};

const mainnetActions = [
// 1. Add weETH as asset
{
contract: configurator,
signature: 'addAsset(address,(address,address,uint8,uint64,uint64,uint64,uint128))',
args: [comet.address, weETHAssetConfig],
},
// 2. Set new Annual Supply Interest Rate Slope High to 100%
{
contract: configurator,
signature: 'setSupplyPerYearInterestRateSlopeHigh(address,uint64)',
args: [
comet.address,
exp(1, 18) // newSupplyPerYearInterestRateSlopeHigh
],
},
// 3. Set new Annual Borrow Interest Rate Slope High to 115%
{
contract: configurator,
signature: 'setBorrowPerYearInterestRateSlopeHigh(address,uint64)',
args: [
comet.address,
exp(1.15, 18) // newBorrowPerYearInterestRateSlopeHigh
],
},
// 4. Deploy and upgrade to a new version of Comet
{
contract: cometAdmin,
signature: 'deployAndUpgradeTo(address,address)',
args: [configurator.address, comet.address],
},
];

const description = '# Add weETH as collateral into cWETHv3 on Mainnet\n\n## Proposal summary\n\nCompound Growth Program [AlphaGrowth] proposes to add weETH into cWETHv3 on Ethereum network. This proposal takes the governance steps recommended and necessary to update a Compound III WETH market on Ethereum. Simulations have confirmed the market’s readiness, as much as possible, using the [Comet scenario suite](https://github.com/compound-finance/comet/tree/main/scenario). The new parameters include setting the risk parameters based on the [recommendations from Gauntlet weETH](https://www.comp.xyz/t/add-weeth-market-on-ethereum/5179/3).\n\nFurther detailed information can be found on the corresponding [proposal pull request](PR - https://github.com/compound-finance/comet/pull/869) and [forum discussion weETH](https://www.comp.xyz/t/add-weeth-market-on-ethereum/5179).\n\n\n## Proposal Actions\n\nThe first proposal action adds weETH asset as collateral with corresponding configurations.\n\nThe second action sets new Annual Supply Interest Rate Slope High to 100%.\n\nThe third action sets new Annual Borrow Interest Rate Slope High to 115%.\n\nThe fourth action deploys and upgrades Comet to a new version.';
const txn = await deploymentManager.retry(async () =>
trace(
await governor.propose(...(await proposal(mainnetActions, description)))
)
);

const event = txn.events.find(
(event) => event.event === 'ProposalCreated'
);
const [proposalId] = event.args;
trace(`Created proposal ${proposalId}.`);
},

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

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

const weETHAssetIndex = Number(await comet.numAssets()) - 1;

const weETH = await deploymentManager.existing(
'weETH',
WEETH_ADDRESS,
'mainnet',
'contracts/ERC20.sol:ERC20'
);

const weETHAssetConfig = {
asset: weETH.address,
priceFeed: '',
decimals: await weETH.decimals(),
borrowCollateralFactor: exp(0.82, 18),
liquidateCollateralFactor: exp(0.87, 18),
liquidationFactor: exp(0.92, 18),
supplyCap: exp(22_500, 18)
};

// 1. Compare weETH asset config with Comet and Configurator asset config
const cometWeETHAssetInfo = await comet.getAssetInfoByAddress(
WEETH_ADDRESS
);
expect(weETHAssetIndex).to.be.equal(cometWeETHAssetInfo.offset);
expect(weETHAssetConfig.asset).to.be.equal(cometWeETHAssetInfo.asset);
expect(exp(1, weETHAssetConfig.decimals)).to.be.equal(
cometWeETHAssetInfo.scale
);
expect(weETHAssetConfig.borrowCollateralFactor).to.be.equal(
cometWeETHAssetInfo.borrowCollateralFactor
);
expect(weETHAssetConfig.liquidateCollateralFactor).to.be.equal(
cometWeETHAssetInfo.liquidateCollateralFactor
);
expect(weETHAssetConfig.liquidationFactor).to.be.equal(
cometWeETHAssetInfo.liquidationFactor
);
expect(weETHAssetConfig.supplyCap).to.be.equal(
cometWeETHAssetInfo.supplyCap
);

const configuratorWeETHAssetConfig = (
await configurator.getConfiguration(comet.address)
).assetConfigs[weETHAssetIndex];
expect(weETHAssetConfig.asset).to.be.equal(
configuratorWeETHAssetConfig.asset
);
expect(weETHAssetConfig.decimals).to.be.equal(
configuratorWeETHAssetConfig.decimals
);
expect(weETHAssetConfig.borrowCollateralFactor).to.be.equal(
configuratorWeETHAssetConfig.borrowCollateralFactor
);
expect(weETHAssetConfig.liquidateCollateralFactor).to.be.equal(
configuratorWeETHAssetConfig.liquidateCollateralFactor
);
expect(weETHAssetConfig.liquidationFactor).to.be.equal(
configuratorWeETHAssetConfig.liquidationFactor
);
expect(weETHAssetConfig.supplyCap).to.be.equal(
configuratorWeETHAssetConfig.supplyCap
);

// 2. Check new Annual Supply Interest Rate Slope High
expect(exp(1, 18) / BigInt(31_536_000)).to.be.equal(await comet.supplyPerSecondInterestRateSlopeHigh());

// 3. Check new Annual Borrow Interest Rate Slope High
expect(exp(1.15, 18) / BigInt(31_536_000)).to.be.equal(await comet.borrowPerSecondInterestRateSlopeHigh());
},
});
Loading