|
| 1 | +// SPDX-License-Identifier: Apache 2 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import {PythStructs} from "./PythStructs.sol"; |
| 5 | +import {IPyth} from "./IPyth.sol"; |
| 6 | + |
| 7 | +// This interface is forked from the Zerolend Adapter found here: |
| 8 | +// https://github.com/zerolend/pyth-oracles/blob/master/contracts/PythAggregatorV3.sol |
| 9 | +// Original license found under licenses/zerolend-pyth-oracles.md |
| 10 | + |
| 11 | +/** |
| 12 | + * @title A port of the ChainlinkAggregatorV3 interface that supports Pyth price feeds |
| 13 | + * @notice This does not store any roundId information on-chain. Please review the code before using this implementation. |
| 14 | + * Users should deploy an instance of this contract to wrap every price feed id that they need to use. |
| 15 | + */ |
| 16 | +contract PythAggregatorV3 { |
| 17 | + bytes32 public priceId; |
| 18 | + IPyth public pyth; |
| 19 | + |
| 20 | + constructor(address _pyth, bytes32 _priceId) { |
| 21 | + priceId = _priceId; |
| 22 | + pyth = IPyth(_pyth); |
| 23 | + } |
| 24 | + |
| 25 | + // Wrapper function to update the underlying Pyth price feeds. Not part of the AggregatorV3 interface but useful. |
| 26 | + function updateFeeds(bytes[] calldata priceUpdateData) public payable { |
| 27 | + // Update the prices to the latest available values and pay the required fee for it. The `priceUpdateData` data |
| 28 | + // should be retrieved from our off-chain Price Service API using the `pyth-evm-js` package. |
| 29 | + // See section "How Pyth Works on EVM Chains" below for more information. |
| 30 | + uint fee = pyth.getUpdateFee(priceUpdateData); |
| 31 | + pyth.updatePriceFeeds{value: fee}(priceUpdateData); |
| 32 | + |
| 33 | + // refund remaining eth |
| 34 | + payable(msg.sender).call{value: address(this).balance}(""); |
| 35 | + } |
| 36 | + |
| 37 | + function decimals() public view virtual returns (uint8) { |
| 38 | + PythStructs.Price memory price = pyth.getPriceUnsafe(priceId); |
| 39 | + return uint8(-1 * int8(price.expo)); |
| 40 | + } |
| 41 | + |
| 42 | + function description() public pure returns (string memory) { |
| 43 | + return "A port of a chainlink aggregator powered by pyth network feeds"; |
| 44 | + } |
| 45 | + |
| 46 | + function version() public pure returns (uint256) { |
| 47 | + return 1; |
| 48 | + } |
| 49 | + |
| 50 | + function latestAnswer() public view virtual returns (int256) { |
| 51 | + PythStructs.Price memory price = pyth.getPriceUnsafe(priceId); |
| 52 | + return int256(price.price); |
| 53 | + } |
| 54 | + |
| 55 | + function latestTimestamp() public view returns (uint256) { |
| 56 | + PythStructs.Price memory price = pyth.getPriceUnsafe(priceId); |
| 57 | + return price.publishTime; |
| 58 | + } |
| 59 | + |
| 60 | + function latestRound() public view returns (uint256) { |
| 61 | + // use timestamp as the round id |
| 62 | + return latestTimestamp(); |
| 63 | + } |
| 64 | + |
| 65 | + function getAnswer(uint256) public view returns (int256) { |
| 66 | + return latestAnswer(); |
| 67 | + } |
| 68 | + |
| 69 | + function getTimestamp(uint256) external view returns (uint256) { |
| 70 | + return latestTimestamp(); |
| 71 | + } |
| 72 | + |
| 73 | + function getRoundData( |
| 74 | + uint80 _roundId |
| 75 | + ) |
| 76 | + external |
| 77 | + view |
| 78 | + returns ( |
| 79 | + uint80 roundId, |
| 80 | + int256 answer, |
| 81 | + uint256 startedAt, |
| 82 | + uint256 updatedAt, |
| 83 | + uint80 answeredInRound |
| 84 | + ) |
| 85 | + { |
| 86 | + PythStructs.Price memory price = pyth.getPriceUnsafe(priceId); |
| 87 | + return ( |
| 88 | + _roundId, |
| 89 | + int256(price.price), |
| 90 | + price.publishTime, |
| 91 | + price.publishTime, |
| 92 | + _roundId |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + function latestRoundData() |
| 97 | + external |
| 98 | + view |
| 99 | + returns ( |
| 100 | + uint80 roundId, |
| 101 | + int256 answer, |
| 102 | + uint256 startedAt, |
| 103 | + uint256 updatedAt, |
| 104 | + uint80 answeredInRound |
| 105 | + ) |
| 106 | + { |
| 107 | + PythStructs.Price memory price = pyth.getPriceUnsafe(priceId); |
| 108 | + roundId = uint80(price.publishTime); |
| 109 | + return ( |
| 110 | + roundId, |
| 111 | + int256(price.price), |
| 112 | + price.publishTime, |
| 113 | + price.publishTime, |
| 114 | + roundId |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments