|
1 |
| -## pyth-lazer-example-evm |
| 1 | +# Pyth Lazer EVM Examples |
2 | 2 |
|
3 |
| -This package is built using [Foundry](https://book.getfoundry.sh/). |
| 3 | +This directory contains Solidity smart contract examples demonstrating how to integrate Pyth Lazer price feeds into your Ethereum applications using Foundry. |
4 | 4 |
|
5 |
| -This example contract shows how to use Pyth Lazer SDK to verify signatures and parse payloads from Pyth Lazer. |
| 5 | +## What is Pyth Lazer? |
| 6 | + |
| 7 | +Pyth Lazer is a high-performance, low-latency price feed service that provides real-time financial market data to blockchain applications. It supports multiple blockchain networks and offers both JSON and binary message formats for optimal performance. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +Before running these examples, make sure you have the following installed: |
| 12 | + |
| 13 | +- **Foundry** - [Installation Guide](https://book.getfoundry.sh/getting-started/installation) |
| 14 | +- Basic understanding of Solidity and smart contracts |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +1. Navigate to the `lazer/evm` directory: |
| 19 | + ```bash |
| 20 | + cd lazer/evm |
| 21 | + ``` |
| 22 | + |
| 23 | +2. Install dependencies: |
| 24 | + ```bash |
| 25 | + forge install |
| 26 | + ``` |
| 27 | + |
| 28 | +3. Build the contracts: |
| 29 | + ```bash |
| 30 | + forge build |
| 31 | + ``` |
| 32 | + |
| 33 | +## Examples |
| 34 | + |
| 35 | +### 1. ExampleReceiver Contract (`src/ExampleReceiver.sol`) |
| 36 | +Demonstrates how to receive and process Pyth Lazer price updates in a smart contract. |
| 37 | + |
| 38 | +**What it does:** |
| 39 | +- Verifies Pyth Lazer signatures on-chain |
| 40 | +- Parses price feed payloads to extract price data |
| 41 | +- Handles verification fees and refunds excess payments |
| 42 | +- Extracts multiple price feed properties (price, timestamps, exponents, etc.) |
| 43 | +- Filters updates by feed ID and timestamp |
| 44 | + |
| 45 | +**Key Functions:** |
| 46 | +```solidity |
| 47 | +function updatePrice(bytes calldata update) public payable |
| 48 | +``` |
| 49 | + |
| 50 | +**How to run the test:** |
| 51 | +```bash |
| 52 | +forge test -v |
| 53 | +``` |
| 54 | + |
| 55 | +### 2. Test Suite (`test/ExampleReceiver.t.sol`) |
| 56 | +Comprehensive test demonstrating the contract functionality with real price data. |
| 57 | + |
| 58 | +**What it does:** |
| 59 | +- Sets up a PythLazer contract with trusted signer |
| 60 | +- Creates and funds test accounts |
| 61 | +- Submits a price update with verification fee |
| 62 | +- Validates parsed price data and fee handling |
| 63 | + |
| 64 | +**How to run:** |
| 65 | +```bash |
| 66 | +forge test -v |
| 67 | +``` |
| 68 | + |
| 69 | +**Expected output:** |
| 70 | +- **Timestamp**: `1738270008001000` (microseconds since Unix epoch) |
| 71 | +- **Price**: `100000000` (raw price value) |
| 72 | +- **Exponent**: `-8` (price = 100000000 × 10^-8 = $1.00) |
| 73 | +- **Publisher Count**: `1` |
| 74 | + |
| 75 | +## Understanding Price Data |
| 76 | + |
| 77 | +Pyth Lazer prices use a fixed-point representation: |
| 78 | +``` |
| 79 | +actual_price = price × 10^exponent |
| 80 | +``` |
| 81 | + |
| 82 | +**Example from the test:** |
| 83 | +- Raw price: `100000000` |
| 84 | +- Exponent: `-8` |
| 85 | +- Actual price: `100000000 × 10^-8 = $1.00` |
| 86 | + |
| 87 | +### Feed Properties |
| 88 | + |
| 89 | +The contract can extract multiple price feed properties: |
| 90 | + |
| 91 | +- **Price**: Main price value |
| 92 | +- **BestBidPrice**: Highest bid price in the market |
| 93 | +- **BestAskPrice**: Lowest ask price in the market |
| 94 | +- **Exponent**: Decimal exponent for price normalization |
| 95 | +- **PublisherCount**: Number of publishers contributing to this price |
| 96 | + |
| 97 | +## Integration Guide |
| 98 | + |
| 99 | +To integrate Pyth Lazer into your own contract: |
| 100 | + |
| 101 | +1. **Import the required libraries:** |
| 102 | + ```solidity |
| 103 | + import {PythLazer} from "pyth-lazer/PythLazer.sol"; |
| 104 | + import {PythLazerLib} from "pyth-lazer/PythLazerLib.sol"; |
| 105 | + ``` |
| 106 | + |
| 107 | +2. **Set up the PythLazer contract:** |
| 108 | + ```solidity |
| 109 | + PythLazer pythLazer; |
| 110 | + constructor(address pythLazerAddress) { |
| 111 | + pythLazer = PythLazer(pythLazerAddress); |
| 112 | + } |
| 113 | + ``` |
| 114 | + |
| 115 | +3. **Handle verification fees:** |
| 116 | + ```solidity |
| 117 | + uint256 verification_fee = pythLazer.verification_fee(); |
| 118 | + require(msg.value >= verification_fee, "Insufficient fee"); |
| 119 | + ``` |
| 120 | + |
| 121 | +4. **Verify and parse updates:** |
| 122 | + ```solidity |
| 123 | + (bytes memory payload,) = pythLazer.verifyUpdate{value: verification_fee}(update); |
| 124 | + // Parse payload using PythLazerLib functions |
| 125 | + ``` |
| 126 | + |
| 127 | +## Configuration |
| 128 | + |
| 129 | +### Feed IDs |
| 130 | + |
| 131 | +The example filters for feed ID `6`. To use different feeds: |
| 132 | + |
| 133 | +1. Update the feed ID check in `updatePrice()`: |
| 134 | + ```solidity |
| 135 | + if (feedId == YOUR_FEED_ID && _timestamp > timestamp) { |
| 136 | + // Update logic |
| 137 | + } |
| 138 | + ``` |
| 139 | + |
| 140 | +2. Obtain feed IDs from the Pyth Lazer documentation or API |
| 141 | + |
| 142 | +## Troubleshooting |
| 143 | + |
| 144 | +### Common Issues |
| 145 | + |
| 146 | +1. **Build Errors**: Make sure all dependencies are installed with `forge install` |
| 147 | + |
| 148 | +2. **Test Failures**: Ensure you're using a compatible Foundry version and all submodules are properly initialized |
| 149 | + |
| 150 | +3. **Gas Issues**: The contract includes gas optimization for parsing multiple feed properties |
| 151 | + |
| 152 | +## Resources |
| 153 | + |
| 154 | +- [Pyth Network Documentation](https://docs.pyth.network/) |
| 155 | +- [Foundry Book](https://book.getfoundry.sh/) |
| 156 | +- [Pyth Lazer SDK](https://github.com/pyth-network/pyth-crosschain) |
| 157 | + |
| 158 | +## License |
| 159 | + |
| 160 | +This project is licensed under the Apache-2.0 license. |
0 commit comments