@@ -11,6 +11,7 @@ are as follows:
11
11
12
12
1 . [ ` update_price_feeds ` ] ( #update_price_feeds )
13
13
_ (updates Pyth smart contract with the price feed you provide)_
14
+
14
15
- args: ` data `
15
16
- type: ` object `
16
17
- example: ` { "data": "504e41...' } `
@@ -22,7 +23,7 @@ are as follows:
22
23
23
24
These functions are core for interacting with Pyth price feeds in
24
25
NEAR-based applications, providing a reliable and up-to-date source of
25
- price information.
26
+ price information.
26
27
27
28
For a full overview of methods provided by the NEAR
28
29
contract, see [ the interface] ( https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/near/receiver/src/ext.rs )] exposed by the receiver contract.
@@ -68,33 +69,39 @@ Here is a node.js example of fetching the latest price feed using `/v2/updates/p
68
69
` Example: `
69
70
70
71
``` js
71
- const axios = require (' axios' );
72
+ const axios = require (" axios" );
72
73
73
74
// There are separate endpoints for testnet and mainnet
74
- const HERMES_TESTNET_URL = ' https://hermes-beta.pyth.network' ;
75
- const HERMES_MAINNET_URL = ' https://hermes.pyth.network' ;
75
+ const HERMES_TESTNET_URL = " https://hermes-beta.pyth.network" ;
76
+ const HERMES_MAINNET_URL = " https://hermes.pyth.network" ;
76
77
77
78
async function getHermesPriceData (priceId , network ) {
78
79
try {
79
80
let url;
80
- network === ' testnet'
81
+ network === " testnet"
81
82
? (url = HERMES_TESTNET_URL )
82
83
: (url = HERMES_MAINNET_URL );
83
84
84
85
// Fetch the price data from the Hermes API
85
- const response = await axios .get (` ${ url} /v2/updates/price/latest?ids[]=${ priceId} ` );
86
+ const response = await axios .get (
87
+ ` ${ url} /v2/updates/price/latest?ids[]=${ priceId} `
88
+ );
86
89
87
90
return response .data .binary .data [0 ];
88
91
} catch (error) {
89
- console .error (' Error:' , error .response ? error .response .data : error .message );
92
+ console .error (
93
+ " Error:" ,
94
+ error .response ? error .response .data : error .message
95
+ );
90
96
}
91
97
}
92
98
93
99
module .exports = { getHermesPriceData };
94
100
```
95
101
96
- <center >z
97
- [ See full example on GitHub] ( https://github.com/near-examples/near-js/blob/main/node-js/utils/fetch-hermes-price-data.js )
102
+ <center >
103
+ z [ See full example on
104
+ GitHub] ( https://github.com/near-examples/near-js/blob/main/node-js/utils/fetch-hermes-price-data.js )
98
105
</center >
99
106
100
107
---
@@ -122,7 +129,7 @@ npm install -g near-cli-rs@latest
122
129
123
130
```
124
131
125
- This CLI allows you to simply run ` near ` and let the prompts guide you through the process.
132
+ This CLI allows you to simply run ` near ` and let the prompts guide you through the process.
126
133
127
134
To quickly create a NEAR account, run the following command (replacing ` your-new-account.testnet ` with your desired account name):
128
135
@@ -135,10 +142,11 @@ network-config testnet \
135
142
create
136
143
```
137
144
138
- To perform a contract call to the Pyth Oracle contract, run the following command:
145
+ To perform a contract call to the Pyth Oracle contract, run the following command:
146
+
147
+ Replace:
139
148
140
- Replace:
141
- - ` your-account.testnet ` with your account name
149
+ - ` your-account.testnet ` with your account name
142
150
- ` '{"data": "504e41550100..."}' ` with your off-chain price feed
143
151
144
152
```
@@ -166,43 +174,46 @@ To setup a NEAR connection, we'll create a `connect.js` file that will initializ
166
174
167
175
``` js
168
176
// node.js imports
169
- const { join } = require (' node:path' );
170
- const { homedir } = require (' node:os' );
177
+ const { join } = require (" node:path" );
178
+ const { homedir } = require (" node:os" );
171
179
172
180
// near-js imports
173
- const { getTestnetRpcProvider , getSignerFromKeystore } = require (' @near-js/client' );
174
- const { UnencryptedFileSystemKeyStore } = require (' @near-js/keystores-node' );
181
+ const {
182
+ getTestnetRpcProvider ,
183
+ getSignerFromKeystore ,
184
+ } = require (" @near-js/client" );
185
+ const { UnencryptedFileSystemKeyStore } = require (" @near-js/keystores-node" );
175
186
176
187
// initialize RPC provider and signer
177
188
const nearConnect = (sender , network ) => ({
178
189
rpcProvider: getTestnetRpcProvider (),
179
190
signer: getSignerFromKeystore (
180
- sender,
191
+ sender,
181
192
network,
182
- new UnencryptedFileSystemKeyStore (join (homedir (), ' .near-credentials' ))
183
- )
193
+ new UnencryptedFileSystemKeyStore (join (homedir (), " .near-credentials" ))
194
+ ),
184
195
});
185
196
186
197
module .exports = { nearConnect };
187
198
```
188
199
189
200
Next we can create a ` update-oracle.js ` file that will perform the contract call to update the Pyth Oracle contract's price feed.
190
- ``` js
191
201
202
+ ``` js
192
203
// near-js imports
193
204
// https://www.npmjs.com/package/@near-js/client
194
- const { nearConnect } = require (' ../utils/connect' );
195
- const { functionCall } = require (' @near-js/client' );
205
+ const { nearConnect } = require (" ../utils/connect" );
206
+ const { functionCall } = require (" @near-js/client" );
196
207
197
- const sender = ' your-account.testnet' ;
198
- const receiver = ' pyth-oracle.testnet' ;
199
- const network = ' testnet' ;
208
+ const sender = " your-account.testnet" ;
209
+ const receiver = " pyth-oracle.testnet" ;
210
+ const network = " testnet" ;
200
211
201
212
const PRICE_IDS = [
202
213
// Price ids can be found at https://www.pyth.network/developers/price-feed-ids#near-testnet
203
214
// NOTE: Ensure you are using NEAR specific price ids & remove the '0x' prefix before using them
204
- ' f9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b' , // BTC/USD price id
205
- ' ca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6' , // ETH/USD price id
215
+ " f9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b" , // BTC/USD price id
216
+ " ca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6" , // ETH/USD price id
206
217
];
207
218
208
219
async function updatePythContractPriceFeeds (network ) {
@@ -215,9 +226,9 @@ async function updatePythContractPriceFeeds(network) {
215
226
const result = await functionCall ({
216
227
sender,
217
228
receiver,
218
- method: ' update_price_feeds' ,
219
- args: { data: ' 504e4155010...' },
220
- deposit: 10000000000000000000000 ,
229
+ method: " update_price_feeds" ,
230
+ args: { data: " 504e4155010..." },
231
+ deposit: 10000000000000000000000 ,
221
232
deps: { rpcProvider, signer },
222
233
});
223
234
@@ -228,11 +239,11 @@ async function updatePythContractPriceFeeds(network) {
228
239
}
229
240
230
241
updatePythOracle ();
231
-
232
242
```
233
243
234
244
<center >
235
- [ See full example on GitHub] ( https://github.com/near-examples/near-js/blob/main/node-js/oracle-example/pyth-oracle-update.js )
245
+ [ See full example on
246
+ GitHub] ( https://github.com/near-examples/near-js/blob/main/node-js/oracle-example/pyth-oracle-update.js )
236
247
</center >
237
248
238
249
Although unused deposit will be refunded, you can calculate an estimate by calling the ` get_update_fee_estimate ` method against the Pyth contract.
@@ -274,21 +285,21 @@ Create a `get-price.js` file that will perform the view call from the Pyth Oracl
274
285
``` js
275
286
// near-js import
276
287
// https://www.npmjs.com/package/@near-js/client
277
- const { getTestnetRpcProvider , view } = require (' @near-js/client' );
288
+ const { getTestnetRpcProvider , view } = require (" @near-js/client" );
278
289
279
290
const PRICE_IDS = [
280
291
// Price ids can be found at https://www.pyth.network/developers/price-feed-ids#near-testnet
281
292
// NOTE: Ensure you are using NEAR specific price ids & remove the '0x' prefix before using them
282
- ' f9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b' , // BTC/USD price id
283
- ' ca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6' , // ETH/USD price id
293
+ " f9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b" , // BTC/USD price id
294
+ " ca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6" , // ETH/USD price id
284
295
];
285
296
286
297
async function getPrice (price_ID , symbol ) {
287
298
try {
288
299
const rpcProvider = getTestnetRpcProvider ();
289
300
const result = await view ({
290
- account: ' pyth-oracle.testnet' ,
291
- method: ' get_price' ,
301
+ account: " pyth-oracle.testnet" ,
302
+ method: " get_price" ,
292
303
args: { price_identifier: price_ID },
293
304
deps: { rpcProvider },
294
305
});
@@ -298,15 +309,16 @@ async function getPrice(price_ID, symbol) {
298
309
}
299
310
}
300
311
301
- getPrice (PRICE_IDS [0 ], ' BTC/USD:' );
302
-
312
+ getPrice (PRICE_IDS [0 ], " BTC/USD:" );
303
313
```
304
314
305
315
<center >
306
- [ See full example on GitHub] ( https://github.com/near-examples/near-js/blob/main/node-js/oracle-example/pyth-oracle-view.js )
316
+ [ See full example on
317
+ GitHub] ( https://github.com/near-examples/near-js/blob/main/node-js/oracle-example/pyth-oracle-view.js )
307
318
</center >
308
319
309
320
---
321
+
310
322
## On-Chain Prices
311
323
312
324
For on-chain price interactions, see the [ example contract] [ ] in the
0 commit comments