-
Notifications
You must be signed in to change notification settings - Fork 129
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
Add Open Oracle v1 #138
Open
hayesgm
wants to merge
2
commits into
master
Choose a base branch
from
hayesgm/open-oracle-v1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Open Oracle v1 #138
Changes from 1 commit
Commits
Show all changes
2 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 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 |
---|---|---|
|
@@ -10,3 +10,4 @@ | |
yarn-error.log | ||
junit.xml | ||
coverage/* | ||
.saddle_history |
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,44 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
pragma solidity ^0.6.10; | ||
pragma experimental ABIEncoderV2; | ||
|
||
import "../Uniswap/UniswapAnchoredView.sol"; | ||
|
||
interface IPriceOracle { | ||
function assetPrices(address) external returns (uint256); | ||
function getPrices(address) external returns (uint256); | ||
} | ||
|
||
/** | ||
* @title Open Oracle Adapater for v1 Interface | ||
* @author Compound Labs, Inc. | ||
*/ | ||
contract OpenOracleV1 is IPriceOracle { | ||
UniswapAnchoredView public immutable openOracleView; | ||
|
||
constructor(UniswapAnchoredView openOracleView_) public { | ||
openOracleView = openOracleView_; | ||
} | ||
|
||
function getPrices(address asset) public override returns (uint256) { | ||
UniswapConfig.TokenConfig memory ethConfig = openOracleView.getTokenConfigBySymbol("ETH"); | ||
UniswapConfig.TokenConfig memory cTokenConfig = openOracleView.getTokenConfigByUnderlying(asset); | ||
uint tokenPrice = openOracleView.getUnderlyingPrice(cTokenConfig.cToken); | ||
uint ethPrice = openOracleView.getUnderlyingPrice(ethConfig.cToken); | ||
require(ethPrice != 0, "eth price 0"); | ||
return mul(tokenPrice, 1e18) / ethPrice; | ||
} | ||
|
||
function assetPrices(address asset) public override returns (uint256) { | ||
return getPrices(asset); | ||
} | ||
|
||
/// @dev Overflow proof multiplication | ||
function mul(uint a, uint b) internal pure returns (uint) { | ||
if (a == 0) return 0; | ||
uint c = a * b; | ||
require(c / a == b, "multiplication overflow"); | ||
return c; | ||
} | ||
} |
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,70 @@ | ||
const { setup } = require('./UniswapAnchoredViewTest.js'); | ||
const { keccak256, time } = require('./Helpers'); | ||
const BigNumber = require('bignumber.js'); | ||
|
||
|
||
describe('UniswapAnchoredView', () => { | ||
let cToken; | ||
let token; | ||
let uniswapAnchoredView; | ||
let postPrices; | ||
|
||
describe('getUnderlyingPrice', () => { | ||
beforeEach(async () => { | ||
console.log("Hi"); | ||
({ | ||
cToken, | ||
token, | ||
postPrices, | ||
uniswapAnchoredView, | ||
} = await setup({isMockedView: true})); | ||
}); | ||
|
||
it('should return reported WBTC price', async () => { | ||
let openOracleV1 = await deploy('OpenOracleV1', [uniswapAnchoredView._address]); | ||
const timestamp = time() - 5; | ||
await send(uniswapAnchoredView, 'setAnchorPrice', ['ETH', 200e6]); | ||
await send(uniswapAnchoredView, 'setAnchorPrice', ['BTC', 10000e6]); | ||
await send(uniswapAnchoredView, 'setAnchorPrice', ['DAI', 1e6]); | ||
|
||
const tx = await postPrices(timestamp, [[['ETH', 200], ['BTC', 10000], ['DAI', 1]]], ['ETH', 'BTC', 'DAI']); | ||
const btcPrice = await call(uniswapAnchoredView, 'prices', [keccak256('BTC')]); | ||
|
||
expect(btcPrice).numEquals(10000e6); | ||
// priceInternal: returns 10000e6 | ||
// getUnderlyingPrice: 1e30 * 10000e6 / 1e8 = 1e32 | ||
let expected = new BigNumber('1e32'); | ||
expect(await call(uniswapAnchoredView, 'getUnderlyingPrice', [cToken.WBTC])).numEquals(expected.toFixed()); | ||
|
||
/* | ||
BTC = 10000 | ||
ETH = 200 | ||
BTC/ETH = 50 | ||
Decimalized: 50e28 (18 - 6 decimals = 12 + 18) | ||
*/ | ||
expect( | ||
await call(openOracleV1, 'getPrices', [token.WBTC])) | ||
.numEquals(new BigNumber('50e28').toFixed()); | ||
|
||
/* | ||
ETH = 200 | ||
ETH = 200 | ||
ETH/ETH = 1 | ||
Decimalized: 1e18 (18 - 18 decimals = 0 + 18) | ||
*/ | ||
expect( | ||
await call(openOracleV1, 'getPrices', [token.WETH])) | ||
.numEquals(new BigNumber('1e18').toFixed()); | ||
|
||
/* | ||
DAI = 1 | ||
ETH = 200 | ||
DAI/ETH = 0.005 | ||
Decimalized: 0.005e18 (18 - 18 decimals = 0 + 18) | ||
*/ | ||
expect( | ||
await call(openOracleV1, 'getPrices', [token.DAI])) | ||
.numEquals(new BigNumber('0.005e18').toFixed()); | ||
}); | ||
}); | ||
}); |
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
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.
holy crow, good thing those get configs were public
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.
Yeah, otherwise this would have been way more complex to build