Skip to content
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

feat(apps/hermes): new client package for hermes #1653

Merged
merged 40 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
4968032
add functions for v2 endpoints
cctdaniel May 31, 2024
cda6b2c
precommit
cctdaniel May 31, 2024
1ee28a1
remove oracle-swap and send-usd examples
cctdaniel Jun 3, 2024
6ef0fcd
fix error
cctdaniel Jun 3, 2024
ac0f58e
fix precommit
cctdaniel Jun 3, 2024
662b529
fix error
cctdaniel Jun 3, 2024
ce093dc
replace axios with fetch
cctdaniel Jun 4, 2024
d06cb44
create new hermes client package
cctdaniel Jun 4, 2024
a6f9525
revert price-service changes
cctdaniel Jun 4, 2024
140e568
update readme
cctdaniel Jun 4, 2024
56083da
update ci workflow
cctdaniel Jun 4, 2024
fe87902
update package-lock.json
cctdaniel Jun 4, 2024
9c590e9
fix cargo
cctdaniel Jun 4, 2024
c78145c
fix cargo
cctdaniel Jun 4, 2024
34e87b3
fix errors
cctdaniel Jun 4, 2024
9b1ef4a
refactor
cctdaniel Jun 5, 2024
09c020d
address comments
cctdaniel Jun 5, 2024
9472898
address comments
cctdaniel Jun 5, 2024
071b24a
address comments
cctdaniel Jun 5, 2024
b3c9c46
address comments
cctdaniel Jun 6, 2024
5ed2da9
add CI to check hermes client api types
cctdaniel Jun 6, 2024
35ec533
fix ci
cctdaniel Jun 6, 2024
d9381ae
remove ci
cctdaniel Jun 6, 2024
792e42b
add CI back
cctdaniel Jun 6, 2024
bb403eb
updated generated types
cctdaniel Jun 6, 2024
716530f
update precommit to exlcude serverTypes.d.ts
cctdaniel Jun 6, 2024
f36eb2f
address comments
cctdaniel Jun 6, 2024
4ed669c
remove dockerfile
cctdaniel Jun 6, 2024
29d7c81
address comments
cctdaniel Jun 6, 2024
fd74424
fix test
cctdaniel Jun 6, 2024
d2a5dfc
address comments
cctdaniel Jun 7, 2024
092db66
update package-lock.json
cctdaniel Jun 7, 2024
6f4b64d
rename to HermesClient
cctdaniel Jun 7, 2024
4ac4ec8
rename HermesConnection to HermesClient
cctdaniel Jun 7, 2024
3bc2bde
address comments
cctdaniel Jun 7, 2024
e10084c
make openapi-zod-client dev-dep
cctdaniel Jun 7, 2024
890f544
address comments
cctdaniel Jun 7, 2024
7d6d1d9
add workflow
cctdaniel Jun 7, 2024
f2e80a3
update workflow
cctdaniel Jun 7, 2024
113f879
update package name
cctdaniel Jun 7, 2024
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
23 changes: 23 additions & 0 deletions .github/workflows/ci-hermes-client-api-types.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Check Hermes Client API Types

