Created
May 9, 2023 09:43
-
-
Save bernard-wagner/0d3bfa3ce4764f4f7fe3933b17a61230 to your computer and use it in GitHub Desktop.
Wrappers for mockCall of common interfaces
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
//SPDX-License-Identifier: ISC | |
pragma solidity ^0.8.0; | |
import {Vm} from "forge-std/Vm.sol"; | |
interface AggregatorInterface { | |
function latestAnswer() external view returns (int); | |
function latestRound() external view returns (uint); | |
function getAnswer(uint roundId) external view returns (int); | |
function getTimestamp(uint roundId) external view returns (uint); | |
} | |
interface AggregatorV3Interface { | |
function decimals() external view returns (uint8); | |
function getRoundData( | |
uint80 _roundId | |
) external view returns (uint80 roundId, int answer, uint startedAt, uint updatedAt, uint80 answeredInRound); | |
function latestRoundData() | |
external | |
view | |
returns (uint80 roundId, int answer, uint startedAt, uint updatedAt, uint80 answeredInRound); | |
} | |
interface AggregatorV2V3Interface is AggregatorInterface, AggregatorV3Interface {} | |
library ForgeMocks { | |
address internal constant VM_ADDRESS = address(uint160(uint256(keccak256("hevm cheat code")))); | |
Vm internal constant vm = Vm(VM_ADDRESS); | |
function mockAggregatorV2V3LatestRoundData(address aggregator_proxy, uint256 baseAssetPriceD18) internal { | |
uint8 decimals = AggregatorV3Interface(aggregator_proxy).decimals(); | |
(uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound) = AggregatorV2V3Interface(aggregator_proxy).latestRoundData(); | |
roundId++; | |
startedAt = updatedAt; | |
answeredInRound++; | |
updatedAt = block.timestamp; | |
answer = int256(baseAssetPriceD18 / (10 ** (18 - decimals))); | |
vm.mockCall(aggregator_proxy, abi.encodeWithSignature('latestRoundData()'), abi.encode(roundId, answer, startedAt, updatedAt, answeredInRound, '(uint80, int256, uint256, uint256, uint80)')); | |
} | |
function mockAggregatorV2V3LatestAnswer(address aggregator_proxy, uint256 baseAssetPriceD18) internal { | |
uint8 decimals = AggregatorV2V3Interface(aggregator_proxy).decimals(); | |
int256 answer = int256(baseAssetPriceD18 / (10 ** (18 - decimals))); | |
vm.mockCall(aggregator_proxy, abi.encodeWithSignature('latestAnswer()'), abi.encode(answer, '(int)')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment