Skip to content
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

Pyth Sample EVM Price Feeds #40

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "lazer/evm/lib/openzeppelin-contracts-upgradeable"]
path = lazer/evm/lib/openzeppelin-contracts-upgradeable
url = https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable
[submodule "price_feeds/evm/pyth_sample/lib/forge-std"]
path = price_feeds/evm/pyth_sample/lib/forge-std
url = https://github.com/foundry-rs/forge-std
14 changes: 14 additions & 0 deletions price_feeds/evm/pyth_sample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env
45 changes: 45 additions & 0 deletions price_feeds/evm/pyth_sample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Pyth Oracle sample app

This directory contains an example of a smart contract using Pyth Price Feeds.

Please see the [Pyth documentation](https://docs.pyth.network/documentation/pythnet-price-feeds) for more information about Pyth and how to integrate it into your application.

### Building

You need to have [Foundry](https://getfoundry.sh/) and `node` installed to run this example.
Once you have installed these tools, run the following commands from root directory to install forge dependencies:

```
forge install foundry-rs/[email protected] --no-git --no-commit
```

After installing the above dependencies, you need to install pyth-sdk-solidity.

```
npm init -y
npm install @pythnetwork/pyth-sdk-solidity
```

### Testing

Simply run `forge test` from root directory.


### Resources

- [Pyth Price Feeds Documentation](https://docs.pyth.network/price-feeds)
- [Pyth Price Feed IDs](https://www.pyth.network/developers/price-feed-ids)
- [Pyth Price Feeds Contracts](https://docs.pyth.network/price-feeds/contract-addresses/evm)

### Retrieve Price Updates.

Price updates can be retrieved using Hermes. It provides various ways to retrieve price updates.

For example

```
curl -X 'GET' \
'https://hermes.pyth.network/v2/updates/price/latest?ids%5B%5D=0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43&ids%5B%5D=0xc96458d393fe9deb7a7d63a0ac41e2898a67a7750dbd166673279e06c868df0a'
```

Checkout [How to Fetch Price Updates](https://docs.pyth.network/price-feeds/fetch-price-updates) for more details.
6 changes: 6 additions & 0 deletions price_feeds/evm/pyth_sample/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions price_feeds/evm/pyth_sample/lib/forge-std
Submodule forge-std added at 1eea5b
22 changes: 22 additions & 0 deletions price_feeds/evm/pyth_sample/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions price_feeds/evm/pyth_sample/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "pyth_sample",
"version": "1.0.0",
"description": "**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**",
"main": "index.js",
"directories": {
"lib": "lib",
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@pythnetwork/pyth-sdk-solidity": "^4.0.0"
}
}
2 changes: 2 additions & 0 deletions price_feeds/evm/pyth_sample/remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@pythnetwork/pyth-sdk-solidity/=node_modules/@pythnetwork/pyth-sdk-solidity

46 changes: 46 additions & 0 deletions price_feeds/evm/pyth_sample/src/PythSample.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;


import "@pythnetwork/pyth-sdk-solidity/IPyth.sol";
import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";


contract PythSample {

IPyth pyth;

// @param pyth: The contract address of the Pyth contract. Instantiate it with the Pyth contract address from https://docs.pyth.network/price-feeds/contract-addresses/evm
constructor(address _pyth) {
pyth = IPyth(_pyth);
}

// @param priceId: Each price feed (e.g., ETH/USD) is identified by a price feed ID. The complete list of feed IDs is available at https://pyth.network/developers/price-feed-ids
// @param priceUpdate: The encoded data to update the contract with the latest pricecontract-addresses/evm
function getLatestPrice(bytes32 priceId, bytes[] calldata priceUpdate) public payable returns (PythStructs.Price memory) {

uint updateFee = pyth.getUpdateFee(priceUpdate);
pyth.updatePriceFeeds{ value: updateFee }(priceUpdate);

return pyth.getPriceNoOlderThan(priceId, 60);
}

// @dev: This function is an example method to update multiple price feeds at once.
// @param priceIds: The price ids of the price feeds.
// @param priceUpdates: The encoded data to update the contract with the latest price
function getLatestPrices(bytes32[] calldata priceIds, bytes[] calldata priceUpdates) public payable returns (PythStructs.Price[] memory) {

// Calculate the update fee for the price feeds
// One can update multiple price feeds by calling the updatePriceFeeds function once.
uint updateFee = pyth.getUpdateFee(priceUpdates);
pyth.updatePriceFeeds{ value: updateFee }(priceUpdates);

// Get the latest prices for the price feeds
PythStructs.Price[] memory prices = new PythStructs.Price[](priceIds.length);
for (uint i = 0; i < priceIds.length; i++) {
prices[i] = pyth.getPriceNoOlderThan(priceIds[i], 60);
}
return prices;
}

}
111 changes: 111 additions & 0 deletions price_feeds/evm/pyth_sample/test/PythSample.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import { Test, console2 } from "forge-std/Test.sol";
import { PythSample } from "../src/PythSample.sol";
import { MockPyth } from "@pythnetwork/pyth-sdk-solidity/MockPyth.sol";
import { PythStructs } from "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";

contract PythSampleTest is Test {
MockPyth public pyth;
PythSample public app;

// @dev: Dummy price ids for testing.
bytes32 constant ETH_PRICE_FEED_ID = bytes32(uint256(0x1));
bytes32 constant BTC_PRICE_FEED_ID = bytes32(uint256(0x2));

// @dev: Setup the test environment.
function setUp() public {
pyth = new MockPyth(60, 1);
app = new PythSample(address(pyth));
}

// @dev: Creating a dummyprice update for a given price id and price.
function createPriceUpdate(
bytes32 priceId,
int64 price
) private view returns (bytes[] memory) {
bytes[] memory updateData = new bytes[](1);
updateData[0] = pyth.createPriceFeedUpdateData(
priceId,
price * 100000, // price
10 * 100000, // confidence
-5, // exponent
price * 100000, // emaPrice
10 * 100000, // emaConfidence
uint64(block.timestamp), // publishTime
uint64(block.timestamp) // prevPublishTime
);
return updateData;
}

// @dev: Testing the getLatestPrice function.
function testGetLatestPrice() public {
bytes[] memory updateData = createPriceUpdate(ETH_PRICE_FEED_ID, 2000);
uint updateFee = pyth.getUpdateFee(updateData);

vm.deal(address(this), updateFee);
PythStructs.Price memory price = app.getLatestPrice{value: updateFee}(
ETH_PRICE_FEED_ID,
updateData
);

assertEq(price.price, 2000 * 100000);
}

function testGetLatestPrices() public {
bytes32[] memory priceIds = new bytes32[](2);
priceIds[0] = ETH_PRICE_FEED_ID;
priceIds[1] = BTC_PRICE_FEED_ID;

bytes[] memory updateData = new bytes[](2);
updateData[0] = pyth.createPriceFeedUpdateData(
ETH_PRICE_FEED_ID,
2000 * 100000, // ETH price
10 * 100000,
-5,
2000 * 100000,
10 * 100000,
uint64(block.timestamp),
uint64(block.timestamp)
);
updateData[1] = pyth.createPriceFeedUpdateData(
BTC_PRICE_FEED_ID,
30000 * 100000, // BTC price
10 * 100000,
-5,
30000 * 100000,
10 * 100000,
uint64(block.timestamp),
uint64(block.timestamp)
);

uint updateFee = pyth.getUpdateFee(updateData);
vm.deal(address(this), updateFee);

PythStructs.Price[] memory prices = app.getLatestPrices{value: updateFee}(
priceIds,
updateData
);

assertEq(prices.length, 2);
assertEq(prices[0].price, 2000 * 100000);
assertEq(prices[1].price, 30000 * 100000);
}

function testStalePrice() public {
bytes[] memory updateData = createPriceUpdate(ETH_PRICE_FEED_ID, 2000);
uint updateFee = pyth.getUpdateFee(updateData);
vm.deal(address(this), updateFee);

// Update price
pyth.updatePriceFeeds{value: updateFee}(updateData);

// Skip 120 seconds (more than the 60-second staleness threshold)
skip(120);

// Expect revert when trying to get price
vm.expectRevert();
app.getLatestPrice{value: updateFee}(ETH_PRICE_FEED_ID, updateData);
}
}