on:
pull_request:
paths: [apps/hermes/client/**]
push:
branches: [main]
paths: [apps/hermes/client/**]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 18
cache: "npm"
- name: Install deps
run: npm ci
- name: Generate API Types
run: npx lerna run generate-hermes-api-types
- name: Check API Types
run: npx lerna run check-hermes-api-types
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: Check Hermes
name: Check Hermes Server

on:
pull_request:
paths: [apps/hermes/**]
paths: [apps/hermes/server/**]
push:
branches: [main]
paths: [apps/hermes/**]
paths: [apps/hermes/server/**]
jobs:
cctdaniel marked this conversation as resolved.
Show resolved Hide resolved
test:
runs-on: ubuntu-latest
Expand All @@ -20,4 +20,4 @@ jobs:
- name: Install protoc
uses: arduino/setup-protoc@v3
- name: Run executor tests
run: cargo test --manifest-path ./apps/hermes/Cargo.toml
run: cargo test --manifest-path ./apps/hermes/server/Cargo.toml
21 changes: 21 additions & 0 deletions .github/workflows/publish-hermes-client.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Publish Hermes Client package

on:
push:
tags:
- hermes-client-v*
jobs:
publish-js:
name: Publish Hermes Client to NPM
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "18"
registry-url: "https://registry.npmjs.org"
- run: npm ci
- run: npx lerna run build --no-private
- run: npx lerna publish from-git --yes --no-private --no-git-tag-version
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ repos:
- id: cargo-fmt-hermes
name: Cargo format for Hermes
language: "rust"
entry: cargo +nightly-2024-03-26 fmt --manifest-path ./apps/hermes/Cargo.toml --all -- --config-path rustfmt.toml
entry: cargo +nightly-2024-03-26 fmt --manifest-path ./apps/hermes/server/Cargo.toml --all -- --config-path rustfmt.toml
pass_filenames: false
files: apps/hermes
- id: cargo-clippy-hermes
name: Cargo clippy for Hermes
language: "rust"
entry: cargo +nightly-2024-03-26 clippy --manifest-path ./apps/hermes/Cargo.toml --tests --fix --allow-dirty --allow-staged -- -D warnings
entry: cargo +nightly-2024-03-26 clippy --manifest-path ./apps/hermes/server/Cargo.toml --tests --fix --allow-dirty --allow-staged -- -D warnings
pass_filenames: false
files: apps/hermes
# Hooks for Fortuna
Expand Down
6 changes: 6 additions & 0 deletions apps/hermes/client/js/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
cctdaniel marked this conversation as resolved.
Show resolved Hide resolved
root: true,
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
};
1 change: 1 addition & 0 deletions apps/hermes/client/js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/
89 changes: 89 additions & 0 deletions apps/hermes/client/js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Hermes Client

[Pyth Network](https://pyth.network/) provides real-time pricing data in a variety of asset classes, including cryptocurrency, equities, FX and commodities.
These prices are available either via HTTP or Streaming from [Hermes](/apps/hermes).
This library is a client for interacting with Hermes, allowing your application to consume Pyth real-time prices in on- and off-chain Javascript/Typescript applications.

## Installation

### npm

```
$ npm install --save @pythnetwork/hermes-client
```

### Yarn

```
$ yarn add @pythnetwork/hermes-client
```

## Quickstart

Typical usage of the connection is along the following lines:

```typescript
const connection = new HermesClient("https://hermes.pyth.network", {}); // See Hermes endpoints section below for other endpoints

const priceIds = [
// You can find the ids of prices at https://pyth.network/developers/price-feed-ids
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43", // BTC/USD price id
"0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace", // ETH/USD price id
];

// Get price feeds
const priceFeeds = await connection.getPriceFeeds("btc", "crypto");
cctdaniel marked this conversation as resolved.
Show resolved Hide resolved
console.log(priceFeeds);

// Latest price updates
const priceUpdates = await connection.getLatestPriceUpdates(priceIds);
console.log(priceUpdates);
```

`HermesClient` also allows subscribing to real-time price updates over a Server-Sent Events (SSE) connection:

```typescript
// Streaming price updates
const eventSource = await connection.getStreamingPriceUpdates(priceIds);

eventSource.onmessage = (event) => {
console.log("Received price update:", event.data);
};

eventSource.onerror = (error) => {
console.error("Error receiving updates:", error);
eventSource.close();
};

await sleep(5000);

// To stop listening to the updates, you can call eventSource.close();
console.log("Closing event source.");
eventSource.close();
```

### On-chain Applications

On-chain applications will need to submit the price updates returned by Hermes to the Pyth contract on their blockchain.
By default, these updates are returned as binary data and is serialized as either a base64 string or a hex string depending on the chosen encoding. This binary data will need to be submitted to the Pyth contract.

### Examples

The [HermesClient](./src/examples/HermesClient.ts) example demonstrates both the examples above.
You can run it with `npm run example`.
A full command that prints BTC and ETH price feeds, in the testnet network, looks like so:

```bash
npm run example -- \
--endpoint https://hermes.pyth.network \
--price-ids \
0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43 \
0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace
```

## Hermes endpoints

Pyth offers a free public endpoint at [https://hermes.pyth.network](https://hermes.pyth.network). However, it is
recommended to obtain a private endpoint from one of the Hermes RPC providers for more reliability. You can find more
information about Hermes RPC providers
[here](https://docs.pyth.network/documentation/pythnet-price-feeds/hermes#public-endpoint).
5 changes: 5 additions & 0 deletions apps/hermes/client/js/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
cctdaniel marked this conversation as resolved.
Show resolved Hide resolved
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
58 changes: 58 additions & 0 deletions apps/hermes/client/js/package.json
ali-bahjati marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "@pythnetwork/hermes-client",
"version": "1.0.0",
"description": "Pyth Hermes Client",
"author": {
"name": "Pyth Data Association"
},
"homepage": "https://pyth.network",
"main": "lib/HermesClient.js",
"types": "lib/HermesClient.d.ts",
"files": [
"lib/**/*"
],
"repository": {
"type": "git",
"url": "https://github.com/pyth-network/pyth-crosschain",
"directory": "apps/hermes/client/js"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"test": "jest --testPathIgnorePatterns=.*.e2e.test.ts --passWithNoTests",
"test:e2e": "jest --testPathPattern=.*.e2e.test.ts",
"build": "tsc",
"generate-hermes-api-types": "openapi-zod-client https://hermes.pyth.network/docs/openapi.json --output src/zodSchemas.ts && prettier --write src/zodSchemas.ts",
"check-hermes-api-types": "git diff --exit-code src/zodSchemas.ts",
"example": "npm run build && node lib/examples/HermesClient.js",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "eslint src/",
"prepublishOnly": "npm run build && npm test && npm run lint",
"preversion": "npm run lint",
"version": "npm run format && git add -A src"
},
"keywords": [
"pyth",
"oracle"
],
"license": "Apache-2.0",
"devDependencies": {
"@types/eventsource": "^1.1.15",
"@types/jest": "^29.4.0",
"@types/yargs": "^17.0.10",
"@typescript-eslint/eslint-plugin": "^5.21.0",
"@typescript-eslint/parser": "^5.21.0",
"eslint": "^8.14.0",
"jest": "^29.4.0",
"openapi-zod-client": "^1.18.1",
"prettier": "^2.6.2",
"ts-jest": "^29.0.5",
"typescript": "^4.6.3",
"yargs": "^17.4.1"
},
"dependencies": {
"eventsource": "^2.0.2",
"zod": "^3.23.8"
}
}
Loading
Loading