-
Notifications
You must be signed in to change notification settings - Fork 308
[solidity sdk] Add zerolend AggregatorV3 adapter to SDK #1437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2022 Antonio | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
117 changes: 117 additions & 0 deletions
117
target_chains/ethereum/sdk/solidity/PythAggregatorV3.sol
This file contains hidden or 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,117 @@ | ||
| // SPDX-License-Identifier: Apache 2 | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import {PythStructs} from "./PythStructs.sol"; | ||
| import {IPyth} from "./IPyth.sol"; | ||
|
|
||
| // This interface is forked from the Zerolend Adapter found here: | ||
| // https://github.com/zerolend/pyth-oracles/blob/master/contracts/PythAggregatorV3.sol | ||
| // Original license found under licenses/zerolend-pyth-oracles.md | ||
|
|
||
| /** | ||
| * @title A port of the ChainlinkAggregatorV3 interface that supports Pyth price feeds | ||
| * @notice This does not store any roundId information on-chain. Please review the code before using this implementation. | ||
| * Users should deploy an instance of this contract to wrap every price feed id that they need to use. | ||
| */ | ||
| contract PythAggregatorV3 { | ||
| bytes32 public priceId; | ||
| IPyth public pyth; | ||
|
|
||
| constructor(address _pyth, bytes32 _priceId) { | ||
| priceId = _priceId; | ||
| pyth = IPyth(_pyth); | ||
| } | ||
|
|
||
| // Wrapper function to update the underlying Pyth price feeds. Not part of the AggregatorV3 interface but useful. | ||
| function updateFeeds(bytes[] calldata priceUpdateData) public payable { | ||
| // Update the prices to the latest available values and pay the required fee for it. The `priceUpdateData` data | ||
| // should be retrieved from our off-chain Price Service API using the `pyth-evm-js` package. | ||
| // See section "How Pyth Works on EVM Chains" below for more information. | ||
| uint fee = pyth.getUpdateFee(priceUpdateData); | ||
| pyth.updatePriceFeeds{value: fee}(priceUpdateData); | ||
|
|
||
| // refund remaining eth | ||
| payable(msg.sender).call{value: address(this).balance}(""); | ||
| } | ||
|
|
||
| function decimals() public view virtual returns (uint8) { | ||
| PythStructs.Price memory price = pyth.getPriceUnsafe(priceId); | ||
| return uint8(-1 * int8(price.expo)); | ||
| } | ||
|
|
||
| function description() public pure returns (string memory) { | ||
| return "A port of a chainlink aggregator powered by pyth network feeds"; | ||
| } | ||
|
|
||
| function version() public pure returns (uint256) { | ||
| return 1; | ||
| } | ||
|
|
||
| function latestAnswer() public view virtual returns (int256) { | ||
| PythStructs.Price memory price = pyth.getPriceUnsafe(priceId); | ||
| return int256(price.price); | ||
| } | ||
|
|
||
| function latestTimestamp() public view returns (uint256) { | ||
| PythStructs.Price memory price = pyth.getPriceUnsafe(priceId); | ||
| return price.publishTime; | ||
| } | ||
|
|
||
| function latestRound() public view returns (uint256) { | ||
| // use timestamp as the round id | ||
| return latestTimestamp(); | ||
| } | ||
|
|
||
| function getAnswer(uint256) public view returns (int256) { | ||
| return latestAnswer(); | ||
| } | ||
|
|
||
| function getTimestamp(uint256) external view returns (uint256) { | ||
| return latestTimestamp(); | ||
| } | ||
|
|
||
| function getRoundData( | ||
| uint80 _roundId | ||
| ) | ||
| external | ||
| view | ||
| returns ( | ||
| uint80 roundId, | ||
| int256 answer, | ||
| uint256 startedAt, | ||
| uint256 updatedAt, | ||
| uint80 answeredInRound | ||
| ) | ||
| { | ||
| PythStructs.Price memory price = pyth.getPriceUnsafe(priceId); | ||
| return ( | ||
| _roundId, | ||
| int256(price.price), | ||
| price.publishTime, | ||
| price.publishTime, | ||
| _roundId | ||
| ); | ||
| } | ||
|
|
||
| function latestRoundData() | ||
| external | ||
| view | ||
| returns ( | ||
| uint80 roundId, | ||
| int256 answer, | ||
| uint256 startedAt, | ||
| uint256 updatedAt, | ||
| uint80 answeredInRound | ||
| ) | ||
| { | ||
| PythStructs.Price memory price = pyth.getPriceUnsafe(priceId); | ||
| roundId = uint80(price.publishTime); | ||
| return ( | ||
| roundId, | ||
| int256(price.price), | ||
| price.publishTime, | ||
| price.publishTime, | ||
| roundId | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "name": "@pythnetwork/pyth-sdk-solidity", | ||
| "version": "3.0.0", | ||
| "version": "3.1.0", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use this command to make sure lock also gets updated: |
||
| "description": "Read prices from the Pyth oracle", | ||
| "repository": { | ||
| "type": "git", | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MIT right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MIT lets us relicense under other licenses. I think it's better to use our standard license for uniformity.