Skip to content

Commit bbe10ba

Browse files
authored
[sdk] Move JS sdks into this repo (#611)
* move files to subdir and js sdk here * update pointer * fix move * delete lib * fix this * gitignore * aptos sdk * gitignore * terra js * gitignore * move price pusher * add lib to gitignore * move examples * fix workflow * gr * contracts * hm * fix * gr * grrr * fix dockerfile * wtf * fix
1 parent 20b18e2 commit bbe10ba

File tree

181 files changed

+73450
-85
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+73450
-85
lines changed

.github/workflows/ethereum-contract.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
on:
22
pull_request:
33
paths:
4-
- target_chains/ethereum/**
4+
- target_chains/ethereum/contracts/**
55
- governance/xc_governance_sdk_js/**
66
push:
77
branches:
88
- main
99
paths:
10-
- target_chains/ethereum/**
10+
- target_chains/ethereum/contracts/**
1111
- governance/xc_governance_sdk_js/**
1212

1313
name: Ethereum Contract
@@ -18,7 +18,7 @@ jobs:
1818
runs-on: ubuntu-latest
1919
defaults:
2020
run:
21-
working-directory: target_chains/ethereum/
21+
working-directory: target_chains/ethereum/contracts/
2222
steps:
2323
- uses: actions/checkout@v3
2424

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ bigtable-writer.json
1414
.dccache
1515
.aptos
1616
tsconfig.tsbuildinfo
17+
*~

package-lock.json

Lines changed: 43 additions & 71 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"price_service/server",
88
"price_service/sdk/js",
99
"price_service/client/js",
10-
"target_chains/ethereum",
10+
"target_chains/ethereum/contracts",
1111
"third_party/pyth/p2w-relay",
1212
"wormhole_attester/sdk/js"
1313
],

price_pusher/.eslintrc.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
root: true,
3+
parser: "@typescript-eslint/parser",
4+
plugins: ["@typescript-eslint"],
5+
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
6+
rules: {
7+
"@typescript-eslint/no-explicit-any": "off",
8+
"@typescript-eslint/no-non-null-assertion": "off",
9+
},
10+
};

price_pusher/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
docker-compose.yaml
2+
price-config.yaml
3+
lib

price_pusher/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM node:16-alpine@sha256:72a490e7ed8aed68e16b8dc8f37b5bcc35c5b5c56ee3256effcdee63e2546f93
2+
3+
RUN addgroup -S pyth -g 10001 && adduser -S pyth -G pyth -u 10001
4+
USER pyth
5+
6+
ARG PRICE_PUSHER_PATH=/usr/src/pyth-evm-price-pusher
7+
WORKDIR ${PRICE_PUSHER_PATH}
8+
COPY --chown=pyth:pyth . .
9+
RUN npm ci && npm run build && npm cache clean --force
10+
11+
ENTRYPOINT [ "npm", "run", "start" ]

price_pusher/README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Pyth EVM price pusher
2+
3+
Pyth EVM price pusher is a service that regularly pushes updates to the on-chain Pyth price based on configurable conditions.
4+
5+
## Background
6+
7+
Pyth is a cross-chain oracle that streams price updates over the peer-to-peer [Wormhole Network](https://wormholenetwork.com/).
8+
These price updates can be consumed on any chain that has a deployment of the Pyth contract.
9+
By default, Pyth does not automatically update the on-chain price every time the off-chain price changes;
10+
instead, anyone can permissionlessly update the on-chain price prior to using it.
11+
For more information please refer to [this document](../pyth-evm-js/README.md#how-pyth-works-on-evm-chains).
12+
13+
Protocols integrating with can update the on-chain Pyth prices in two different ways.
14+
The first approach is on-demand updates: package a Pyth price update together with each transaction that depends on it.
15+
On-demand updates minimize latency and are more gas efficient, as prices are only updated on-chain when they are needed.
16+
17+
The second approach is to run this service to regularly push updates to the on-chain price.
18+
This approach is useful for protocols that already depend on regular push updates.
19+
20+
## Running Price Pusher
21+
22+
The price pusher service monitors both the off-chain and on-chain Pyth price for a configured set of price feeds.
23+
It then pushes a price update to an on-chain Pyth contract if any of the following conditions are met:
24+
25+
- Time difference: The on-chain price is older than `time_difference` seconds
26+
from the latest Pyth price.
27+
- Price deviation: The latest Pyth price feed has changed more than `price_deviation` percent
28+
from the on-chain price feed price.
29+
- Confidence ratio: The latest Pyth price feed has confidence to price ratio of more than
30+
`confidence_ratio`.
31+
32+
The parameters above are configured per price feed in a price configuration YAML file. The structure looks like this:
33+
34+
```yaml
35+
- alias: A/USD # Arbitrary alias for the price feed. It is used in enhance logging.
36+
id: 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef # id of a price feed, a 32-byte hex string.
37+
time_difference: 60 # Time difference threshold (in seconds) to push a newer price feed.
38+
price_deviation: 0.5 # The price deviation (%) threshold to push a newer price feed.
39+
confidence_ratio: 1 # The confidence/price (%) threshold to push a newer price feed.
40+
- ...
41+
```
42+
43+
You can get the list of available price feeds from
44+
[here](https://pyth.network/developers/price-feed-ids/).
45+
46+
To run the price pusher, please run the following commands, replacing the command line arguments as necessary:
47+
48+
```sh
49+
npm install # Only run it the first time
50+
51+
npm run start -- --evm-endpoint wss://example-rpc.com --mnemonic-file "path/to/mnemonic.txt" \
52+
--pyth-contract example_network --price-endpoint https://example-pyth-price.com \
53+
--price-config-file "path/to/price-config-file.yaml" \
54+
[--cooldown-duration 10] \
55+
[--evm-polling-frequency 5]
56+
57+
# Or, run the price pusher docker image instead of building from the source
58+
docker run public.ecr.aws/pyth-network/xc-evm-price-pusher:v<version> -- <above-arguments>
59+
```
60+
61+
### Command Line Arguments
62+
63+
The program accepts the following command line arguments:
64+
65+
- `evm-endpoint`: RPC endpoint URL for the EVM network. If you provide a normal HTTP endpoint,
66+
the pusher will periodically poll for updates. The polling interval is configurable via the
67+
`evm-polling-frequency` command-line argument (described below). If you provide a websocket RPC endpoint
68+
(`ws[s]://...`), the price pusher will use event subscriptions to read the current EVM
69+
price in addition to polling.
70+
- `mnemonic-file`: Path to payer mnemonic (private key) file.
71+
- `pyth-contract`: The Pyth contract address. Provide the network name on which Pyth is deployed
72+
or the Pyth contract address if you use a local network.
73+
You can find the networks on which pyth is live and their corresponding names
74+
[here](../pyth-evm-js/src/index.ts#L13). An example is `bnb_testnet`.
75+
- `price-endpoint`: Endpoint URL for the price service. You can use
76+
`https://xc-testnet.pyth.network` for testnet and
77+
`https://xc-mainnet.pyth.network` for mainnet. It is recommended
78+
to run a standalone price service for more resiliency.
79+
- `price-config-file`: Path to price configuration YAML file.
80+
- `cooldown-duration` (Optional): The amount of time (in seconds) to wait between pushing
81+
price updates. It should be greater than the block time of the network, so this
82+
program confirms the price is updated and does not push it twice. Default: 10 seconds.
83+
- `evm-polling-frequency` (Optional): The frequency to poll price info data from the EVM network
84+
if the RPC is not a Websocket. It has no effect if the RPC is a Websocket.
85+
Default: 5 seconds.
86+
87+
### Example
88+
89+
For example, to push `BTC/USD` and `BNB/USD` prices on BNB testnet, run the following command:
90+
91+
```sh
92+
npm run start -- --evm-endpoint "https://data-seed-prebsc-1-s1.binance.org:8545" --mnemonic-file "path/to/mnemonic.txt" \
93+
--pyth-contract bnb_testnet --price-endpoint https://xc-testnet.pyth.network \
94+
--price-config-file "price-config.testnet.sample.yaml"
95+
```
96+
97+
[`price-config.testnet.sample.yaml`](./price-config.testnet.sample.yaml) contains configuration for `BTC/USD`
98+
and `BNB/USD` price feeds on Pyth testnet. [`price-config.mainnet.sample.yaml`](./price-config.mainnet.sample.yaml)
99+
contains the same configuration for `BTC/USD` and `BNB/USD` on Pyth mainnet.
100+
101+
## Running using a standalone price service (via docker-compose)
102+
103+
EVM price pusher communicates with [Pyth price service][] to get the most recent price updates. Pyth price service listens to the
104+
Wormhole network to get latest price updates, and serves REST and websocket APIs for consumers to fetch the updates.
105+
Pyth hosts public endpoints for the price service; however, it is recommended to run it standalone to achieve more resiliency and
106+
scalability.
107+
108+
This directory contains sample docker compose files ([testnet](./docker-compose.testnet.sample.yaml),
109+
[mainnet](./docker-compose.mainnet.sample.yaml)) an EVM price pusher and its dependencies, including a
110+
price service and a Wormhole spy. A price service depends on a Wormhole spy. A spy listens to the Wormhole
111+
network and reports all Pyth-related Wormhole messages to the price service.
112+
113+
To run the services via docker-compose, please modify the your target network (testnet, mainnet) sample docker-compose file to adjust
114+
the path to your mnemonic file, the path to your price configuration file, the EVM endpoint, and the Pyth contract address
115+
as necessary.
116+
117+
Then, start the docker-compose like this:
118+
119+
```
120+
docker-compose -f docker-compose.testnet.sample.yaml up
121+
```
122+
123+
It will take a few minutes until all the services are up and running.
124+
125+
[pyth price service]: https://github.com/pyth-network/pyth-crosschain/tree/main/third_party/pyth/price-service

0 commit comments

Comments
 (0)