-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[solidity sdk] Add zerolend AggregatorV3 adapter to SDK (#1437)
* add zerolend cl adapter to sdk * bump versions
- Loading branch information
Showing
4 changed files
with
141 additions
and
3 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,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 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 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