Skip to content

Chore(lazer) add lazer docs #554

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 16 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 15 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
98 changes: 98 additions & 0 deletions components/LazerPriceIdTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { useEffect, useState } from "react";
import { StyledTd } from "./Table";
import { Spinner } from "./Spinner";
const fetchLazerPriceIdMetadata = async () => {
const response = await fetch(
"https://pyth-lazer-staging.dourolabs.app/history/v1/symbols"
);
const data = await response.json();
return data;
};

type LazerPriceIdMetadata = {
asset_type: string;
description: string;
exponent: number;
name: string;
pyth_lazer_id: number;
symbol: string;
};

enum LazerPriceIdStateType {
NotLoaded,
Loading,
Loaded,
Error,
}

const LazerPriceIdState = {
NotLoaded: () => ({ type: LazerPriceIdStateType.NotLoaded as const }),
Loading: () => ({ type: LazerPriceIdStateType.Loading as const }),
Loaded: (priceFeeds: LazerPriceIdMetadata[]) => ({
type: LazerPriceIdStateType.Loaded as const,
priceFeeds,
}),
Error: (error: unknown) => ({
type: LazerPriceIdStateType.Error as const,
error,
}),
};

type LazerPriceIdState = ReturnType<
typeof LazerPriceIdState[keyof typeof LazerPriceIdState]
>;

const useLazerPriceIdState = () => {
const [state, setState] = useState<LazerPriceIdState>(
LazerPriceIdState.NotLoaded()
);

useEffect(() => {
setState(LazerPriceIdState.Loading());
fetchLazerPriceIdMetadata()
.then((priceFeeds) => setState(LazerPriceIdState.Loaded(priceFeeds)))
.catch((error) => setState(LazerPriceIdState.Error(error)));
}, []);

return state;
};

export function LazerPriceIdTable() {
const lazerPriceIdState = useLazerPriceIdState();

switch (lazerPriceIdState.type) {
case LazerPriceIdStateType.NotLoaded:
return <div>Loading...</div>;
case LazerPriceIdStateType.Loading:
return <Spinner />;
case LazerPriceIdStateType.Loaded:
return (
<table>
<thead>
<tr>
<th>Asset Type</th>
<th>Description</th>
<th>Name</th>
<th>Symbol</th>
<th>Pyth Lazer Id</th>
<th>Exponent</th>
</tr>
</thead>
<tbody>
{lazerPriceIdState.priceFeeds.map((priceFeed) => (
<tr key={priceFeed.symbol}>
<StyledTd>{priceFeed.asset_type}</StyledTd>
<StyledTd>{priceFeed.description}</StyledTd>
<StyledTd>{priceFeed.name}</StyledTd>
<StyledTd>{priceFeed.symbol}</StyledTd>
<StyledTd>{priceFeed.pyth_lazer_id}</StyledTd>
<StyledTd>{priceFeed.exponent}</StyledTd>
</tr>
))}
</tbody>
</table>
);
case LazerPriceIdStateType.Error:
return <div>Error</div>;
}
}
23 changes: 23 additions & 0 deletions components/Spinner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { SVGProps } from "react";
export const Spinner = (props: SVGProps<SVGSVGElement>) => (
<div role="status">
<svg
aria-hidden="true"
className="w-8 h-8 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
{...props}
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
);
Binary file added images/lazer/Pyth_Laser_table.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions pages/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"type": "page"
},

"lazer": {
"title": "Lazer",
"type": "page"
},

"benchmarks": {
"title": "Benchmarks",
"type": "page"
Expand Down
5 changes: 5 additions & 0 deletions pages/home/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
"href": "/price-feeds"
},

"Lazer": {
"title": "Lazer →",
"href": "/lazer"
},

"Benchmarks": {
"title": "Benchmarks →",
"href": "/benchmarks"
Expand Down
1 change: 1 addition & 0 deletions pages/home/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Pyth Network offers several products for developers:

- [Price Feeds](../price-feeds) provide real-time prices for 500+ assets on 55+ blockchain ecosystems, including Solana, many EVM chains,
Aptos, Sui, NEAR, and several Cosmos chains.
- [Lazer](../lazer) [TODO]
- [Benchmarks](../benchmarks) provides historical Pyth prices for both on- and off-chain use.
- [Express Relay](../express-relay/) enables protocols to eliminate their MEV while gaining access to active searchers and liquidators.
- [Entropy](../entropy) allows developers to generate secure random numbers on the blockchain.
Expand Down
49 changes: 49 additions & 0 deletions pages/lazer/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"documentation-home": {
"title": "← Documentation Home",
"href": "/home"
},

"-- Lazer": {
"title": "Lazer",
"type": "separator"
},
"index": "Introduction",
"getting-started": "Getting Started",

"-- How-to Guides": {
"title": "How-To Guides",
"type": "separator"
},

"subscribe-price-updates": "Subscribe to Price Updates",
"integrate-as-consumer": "Integrate as a Consumer",
"integrate-as-publisher": "Integrate as a Publisher",

"-- Reference Material": {
"title": "Reference Material",
"type": "separator"
},
"price-feeds-ids": "Price Feed IDs",

"websocket-api-reference": {
"title": "Websocket API Reference ↗",
"href": "https://pyth-lazer.dourolabs.app/docs",
"newWindow": true
},

"contract-addresses": "Contract Addresses",

"errors": "Error Codes",
"examples": {
"title": "Example Application ↗",
"href": "https://github.com/pyth-network/pyth-examples/tree/main/lazer",
"newWindow": true
},
"-- Understand Lazer": {
"title": "Understanding Lazer",
"type": "separator"
},

"how-lazer-works": "How Lazer Works"
}
131 changes: 131 additions & 0 deletions pages/lazer/fetch-price-updates.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
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.

