Skip to content

Commit b42aac1

Browse files
committed
evm example
1 parent b678762 commit b42aac1

File tree

1 file changed

+85
-8
lines changed
  • pages/lazer/integrate-as-consumer

1 file changed

+85
-8
lines changed

pages/lazer/integrate-as-consumer/evm.mdx

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@ import { Callout, Tabs, Steps } from "nextra/components";
22

33
# Integrate Pyth Lazer as a Consumer on EVM chains
44

5-
This guide is intended to serve for users who wants to consume prices from the Pyth Lazer price feed on EVM chains.
5+
This guide is intended to serve users who wants to consume prices from the Pyth Lazer on EVM chains.
66

7-
Integrating with Pyth Lazer in your smart contracts as a consumer is a three-step process:
7+
Integrating with Pyth Lazer in smart contracts as a consumer is a three-step process:
88

9-
1. **Use** Pyth Lazer SDK into your smart contracts to parse the price updates.
10-
2. **Subscribe** to Pyth Lazer websocket to receive price updates on your backend or frontend.
11-
3. **Include** the price updates into your smart contract transactions.
9+
1. **Use** Pyth Lazer SDK into smart contracts to parse the price updates.
10+
2. **Subscribe** to Pyth Lazer websocket to receive price updates on backend or frontend.
11+
3. **Include** the price updates into the smart contract transactions.
1212

1313
<Steps>
1414

15-
### Use Pyth Lazer SDK into your smart contracts
15+
### Use Pyth Lazer SDK into smart contracts
1616

1717
Pyth Lazer provides a solidity SDK, which allows consumers to parse the price updates.
1818

1919
```bash copy
2020
forge install pythnet/pyth-crosschain
2121
```
2222

23-
Add the following to your `requirements.txt` file:
23+
Add the following to `requirements.txt{:js}` file:
2424

2525
```bash copy
2626
pyth-lazer-sdk/=lib/pythnet/pyth-crosschain/lazer/contracts/evm
2727
```
2828

29-
Once the SDK is installed, one can import the sdk into your smart contracts:
29+
Once the SDK is installed, one can import the sdk into smart contracts:
3030

3131
```solidity copy
3232
import { PythLazer } from "pyth-lazer/PythLazer.sol";
@@ -65,4 +65,81 @@ function updatePrice(bytes calldata priceUpdate) public payable {
6565

6666
The `verifyUpdate` function will verify the price update and return the payload and the verification fee. This call takes a fee which can be queried from `verification_fee()` function and passed to the `verifyUpdate` call. This fee is used to cover the cost of verifying the price update.
6767

68+
This SDK provides `parsePayloadHeader` method to retrieve the values from the payload header.
69+
70+
```solidity copy
71+
(uint64 _timestamp, Channel channel, uint8 feedsLen, uint16 pos) = parsePayloadHeader(payload);
72+
```
73+
74+
This method returns:
75+
76+
- `_timestamp`: The timestamp of the price update.
77+
- `channel`: The channel of the price update.
78+
- `feedsLen`: The number of feeds in the price update.
79+
- `pos`: The cursor position of the payload.
80+
81+
One can iterate over all the feeds and properties present within the price update, modifying the state variables as necessary.
82+
83+
Here is an example of how to iterate over the feeds and properties:
84+
85+
```solidity copy
86+
for (uint8 i = 0; i < feedsLen; i++) {
87+
uint32 feedId;
88+
uint8 num_properties;
89+
(feedId, num_properties, pos) = parseFeedHeader(payload, pos);
90+
for (uint8 j = 0; j < num_properties; j++) {
91+
PriceFeedProperty property;
92+
(property, pos) = parseFeedProperty(payload, pos);
93+
if (property == PriceFeedProperty.Price) {
94+
uint64 _price;
95+
(_price, pos) = parseFeedValueUint64(payload, pos);
96+
if (feedId == 2 && _timestamp > timestamp) {
97+
price = _price;
98+
timestamp = _timestamp;
99+
}
100+
} else if (property == PriceFeedProperty.BestBidPrice) {
101+
uint64 _price;
102+
(_price, pos) = parseFeedValueUint64(payload, pos);
103+
} else if (property == PriceFeedProperty.BestAskPrice) {
104+
uint64 _price;
105+
(_price, pos) = parseFeedValueUint64(payload, pos);
106+
} else {
107+
revert("unknown property");
108+
}
109+
}
110+
}
111+
```
112+
113+
### Subscribe to Pyth Lazer to receive Price Updates
114+
115+
Pyth Lazer provides a websocket endpoint to receive price updates. Moreover, Pyth Lazer provides a typescript SDK to subscribe to the websocket endpoint.
116+
117+
Consult [How to fetch price updates from Pyth Lazer](../fetch-price-updates.mdx) for a complete step-by-step guide.
118+
119+
### Include the price updates into smart contract transactions
120+
121+
Now that one have the price updates, and the smart contract is able to parse the price updates, one can include the price updates into the smart contract transactions by passing the price updates received from the websocket to the `updatePrice` method.
122+
68123
</Steps>
124+
125+
## Additional Resources
126+
127+
You may find these additional resources helpful for integrating Pyth Lazer into your EVM smart contracts.
128+
129+
### Price Feed IDs
130+
131+
Pyth Lazer supports a wide range of price feeds. Consult the [Price Feed IDs](../price-feeds.mdx) page for a complete list of supported price feeds.
132+
133+
### Examples
134+
135+
[Pyth-lazer-example-evm](https://github.com/pyth-network/pyth-examples/tree/main/lazer/evm) is a simple example contract that parses and consumes price updates from Pyth Lazer.
136+
137+
[pyth-lazer-example-js](https://github.com/pyth-network/pyth-examples/tree/main/lazer/js) is a simple example for subscribing to the Pyth Lazer websocket.
138+
139+
### API Reference
140+
141+
TODO:
142+
143+
### Error Codes
144+
145+
TODO: Add error codes for EVM.

0 commit comments

Comments
 (0)