Skip to content

Commit a7de728

Browse files
committed
tests/: add tests
1 parent 051bdd3 commit a7de728

3 files changed

Lines changed: 61 additions & 5 deletions

File tree

tests/Contracts/MockAggregator.sol

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ contract MockAggregator {
4242

4343
uint8 public decimals = 18;
4444
int256 public answer;
45+
uint256 public blockTimestamp = 100000;
4546

4647
constructor(int256 _answer) public {
4748
answer = _answer;
@@ -61,7 +62,7 @@ contract MockAggregator {
6162
// Shh
6263
_roundId;
6364

64-
return (roundId, answer, block.timestamp, block.timestamp, roundId);
65+
return (roundId, answer, blockTimestamp, blockTimestamp, roundId);
6566
}
6667

6768
function latestRoundData()
@@ -75,7 +76,7 @@ contract MockAggregator {
7576
uint80
7677
)
7778
{
78-
return (roundId, answer, block.timestamp, block.timestamp, roundId);
79+
return (roundId, answer, blockTimestamp, blockTimestamp, roundId);
7980
}
8081

8182
function setAnswer(int256 _answer) external {
@@ -85,6 +86,10 @@ contract MockAggregator {
8586
function setDecimals(uint8 _decimals) external {
8687
decimals = _decimals;
8788
}
89+
90+
function setBlockTimestamp(uint256 newBlockTimestamp) external {
91+
blockTimestamp = newBlockTimestamp;
92+
}
8893
}
8994

9095
contract MockRegistry is FeedRegistryInterface {
@@ -93,6 +98,7 @@ contract MockRegistry is FeedRegistryInterface {
9398
mapping(address => mapping(address => int256)) private answer;
9499
bool public getFeedFailed;
95100
bool public feedDisabled;
101+
uint256 public blockTimestamp = 100000;
96102

97103
function getRoundData(
98104
address base,
@@ -109,7 +115,7 @@ contract MockRegistry is FeedRegistryInterface {
109115
uint80
110116
)
111117
{
112-
return (roundId, answer[base][quote], block.timestamp, block.timestamp, _roundId);
118+
return (roundId, answer[base][quote], blockTimestamp, blockTimestamp, _roundId);
113119
}
114120

115121
function latestRoundData(address base, address quote)
@@ -123,7 +129,7 @@ contract MockRegistry is FeedRegistryInterface {
123129
uint80
124130
)
125131
{
126-
return (roundId, answer[base][quote], block.timestamp, block.timestamp, roundId);
132+
return (roundId, answer[base][quote], blockTimestamp, blockTimestamp, roundId);
127133
}
128134

129135
function decimals(address base, address quote) external view returns (uint8) {
@@ -183,4 +189,8 @@ contract MockRegistry is FeedRegistryInterface {
183189
) external {
184190
answer[base][quote] = _answer;
185191
}
192+
193+
function setBlockTimestamp(uint256 newBlockTimestamp) external {
194+
blockTimestamp = newBlockTimestamp;
195+
}
186196
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pragma solidity ^0.5.16;
2+
3+
import "../../contracts/PriceOracle/PriceOracleProxy.sol";
4+
5+
contract PriceOracleProxyHarness is PriceOracleProxy {
6+
uint256 public blockTimestamp = 100000;
7+
8+
constructor(
9+
address admin_,
10+
address v1PriceOracle_,
11+
address cEthAddress_,
12+
address registry_
13+
) public PriceOracleProxy(admin_, v1PriceOracle_, cEthAddress_, registry_) {}
14+
15+
function getBlockTimestamp() internal view returns (uint256) {
16+
return blockTimestamp;
17+
}
18+
19+
function setBlockTimestamp(uint256 newBlockTimestamp) public {
20+
blockTimestamp = newBlockTimestamp;
21+
}
22+
}

tests/PriceOracleProxyTest.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('PriceOracleProxy', () => {
4747
cYv2CrvLP = await makeCToken({comptroller: cEth.comptroller, supportMarket: true, underlying: yv2CrvLP});
4848

4949
backingOracle = await makePriceOracle();
50-
oracle = await deploy('PriceOracleProxy',
50+
oracle = await deploy('PriceOracleProxyHarness',
5151
[
5252
root,
5353
backingOracle._address,
@@ -196,6 +196,30 @@ describe('PriceOracleProxy', () => {
196196
let proxyPrice = await call(oracle, "getUnderlyingPrice", [cOther._address]);
197197
expect(Number(proxyPrice)).toEqual(0.0005 * 1e18);
198198
});
199+
200+
it("fallbacks to v1 if the price from chainlink is invalid", async () => {
201+
const v1Price = 10;
202+
const oraclePrice = '0';
203+
204+
await setAndVerifyBackingPrice(cOther, v1Price);
205+
await setPrice(cOther.underlying._address, cOther.underlying._address, ethAddress, oraclePrice);
206+
await readAndVerifyProxyPrice(cOther, v1Price);
207+
});
208+
209+
it("fallbacks to v1 if the price from chainlink is stale", async () => {
210+
const v1Price = 10;
211+
const oraclePrice = '2';
212+
213+
const timestamp1 = 10000;
214+
const timestamp2 = 96500; // greater than timestamp1 + 86400 (1 day)
215+
await send(mockAggregator, "setBlockTimestamp", [timestamp1]);
216+
await send(oracle, "setBlockTimestamp", [timestamp2]);
217+
218+
await setAndVerifyBackingPrice(cOther, v1Price);
219+
await setPrice(cOther.underlying._address, cOther.underlying._address, ethAddress, oraclePrice);
220+
221+
await readAndVerifyProxyPrice(cOther, v1Price);
222+
});
199223
});
200224

201225
describe("_setAdmin", () => {

0 commit comments

Comments
 (0)