Skip to content

Commit

Permalink
tweak to also support chainlink interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrent committed Sep 24, 2024
1 parent 14da963 commit d7e68b0
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 105 deletions.
48 changes: 0 additions & 48 deletions contracts/facade/factories/CurveOracleFactory.sol

This file was deleted.

79 changes: 79 additions & 0 deletions contracts/facade/factories/ExchangeRateOracleFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// SPDX-License-Identifier: BlueOak-1.0.0
pragma solidity 0.8.19;

import { FIX_ONE, divuu } from "../../libraries/Fixed.sol";

// weird circular inheritance preventing us from using proper IRToken, not worth figuring out
interface IMinimalRToken {
function basketsNeeded() external view returns (uint192);

function totalSupply() external view returns (uint256);
}

contract ExchangeRateOracle {
error MissingRToken();

address public immutable rToken;

constructor(address _rToken) {
// allow address(0)
rToken = _rToken;
}

function exchangeRate() public view returns (uint256) {
address _rToken = rToken;
if (_rToken == address(0)) revert MissingRToken();

uint256 supply = IMinimalRToken(_rToken).totalSupply();
if (supply == 0) return FIX_ONE;

return divuu(uint256(IMinimalRToken(_rToken).basketsNeeded()), supply);
}

// basic chainlink interface sufficient for Morpho
function latestRoundData()
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
)
{
return (0, int256(exchangeRate()), 0, 0, 0);
}

function decimals() external pure returns (uint8) {
return 18;
}
}


/**
* @title ExchangeRateOracleFactory
* @notice An immutable factory for Exchange Rate Oracles
*/
contract ExchangeRateOracleFactory {
error OracleAlreadyDeployed();

event OracleDeployed(address indexed rToken, address indexed oracle);

mapping(address rToken => ExchangeRateOracle oracle) public oracles;

function deployOracle(address rToken) external returns (address) {
if (address(oracles[rToken]) != address(0)) revert OracleAlreadyDeployed();
ExchangeRateOracle oracle = new ExchangeRateOracle(rToken);

if (rToken != address(0)){
oracle.exchangeRate();
oracle.latestRoundData();
oracle.decimals();
}

oracles[rToken] = oracle;
emit OracleDeployed(address(rToken), address(oracle));
return address(oracle);
}
}
56 changes: 0 additions & 56 deletions tasks/deployment/create-curve-oracle-factory.ts

This file was deleted.

78 changes: 78 additions & 0 deletions tasks/deployment/create-exchange-rate-oracle-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { getChainId } from '../../common/blockchain-utils'
import { task, types } from 'hardhat/config'
import { ExchangeRateOracleFactory } from '../../typechain'

task('create-exchange-rate-oracle-factory', 'Deploys an ExchangeRateOracleFactory')
.addOptionalParam('noOutput', 'Suppress output', false, types.boolean)
.setAction(async (params, hre) => {
const [wallet] = await hre.ethers.getSigners()

const chainId = await getChainId(hre)

if (!params.noOutput) {
console.log(
`Deploying ExchangeRateOracleFactory to ${hre.network.name} (${chainId}) with burner account ${wallet.address}`
)
}

const CurveOracleFactoryFactory = await hre.ethers.getContractFactory(
'ExchangeRateOracleFactory'
)
const oracleFactory = <ExchangeRateOracleFactory>(
await CurveOracleFactoryFactory.connect(wallet).deploy()
)
await oracleFactory.deployed()

if (!params.noOutput) {
console.log(
`Deployed ExchangeRateOracleFactory to ${hre.network.name} (${chainId}): ${oracleFactory.address}`
)
console.log(
`Deploying dummy ExchangeRateOracle to ${hre.network.name} (${chainId}): ${oracleFactory.address}`
)
}

// Deploy dummy zero address oracle
const addr = await oracleFactory.callStatic.deployOracle(hre.ethers.constants.AddressZero)
await (await oracleFactory.deployOracle(hre.ethers.constants.AddressZero)).wait()

if (!params.noOutput) {
console.log(`Deployed dummy ExchangeRateOracle to ${hre.network.name} (${chainId}): ${addr}`)
}

// Uncomment to verify
if (!params.noOutput) {
console.log('sleeping 10s')
}

// Sleep to ensure API is in sync with chain
await new Promise((r) => setTimeout(r, 10000)) // 10s

if (!params.noOutput) {
console.log('verifying')
}

/** ******************** Verify ExchangeRateOracleFactory ****************************************/
console.time('Verifying ExchangeRateOracleFactory')
await hre.run('verify:verify', {
address: oracleFactory.address,
constructorArguments: [],
contract:
'contracts/facade/factories/ExchangeRateOracleFactory.sol:ExchangeRateOracleFactory',
})
console.timeEnd('Verifying ExchangeRateOracleFactory')

console.time('Verifying ExchangeRateOracle')
await hre.run('verify:verify', {
address: addr,
constructorArguments: [hre.ethers.constants.AddressZero],
contract: 'contracts/facade/factories/ExchangeRateOracleFactory.sol:ExchangeRateOracle',
})
console.timeEnd('Verifying ExchangeRateOracle')

if (!params.noOutput) {
console.log('verified')
}

return { oracleFactory: oracleFactory.address }
})
2 changes: 1 addition & 1 deletion tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import './deployment/mock/deploy-mock-aave'
import './deployment/mock/deploy-mock-wbtc'
import './deployment/deploy-easyauction'
import './deployment/create-deployer-registry'
import './deployment/create-curve-oracle-factory'
import './deployment/create-exchange-rate-oracle-factory'
import './deployment/deploy-facade-monitor'
import './deployment/empty-wallet'
import './deployment/cancel-tx'
Expand Down

0 comments on commit d7e68b0

Please sign in to comment.