-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tweak to also support chainlink interface
- Loading branch information
Showing
5 changed files
with
158 additions
and
105 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,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 } | ||
}) |
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