1
- import { Account , Contract , RpcProvider , shortString } from " starknet" ;
2
- import { PriceServiceConnection } from " @pythnetwork/price-service-client" ;
1
+ import { Account , Contract , RpcProvider , shortString } from ' starknet' ;
2
+ import { PriceServiceConnection } from ' @pythnetwork/price-service-client' ;
3
3
import {
4
4
ByteBuffer ,
5
5
ERC20_ABI ,
6
6
STRK_TOKEN_ADDRESS ,
7
7
PYTH_ABI ,
8
8
PYTH_CONTRACT_ADDRESS_SEPOLIA ,
9
- } from "@pythnetwork/pyth-starknet-js" ;
9
+ PYTH_CONTRACT_ADDRESS_MAINNET ,
10
+ } from '@pythnetwork/pyth-starknet-js' ;
10
11
11
12
async function main ( ) {
13
+ const chain = process . env . CHAIN || 'sepolia' ;
14
+ let nodeUrl ;
15
+ let pythAddress ;
16
+ if ( chain == 'mainnet' ) {
17
+ nodeUrl = 'https://starknet-mainnet.public.blastapi.io/rpc/v0_6' ;
18
+ pythAddress = PYTH_CONTRACT_ADDRESS_MAINNET ;
19
+ } else if ( chain == 'sepolia' ) {
20
+ nodeUrl = 'https://starknet-sepolia.public.blastapi.io/rpc/v0_6' ;
21
+ pythAddress = PYTH_CONTRACT_ADDRESS_SEPOLIA ;
22
+ } else {
23
+ throw new Error ( 'unknown chain' ) ;
24
+ }
12
25
// Create a provider for interacting with Starknet RPC.
13
- const provider = new RpcProvider ( {
14
- nodeUrl : "https://starknet-sepolia.public.blastapi.io/rpc/v0_6" ,
15
- } ) ;
26
+ const provider = new RpcProvider ( { nodeUrl} ) ;
16
27
console . log (
17
- " chain id: " ,
28
+ ' chain id: ' ,
18
29
shortString . decodeShortString ( await provider . getChainId ( ) )
19
30
) ;
20
- console . log ( " rpc version: " , await provider . getSpecVersion ( ) ) ;
31
+ console . log ( ' rpc version: ' , await provider . getSpecVersion ( ) ) ;
21
32
22
33
// Create a `Contract` instance to interact with a fee token contract on Starknet
23
34
// (you can use either STRK or ETH to pay fees, but using STRK is recommended).
@@ -28,34 +39,30 @@ async function main() {
28
39
) ;
29
40
30
41
// Create a `Contract` instance to interact with the Pyth contract on Starknet.
31
- const pythContract = new Contract (
32
- PYTH_ABI ,
33
- PYTH_CONTRACT_ADDRESS_SEPOLIA ,
34
- provider
35
- ) ;
42
+ const pythContract = new Contract ( PYTH_ABI , pythAddress , provider ) ;
36
43
const chain_id = await pythContract . chain_id ( ) ;
37
- console . log ( " pyth chain id:" , chain_id ) ;
44
+ console . log ( ' pyth chain id:' , chain_id ) ;
38
45
39
46
const version = await pythContract . version ( ) ;
40
- console . log ( " pyth version:" , shortString . decodeShortString ( version ) ) ;
47
+ console . log ( ' pyth version:' , shortString . decodeShortString ( version ) ) ;
41
48
42
49
// Import your account data from environment variables.
43
50
// You'll need to set them before running the code.
44
51
const privateKey0 = process . env . ACCOUNT_PRIVATE_KEY ;
45
52
if ( privateKey0 === undefined ) {
46
- throw new Error ( " missing ACCOUNT_PRIVATE_KEY" ) ;
53
+ throw new Error ( ' missing ACCOUNT_PRIVATE_KEY' ) ;
47
54
}
48
55
const account0Address = process . env . ACCOUNT_ADDRESS ;
49
56
if ( account0Address === undefined ) {
50
- throw new Error ( " missing ACCOUNT_ADDRESS" ) ;
57
+ throw new Error ( ' missing ACCOUNT_ADDRESS' ) ;
51
58
}
52
59
const account0 = new Account ( provider , account0Address , privateKey0 ) ;
53
60
54
61
const balanceInitial = await strkErc0Contract . balanceOf ( account0Address ) ;
55
- console . log ( " account0 balance:" , balanceInitial ) ;
62
+ console . log ( ' account0 balance:' , balanceInitial ) ;
56
63
57
64
// Create a client for pulling price updates from Hermes.
58
- const connection = new PriceServiceConnection ( " https://hermes.pyth.network" , {
65
+ const connection = new PriceServiceConnection ( ' https://hermes.pyth.network' , {
59
66
priceFeedRequestConfig : {
60
67
// Provide this option to retrieve signed price updates for on-chain contracts.
61
68
// Ignore this option for off-chain use.
@@ -64,51 +71,51 @@ async function main() {
64
71
} ) ;
65
72
66
73
const priceFeedId =
67
- " 0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace" ; // ETH/USD
74
+ ' 0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace' ; // ETH/USD
68
75
const previousPrice = await pythContract . get_price_unsafe ( priceFeedId ) ;
69
- console . log ( " previous price:" , previousPrice ) ;
76
+ console . log ( ' previous price:' , previousPrice ) ;
70
77
71
- console . log ( " querying pyth update" ) ;
78
+ console . log ( ' querying pyth update' ) ;
72
79
// Get the latest values of the price feeds as json objects.
73
80
const currentPrices = await connection . getLatestPriceFeeds ( [ priceFeedId ] ) ;
74
81
if ( currentPrices === undefined ) {
75
- throw new Error ( " failed to get prices" ) ;
82
+ throw new Error ( ' failed to get prices' ) ;
76
83
}
77
- console . log ( " current price:" , currentPrices [ 0 ] ) ;
84
+ console . log ( ' current price:' , currentPrices [ 0 ] ) ;
78
85
79
86
if ( ! currentPrices [ 0 ] . vaa ) {
80
- throw new Error ( " missing vaa in response" ) ;
87
+ throw new Error ( ' missing vaa in response' ) ;
81
88
}
82
89
83
90
// Convert the price update to Starknet format.
84
91
const pythUpdate = ByteBuffer . fromBase64 ( currentPrices [ 0 ] . vaa ) ;
85
92
86
93
// Query the amount of fee required by Pyth.
87
- console . log ( " querying pyth fee" ) ;
94
+ console . log ( ' querying pyth fee' ) ;
88
95
const fee = await pythContract . get_update_fee (
89
96
pythUpdate ,
90
97
strkErc0Contract . address
91
98
) ;
92
- console . log ( " pyth fee:" , fee ) ;
99
+ console . log ( ' pyth fee:' , fee ) ;
93
100
94
101
// Approve fee withdrawal.
95
- console . log ( " approving fee" ) ;
102
+ console . log ( ' approving fee' ) ;
96
103
strkErc0Contract . connect ( account0 ) ;
97
104
let tx = await strkErc0Contract . approve ( pythContract . address , fee ) ;
98
- console . log ( " waiting for tx" ) ;
105
+ console . log ( ' waiting for tx' ) ;
99
106
await provider . waitForTransaction ( tx . transaction_hash ) ;
100
107
101
108
pythContract . connect ( account0 ) ;
102
109
103
110
// Create a transaction and submit to your contract using the price update data.
104
- console . log ( " updating price feeds" ) ;
111
+ console . log ( ' updating price feeds' ) ;
105
112
tx = await pythContract . update_price_feeds ( pythUpdate ) ;
106
- console . log ( " waiting for tx" ) ;
113
+ console . log ( ' waiting for tx' ) ;
107
114
await provider . waitForTransaction ( tx . transaction_hash ) ;
108
- console . log ( " transaction confirmed:" , tx . transaction_hash ) ;
115
+ console . log ( ' transaction confirmed:' , tx . transaction_hash ) ;
109
116
110
- const newPrice = await pythContract . get_price_no_older_than ( priceFeedId , 60 ) ;
111
- console . log ( " new price:" , newPrice ) ;
117
+ const newPrice = await pythContract . get_price_no_older_than ( priceFeedId , 120 ) ;
118
+ console . log ( ' new price:' , newPrice ) ;
112
119
}
113
120
114
121
main ( ) ;
0 commit comments