@@ -91,7 +91,7 @@ public fun get_sui_price(
91
91
price_info_object : & PriceInfoObject ,
92
92
): I64 {
93
93
let max_age = 60 ;
94
-
94
+
95
95
// Make sure the price is not older than max_age seconds
96
96
let price_struct = pyth :: get_price_no_older_than (price_info_object , clock , max_age );
97
97
@@ -104,7 +104,7 @@ public fun get_sui_price(
104
104
// Note: Sui uses the Pyth price feed ID without the `0x` prefix.
105
105
let testnet_sui_price_id = x " 50c67b3fd225db8912a424dd4baed60ffdde625ed2feaaf283724f9608fea266" ;
106
106
assert! (price_id == testnet_sui_price_id , E_INVALID_ID );
107
-
107
+
108
108
// Extract the price, decimal, and timestamp from the price struct and use them.
109
109
let decimal_i64 = price :: get_expo (& price_struct );
110
110
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
117
117
The code snippet below provides an example of how to update the Pyth price feeds:
118
118
119
119
``` 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" ;
123
126
import { Ed25519Keypair } from " @mysten/sui/keypairs/ed25519" ;
124
127
125
128
// / Step 1: Get the off-chain data.
126
129
const 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
+ }
135
138
);
136
139
const priceIDs = [
137
140
// You can find the IDs of prices at:
138
141
// - https://pyth.network/developers/price-feed-ids for Mainnet
139
142
// - https://www.pyth.network/developers/price-feed-ids#beta for Testnet
140
143
" 0x50c67b3fd225db8912a424dd4baed60ffdde625ed2feaaf283724f9608fea266" , // SUI/USD price ID
141
- ];
142
- const priceUpdateData = await connection .getPriceFeedsUpdateData (priceIDs );
144
+ ];
145
+ const priceUpdateData = await connection .getPriceFeedsUpdateData (priceIDs );
143
146
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" });
146
149
147
- // Fixed the StateIds using the CLI example extracting them from
150
+ // Fixed the StateIds using the CLI example extracting them from
148
151
// 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
+ );
153
162
154
163
const transaction = new Transaction ();
155
164
156
- // / By calling the updatePriceFeeds function, the SuiPythClient adds the necessary
165
+ // / By calling the updatePriceFeeds function, the SuiPythClient adds the necessary
157
166
// / 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
+ );
159
172
160
- let suiPriceObjectId = priceInfoObjectIds [0 ]
173
+ let suiPriceObjectId = priceInfoObjectIds [0 ];
161
174
if (! suiPriceObjectId ) {
162
- throw new Error (" suiPriceObjectId is undefined" );
175
+ throw new Error (" suiPriceObjectId is undefined" );
163
176
}
164
177
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" ;
168
183
transaction .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 )],
174
186
});
175
187
transaction .setGasBudget (1000000000 );
176
188
177
189
const keypair = Ed25519Keypair .fromSecretKey (
178
- process .env .ADMIN_SECRET_KEY ! .toLowerCase ()
179
- );
190
+ process .env .ADMIN_SECRET_KEY ! .toLowerCase ()
191
+ );
180
192
const 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
+ },
187
199
});
188
200
```
189
201
0 commit comments