@@ -6,6 +6,10 @@ import { HexString } from "@pythnetwork/price-service-client";
6
6
import { Buffer } from "buffer" ;
7
7
8
8
const MAX_ARGUMENT_SIZE = 16 * 1024 ;
9
+ type NestedTransactionResult = {
10
+ $kind : "NestedResult" ;
11
+ NestedResult : [ number , number ] ;
12
+ } ;
9
13
export type ObjectId = string ;
10
14
11
15
export class SuiPythClient {
@@ -104,28 +108,19 @@ export class SuiPythClient {
104
108
return verifiedVaas ;
105
109
}
106
110
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 (
114
112
tx : Transaction ,
115
113
updates : Buffer [ ] ,
116
- feedIds : HexString [ ] ,
117
- ) : Promise < ObjectId [ ] > {
118
- const packageId = await this . getPythPackageId ( ) ;
119
-
120
- let priceUpdatesHotPotato ;
114
+ packageId : string ,
115
+ ) : Promise < NestedTransactionResult > {
121
116
if ( updates . length > 1 ) {
122
117
throw new Error (
123
118
"SDK does not support sending multiple accumulator messages in a single transaction" ,
124
119
) ;
125
120
}
126
121
const vaa = this . extractVaaBytesFromAccumulatorMessage ( updates [ 0 ] ) ;
127
122
const verifiedVaas = await this . verifyVaas ( [ vaa ] , tx ) ;
128
- [ priceUpdatesHotPotato ] = tx . moveCall ( {
123
+ const [ priceUpdatesHotPotato ] = tx . moveCall ( {
129
124
target : `${ packageId } ::pyth::create_authenticated_price_infos_using_accumulator` ,
130
125
arguments : [
131
126
tx . object ( this . pythStateId ) ,
@@ -141,13 +136,17 @@ export class SuiPythClient {
141
136
tx . object ( SUI_CLOCK_OBJECT_ID ) ,
142
137
] ,
143
138
} ) ;
139
+ return priceUpdatesHotPotato ;
140
+ }
144
141
142
+ async executePriceFeedUpdates (
143
+ tx : Transaction ,
144
+ packageId : string ,
145
+ feedIds : HexString [ ] ,
146
+ priceUpdatesHotPotato : any ,
147
+ coins : NestedTransactionResult [ ] ,
148
+ ) {
145
149
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
- ) ;
151
150
let coinId = 0 ;
152
151
for ( const feedId of feedIds ) {
153
152
const priceInfoObjectId = await this . getPriceFeedObjectId ( feedId ) ;
@@ -176,6 +175,69 @@ export class SuiPythClient {
176
175
} ) ;
177
176
return priceInfoObjects ;
178
177
}
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
+
179
241
async createPriceFeed ( tx : Transaction , updates : Buffer [ ] ) {
180
242
const packageId = await this . getPythPackageId ( ) ;
181
243
if ( updates . length > 1 ) {
0 commit comments