Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Commit 9fa6dff

Browse files
authored
Merge pull request #44 from pyth-network/aptos-sdk-initial
Initial Pyth Aptos JS SDK
2 parents e0a4fe0 + 312a1ff commit 9fa6dff

File tree

11 files changed

+20505
-0
lines changed

11 files changed

+20505
-0
lines changed

.github/workflows/build.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,29 @@ jobs:
4444
working-directory: "pyth-evm-js"
4545
- run: npm run build
4646
working-directory: "pyth-evm-js"
47+
build-pyth-aptos-js:
48+
name: Build Pyth Aptos JS
49+
runs-on: ubuntu-latest
50+
strategy:
51+
matrix:
52+
node-version: [14.x, 16.x, 18.x]
53+
steps:
54+
- uses: actions/checkout@v2
55+
with:
56+
persist-credentials: false
57+
# This step is required for npm 14 to work for installing this
58+
- name: Reconfigure git to use HTTP authentication
59+
run: >
60+
git config --global url."https://github.com/".insteadOf
61+
62+
- name: Use Node.js ${{ matrix.node-version }}
63+
uses: actions/setup-node@v2
64+
with:
65+
node-version: ${{ matrix.node-version }}
66+
- run: npm ci
67+
working-directory: "pyth-aptos-js"
68+
- run: npm run build
69+
working-directory: "pyth-aptos-js"
4770
build-pyth-terra-js:
4871
name: Build Pyth Common JS
4972
runs-on: ubuntu-latest

.github/workflows/publish.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ jobs:
3737
env:
3838
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
3939
working-directory: "pyth-evm-js"
40+
publish-pyth-aptos-js:
41+
name: Publish Pyth Aptos JS
42+
if: ${{ startsWith(github.ref, 'refs/tags/pyth-aptos-js-v') }}
43+
runs-on: ubuntu-latest
44+
steps:
45+
- uses: actions/checkout@v2
46+
- uses: actions/setup-node@v2
47+
with:
48+
node-version: "16"
49+
registry-url: "https://registry.npmjs.org"
50+
- run: npm ci
51+
working-directory: "pyth-aptos-js"
52+
- run: npm publish --access public
53+
env:
54+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
55+
working-directory: "pyth-aptos-js"
4056
publish-pyth-terra-js:
4157
name: Publish Pyth Common JS
4258
if: ${{ startsWith(github.ref, 'refs/tags/pyth-terra-js-v') }}

pyth-aptos-js/.eslintrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
},
9+
};

