Skip to content

Commit

Permalink
Merge pull request #81 from neptune-mutual-blue/fix/oracle
Browse files Browse the repository at this point in the history
Oracle Bug Fix
  • Loading branch information
heyaibi authored Jun 2, 2022
2 parents 94a76d9 + 249354d commit de4e313
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
15 changes: 12 additions & 3 deletions oracle/contracts/NpmPriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,47 @@
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 {
using SafeMath for uint256;
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;

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");

pair = _pair;
token0 = _pair.token0();
token1 = _pair.token1();

npm = _npm;

price0CumulativeLast = _pair.price0CumulativeLast();
price1CumulativeLast = _pair.price1CumulativeLast();
blockTimestampLast = lastTimestamp;
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions oracle/scripts/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion scripts/config/network/mumbai.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const config = {
}
],
deployedTokens: {
DAI: '0x76061C192fBBBF210d2dA25D4B8aaA34b798ccaB', // NPM DAI
DAI: '0x76061C192fBBBF210d2dA25D4B8aaA34b798ccaB',
USDC: '0x29f1E04692c0EAAc061EC6bf29b44F01c041DA87',
NPM: '0x001Ffb65fF6E15902072C5133C016fD89cB56a7e',
CRPOOL: '0xF1DD0d797b720578DE3075241941451A793D383E',
HWT: '0xC8B328a39570620f67c795Ae79a0aE12066dc51a',
Expand All @@ -58,6 +59,7 @@ const config = {
},
stablecoinPairs: {
NPM_DAI: '0x40D135283d8aE7815F26c39D3980cA47B062e473',
NPM_USDC: '0x1bFa43EE2A5e3FF1ecBCEa2Cd78CD5Dac2E310b6',
CRPOOL_DAI: '0x9c799908cbDEAf9622843F6493C975262bd880D0',
HWT_DAI: '0xfcAd7c5372688C4dBe00611714075ACb8D1ED8b7',
OBK_DAI: '0x6e8D6D5F8C079efC5CDc950CB946b773E162B3fb',
Expand Down

0 comments on commit de4e313

Please sign in to comment.