-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MockV3Aggregator contract for anvil/local blockchain testing of the p…
…rice feeds
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.18; | ||
|
||
/** | ||
* @title MockV3Aggregator | ||
* @notice Based on the FluxAggregator contract | ||
* @notice Use this contract when you need to test | ||
* other contract's ability to read data from an | ||
* aggregator contract, but how the aggregator got | ||
* its answer is unimportant | ||
*/ | ||
contract MockV3Aggregator { | ||
uint256 public constant version = 0; | ||
|
||
uint8 public decimals; | ||
int256 public latestAnswer; | ||
uint256 public latestTimestamp; | ||
uint256 public latestRound; | ||
|
||
mapping(uint256 => int256) public getAnswer; | ||
mapping(uint256 => uint256) public getTimestamp; | ||
mapping(uint256 => uint256) private getStartedAt; | ||
|
||
constructor(uint8 _decimals, int256 _initialAnswer) { | ||
decimals = _decimals; | ||
updateAnswer(_initialAnswer); | ||
} | ||
|
||
function updateAnswer(int256 _answer) public { | ||
latestAnswer = _answer; | ||
latestTimestamp = block.timestamp; | ||
latestRound++; | ||
getAnswer[latestRound] = _answer; | ||
getTimestamp[latestRound] = block.timestamp; | ||
getStartedAt[latestRound] = block.timestamp; | ||
} | ||
|
||
function updateRoundData(uint80 _roundId, int256 _answer, uint256 _timestamp, uint256 _startedAt) public { | ||
latestRound = _roundId; | ||
latestAnswer = _answer; | ||
latestTimestamp = _timestamp; | ||
getAnswer[latestRound] = _answer; | ||
getTimestamp[latestRound] = _timestamp; | ||
getStartedAt[latestRound] = _startedAt; | ||
} | ||
|
||
function getRoundData(uint80 _roundId) | ||
external | ||
view | ||
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound) | ||
{ | ||
return (_roundId, getAnswer[_roundId], getStartedAt[_roundId], getTimestamp[_roundId], _roundId); | ||
} | ||
|
||
function latestRoundData() | ||
external | ||
view | ||
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound) | ||
{ | ||
return ( | ||
uint80(latestRound), | ||
getAnswer[latestRound], | ||
getStartedAt[latestRound], | ||
getTimestamp[latestRound], | ||
uint80(latestRound) | ||
); | ||
} | ||
|
||
function description() external pure returns (string memory) { | ||
return "v0.6/tests/MockV3Aggregator.sol"; | ||
} | ||
} |