diff --git a/contracts/test/TestPriceFeed.sol b/contracts/test/TestPriceFeed.sol index 8737059..5e7dc45 100644 --- a/contracts/test/TestPriceFeed.sol +++ b/contracts/test/TestPriceFeed.sol @@ -2,15 +2,18 @@ pragma solidity 0.7.6; import { IPriceFeedV2 } from "../interface/IPriceFeedV2.sol"; +import { IPriceFeed } from "../interface/IPriceFeed.sol"; contract TestPriceFeed { - address public chainlink; + address public chainlinkV1; + address public chainlinkV2; address public bandProtocol; uint256 public currentPrice; - constructor(address _chainlink, address _bandProtocol) { - chainlink = _chainlink; + constructor(address _chainlinkV1, address _chainlinkV2, address _bandProtocol) { + chainlinkV1 = _chainlinkV1; + chainlinkV2 = _chainlinkV2; bandProtocol = _bandProtocol; currentPrice = 10; } @@ -18,32 +21,28 @@ contract TestPriceFeed { // // for gas usage testing // - function fetchChainlinkPrice(uint256 interval) external { - for (uint256 i = 0; i < 17; i++) { - IPriceFeedV2(chainlink).getPrice(interval); - } - currentPrice = IPriceFeedV2(chainlink).getPrice(interval); + function fetchChainlinkV2Price(uint256 interval) external { + currentPrice = IPriceFeedV2(chainlinkV2).getPrice(interval); + } + + function fetchChainlinkV1Price(uint256 interval) external { + currentPrice = IPriceFeed(chainlinkV1).getPrice(interval); } function fetchBandProtocolPrice(uint256 interval) external { - for (uint256 i = 0; i < 17; i++) { - IPriceFeedV2(bandProtocol).getPrice(interval); - } currentPrice = IPriceFeedV2(bandProtocol).getPrice(interval); } - function cachedChainlinkPrice(uint256 interval) external { - for (uint256 i = 0; i < 17; i++) { - IPriceFeedV2(chainlink).cacheTwap(interval); - } - currentPrice = IPriceFeedV2(chainlink).cacheTwap(interval); + function cachedChainlinkV2Price(uint256 interval) external { + try IPriceFeedV2(chainlinkV2).cacheTwap(interval) {} catch {} + } + + function cachedChainlinkV2PriceWithoutTry(uint256 interval) external { + IPriceFeedV2(chainlinkV2).cacheTwap(interval); } function cachedBandProtocolPrice(uint256 interval) external { - for (uint256 i = 0; i < 17; i++) { - IPriceFeedV2(bandProtocol).cacheTwap(interval); - } - currentPrice = IPriceFeedV2(bandProtocol).cacheTwap(interval); + try IPriceFeedV2(bandProtocol).cacheTwap(interval) {} catch {} } // diff --git a/test/PriceFeed.gas.test.ts b/test/PriceFeed.gas.test.ts index 457a3d4..84a9855 100644 --- a/test/PriceFeed.gas.test.ts +++ b/test/PriceFeed.gas.test.ts @@ -1,8 +1,16 @@ import { parseEther } from "ethers/lib/utils" import { ethers, waffle } from "hardhat" -import { BandPriceFeed, ChainlinkPriceFeedV2, TestAggregatorV3, TestPriceFeed, TestStdReference } from "../typechain" +import { + BandPriceFeed, + ChainlinkPriceFeed, + ChainlinkPriceFeedV2, + TestAggregatorV3, + TestPriceFeed, + TestStdReference, +} from "../typechain" const twapInterval = 900 + interface PriceFeedFixture { bandPriceFeed: BandPriceFeed bandReference: TestStdReference @@ -10,6 +18,7 @@ interface PriceFeedFixture { // chainlinik chainlinkPriceFeed: ChainlinkPriceFeedV2 + chainlinkPriceFeedV1: ChainlinkPriceFeed aggregator: TestAggregatorV3 } @@ -36,15 +45,28 @@ async function priceFeedFixture(): Promise { twapInterval, )) as ChainlinkPriceFeedV2 - return { bandPriceFeed, bandReference: testStdReference, baseAsset, chainlinkPriceFeed, aggregator: testAggregator } + const chainlinkPriceFeedFactoryV1 = await ethers.getContractFactory("ChainlinkPriceFeed") + const chainlinkPriceFeedV1 = (await chainlinkPriceFeedFactoryV1.deploy( + testAggregator.address, + )) as ChainlinkPriceFeed + + return { + bandPriceFeed, + bandReference: testStdReference, + baseAsset, + chainlinkPriceFeed, + chainlinkPriceFeedV1, + aggregator: testAggregator, + } } -describe.skip("Price feed gas test", () => { +describe("Price feed gas test", () => { const [admin] = waffle.provider.getWallets() const loadFixture: ReturnType = waffle.createFixtureLoader([admin]) let bandPriceFeed: BandPriceFeed let bandReference: TestStdReference let chainlinkPriceFeed: ChainlinkPriceFeedV2 + let chainlinkPriceFeedV1: ChainlinkPriceFeed let aggregator: TestAggregatorV3 let currentTime: number let testPriceFeed: TestPriceFeed @@ -63,7 +85,8 @@ describe.skip("Price feed gas test", () => { await chainlinkPriceFeed.update() if (forward) { - currentTime += 15 + // assume that every 10s price get updated + currentTime += 10 await ethers.provider.send("evm_setNextBlockTimestamp", [currentTime]) await ethers.provider.send("evm_mine", []) } @@ -74,11 +97,13 @@ describe.skip("Price feed gas test", () => { bandReference = _fixture.bandReference bandPriceFeed = _fixture.bandPriceFeed chainlinkPriceFeed = _fixture.chainlinkPriceFeed + chainlinkPriceFeedV1 = _fixture.chainlinkPriceFeedV1 aggregator = _fixture.aggregator round = 0 const TestPriceFeedFactory = await ethers.getContractFactory("TestPriceFeed") testPriceFeed = (await TestPriceFeedFactory.deploy( + chainlinkPriceFeedV1.address, chainlinkPriceFeed.address, bandPriceFeed.address, )) as TestPriceFeed @@ -90,21 +115,29 @@ describe.skip("Price feed gas test", () => { } }) - describe("900 seconds twapInterval", () => { - it("band protocol ", async () => { + describe.skip("900 seconds twapInterval", () => { + it.skip("band protocol ", async () => { await testPriceFeed.fetchBandProtocolPrice(twapInterval) }) - it("band protocol - cached", async () => { + it.skip("band protocol - cached", async () => { await testPriceFeed.cachedBandProtocolPrice(twapInterval) }) it("chainlink", async () => { - await testPriceFeed.fetchChainlinkPrice(twapInterval) + await testPriceFeed.fetchChainlinkV2Price(twapInterval) + }) + + it("chainlinkv1", async () => { + await testPriceFeed.fetchChainlinkV1Price(twapInterval) }) it("chainlink - cached", async () => { - await testPriceFeed.cachedChainlinkPrice(twapInterval) + await aggregator.setRoundData(round, parseEther("400"), currentTime, currentTime, ++round) + await ethers.provider.send("evm_setNextBlockTimestamp", [currentTime + 15]) + await ethers.provider.send("evm_mine", [currentTime + 15]) + await testPriceFeed.cachedChainlinkV2PriceWithoutTry(twapInterval) + await testPriceFeed.cachedChainlinkV2Price(twapInterval) }) }) })