Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gas profiling test #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions contracts/test/TestPriceFeed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,47 @@
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;
}

//
// 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 {}
}

//
Expand Down
51 changes: 42 additions & 9 deletions test/PriceFeed.gas.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
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
baseAsset: string

// chainlinik
chainlinkPriceFeed: ChainlinkPriceFeedV2
chainlinkPriceFeedV1: ChainlinkPriceFeed
aggregator: TestAggregatorV3
}

Expand All @@ -36,15 +45,28 @@ async function priceFeedFixture(): Promise<PriceFeedFixture> {
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<typeof waffle.createFixtureLoader> = waffle.createFixtureLoader([admin])
let bandPriceFeed: BandPriceFeed
let bandReference: TestStdReference
let chainlinkPriceFeed: ChainlinkPriceFeedV2
let chainlinkPriceFeedV1: ChainlinkPriceFeed
let aggregator: TestAggregatorV3
let currentTime: number
let testPriceFeed: TestPriceFeed
Expand All @@ -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", [])
}
Expand All @@ -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
Expand All @@ -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)
})
})
})