Skip to content

Commit

Permalink
feat(lazer): add js example (#35)
Browse files Browse the repository at this point in the history
* feat(lazer): add js example

* chore: set up CI
  • Loading branch information
Riateche authored Nov 29, 2024
1 parent 100ffcf commit 83c15bc
Show file tree
Hide file tree
Showing 6 changed files with 326 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci-lazer-js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: "Lazer JS Test Suite"
on:
push:
branches:
- main
pull_request:

jobs:
lazer-js-test-suite:
name: Lazer JS Test Suite
runs-on: ubuntu-22.04
defaults:
run:
working-directory: lazer/js
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: npm install --global [email protected]
- run: pnpm install --frozen-lockfile
- run: pnpm run test
2 changes: 2 additions & 0 deletions lazer/js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tsconfig.tsbuildinfo
dist
27 changes: 27 additions & 0 deletions lazer/js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "pyth-lazer-js-example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node --loader ts-node/esm src/index.js",
"test": "pnpm run test:format && pnpm run build:cjs && pnpm run build:esm",
"build:cjs": "tsc --project tsconfig.json --verbatimModuleSyntax false --module commonjs --outDir ./dist/cjs && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
"build:esm": "tsc --project tsconfig.json --outDir ./dist/esm && echo '{\"type\":\"module\"}' > dist/esm/package.json",
"fix:format": "prettier --write **/*.*",
"test:format": "prettier --check **/*.*"
},
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@pythnetwork/pyth-lazer-sdk": "^0.1.1"
},
"devDependencies": {
"@cprussin/tsconfig": "^3.0.1",
"@types/node": "^22.10.0",
"prettier": "^3.4.1",
"ts-node": "^10.9.2",
"typescript": "^5.7.2"
}
}
224 changes: 224 additions & 0 deletions lazer/js/pnpm-lock.yaml

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

44 changes: 44 additions & 0 deletions lazer/js/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { PythLazerClient } from "@pythnetwork/pyth-lazer-sdk";

/* eslint-disable no-console */
const client = new PythLazerClient(
"wss://pyth-lazer-staging.dourolabs.app/v1/stream",
"my_token",
);
client.addMessageListener((message) => {
console.log("got message:", message);
switch (message.type) {
case "json": {
if (message.value.type == "streamUpdated") {
console.log(
"stream updated for subscription",
message.value.subscriptionId,
":",
message.value.parsed?.priceFeeds,
);
}
break;
}
case "binary": {
if ("solana" in message.value) {
console.log("solana message:", message.value.solana?.toString("hex"));
}
if ("evm" in message.value) {
console.log("evm message:", message.value.evm?.toString("hex"));
}
break;
}
}
});
client.ws.addEventListener("open", () => {
client.send({
type: "subscribe",
subscriptionId: 1,
priceFeedIds: [1, 2],
properties: ["price"],
chains: ["solana"],
deliveryFormat: "json",
channel: "fixed_rate@200ms",
jsonBinaryEncoding: "hex",
});
});
7 changes: 7 additions & 0 deletions lazer/js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@cprussin/tsconfig/base.json",
"exclude": [
"node_modules",
"dist"
]
}

0 comments on commit 83c15bc

Please sign in to comment.