-
Notifications
You must be signed in to change notification settings - Fork 107
port ctoken oracle #57
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,36 @@ | ||
| /* | ||
| Copyright 2021 Set Labs Inc. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| pragma solidity 0.6.10; | ||
|
|
||
|
|
||
| /** | ||
| * @title ICToken | ||
| * @author Set Protocol | ||
| * | ||
| * Interface for interacting with Compound cTokens | ||
| */ | ||
| interface ICToken { | ||
|
|
||
| /** | ||
| * Calculates the exchange rate from the underlying to the CToken | ||
| * | ||
| * @notice Accrue interest then return the up-to-date exchange rate | ||
| * @return Calculated exchange rate scaled by 1e18 | ||
| */ | ||
| function exchangeRateCurrent() external returns (uint256); | ||
|
|
||
| function exchangeRateStored() external view returns (uint256); | ||
|
|
||
| function decimals() external view returns(uint8); | ||
| } | ||
This file contains hidden or 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,99 @@ | ||
| /* | ||
| Copyright 2021 Set Labs Inc. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| pragma solidity 0.6.10; | ||
|
|
||
| import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol"; | ||
|
|
||
| import { ICToken } from "../../../interfaces/external/ICToken.sol"; | ||
| import { IOracle } from "../../../interfaces/IOracle.sol"; | ||
|
|
||
|
|
||
| /** | ||
| * @title CTokenOracle | ||
| * @author Set Protocol | ||
| * | ||
| * Oracle built to retrieve the cToken price | ||
MarioCerdan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| contract CTokenOracle is IOracle | ||
| { | ||
MarioCerdan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| using SafeMath for uint256; | ||
|
|
||
| /* ============ State Variables ============ */ | ||
| ICToken public cToken; | ||
|
||
| IOracle public underlyingOracle; // Underlying token oracle | ||
| string public dataDescription; | ||
|
|
||
| // Exchange Rate values are scaled by 1e18 | ||
| uint256 internal constant scalingFactor = 10 ** 18; | ||
MarioCerdan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // CToken Full Unit | ||
| uint256 public cTokenFullUnit; | ||
|
|
||
| // Underlying Asset Full Unit | ||
| uint256 public underlyingFullUnit; | ||
|
|
||
| /* ============ Constructor ============ */ | ||
|
|
||
| /* | ||
| * @param _cToken The address of Compound Token | ||
| * @param _underlyingOracle The address of the underlying oracle | ||
| * @param _cTokenFullUnit The full unit of the Compound Token | ||
| * @param _underlyingFullUnit The full unit of the underlying asset | ||
| * @param _dataDescription Human readable description of oracle | ||
| */ | ||
| constructor( | ||
| ICToken _cToken, | ||
| IOracle _underlyingOracle, | ||
| uint256 _cTokenFullUnit, | ||
| uint256 _underlyingFullUnit, | ||
| string memory _dataDescription | ||
| ) | ||
| public | ||
| { | ||
| cToken = _cToken; | ||
| cTokenFullUnit = _cTokenFullUnit; | ||
| underlyingFullUnit = _underlyingFullUnit; | ||
| underlyingOracle = _underlyingOracle; | ||
| dataDescription = _dataDescription; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the price value of a full cToken denominated in underlyingOracle value | ||
| & | ||
MarioCerdan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * The underlying oracle is assumed to return a price of 18 decimal | ||
| * for a single full token of the underlying asset. The derived price | ||
| * of the cToken is then the price of a unit of underlying multiplied | ||
| * by the exchangeRate, adjusted for decimal differences, and descaled. | ||
| */ | ||
| function read() | ||
| external | ||
| override | ||
| view | ||
| returns (uint256) | ||
| { | ||
| // Retrieve the price of the underlying | ||
| uint256 underlyingPrice = underlyingOracle.read(); | ||
|
|
||
| // Retrieve cToken underlying to cToken stored conversion rate | ||
| uint256 conversionRate = cToken.exchangeRateStored(); | ||
|
|
||
| // Price of underlying is the price value / Token * conversion / scaling factor | ||
| // Values need to be converted based on full unit quantities | ||
| return underlyingPrice | ||
| .mul(conversionRate) | ||
| .mul(cTokenFullUnit) | ||
| .div(underlyingFullUnit) | ||
| .div(scalingFactor); | ||
MarioCerdan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or 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,155 @@ | ||
| import "module-alias/register"; | ||
| import { BigNumber } from "@ethersproject/bignumber"; | ||
|
|
||
| import { Address } from "@utils/types"; | ||
| import { Account } from "@utils/test/types"; | ||
| import { CERc20 } from "@utils/contracts/compound"; | ||
| import { OracleMock, CTokenOracle } from "@utils/contracts"; | ||
| import DeployHelper from "@utils/deploys"; | ||
|
|
||
| import { | ||
| ether | ||
| } from "@utils/index"; | ||
| import { | ||
| getAccounts, | ||
| getWaffleExpect, | ||
| getSystemFixture, | ||
| getCompoundFixture, | ||
| addSnapshotBeforeRestoreAfterEach, | ||
| } from "@utils/test/index"; | ||
| import { CompoundFixture, SystemFixture } from "@utils/fixtures"; | ||
|
|
||
| const expect = getWaffleExpect(); | ||
|
|
||
| describe("CTokenOracle", () => { | ||
| let owner: Account; | ||
| let deployer: DeployHelper; | ||
| let setup: SystemFixture; | ||
|
|
||
| let compoundSetup: CompoundFixture; | ||
| let cDai: CERc20; | ||
| let exchangeRate: BigNumber; | ||
| let daiUsdcOracle: OracleMock; | ||
| let cDaiOracle: CTokenOracle; | ||
| let cDaiFullUnit: BigNumber; | ||
| let daiFullUnit: BigNumber; | ||
|
|
||
| before(async () => { | ||
| [ | ||
| owner, | ||
| ] = await getAccounts(); | ||
|
|
||
| // System setup | ||
| deployer = new DeployHelper(owner.wallet); | ||
| setup = getSystemFixture(owner.address); | ||
| await setup.initialize(); | ||
|
|
||
| // Compound setup | ||
| compoundSetup = getCompoundFixture(owner.address); | ||
| await compoundSetup.initialize(); | ||
|
|
||
| exchangeRate = ether(0.5); | ||
|
|
||
| cDai = await compoundSetup.createAndEnableCToken( | ||
| setup.dai.address, | ||
| exchangeRate, | ||
| compoundSetup.comptroller.address, | ||
| compoundSetup.interestRateModel.address, | ||
| "Compound DAI", | ||
| "cDAI", | ||
| 8, | ||
| ether(0.75), // 75% collateral factor | ||
| ether(1) | ||
| ); | ||
|
|
||
| daiUsdcOracle = await deployer.mocks.deployOracleMock(ether(1)); | ||
| cDaiFullUnit = BigNumber.from("100000000"); | ||
| daiFullUnit = BigNumber.from("1000000000000000000"); | ||
| cDaiOracle = await deployer.oracles.deployCTokenOracle( | ||
| cDai.address, | ||
| daiUsdcOracle.address, | ||
| cDaiFullUnit, | ||
| daiFullUnit, | ||
| "cDAI Oracle" | ||
| ); | ||
|
|
||
| }); | ||
|
|
||
| addSnapshotBeforeRestoreAfterEach(); | ||
|
|
||
| describe("#constructor", async () => { | ||
| let subjectCToken: Address; | ||
| let subjectUnderlyingOracle: Address; | ||
| let subjectCTokenFullUnit: BigNumber; | ||
| let subjectUnderlyingFullUnit: BigNumber; | ||
| let subjectDataDescription: string; | ||
|
|
||
| before(async () => { | ||
| subjectCToken = cDai.address; | ||
| subjectCTokenFullUnit = BigNumber.from("100000000"); | ||
| subjectUnderlyingFullUnit = BigNumber.from("1000000000000000000"); | ||
| subjectUnderlyingOracle = daiUsdcOracle.address; | ||
| subjectDataDescription = "cDAI Oracle"; | ||
| }); | ||
|
|
||
| async function subject(): Promise<CTokenOracle> { | ||
| return deployer.oracles.deployCTokenOracle( | ||
| subjectCToken, | ||
| subjectUnderlyingOracle, | ||
| subjectCTokenFullUnit, | ||
| subjectUnderlyingFullUnit, | ||
| subjectDataDescription | ||
| ); | ||
| } | ||
|
|
||
| it("sets the correct cToken address", async () => { | ||
| const cTokenOracle = await subject(); | ||
| const cTokenAddress = await cTokenOracle.cToken(); | ||
| expect(cTokenAddress).to.equal(subjectCToken); | ||
| }); | ||
|
|
||
| it("sets the correct cToken full unit", async () => { | ||
| const cTokenOracle = await subject(); | ||
| const cTokenFullUnit = await cTokenOracle.cTokenFullUnit(); | ||
| expect(cTokenFullUnit).to.eq(subjectCTokenFullUnit); | ||
| }); | ||
|
|
||
| it("sets the correct underlying full unit", async () => { | ||
| const cTokenOracle = await subject(); | ||
| const underlyingFullUnit = await cTokenOracle.underlyingFullUnit(); | ||
| expect(underlyingFullUnit).to.eq(subjectUnderlyingFullUnit); | ||
| }); | ||
|
|
||
| it("sets the correct underlying oracle address", async () => { | ||
| const cTokenOracle = await subject(); | ||
| const underlyingOracleAddress = await cTokenOracle.underlyingOracle(); | ||
| expect(underlyingOracleAddress).to.eq(subjectUnderlyingOracle); | ||
| }); | ||
|
|
||
| it("sets the correct data description", async () => { | ||
| const cTokenOracle = await subject(); | ||
| const actualDataDescription = await cTokenOracle.dataDescription(); | ||
| expect(actualDataDescription).to.eq(subjectDataDescription); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
|
|
||
| describe("#read", async () => { | ||
|
|
||
| async function subject(): Promise<BigNumber> { | ||
| return cDaiOracle.read(); | ||
| } | ||
|
|
||
| it("returns the correct cTokenValue", async () => { | ||
| const result = await subject(); | ||
| const expectedResult = ether(1) | ||
| .mul(exchangeRate) | ||
| .mul(cDaiFullUnit) | ||
| .div(daiFullUnit) | ||
| .div(ether(1)); | ||
|
|
||
| expect(result).to.eq(expectedResult); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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
This file contains hidden or 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,27 @@ | ||
| import { Signer } from "ethers"; | ||
| import { Address } from "../types"; | ||
| import { BigNumber } from "@ethersproject/bignumber"; | ||
|
|
||
| import { | ||
| CTokenOracle, | ||
| } from "../contracts"; | ||
|
|
||
| import { CTokenOracle__factory } from "../../typechain/factories/CTokenOracle__factory"; | ||
|
|
||
| export default class DeployOracles { | ||
| private _deployerSigner: Signer; | ||
|
|
||
| constructor(deployerSigner: Signer) { | ||
| this._deployerSigner = deployerSigner; | ||
| } | ||
|
|
||
| public async deployCTokenOracle( | ||
| cToken: Address, | ||
| underlyingOracle: Address, | ||
| cTokenFullUnit: BigNumber, | ||
| underlyingFullUnit: BigNumber, | ||
| dataDescription: string): Promise<CTokenOracle> { | ||
| return await new CTokenOracle__factory(this._deployerSigner) | ||
| .deploy(cToken, underlyingOracle, cTokenFullUnit, underlyingFullUnit, dataDescription); | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I just realized we have an implementation for this, it's
ICErc20. Do you mind if we use that instead so we aren't duplicating code? It's in the same folder