diff --git a/.gitignore b/.gitignore index bf0af602..907dae91 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ yarn-error.log junit.xml coverage/* +.saddle_history diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..c2b8523c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "compound-config"] + path = compound-config + url = https://github.com/compound-finance/compound-config.git diff --git a/compound-config b/compound-config new file mode 160000 index 00000000..66c40518 --- /dev/null +++ b/compound-config @@ -0,0 +1 @@ +Subproject commit 66c40518ecd2a398990be6e5ed26d22c0a9b64e8 diff --git a/contracts/Uniswap/UniswapAnchoredView.sol b/contracts/Uniswap/UniswapAnchoredView.sol index 81695a84..d4bc196b 100644 --- a/contracts/Uniswap/UniswapAnchoredView.sol +++ b/contracts/Uniswap/UniswapAnchoredView.sol @@ -234,12 +234,16 @@ contract UniswapAnchoredView is UniswapConfig { // Adjust rawUniswapPrice according to the units of the non-ETH asset // In the case of ETH, we would have to scale by 1e6 / USDC_UNITS, but since baseUnit2 is 1e6 (USDC), it cancels - if (config.isUniswapReversed) { - // unscaledPriceMantissa * ethBaseUnit / config.baseUnit / expScale, but we simplify bc ethBaseUnit == expScale - anchorPrice = unscaledPriceMantissa / config.baseUnit; - } else { - anchorPrice = mul(unscaledPriceMantissa, config.baseUnit) / ethBaseUnit / expScale; - } + + // In the case of non-ETH tokens + // a. pokeWindowValues already handled uniswap reversed cases, so priceAverage will always be Token/ETH TWAP price. + // b. conversionFactor = ETH price * 1e6 + // unscaledPriceMantissa = priceAverage(token/ETH TWAP price) * expScale * conversionFactor + // so -> + // anchorPrice = priceAverage * tokenBaseUnit / ethBaseUnit * ETH_price * 1e6 + // = priceAverage * conversionFactor * tokenBaseUnit / ethBaseUnit + // = unscaledPriceMantissa / expScale * tokenBaseUnit / ethBaseUnit + anchorPrice = mul(unscaledPriceMantissa, config.baseUnit) / ethBaseUnit / expScale; emit AnchorPriceUpdated(symbol, anchorPrice, oldTimestamp, block.timestamp); diff --git a/package.json b/package.json index fc691140..9b15cc33 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "license": "MIT", "dependencies": { "bignumber.js": "^9.0.0", - "eth-saddle": "0.1.20", + "eth-saddle": "0.1.21", "web3": "^1.2.4", "yargs": "^15.0.2" }, diff --git a/saddle.config.js b/saddle.config.js index 66dd4c3c..414288ff 100644 --- a/saddle.config.js +++ b/saddle.config.js @@ -132,5 +132,27 @@ module.exports = { { file: "~/.ethereum/mainnet" } // Load from given file with contents as the private key (e.g. 0x...) ] } + }, + get_network_file: (network) => { + return null; + }, + read_network_file: (network) => { + const fs = require('fs'); + const path = require('path'); + const util = require('util'); + + const networkFile = path.join(process.cwd(), 'compound-config', 'networks', `${network}.json`); + return util.promisify(fs.readFile)(networkFile).then((json) => { + const contracts = JSON.parse(json)['Contracts'] || {}; + + return Object.fromEntries(Object.entries(contracts).map(([contract, address]) => { + const mapper = { + PriceFeed: 'UniswapAnchoredView', + PriceData: 'OpenOraclePriceData' + }; + + return [mapper[contract] || contract, address]; + })); + }); } }; diff --git a/tests/UniswapAnchoredViewTest.js b/tests/UniswapAnchoredViewTest.js index 65c46228..ab32854a 100644 --- a/tests/UniswapAnchoredViewTest.js +++ b/tests/UniswapAnchoredViewTest.js @@ -16,8 +16,7 @@ async function setup({isMockedView, freeze}) { const anchorMantissa = numToHex(1e17); const priceData = await deploy('OpenOraclePriceData', []); const anchorPeriod = 60; - const timestamp = 1600000000; - + const timestamp = Math.floor(Date.now() / 1000); if (freeze) { await sendRPC(web3, 'evm_freezeTime', [timestamp]); diff --git a/yarn.lock b/yarn.lock index 5b51e1c6..32db4f48 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3673,10 +3673,10 @@ eth-query@^2.0.2, eth-query@^2.1.0, eth-query@^2.1.2: json-rpc-random-id "^1.0.0" xtend "^4.0.1" -eth-saddle@0.1.20: - version "0.1.20" - resolved "https://registry.yarnpkg.com/eth-saddle/-/eth-saddle-0.1.20.tgz#1ce8217e36735c23667a003beaeaa65e0f07e150" - integrity sha512-LiOfheTXtyUd2ZZYORfKL4Lx42yach8SabbtyyGM5MwH6Th7GFk+TGDDerIJ9lvmCxeZBqxG0jVmKXfFez/0Dg== +eth-saddle@0.1.21: + version "0.1.21" + resolved "https://registry.yarnpkg.com/eth-saddle/-/eth-saddle-0.1.21.tgz#692ac403f6f68c71f3e81336869e2259ab77d97e" + integrity sha512-XsswjrWJaQnixrQk7ycL8fOUoqqyRbkSo9TkGJrgxNNcYjtF6/wFOjxgfwxxd3FIQWLCuAF4jcO6RpFRiEAh4Q== dependencies: "@0x/subproviders" "^6.0.0" "@compound-finance/sol-coverage" "^4.0.10-alpha.7"