From 249354d3fc4648d9f4aebbde61584a8fed1149e9 Mon Sep 17 00:00:00 2001 From: tokyo Date: Thu, 2 Jun 2022 21:48:35 +0530 Subject: [PATCH] Oracle Bug Fix Fixed a bug in `consultPair` feature. Refactored deployment script. --- oracle/contracts/NpmPriceOracle.sol | 15 ++++++++++++--- oracle/scripts/deploy.js | 4 ++-- scripts/config/network/mumbai.js | 4 +++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/oracle/contracts/NpmPriceOracle.sol b/oracle/contracts/NpmPriceOracle.sol index 93d723c9..a66a7a82 100644 --- a/oracle/contracts/NpmPriceOracle.sol +++ b/oracle/contracts/NpmPriceOracle.sol @@ -3,10 +3,11 @@ pragma solidity 0.6.6; import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol"; -import "@uniswap/lib/contracts/libraries/FixedPoint.sol"; import "@uniswap/v2-periphery/contracts/libraries/UniswapV2OracleLibrary.sol"; +import "@uniswap/lib/contracts/libraries/FixedPoint.sol"; import "@uniswap/lib/contracts/libraries/Babylonian.sol"; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; +import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol"; import "./IPriceOracle.sol"; contract NpmPriceOracle is IPriceOracle { @@ -14,11 +15,14 @@ contract NpmPriceOracle is IPriceOracle { using FixedPoint for *; uint256 public constant UPDATE_INTERVAL = 30 minutes; + uint256 public constant NPM_MULTIPLIER = 1 ether; address public immutable token0; address public immutable token1; IUniswapV2Pair public immutable pair; + address public immutable npm; + uint256 public price0CumulativeLast; uint256 public price1CumulativeLast; uint32 public blockTimestampLast; @@ -26,8 +30,11 @@ contract NpmPriceOracle is IPriceOracle { FixedPoint.uq112x112 public price0Average; FixedPoint.uq112x112 public price1Average; - constructor(IUniswapV2Pair _pair) public { + constructor(IUniswapV2Pair _pair, address _npm) public { require(address(_pair) != address(0), "Invalid pair"); + require(_npm != address(0), "Invalid NPM token"); + require(_pair.token0() == _npm || _pair.token1() == _npm, "Not NPM pair"); + (uint256 reserve0, uint256 reserve1, uint32 lastTimestamp) = _pair.getReserves(); require(reserve0 != 0 && reserve1 != 0, "No reserve"); @@ -35,6 +42,8 @@ contract NpmPriceOracle is IPriceOracle { token0 = _pair.token0(); token1 = _pair.token1(); + npm = _npm; + price0CumulativeLast = _pair.price0CumulativeLast(); price1CumulativeLast = _pair.price1CumulativeLast(); blockTimestampLast = lastTimestamp; @@ -71,7 +80,7 @@ contract NpmPriceOracle is IPriceOracle { uint256 supply = pair.totalSupply(); (uint256 r0, uint256 r1, ) = pair.getReserves(); - uint256 p0 = consult(token0, 1 ether); + uint256 p0 = token0 == npm ? consult(token0, NPM_MULTIPLIER) : consult(token1, NPM_MULTIPLIER); return _calculateFairLpPrice(r0, r1, p0, supply, amountIn); } diff --git a/oracle/scripts/deploy.js b/oracle/scripts/deploy.js index 0064e194..dc49abee 100644 --- a/oracle/scripts/deploy.js +++ b/oracle/scripts/deploy.js @@ -6,9 +6,9 @@ const main = async () => { console.log('Deployer: %s. Balance: %s', deployer.address, await deployer.getBalance()) - const ContractFactory = await ethers.getContractFactory('NPMPriceOracle') + const ContractFactory = await ethers.getContractFactory('NpmPriceOracle') - const oracle = await ContractFactory.deploy(config[80001].stablecoinPairs.NPM_DAI) + const oracle = await ContractFactory.deploy(config[80001].stablecoinPairs.NPM_DAI, config[80001].deployedTokens.NPM) await oracle.deployed() console.log('Deployed: https://mumbai.polygonscan.com/address/%s', oracle.address) diff --git a/scripts/config/network/mumbai.js b/scripts/config/network/mumbai.js index bb3aabc2..dd0f00bf 100644 --- a/scripts/config/network/mumbai.js +++ b/scripts/config/network/mumbai.js @@ -45,7 +45,8 @@ const config = { } ], deployedTokens: { - DAI: '0x76061C192fBBBF210d2dA25D4B8aaA34b798ccaB', // NPM DAI + DAI: '0x76061C192fBBBF210d2dA25D4B8aaA34b798ccaB', + USDC: '0x29f1E04692c0EAAc061EC6bf29b44F01c041DA87', NPM: '0x001Ffb65fF6E15902072C5133C016fD89cB56a7e', CRPOOL: '0xF1DD0d797b720578DE3075241941451A793D383E', HWT: '0xC8B328a39570620f67c795Ae79a0aE12066dc51a', @@ -58,6 +59,7 @@ const config = { }, stablecoinPairs: { NPM_DAI: '0x40D135283d8aE7815F26c39D3980cA47B062e473', + NPM_USDC: '0x1bFa43EE2A5e3FF1ecBCEa2Cd78CD5Dac2E310b6', CRPOOL_DAI: '0x9c799908cbDEAf9622843F6493C975262bd880D0', HWT_DAI: '0xfcAd7c5372688C4dBe00611714075ACb8D1ED8b7', OBK_DAI: '0x6e8D6D5F8C079efC5CDc950CB946b773E162B3fb',