@@ -6,6 +6,10 @@ import { HexString } from "@pythnetwork/price-service-client";
66import { Buffer } from "buffer" ;
77
88const MAX_ARGUMENT_SIZE = 16 * 1024 ;
9+ type NestedTransactionResult = {
10+ $kind : "NestedResult" ;
11+ NestedResult : [ number , number ] ;
12+ } ;
913export type ObjectId = string ;
1014
1115export class SuiPythClient {
@@ -104,28 +108,19 @@ export class SuiPythClient {
104108 return verifiedVaas ;
105109 }
106110
107- /**
108- * Adds the necessary commands for updating the pyth price feeds to the transaction block.
109- * @param tx transaction block to add commands to
110- * @param updates array of price feed updates received from the price service
111- * @param feedIds array of feed ids to update (in hex format)
112- */
113- async updatePriceFeeds (
111+ async verifyVaasAndGetHotPotato (
114112 tx : Transaction ,
115113 updates : Buffer [ ] ,
116- feedIds : HexString [ ] ,
117- ) : Promise < ObjectId [ ] > {
118- const packageId = await this . getPythPackageId ( ) ;
119-
120- let priceUpdatesHotPotato ;
114+ packageId : string ,
115+ ) : Promise < NestedTransactionResult > {
121116 if ( updates . length > 1 ) {
122117 throw new Error (
123118 "SDK does not support sending multiple accumulator messages in a single transaction" ,
124119 ) ;
125120 }
126121 const vaa = this . extractVaaBytesFromAccumulatorMessage ( updates [ 0 ] ) ;
127122 const verifiedVaas = await this . verifyVaas ( [ vaa ] , tx ) ;
128- [ priceUpdatesHotPotato ] = tx . moveCall ( {
123+ const [ priceUpdatesHotPotato ] = tx . moveCall ( {
129124 target : `${ packageId } ::pyth::create_authenticated_price_infos_using_accumulator` ,
130125 arguments : [
131126 tx . object ( this . pythStateId ) ,
@@ -141,13 +136,17 @@ export class SuiPythClient {
141136 tx . object ( SUI_CLOCK_OBJECT_ID ) ,
142137 ] ,
143138 } ) ;
139+ return priceUpdatesHotPotato ;
140+ }
144141
142+ async executePriceFeedUpdates (
143+ tx : Transaction ,
144+ packageId : string ,
145+ feedIds : HexString [ ] ,
146+ priceUpdatesHotPotato : any ,
147+ coins : NestedTransactionResult [ ] ,
148+ ) {
145149 const priceInfoObjects : ObjectId [ ] = [ ] ;
146- const baseUpdateFee = await this . getBaseUpdateFee ( ) ;
147- const coins = tx . splitCoins (
148- tx . gas ,
149- feedIds . map ( ( ) => tx . pure . u64 ( baseUpdateFee ) ) ,
150- ) ;
151150 let coinId = 0 ;
152151 for ( const feedId of feedIds ) {
153152 const priceInfoObjectId = await this . getPriceFeedObjectId ( feedId ) ;
@@ -176,6 +175,69 @@ export class SuiPythClient {
176175 } ) ;
177176 return priceInfoObjects ;
178177 }
178+
179+ /**
180+ * Adds the necessary commands for updating the pyth price feeds to the transaction block.
181+ * @param tx transaction block to add commands to
182+ * @param updates array of price feed updates received from the price service
183+ * @param feedIds array of feed ids to update (in hex format)
184+ */
185+ async updatePriceFeeds (
186+ tx : Transaction ,
187+ updates : Buffer [ ] ,
188+ feedIds : HexString [ ] ,
189+ ) : Promise < ObjectId [ ] > {
190+ const packageId = await this . getPythPackageId ( ) ;
191+ const priceUpdatesHotPotato = await this . verifyVaasAndGetHotPotato (
192+ tx ,
193+ updates ,
194+ packageId ,
195+ ) ;
196+
197+ const baseUpdateFee = await this . getBaseUpdateFee ( ) ;
198+ const coins = tx . splitCoins (
199+ tx . gas ,
200+ feedIds . map ( ( ) => tx . pure . u64 ( baseUpdateFee ) ) ,
201+ ) ;
202+
203+ return await this . executePriceFeedUpdates (
204+ tx ,
205+ packageId ,
206+ feedIds ,
207+ priceUpdatesHotPotato ,
208+ coins ,
209+ ) ;
210+ }
211+
212+ /**
213+ * Updates price feeds using the coin input for payment. Coins can be generated by calling splitCoin on tx.gas.
214+ * @param tx transaction block to add commands to
215+ * @param updates array of price feed updates received from the price service
216+ * @param feedIds array of feed ids to update (in hex format)
217+ * @param coins array of Coins for payment of update operations
218+ */
219+ async updatePriceFeedsWithCoins (
220+ tx : Transaction ,
221+ updates : Buffer [ ] ,
222+ feedIds : HexString [ ] ,
223+ coins : NestedTransactionResult [ ] ,
224+ ) : Promise < ObjectId [ ] > {
225+ const packageId = await this . getPythPackageId ( ) ;
226+ const priceUpdatesHotPotato = await this . verifyVaasAndGetHotPotato (
227+ tx ,
228+ updates ,
229+ packageId ,
230+ ) ;
231+
232+ return await this . executePriceFeedUpdates (
233+ tx ,
234+ packageId ,
235+ feedIds ,
236+ priceUpdatesHotPotato ,
237+ coins ,
238+ ) ;
239+ }
240+
179241 async createPriceFeed ( tx : Transaction , updates : Buffer [ ] ) {
180242 const packageId = await this . getPythPackageId ( ) ;
181243 if ( updates . length > 1 ) {
0 commit comments