@@ -80,17 +80,41 @@ async function run() {
80
80
. call ( ) ;
81
81
console . log ( `Update fee: ${ updateFee } ` ) ;
82
82
83
- pythContract . methods
83
+ let txHash = undefined ;
84
+ await pythContract . methods
84
85
. updatePriceFeeds ( priceFeedUpdateData )
85
86
. send ( { value : updateFee } )
86
87
. on ( "transactionHash" , ( hash : string ) => {
87
- console . log ( `Tx hash: ${ hash } ` ) ;
88
+ txHash = hash ;
88
89
} )
89
90
. on ( "error" , ( err : any , receipt : any ) => {
90
91
console . error ( receipt ) ;
91
92
throw err ;
92
93
} ) ;
93
94
95
+ console . log ( `Tx hash: ${ txHash } ` ) ;
96
+ if ( txHash === undefined ) {
97
+ console . error ( "Something went wrong. Could not send price update tx." ) ;
98
+ } else {
99
+ console . log ( "Awaiting tx confirmation..." ) ;
100
+ let receipt = undefined ;
101
+ while ( ! receipt ) {
102
+ receipt = await web3 . eth . getTransactionReceipt ( txHash ) ;
103
+ }
104
+
105
+ // For on-chain use, you will typically perform the getPrice call within the same transaction as updatePriceFeeds.
106
+ // The call to getPrice below simply demonstrates that the on-chain price was in fact updated.
107
+ // Note that the code above for waiting for tx confirmation is a little flaky -- if so, you may see an old price printed here.
108
+ for ( const priceId of priceIds ) {
109
+ const [ price , conf , expo , publishTime ] = await pythContract . methods
110
+ . getPrice ( priceId )
111
+ . call ( ) ;
112
+ console . log (
113
+ `Updated ${ priceId } to (${ price } +- ${ conf } ) * 10^${ expo } at unix timestamp ${ publishTime } `
114
+ ) ;
115
+ }
116
+ }
117
+
94
118
provider . engine . stop ( ) ;
95
119
}
96
120
0 commit comments