Skip to content

Lazer doc updates #569

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

Merged
merged 2 commits into from
Jan 16, 2025
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
16 changes: 8 additions & 8 deletions pages/lazer/integrate-as-consumer/evm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ This guide is intended to serve users who wants to consume prices from the Pyth

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

1. **Use** Pyth Lazer SDK into EVM smart contracts to parse the price updates.
1. **Use** Pyth Lazer SDK in EVM smart contracts to parse the price updates.
2. **Subscribe** to Pyth Lazer websocket to receive price updates on backend or frontend.
3. **Include** the price updates into the smart contract transactions.

<Steps>

### Use Pyth Lazer SDK into smart contracts
### Use Pyth Lazer SDK in smart contracts

Pyth Lazer provides a [solidity SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/lazer/contracts/evm), which allows consumers to parse the price updates.
Pyth Lazer provides a [Solidity SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/lazer/contracts/evm), which allows consumers to parse the price updates.

```bash copy
forge install pythnet/pyth-crosschain
forge install pyth-network/pyth-crosschain
```

Add the following to `requirements.txt{:js}` file:

```bash copy
pyth-lazer-sdk/=lib/pythnet/pyth-crosschain/lazer/contracts/evm
pyth-lazer-sdk/=lib/pyth-network/pyth-crosschain/lazer/contracts/evm
```

Once the SDK is installed, one can import the sdk into smart contracts:

```solidity copy
import { PythLazer } from "pyth-lazer/PythLazer.sol";
import { PythLazerLib } from "pyth-lazer/PythLazerLib.sol";
import { PythLazer } from "pyth-lazer-sdk/PythLazer.sol";
import { PythLazerLib } from "pyth-lazer-sdk/PythLazerLib.sol";

```

