@@ -91,7 +91,7 @@ public fun get_sui_price(
9191 price_info_object : & PriceInfoObject ,
9292): I64 {
9393 let max_age = 60 ;
94-
94+
9595 // Make sure the price is not older than max_age seconds
9696 let price_struct = pyth :: get_price_no_older_than (price_info_object , clock , max_age );
9797
@@ -104,7 +104,7 @@ public fun get_sui_price(
104104 // Note: Sui uses the Pyth price feed ID without the `0x` prefix.
105105 let testnet_sui_price_id = x " 50c67b3fd225db8912a424dd4baed60ffdde625ed2feaaf283724f9608fea266" ;
106106 assert! (price_id == testnet_sui_price_id , E_INVALID_ID );
107-
107+
108108 // Extract the price, decimal, and timestamp from the price struct and use them.
109109 let decimal_i64 = price :: get_expo (& price_struct );
110110 let price_i64 = price :: get_price (& price_struct );
@@ -117,73 +117,85 @@ One can consume the price by calling `pyth::get_price` abovementioned or other u
117117The code snippet below provides an example of how to update the Pyth price feeds:
118118
119119``` ts copy
120- import { SuiPythClient , SuiPriceServiceConnection } from " @pythnetwork/pyth-sui-js"
121- import { SuiClient } from " @mysten/sui/client"
122- import { Transaction } from " @mysten/sui/transactions"
120+ import {
121+ SuiPythClient ,
122+ SuiPriceServiceConnection ,
123+ } from " @pythnetwork/pyth-sui-js" ;
124+ import { SuiClient } from " @mysten/sui/client" ;
125+ import { Transaction } from " @mysten/sui/transactions" ;
123126import { Ed25519Keypair } from " @mysten/sui/keypairs/ed25519" ;
124127
125128// / Step 1: Get the off-chain data.
126129const connection = new SuiPriceServiceConnection (
127- " https://hermes-beta.pyth.network" , // [!] Only for Sui Testnet
128- // "https://hermes.pyth.network/", // Use this for Mainnet
129- {
130- // Provide this option to retrieve signed price updates for on-chain contracts!
131- priceFeedRequestConfig: {
132- ' binary' : true
133- }
134- }
130+ " https://hermes-beta.pyth.network" , // [!] Only for Sui Testnet
131+ // "https://hermes.pyth.network/", // Use this for Mainnet
132+ {
133+ // Provide this option to retrieve signed price updates for on-chain contracts!
134+ priceFeedRequestConfig: {
135+ binary: true ,
136+ },
137+ }
135138);
136139const priceIDs = [
137140 // You can find the IDs of prices at:
138141 // - https://pyth.network/developers/price-feed-ids for Mainnet
139142 // - https://www.pyth.network/developers/price-feed-ids#beta for Testnet
140143 " 0x50c67b3fd225db8912a424dd4baed60ffdde625ed2feaaf283724f9608fea266" , // SUI/USD price ID
141- ];
142- const priceUpdateData = await connection .getPriceFeedsUpdateData (priceIDs );
144+ ];
145+ const priceUpdateData = await connection .getPriceFeedsUpdateData (priceIDs );
143146
144- // / Step 2: Submit the new price on-chain and verify it using the contract.
145- const suiClient = new SuiClient ({ url: ' https://fullnode.testnet.sui.io:443' });
147+ // / Step 2: Submit the new price on-chain and verify it using the contract.
148+ const suiClient = new SuiClient ({ url: " https://fullnode.testnet.sui.io:443" });
146149
147- // Fixed the StateIds using the CLI example extracting them from
150+ // Fixed the StateIds using the CLI example extracting them from
148151// here: https://docs.pyth.network/price-feeds/contract-addresses/sui
149- const pythTestnetStateId = " 0x243759059f4c3111179da5878c12f68d612c21a8d54d85edc86164bb18be1c7c" ; // Testnet
150- const wormholeTestnetStateId = " 0x31358d198147da50db32eda2562951d53973a0c0ad5ed738e9b17d88b213d790" ; // Testnet
151-
152- const pythClient = new SuiPythClient (suiClient , pythTestnetStateId , wormholeTestnetStateId );
152+ const pythTestnetStateId =
153+ " 0x243759059f4c3111179da5878c12f68d612c21a8d54d85edc86164bb18be1c7c" ; // Testnet
154+ const wormholeTestnetStateId =
155+ " 0x31358d198147da50db32eda2562951d53973a0c0ad5ed738e9b17d88b213d790" ; // Testnet
156+
157+ const pythClient = new SuiPythClient (
158+ suiClient ,
159+ pythTestnetStateId ,
160+ wormholeTestnetStateId
161+ );
153162
154163const transaction = new Transaction ();
155164
156- // / By calling the updatePriceFeeds function, the SuiPythClient adds the necessary
165+ // / By calling the updatePriceFeeds function, the SuiPythClient adds the necessary
157166// / transactions to the transaction block to update the price feeds.
158- const priceInfoObjectIds = await pythClient .updatePriceFeeds (transaction , priceUpdateData , priceIDs );
167+ const priceInfoObjectIds = await pythClient .updatePriceFeeds (
168+ transaction ,
169+ priceUpdateData ,
170+ priceIDs
171+ );
159172
160- let suiPriceObjectId = priceInfoObjectIds [0 ]
173+ let suiPriceObjectId = priceInfoObjectIds [0 ];
161174if (! suiPriceObjectId ) {
162- throw new Error (" suiPriceObjectId is undefined" );
175+ throw new Error (" suiPriceObjectId is undefined" );
163176}
164177
165- // / This is the package id that we receive after publishing `oracle` contract from the previous step.
166- let testnetExampleContractPackageId = " 0x42d05111a160febe4144338647e0b7a80daea459c765c1e29a7a6198b235f67c" ;
167- const CLOCK = " 0x0000000000000000000000000000000000000000000000000000000000000006" ;
178+ // / This is the package id that we receive after publishing `oracle` contract from the previous step.
179+ let testnetExampleContractPackageId =
180+ " 0x42d05111a160febe4144338647e0b7a80daea459c765c1e29a7a6198b235f67c" ;
181+ const CLOCK =
182+ " 0x0000000000000000000000000000000000000000000000000000000000000006" ;
168183transaction .moveCall ({
169- target: ` ${testnetExampleContractPackageId }::oracle::get_sui_price ` ,
170- arguments: [
171- transaction .object (CLOCK ),
172- transaction .object (suiPriceObjectId ),
173- ],
184+ target: ` ${testnetExampleContractPackageId }::oracle::get_sui_price ` ,
185+ arguments: [transaction .object (CLOCK ), transaction .object (suiPriceObjectId )],
174186});
175187transaction .setGasBudget (1000000000 );
176188
177189const keypair = Ed25519Keypair .fromSecretKey (
178- process .env .ADMIN_SECRET_KEY ! .toLowerCase ()
179- );
190+ process .env .ADMIN_SECRET_KEY ! .toLowerCase ()
191+ );
180192const result = await suiClient .signAndExecuteTransaction ({
181- transaction ,
182- signer: keypair ,
183- options: {
184- showEffects: true ,
185- showEvents: true ,
186- },
193+ transaction ,
194+ signer: keypair ,
195+ options: {
196+ showEffects: true ,
197+ showEvents: true ,
198+ },
187199});
188200```
189201
0 commit comments