@@ -6,6 +6,10 @@ import { HexString } from "@pythnetwork/price-service-client";
66import { Buffer } from "buffer" ;
77
88const MAX_ARGUMENT_SIZE = 16 * 1024 ;
9+ type Coin = {
10+ $kind : "NestedResult" ;
11+ NestedResult : [ number , number ] ;
12+ } ;
913export type ObjectId = string ;
1014
1115export class SuiPythClient {
@@ -104,19 +108,7 @@ 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 (
114- tx : Transaction ,
115- updates : Buffer [ ] ,
116- feedIds : HexString [ ] ,
117- ) : Promise < ObjectId [ ] > {
118- const packageId = await this . getPythPackageId ( ) ;
119-
111+ async verifyVaasAndGetHotPotato ( tx : Transaction , updates : Buffer [ ] , packageId : string ) : Promise < any > {
120112 let priceUpdatesHotPotato ;
121113 if ( updates . length > 1 ) {
122114 throw new Error (
@@ -141,13 +133,17 @@ export class SuiPythClient {
141133 tx . object ( SUI_CLOCK_OBJECT_ID ) ,
142134 ] ,
143135 } ) ;
136+ return priceUpdatesHotPotato ;
137+ }
144138
139+ async executePriceFeedUpdates (
140+ tx : Transaction ,
141+ packageId : string ,
142+ feedIds : HexString [ ] ,
143+ priceUpdatesHotPotato : any ,
144+ coins : Coin [ ]
145+ ) {
145146 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- ) ;
151147 let coinId = 0 ;
152148 for ( const feedId of feedIds ) {
153149 const priceInfoObjectId = await this . getPriceFeedObjectId ( feedId ) ;
@@ -176,6 +172,49 @@ export class SuiPythClient {
176172 } ) ;
177173 return priceInfoObjects ;
178174 }
175+
176+ /**
177+ * Adds the necessary commands for updating the pyth price feeds to the transaction block.
178+ * @param tx transaction block to add commands to
179+ * @param updates array of price feed updates received from the price service
180+ * @param feedIds array of feed ids to update (in hex format)
181+ */
182+ async updatePriceFeeds (
183+ tx : Transaction ,
184+ updates : Buffer [ ] ,
185+ feedIds : HexString [ ] ,
186+ ) : Promise < ObjectId [ ] > {
187+ const packageId = await this . getPythPackageId ( ) ;
188+ let priceUpdatesHotPotato = await this . verifyVaasAndGetHotPotato ( tx , updates , packageId ) ;
189+
190+ const baseUpdateFee = await this . getBaseUpdateFee ( ) ;
191+ const coins = tx . splitCoins (
192+ tx . gas ,
193+ feedIds . map ( ( ) => tx . pure . u64 ( baseUpdateFee ) ) ,
194+ ) ;
195+
196+ return await this . executePriceFeedUpdates ( tx , packageId , feedIds , priceUpdatesHotPotato , coins ) ;
197+ }
198+
199+ /**
200+ * Updates price feeds using the coin input for payment. Coins can be generated by calling splitCoin on tx.gas.
201+ * @param tx transaction block to add commands to
202+ * @param updates array of price feed updates received from the price service
203+ * @param feedIds array of feed ids to update (in hex format)
204+ * @param coins array of Coins for payment of update operations
205+ */
206+ async updatePriceFeedsWithCoins (
207+ tx : Transaction ,
208+ updates : Buffer [ ] ,
209+ feedIds : HexString [ ] ,
210+ coins : Coin [ ] ,
211+ ) : Promise < ObjectId [ ] > {
212+ const packageId = await this . getPythPackageId ( ) ;
213+ let priceUpdatesHotPotato = await this . verifyVaasAndGetHotPotato ( tx , updates , packageId ) ;
214+
215+ return await this . executePriceFeedUpdates ( tx , packageId , feedIds , priceUpdatesHotPotato , coins ) ;
216+ }
217+
179218 async createPriceFeed ( tx : Transaction , updates : Buffer [ ] ) {
180219 const packageId = await this . getPythPackageId ( ) ;
181220 if ( updates . length > 1 ) {
0 commit comments