Expand Down Expand Up @@ -127,7 +127,7 @@ for (uint8 i = 0; i < feedsLen; i++) {

### Subscribe to Pyth Lazer to receive Price Updates

Pyth Lazer provides a websocket endpoint to receive price updates. Moreover, Pyth Lazer also provides a [typescript SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/lazer/sdk/js) to subscribe to the websocket endpoint.
Pyth Lazer provides a websocket endpoint to receive price updates. Moreover, Pyth Lazer also provides a [Typescript SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/lazer/sdk/js) to subscribe to the websocket endpoint.

Consult [How to fetch price updates from Pyth Lazer](../fetch-price-updates.mdx) for a complete step-by-step guide.

Expand Down
4 changes: 2 additions & 2 deletions pages/lazer/integrate-as-consumer/svm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Integrating with Pyth Lazer in smart contracts as a consumer is a three-step pro

<Steps>

### Use Pyth Lazer SDK into smart contracts
### Use Pyth Lazer SDK in smart contracts

Pyth Lazer provides a [Solana SDK](https://docs.rs/pyth-lazer-solana-contract/latest/pyth_lazer_solana_contract/), which allows consumers to parse and verify the price updates on Solana-compatible chains.

Expand Down Expand Up @@ -174,7 +174,7 @@ if data.feeds.is_empty() || data.feeds[0].properties.is_empty() {
}
```

Now you can update the state accourding to the contract's logic.
Now you can update the state according to the contract's logic:

```rust copy
// Read the data PDA of our example contract.
Expand Down
74 changes: 38 additions & 36 deletions pages/lazer/subscribe-price-updates.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Callout, Steps } from "nextra/components";

# How to Subscribe to Price Updates from Pyth Lazer

This guide explains how to subscribe to price updates from Pyth Lazer. This guide will also explain various properties and channels that one can use to customize the price updates.
This guide explains how to subscribe to price updates from Pyth Lazer. This guide will also explain various properties and configuration options to customize the price updates.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the "one" wording in this doc was weird. I made everything imperative.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I tried not to use personal "You" and "your" initially.


Subscribing to price updates is a three-step process:

1. **Acquire** an access token.
2. **Adjust** subscription parameters.
2. **Configure** subscription parameters.
3. **Subscribe** to the price updates via [websocket API](https://pyth-lazer.dourolabs.app/docs).

The websocket server is available at `wss://pyth-lazer.dourolabs.app/v1/stream{:bash}`.
Expand All @@ -20,11 +20,13 @@ Please fill out [this form](https://tally.so/r/nP2lG5) to contact the Pyth team

Use the access token to authenticate the websocket connection by passing it as an `Authorization{:bash}` header with the value `Bearer {token}{:bash}`.

### 2. Adjust subscription parameters
### 2. Configure subscription parameters

One can configure the request/subscription parameters to customize the received price updates. A sample request is shown below:
Lazer supports several request/subscription parameters to customize the received price updates.
These parameters are configured by sending a subscription message to the webservice.
A sample request (using the Lazer SDK client -- see step 3) is shown below:

```js
```js copy
client.send({
type: "subscribe",
subscriptionId: 1,
Expand All @@ -35,64 +37,64 @@ client.send({
});
```

Here:
The most significant parameters are:

- `subscriptionId` is an arbitrary numeric identifier one can choose for a subscription. It will be returned back in response by the server. It doesn not affect the signed payload.
- `priceFeedIds` is the list of price feeds one like to receive. Data for all price feeds will be present in the signed price updates generated. Refer to the [Price Feed IDs list](./price-feed-ids.mdx) for the supported price feeds.
- `properties` is the list of properties one can request, such as **price**, **bestBidPrice**, **bestAskPrice**, etc.
- `chains` is the list of chains for which one need a signed payload, such as **evm**, **solana**, etc.
- `channel` allows to configure the update rate: updates in the **real_time** channel are sent as frequently as possible, while **fixed_rate@200ms** and **fixed_rate@50ms** channels are updated at fixed rates.
- `subscriptionId` is an arbitrary numeric identifier for a subscription. It will be returned back in response by the server. It does not affect the signed payload.
- `priceFeedIds` is the list of price feeds to receive updates for. Data for all price feeds will be present in the signed price updates generated. Refer to the [Price Feed IDs list](./price-feed-ids.mdx) for the supported price feeds.
- `properties` is the list of properties to retrieve, such as **price**, **bestBidPrice**, **bestAskPrice**, etc.
- `chains` is the list of chains to receive a signed payload for, such as **evm**, **solana**, etc.
- `channel` determines the update rate: updates in the **real_time** channel are sent as frequently as possible, while **fixed_rate@200ms** and **fixed_rate@50ms** channels are updated at fixed rates.

There are also a few other parameters one may use. Refer to the [API documentation](https://pyth-lazer.dourolabs.app/docs) for more details.
There are also a few other configuration parameters -- see the [API documentation](https://pyth-lazer.dourolabs.app/docs) for more details.

Determine the most suitable values for your application -- they will be used in the next step.

### 3. Subscribe to the price updates

To subscribe to the price updates, one needs to send the request to the websocket server. The server will respond with a signed price update.
To subscribe to the price updates, send a request to the websocket server. The server will respond with a signed price update.

1. Pyth Lazer provides a [SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/lazer/sdk/js) to seamlessly integrate the websocket API into your application.
It can be installed using the following command:
1. Pyth Lazer provides an [SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/lazer/sdk/js) to seamlessly integrate the websocket API into your application.
Install it using the following command:

```bash
```bash copy
npm install --save @pythnetwork/pyth-lazer-sdk
```

2. Then create a [`PythLazerClient`](https://github.com/pyth-network/pyth-crosschain/blob/main/lazer/sdk/js/src/client.ts#L32) object using the URL and the access token requested from the Pyth team in the first step.
2. Then create a [`PythLazerClient`](https://github.com/pyth-network/pyth-crosschain/blob/main/lazer/sdk/js/src/client.ts#L32) object using the URL and the access token requested from the Pyth team in the first step:

```js
```js copy
import { PythLazerClient } from "@pythnetwork/pyth-lazer-sdk";

const client = new PythLazerClient(
"wss://pyth-lazer.dourolabs.app/v1/stream",
"ctoken1"
const client = await PythLazerClient.create(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like the SDK interface changed since this was written

(by the way the example is wrong now as well)

["wss://pyth-lazer.dourolabs.app/v1/stream"],
"YOUR_ACCESS_TOKEN"
);
```

3. After the client is created, one can adjust the subscription parameters and subscribe to the price updates.

```js
client.ws.addEventListener("open", () => {
client.send({
type: "subscribe",
subscriptionId: 1,
priceFeedIds: [1, 2],
properties: ["price"],
chains: ["solana"],
channel: "fixed_rate@200ms",
});
3. After the client is created, subscribe to updates (using the configuration parameters from step 2):

```js copy
client.subscribe({
type: "subscribe",
subscriptionId: 1,
priceFeedIds: [1, 2],
properties: ["price"],
chains: ["solana"],
channel: "fixed_rate@200ms",
});
```

4. One the connection is established, the server will start sending the price updates to the client.
4. Once the connection is established, the server will start sending the price updates to the client:

```js
```js copy
client.addMessageListener((message) => {
console.log(message);
});
```

By default, price updates contain the `parsed` field that one can use to easily interpret the price update in their backend or frontend, as well as `evm` and/or `solana` fields that contain data that one should include in the on-chain transaction:

```json
```json copy
{
"type": "streamUpdated",
"subscriptionId": 1,
Expand Down
Loading