Subscribing to price updates is a three-step process:

1. **Acquire** an access token.
2. **Adjust** 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}`.

<Steps>

### 1. Acquire an access token

Please fill out [this form](https://tally.so/r/nP2lG5) to contact the Pyth team and get the access token.

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

One can configure the request/subscription parameters to customize the received price updates. A sample request is shown below:

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

Here:

- `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-feeds-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.

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

### 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.

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:

```bash
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.

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

const client = new PythLazerClient(
"wss://pyth-lazer.dourolabs.app/v1/stream",
"ctoken1"
);
```

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",
});
});
```

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

```js
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
{
"type": "streamUpdated",
"subscriptionId": 1,
"parsed": {
"timestampUs": "1730986152400000",
"priceFeeds": [
{
"priceFeedId": 1,
"price": "1006900000000"
},
{
"priceFeedId": 2,
"price": "2006900000000"
}
]
},
"solana": {
"encoding": "hex",
"data": "b9011a82d239c094c52016990d6ca2b261dbb1157ad503cbd3ea0679493316150cf3457624d19ec3f6e0a0e94373ab0971e39d939beda15cc02eb3c5454eb700f1f7310df65210bee4fcf5b1cee1e537fabcfd95010297653b94af04d454fc473e94834f2a0075d3c7938094b99e52260600030201000000010000b5ea6fea00000002000000010000c58f44d3010000"
}
}
```

</Steps>

## Additional Resources

You may find these additional resources helpful for subscribing to price updates from Pyth Lazer.

### Price Feed IDs

Pyth Lazer supports a wide range of price feeds. Consult the [Price Feed IDs](../price-feeds-ids.mdx) page for a complete list of supported price feeds.

### Examples

[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.
1 change: 1 addition & 0 deletions pages/lazer/getting-started.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Getting Started with Pyth Lazer
5 changes: 5 additions & 0 deletions pages/lazer/how-lazer-works.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# How Pyth Lazer works

Pyth Lazer is a permissioned service that provides ultra-low-latency price and market data to highly latency-sensitive users.

We are working on writing a detailed technical overview how Lazer works. Details will be added here as they are completed.
20 changes: 20 additions & 0 deletions pages/lazer/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Image from "next/image";

# Pyth Lazer

Pyth Lazer is a low latency, highly customizable price oracle.
It offers a customizable set of price feeds, target chains (EVM or Solana) and channels (real time or fixed rate):

- Real time channels send updates as frequently as they become available;
- Fixed rate channels send updates at fixed time intervals (you can choose between 50 ms or 200 ms).

The table below shows the difference between Pyth Core and Pyth Lazer:

| | **Pyth Core** | **Pyth Lazer** |
| ----------------------- | -------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| **Solution Type** | Stable, secure, and decentralized price data source for a broad spectrum of DeFi or TradFi applications. | **Permissioned** service focused on **ultra-low-latency** price and market data for highly latency-sensitive users. |
| **Frequency** | 400ms on Pythnet appchain with support for risk mitigation via Benchmarks and confidence intervals. | **1ms** (**real-time**), 50ms, and 200ms channels, **customizable** frequencies, and throttling support to address different needs. |
| **Data Types** | Aggregate price and confidence intervals. | Aggregate price, bid/ask price, and **customizable** market data (market depth and more). |
| **Fees** | On-chain fee per signed cross-chain price update. | On-chain fee per signed cross-chain price update. |
| **Update Costs** | >1,000-byte proofs and complex signature verification. | **100-byte proofs** and simple signature verification. |
| **Integration Process** | Open and permissionless integration for any Web3 or Web2 protocol. | **Specialized** and **permissioned** solution for protocols prioritizing performance over some elements of decentralization. |
3 changes: 3 additions & 0 deletions pages/lazer/integrate-as-consumer.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# How to Integrate Pyth Lazer as a Consumer

TODO: Fill this up.
4 changes: 4 additions & 0 deletions pages/lazer/integrate-as-consumer/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"evm": "on EVM chains",
"svm": "on Solana"
}
Loading
Loading