Skip to content

Commit f6ceba7

Browse files
author
Opass Chang
committed
Change contract name ChainlinkPriceFeedWithCachedTwap to ChainlinkPriceFeedV2, add IPriceFeedV2
1 parent f17b691 commit f6ceba7

11 files changed

+44
-59
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [unreleased]
9-
- Change contract name `ChainlinkPriceFeed` to `ChainlinkPriceFeedWithCachedTwap`.
9+
- Change contract name `ChainlinkPriceFeed` to `ChainlinkPriceFeedV2`.
1010
- Add origin `ChainlinkPriceFeed`, which calculates the twap by round data instead of cached twap.
1111

1212
## [0.3.4] - 2022-04-01

contracts/BandPriceFeed.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ pragma experimental ABIEncoderV2;
44

55
import { Address } from "@openzeppelin/contracts/utils/Address.sol";
66
import { BlockContext } from "./base/BlockContext.sol";
7-
import { IPriceFeed } from "./interface/IPriceFeed.sol";
7+
import { IPriceFeedV2 } from "./interface/IPriceFeedV2.sol";
88
import { IStdReference } from "./interface/bandProtocol/IStdReference.sol";
99
import { CachedTwap } from "./twap/CachedTwap.sol";
1010

11-
contract BandPriceFeed is IPriceFeed, BlockContext, CachedTwap {
11+
contract BandPriceFeed is IPriceFeedV2, BlockContext, CachedTwap {
1212
using Address for address;
1313

1414
//

contracts/ChainlinkPriceFeed.sol

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ contract ChainlinkPriceFeed is IPriceFeed, BlockContext {
2020
_aggregator = aggregator;
2121
}
2222

23-
/// @dev not support cached twap
24-
function cacheTwap(uint256 interval) external override returns (uint256) {
25-
// CPF_NS: not supported
26-
revert("CPF_NS");
27-
}
28-
2923
function decimals() external view override returns (uint8) {
3024
return _aggregator.decimals();
3125
}

contracts/ChainlinkPriceFeedWithCachedTwap.sol renamed to contracts/ChainlinkPriceFeedV2.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ pragma solidity 0.7.6;
33

44
import { Address } from "@openzeppelin/contracts/utils/Address.sol";
55
import { AggregatorV3Interface } from "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
6-
import { IPriceFeed } from "./interface/IPriceFeed.sol";
6+
import { IPriceFeedV2 } from "./interface/IPriceFeedV2.sol";
77
import { BlockContext } from "./base/BlockContext.sol";
88
import { CachedTwap } from "./twap/CachedTwap.sol";
99

10-
contract ChainlinkPriceFeedWithCachedTwap is IPriceFeed, BlockContext, CachedTwap {
10+
contract ChainlinkPriceFeedV2 is IPriceFeedV2, BlockContext, CachedTwap {
1111
using Address for address;
1212

1313
AggregatorV3Interface private immutable _aggregator;

contracts/EmergencyPriceFeed.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import { IUniswapV3Pool } from "@uniswap/v3-core/contracts/interfaces/IUniswapV3
77
import { FixedPoint96 } from "@uniswap/v3-core/contracts/libraries/FixedPoint96.sol";
88
import { FullMath } from "@uniswap/v3-core/contracts/libraries/FullMath.sol";
99
import { TickMath } from "@uniswap/v3-core/contracts/libraries/TickMath.sol";
10-
import { IPriceFeed } from "./interface/IPriceFeed.sol";
10+
import { IPriceFeedV2 } from "./interface/IPriceFeedV2.sol";
1111
import { BlockContext } from "./base/BlockContext.sol";
1212

13-
contract EmergencyPriceFeed is IPriceFeed, BlockContext {
13+
contract EmergencyPriceFeed is IPriceFeedV2, BlockContext {
1414
using Address for address;
1515

1616
//

contracts/interface/IPriceFeed.sol

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
pragma solidity 0.7.6;
33

44
interface IPriceFeed {
5-
/// @dev Returns the cached index price of the token.
6-
/// @param interval The interval represents twap interval.
7-
function cacheTwap(uint256 interval) external returns (uint256);
8-
95
function decimals() external view returns (uint8);
106

117
/// @dev Returns the index price of the token.

contracts/interface/IPriceFeedV2.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
pragma solidity 0.7.6;
3+
4+
import "./IPriceFeed.sol";
5+
6+
interface IPriceFeedV2 is IPriceFeed {
7+
/// @dev Returns the cached index price of the token.
8+
/// @param interval The interval represents twap interval.
9+
function cacheTwap(uint256 interval) external returns (uint256);
10+
}

contracts/test/TestPriceFeed.sol

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: GPL-3.0-or-later
22
pragma solidity 0.7.6;
33

4-
import { IPriceFeed } from "../interface/IPriceFeed.sol";
4+
import { IPriceFeedV2 } from "../interface/IPriceFeedV2.sol";
55

66
contract TestPriceFeed {
77
address public chainlink;
@@ -20,30 +20,30 @@ contract TestPriceFeed {
2020
//
2121
function fetchChainlinkPrice(uint256 interval) external {
2222
for (uint256 i = 0; i < 17; i++) {
23-
IPriceFeed(chainlink).getPrice(interval);
23+
IPriceFeedV2(chainlink).getPrice(interval);
2424
}
25-
currentPrice = IPriceFeed(chainlink).getPrice(interval);
25+
currentPrice = IPriceFeedV2(chainlink).getPrice(interval);
2626
}
2727

2828
function fetchBandProtocolPrice(uint256 interval) external {
2929
for (uint256 i = 0; i < 17; i++) {
30-
IPriceFeed(bandProtocol).getPrice(interval);
30+
IPriceFeedV2(bandProtocol).getPrice(interval);
3131
}
32-
currentPrice = IPriceFeed(bandProtocol).getPrice(interval);
32+
currentPrice = IPriceFeedV2(bandProtocol).getPrice(interval);
3333
}
3434

3535
function cachedChainlinkPrice(uint256 interval) external {
3636
for (uint256 i = 0; i < 17; i++) {
37-
IPriceFeed(chainlink).cacheTwap(interval);
37+
IPriceFeedV2(chainlink).cacheTwap(interval);
3838
}
39-
currentPrice = IPriceFeed(chainlink).cacheTwap(interval);
39+
currentPrice = IPriceFeedV2(chainlink).cacheTwap(interval);
4040
}
4141

4242
function cachedBandProtocolPrice(uint256 interval) external {
4343
for (uint256 i = 0; i < 17; i++) {
44-
IPriceFeed(bandProtocol).cacheTwap(interval);
44+
IPriceFeedV2(bandProtocol).cacheTwap(interval);
4545
}
46-
currentPrice = IPriceFeed(bandProtocol).cacheTwap(interval);
46+
currentPrice = IPriceFeedV2(bandProtocol).cacheTwap(interval);
4747
}
4848

4949
//
@@ -53,7 +53,7 @@ contract TestPriceFeed {
5353
// having this function for testing getPrice() and cacheTwap()
5454
// timestamp moves if any txs happen in hardhat env and which causes cacheTwap() will recalculate all the time
5555
function getPrice(uint256 interval) external returns (uint256 twap, uint256 cachedTwap) {
56-
twap = IPriceFeed(bandProtocol).getPrice(interval);
57-
cachedTwap = IPriceFeed(bandProtocol).cacheTwap(interval);
56+
twap = IPriceFeedV2(bandProtocol).getPrice(interval);
57+
cachedTwap = IPriceFeedV2(bandProtocol).cacheTwap(interval);
5858
}
5959
}

test/CachedTwap.spec.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
import { expect } from "chai"
22
import { parseEther } from "ethers/lib/utils"
33
import { ethers, waffle } from "hardhat"
4-
import {
5-
BandPriceFeed,
6-
ChainlinkPriceFeedWithCachedTwap,
7-
TestAggregatorV3,
8-
TestPriceFeed,
9-
TestStdReference,
10-
} from "../typechain"
4+
import { BandPriceFeed, ChainlinkPriceFeedV2, TestAggregatorV3, TestPriceFeed, TestStdReference } from "../typechain"
115

126
interface PriceFeedFixture {
137
bandPriceFeed: BandPriceFeed
148
bandReference: TestStdReference
159
baseAsset: string
1610

1711
// chainlinik
18-
chainlinkPriceFeed: ChainlinkPriceFeedWithCachedTwap
12+
chainlinkPriceFeed: ChainlinkPriceFeedV2
1913
aggregator: TestAggregatorV3
2014
}
2115
async function priceFeedFixture(): Promise<PriceFeedFixture> {
@@ -36,11 +30,11 @@ async function priceFeedFixture(): Promise<PriceFeedFixture> {
3630
const testAggregatorFactory = await ethers.getContractFactory("TestAggregatorV3")
3731
const testAggregator = await testAggregatorFactory.deploy()
3832

39-
const chainlinkPriceFeedFactory = await ethers.getContractFactory("ChainlinkPriceFeedWithCachedTwap")
33+
const chainlinkPriceFeedFactory = await ethers.getContractFactory("ChainlinkPriceFeedV2")
4034
const chainlinkPriceFeed = (await chainlinkPriceFeedFactory.deploy(
4135
testAggregator.address,
4236
twapInterval,
43-
)) as ChainlinkPriceFeedWithCachedTwap
37+
)) as ChainlinkPriceFeedV2
4438

4539
return { bandPriceFeed, bandReference: testStdReference, baseAsset, chainlinkPriceFeed, aggregator: testAggregator }
4640
}
@@ -50,7 +44,7 @@ describe("Cached Twap Spec", () => {
5044
const loadFixture: ReturnType<typeof waffle.createFixtureLoader> = waffle.createFixtureLoader([admin])
5145
let bandPriceFeed: BandPriceFeed
5246
let bandReference: TestStdReference
53-
let chainlinkPriceFeed: ChainlinkPriceFeedWithCachedTwap
47+
let chainlinkPriceFeed: ChainlinkPriceFeedV2
5448
let aggregator: TestAggregatorV3
5549
let currentTime: number
5650
let testPriceFeed: TestPriceFeed

test/ChainlinkPriceFeedWithCachedTwap.spec.ts renamed to test/ChainlinkPriceFeedV2.spec.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,27 @@ import { FakeContract, smock } from "@defi-wonderland/smock"
22
import { expect } from "chai"
33
import { parseEther } from "ethers/lib/utils"
44
import { ethers, waffle } from "hardhat"
5-
import { ChainlinkPriceFeedWithCachedTwap, TestAggregatorV3 } from "../typechain"
5+
import { ChainlinkPriceFeedV2, TestAggregatorV3 } from "../typechain"
66

77
interface ChainlinkPriceFeedFixture {
8-
chainlinkPriceFeed: ChainlinkPriceFeedWithCachedTwap
8+
chainlinkPriceFeed: ChainlinkPriceFeedV2
99
aggregator: FakeContract<TestAggregatorV3>
1010
}
1111

1212
async function chainlinkPriceFeedFixture(): Promise<ChainlinkPriceFeedFixture> {
1313
const aggregator = await smock.fake<TestAggregatorV3>("TestAggregatorV3")
1414
aggregator.decimals.returns(() => 18)
1515

16-
const chainlinkPriceFeedFactory = await ethers.getContractFactory("ChainlinkPriceFeedWithCachedTwap")
17-
const chainlinkPriceFeed = (await chainlinkPriceFeedFactory.deploy(
18-
aggregator.address,
19-
900,
20-
)) as ChainlinkPriceFeedWithCachedTwap
16+
const chainlinkPriceFeedFactory = await ethers.getContractFactory("ChainlinkPriceFeedV2")
17+
const chainlinkPriceFeed = (await chainlinkPriceFeedFactory.deploy(aggregator.address, 900)) as ChainlinkPriceFeedV2
2118

2219
return { chainlinkPriceFeed, aggregator }
2320
}
2421

25-
describe("ChainlinkPriceFeedWithCachedTwap Spec", () => {
22+
describe("ChainlinkPriceFeedV2 Spec", () => {
2623
const [admin] = waffle.provider.getWallets()
2724
const loadFixture: ReturnType<typeof waffle.createFixtureLoader> = waffle.createFixtureLoader([admin])
28-
let chainlinkPriceFeed: ChainlinkPriceFeedWithCachedTwap
25+
let chainlinkPriceFeed: ChainlinkPriceFeedV2
2926
let aggregator: FakeContract<TestAggregatorV3>
3027
let currentTime: number
3128
let roundData: any[]

0 commit comments

Comments
 (0)