pyth-aptos-js/README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Pyth Aptos JS
2+
3+
[Pyth](https://pyth.network/) provides real-time pricing data in a variety of asset classes, including cryptocurrency, equities, FX and commodities. This library allows you to use these real-time prices on Aptos networks.
4+
5+
## Installation
6+
7+
### npm
8+
9+
```
10+
$ npm install --save @pythnetwork/pyth-aptos-js
11+
```
12+
13+
### Yarn
14+
15+
```
16+
$ yarn add @pythnetwork/pyth-aptos-js
17+
```
18+
19+
## Quickstart
20+
21+
Pyth stores prices off-chain to minimize gas fees, which allows us to offer a wider selection of products and faster update times.
22+
To use Pyth prices on chain,
23+
they must be fetched from an off-chain price service. The `AptosPriceServiceConnection` class can be used to interact with these services,
24+
providing a way to fetch these prices directly in your code. The following example wraps an existing RPC provider and shows how to obtain
25+
Pyth prices and submit them to the network:
26+
27+
```typescript
28+
const connection = new AptosPriceServiceConnection(
29+
"https://xc-testnet.pyth.network"
30+
); // See Price Service endpoints section below for other endpoints
31+
32+
const priceIds = [
33+
// You can find the ids of prices at https://pyth.network/developers/price-feed-ids/#pyth-cross-chain-testnet
34+
"0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b", // BTC/USD price id in testnet
35+
"0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6", // ETH/USD price id in testnet
36+
];
37+
38+
// `getPythLatestPriceFeeds` returns a `PriceFeed` for each price id. It contains all information about a price and has
39+
// utility functions to get the current and exponentially-weighted moving average price, and other functionality.
40+
const priceFeeds = connection.getPythLatestPriceFeeds(priceIds);
41+
console.log(priceFeeds[0].getCurrentPrice()); // Price { conf: '1234', expo: -8, price: '12345678' }
42+
console.log(priceFeeds[1].getEmaPrice()); // Exponentially-weighted moving average price
43+
44+
// In order to use Pyth prices in your protocol you need to submit the price update data to Pyth contract in your target
45+
// chain. `getPriceUpdateData` creates the update data which can be submitted to your contract. Then your contract should
46+
// call the Pyth Contract with this data.
47+
const priceUpdateData = await connection.getPriceUpdateData(priceIds);
48+
49+
// Create a transaction and submit to your contract using the price update data
50+
const client = new AptosClient(endpoint);
51+
let result = await client.generateSignSubmitWaitForTransaction(
52+
sender,
53+
new TxnBuilderTypes.TransactionPayloadEntryFunction(
54+
TxnBuilderTypes.EntryFunction.natural(
55+
"0x..::your_module",
56+
"do_something",
57+
[],
58+
[priceUpdateData]
59+
)
60+
)
61+
);
62+
```
63+
64+
`your_module::do_something` should then call `pyth::update_price_feeds` before querying the data using `pyth::get_price`:
65+
66+
```move
67+
module example::your_module {
68+
use pyth::pyth;
69+
use pyth::price_identifier;
70+
use aptos_framework::coin;
71+
72+
public fun do_something(user: &signer, pyth_update_data: vector<vector<u8>>) {
73+
74+
// First update the Pyth price feeds
75+
let coins = coin::withdraw(user, pyth::get_update_fee());
76+
pyth::update_price_feeds(pyth_update_data, coins);
77+
78+
// Now we can use the prices which we have just updated
79+
let btc_usd_price_id = price_identifier::from_byte_vec(
80+
x"e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43");
81+
let price = pyth::get_price(btc_usd_price_id);
82+
83+
}
84+
}
85+
```
86+
87+
We strongly recommend reading our guide which explains [how to work with Pyth price feeds](https://docs.pyth.network/consume-data/best-practices).
88+
89+
### Example
90+
91+
[This example](./src/examples/AptosRelay.ts) shows how to update prices on an EVM network. It does the following:
92+
93+
1. Fetches update data from the Price Service for the given price feeds.
94+
2. Calls the Pyth Aptos contract with the update data.
95+
96+
You can run this example with `npm run example-relay`. A full command that updates BTC and ETH prices on the BNB Chain testnet network looks like so:
97+
98+
```bash
99+
export APTOS_KEY = "0x...";
100+
npm run example-relay -- --endpoint https://xc-testnet.pyth.network --price-ids 0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b 0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6 --full-node https://fullnode.testnet.aptoslabs.com/v1 --pyth-contract 0xc655df014ec65d43b478e8e580a2e9493db954818efb01f1cb4a24654294a709
101+
```
102+
103+
## How Pyth Works on Aptos
104+
105+
Pyth prices are published on Pythnet, and relayed to Aptos using the [Wormhole Network](https://wormholenetwork.com/) as a cross-chain message passing bridge. The Wormhole Network observes when Pyth prices on Pythnet have changed and publishes an off-chain signed message attesting to this fact. This is explained in more detail [here](https://docs.wormholenetwork.com/wormhole/).
106+
107+
This signed message can then be submitted to the Pyth contract on the Aptos networks, which will verify the Wormhole message and update the Pyth contract with the new price.
108+
109+
### On-demand price updates
110+
111+
Price updates are not submitted on the EVM networks automatically: rather, when a consumer needs to use the value of a price they should first submit the latest Wormhole update for that price to the Pyth contract on the EVM network they are working on. This will make the most recent price update available on-chain for EVM contracts to use.
112+
113+
## Price Service endpoints
114+
115+
We provide a public endpoint for the Price Service for Testnet, and will provide one for Mainnet on launch day.
116+
117+
| Aptos Network | Price Service URL |
118+
| ------------- | ------------------------------- |
119+
| Testnet | https://xc-testnet.pyth.network |

pyth-aptos-js/jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2+
module.exports = {
3+
preset: "ts-jest",
4+
testEnvironment: "node",
5+
};

0 commit comments

Comments
 (0)