Skip to content

Commit ee455f1

Browse files
authored
[solidity sdk] Add zerolend AggregatorV3 adapter to SDK (#1437)
* add zerolend cl adapter to sdk * bump versions
1 parent 2c7dfa9 commit ee455f1

File tree

4 files changed

+141
-3
lines changed

4 files changed

+141
-3
lines changed

licenses/zerolend-pyth-oracles.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Antonio
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}

target_chains/ethereum/sdk/solidity/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/pyth-sdk-solidity",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"description": "Read prices from the Pyth oracle",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)