Skip to content

Commit

Permalink
Added function to allow user control over all mocked price parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandro-immunefi committed Feb 7, 2024
1 parent eed32bf commit 7aa244c
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/oracle/lib/MockPyth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,48 @@ library MockPyth {
PythUpgradable pythUpgradable;
}

/**
* @dev Mocks oracle data for testing purposes. This function allows simulating oracle data
* within a test environment, enabling the testing of contract interactions with oracles.
* @param pid The price feed ID of the oracle data to mock.
* @param pythPrice The pyth price { int64 price, uint64 conf, int32 expo, uint256 publishTime }
*/
function mockOracleData(bytes32 pid, PythUpgradable.Price memory pythPrice) internal {
Context memory context = context();
require(context.pythUpgradable.priceFeedExists(pid), "PythOracle: Price feed not found");

vm.mockCall(
address(context.pythUpgradable),
abi.encodePacked(PythUpgradable.getPrice.selector, pid),
abi.encode(pythPrice)
);
vm.mockCall(
address(context.pythUpgradable),
abi.encodePacked(PythUpgradable.getPriceUnsafe.selector, pid),
abi.encode(pythPrice)
);
vm.mockCall(
address(context.pythUpgradable),
abi.encodePacked(PythUpgradable.getPriceNoOlderThan.selector, pid),
abi.encode(pythPrice)
);
vm.mockCall(
address(context.pythUpgradable),
abi.encodePacked(PythUpgradable.getEmaPrice.selector, pid),
abi.encode(pythPrice)
);
vm.mockCall(
address(context.pythUpgradable),
abi.encodePacked(PythUpgradable.getEmaPriceUnsafe.selector, pid),
abi.encode(pythPrice)
);
vm.mockCall(
address(context.pythUpgradable),
abi.encodePacked(PythUpgradable.getEmaPriceNoOlderThan.selector, pid),
abi.encode(pythPrice)
);
}

/**
* @dev Mocks oracle data for testing purposes. This function allows simulating oracle data
* within a test environment, enabling the testing of contract interactions with oracles.
Expand Down

0 comments on commit 7aa244c

Please sign in to comment.