From 02062182753393f1d982448e043f601226122aef Mon Sep 17 00:00:00 2001 From: Narayan Prusty Date: Wed, 8 May 2024 17:08:23 +0530 Subject: [PATCH] fix: fixed tests --- contracts/oracles/SFrxETHOracle.sol | 2 +- .../oracles/mocks/MockSFrxEthFraxOracle.sol | 23 +++++++++++ helpers/deploymentConfig.ts | 1 + test/SFrxETHOracle.ts | 40 ++++++++++++------- 4 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 contracts/oracles/mocks/MockSFrxEthFraxOracle.sol diff --git a/contracts/oracles/SFrxETHOracle.sol b/contracts/oracles/SFrxETHOracle.sol index 6f188dc7..f05bd0ff 100644 --- a/contracts/oracles/SFrxETHOracle.sol +++ b/contracts/oracles/SFrxETHOracle.sol @@ -44,6 +44,6 @@ contract SFrxETHOracle is CorrelatedTokenOracle { uint256 averagePrice = (priceLow + priceHigh) / 2; // return (1 / averagePrice) as the average price is in sfraxETH - return EXP_SCALE * 2 / averagePrice; + return (EXP_SCALE ** 2) / averagePrice; } } diff --git a/contracts/oracles/mocks/MockSFrxEthFraxOracle.sol b/contracts/oracles/mocks/MockSFrxEthFraxOracle.sol new file mode 100644 index 00000000..8b23ff48 --- /dev/null +++ b/contracts/oracles/mocks/MockSFrxEthFraxOracle.sol @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: BSD-3-Clause +pragma solidity 0.8.25; + +import "../../interfaces/ISfrxEthFraxOracle.sol"; +import "@openzeppelin/contracts/access/Ownable.sol"; + +contract MockSfrxEthFraxOracle is ISfrxEthFraxOracle, Ownable { + bool public isBadData; + uint256 public priceLow; + uint256 public priceHigh; + + constructor() Ownable() {} + + function setPrices(bool _isBadData, uint256 _priceLow, uint256 _priceHigh) external onlyOwner { + isBadData = _isBadData; + priceLow = _priceLow; + priceHigh = _priceHigh; + } + + function getPrices() external view override returns (bool, uint256, uint256) { + return (isBadData, priceLow, priceHigh); + } +} diff --git a/helpers/deploymentConfig.ts b/helpers/deploymentConfig.ts index a194003b..a5167e1f 100644 --- a/helpers/deploymentConfig.ts +++ b/helpers/deploymentConfig.ts @@ -129,6 +129,7 @@ export const ADDRESSES: PreconfiguredAddresses = { PTOracle: "0xbbd487268A295531d299c125F3e5f749884A3e30", EtherFiLiquidityPool: "0x308861A430be4cce5502d0A12724771Fc6DaF216", WETH: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", + SfrxEthFraxOracle: "0x3d3D868522b5a4035ADcb67BF0846D61597A6a6F" }, opbnbtestnet: { vBNBAddress: ethers.constants.AddressZero, diff --git a/test/SFrxETHOracle.ts b/test/SFrxETHOracle.ts index 3111ca3e..b94448cb 100644 --- a/test/SFrxETHOracle.ts +++ b/test/SFrxETHOracle.ts @@ -4,13 +4,13 @@ import { parseUnits } from "ethers/lib/utils"; import { ethers } from "hardhat"; import { ADDRESSES } from "../helpers/deploymentConfig"; -import { BEP20Harness, ISfrxETH, ResilientOracleInterface } from "../typechain-types"; +import { BEP20Harness, ISfrxEthFraxOracle, ResilientOracleInterface } from "../typechain-types"; import { addr0000 } from "./utils/data"; const { expect } = chai; chai.use(smock.matchers); -const { sfrxETH, frxETH } = ADDRESSES.ethereum; +const { sfrxETH, FRAX } = ADDRESSES.ethereum; const ETH_USD_PRICE = parseUnits("3100", 18); // 3100 USD for 1 ETH describe("SFrxETHOracle unit tests", () => { @@ -18,35 +18,47 @@ describe("SFrxETHOracle unit tests", () => { let sfrxETHMock; let SFrxETHOracleFactory; let SFrxETHOracle; - let frxETHMock; + let fraxMock; + let sfrxEthFraxOracleMock; before(async () => { // To initialize the provider we need to hit the node with any request await ethers.getSigners(); resilientOracleMock = await smock.fake("ResilientOracleInterface"); - sfrxETHMock = await smock.fake("ISfrxETH", { address: sfrxETH }); - sfrxETHMock.convertToAssets.returns(parseUnits("1.076546447254363344", 18)); - sfrxETHMock.decimals.returns(18); + // deploy MockSfrxEthFraxOracle + const sfrxEthFraxOracleMockFactory = await ethers.getContractFactory("MockSfrxEthFraxOracle"); + sfrxEthFraxOracleMock = await sfrxEthFraxOracleMockFactory.deploy(); + await sfrxEthFraxOracleMock.deployed(); + await sfrxEthFraxOracleMock.setPrices(false, parseUnits("0.000306430391670677", 18), parseUnits("0.000309520800596522", 18)); + + fraxMock = await smock.fake("BEP20Harness", { address: FRAX }); + fraxMock.decimals.returns(18); - frxETHMock = await smock.fake("BEP20Harness", { address: frxETH }); - frxETHMock.decimals.returns(18); + sfrxETHMock = await smock.fake("BEP20Harness", { address: sfrxETH }); + sfrxETHMock.decimals.returns(18); SFrxETHOracleFactory = await ethers.getContractFactory("SFrxETHOracle"); }); describe("deployment", () => { - it("revert if frxETH address is 0", async () => { - await expect(SFrxETHOracleFactory.deploy(sfrxETHMock.address, addr0000, resilientOracleMock.address)).to.be + it("revert if frax address is 0", async () => { + await expect(SFrxETHOracleFactory.deploy(sfrxEthFraxOracleMock.address, sfrxETHMock.address, addr0000, resilientOracleMock.address)).to.be .reverted; }); it("revert if sfrxETH address is 0", async () => { - await expect(SFrxETHOracleFactory.deploy(addr0000, frxETHMock.address, resilientOracleMock.address)).to.be + await expect(SFrxETHOracleFactory.deploy(sfrxEthFraxOracleMock.address, addr0000, fraxMock.address, resilientOracleMock.address)).to.be + .reverted; + }); + + it("revert if sfrxEthFraxOracle address is 0", async () => { + await expect(SFrxETHOracleFactory.deploy(addr0000, sfrxETHMock.address, fraxMock.address, resilientOracleMock.address)).to.be .reverted; }); it("should deploy contract", async () => { SFrxETHOracle = await SFrxETHOracleFactory.deploy( + sfrxEthFraxOracleMock.address, sfrxETHMock.address, - frxETHMock.address, + fraxMock.address, resilientOracleMock.address, ); }); @@ -61,9 +73,9 @@ describe("SFrxETHOracle unit tests", () => { }); it("should get correct price of sfrxETH", async () => { - resilientOracleMock.getPrice.returns(ETH_USD_PRICE); + resilientOracleMock.getPrice.returns(parseUnits("0.99838881", 18)); const price = await SFrxETHOracle.getPrice(sfrxETHMock.address); - expect(price).to.equal(parseUnits("3337.2939864885263664", 18)); + expect(price).to.equal(parseUnits("3241.778967340326445028", 18)); }); }); });