@@ -7,20 +7,22 @@ import {
7
7
} from "../interface" ;
8
8
import { DurationInSeconds } from "../utils" ;
9
9
import {
10
+ Msgs ,
11
+ Account ,
12
+ TxResponse ,
13
+ PrivateKey ,
14
+ TxGrpcApi ,
10
15
ChainGrpcAuthApi ,
11
16
ChainGrpcWasmApi ,
12
17
MsgExecuteContract ,
13
- Msgs ,
14
- PrivateKey ,
15
- TxGrpcClient ,
16
- TxResponse ,
17
18
createTransactionFromMsg ,
18
19
} from "@injectivelabs/sdk-ts" ;
20
+ import { splitArrayToChunks } from "@injectivelabs/utils" ;
19
21
import { Logger } from "pino" ;
20
- import { Account } from "@injectivelabs/sdk-ts/dist/cjs/client/chain/types/auth" ;
21
22
22
23
const DEFAULT_GAS_PRICE = 160000000 ;
23
24
const DEFAULT_GAS_MULTIPLIER = 1.05 ;
25
+ const DEFAULT_PRICE_IDS_PROCESS_CHUNK_SIZE = - 1 ;
24
26
const INJECTIVE_TESTNET_CHAIN_ID = "injective-888" ;
25
27
26
28
type PriceQueryResponse = {
@@ -90,6 +92,7 @@ type InjectiveConfig = {
90
92
chainId : string ;
91
93
gasMultiplier : number ;
92
94
gasPrice : number ;
95
+ priceIdsProcessChunkSize : number ;
93
96
} ;
94
97
export class InjectivePricePusher implements IPricePusher {
95
98
private wallet : PrivateKey ;
@@ -110,6 +113,9 @@ export class InjectivePricePusher implements IPricePusher {
110
113
chainId : chainConfig ?. chainId ?? INJECTIVE_TESTNET_CHAIN_ID ,
111
114
gasMultiplier : chainConfig ?. gasMultiplier ?? DEFAULT_GAS_MULTIPLIER ,
112
115
gasPrice : chainConfig ?. gasPrice ?? DEFAULT_GAS_PRICE ,
116
+ priceIdsProcessChunkSize :
117
+ chainConfig ?. priceIdsProcessChunkSize ??
118
+ DEFAULT_PRICE_IDS_PROCESS_CHUNK_SIZE ,
113
119
} ;
114
120
}
115
121
@@ -119,6 +125,7 @@ export class InjectivePricePusher implements IPricePusher {
119
125
120
126
private async signAndBroadcastMsg ( msg : Msgs ) : Promise < TxResponse > {
121
127
const chainGrpcAuthApi = new ChainGrpcAuthApi ( this . grpcEndpoint ) ;
128
+
122
129
// Fetch the latest account details only if it's not stored.
123
130
this . account ??= await chainGrpcAuthApi . fetchAccount (
124
131
this . injectiveAddress ( ) ,
@@ -132,7 +139,7 @@ export class InjectivePricePusher implements IPricePusher {
132
139
pubKey : this . wallet . toPublicKey ( ) . toBase64 ( ) ,
133
140
} ) ;
134
141
135
- const txService = new TxGrpcClient ( this . grpcEndpoint ) ;
142
+ const txService = new TxGrpcApi ( this . grpcEndpoint ) ;
136
143
// simulation
137
144
try {
138
145
const {
@@ -207,12 +214,33 @@ export class InjectivePricePusher implements IPricePusher {
207
214
if ( priceIds . length !== pubTimesToPush . length )
208
215
throw new Error ( "Invalid arguments" ) ;
209
216
217
+ const priceIdChunks =
218
+ this . chainConfig . priceIdsProcessChunkSize === - 1
219
+ ? [ priceIds ]
220
+ : splitArrayToChunks ( {
221
+ array : priceIds ,
222
+ chunkSize : this . chainConfig . priceIdsProcessChunkSize ,
223
+ } ) ;
224
+
225
+ for ( const [ chunkIndex , priceIdChunk ] of priceIdChunks . entries ( ) ) {
226
+ await this . updatePriceFeedChunk ( priceIdChunk , chunkIndex ) ;
227
+ }
228
+ }
229
+
230
+ private async updatePriceFeedChunk (
231
+ priceIds : string [ ] ,
232
+ chunkIndex : number ,
233
+ ) : Promise < void > {
210
234
let priceFeedUpdateObject ;
235
+
211
236
try {
212
237
// get the latest VAAs for updatePriceFeed and then push them
213
238
priceFeedUpdateObject = await this . getPriceFeedUpdateObject ( priceIds ) ;
214
239
} catch ( err ) {
215
- this . logger . error ( err , "Error fetching the latest vaas to push" ) ;
240
+ this . logger . error (
241
+ err ,
242
+ `Error fetching the latest vaas to push for chunk ${ chunkIndex } ` ,
243
+ ) ;
216
244
return ;
217
245
}
218
246
@@ -233,7 +261,10 @@ export class InjectivePricePusher implements IPricePusher {
233
261
const json = Buffer . from ( data ) . toString ( ) ;
234
262
updateFeeQueryResponse = JSON . parse ( json ) ;
235
263
} catch ( err ) {
236
- this . logger . error ( err , "Error fetching update fee" ) ;
264
+ this . logger . error (
265
+ err ,
266
+ `Error fetching update fee for chunk ${ chunkIndex } ` ,
267
+ ) ;
237
268
// Throwing an error because it is likely an RPC issue
238
269
throw err ;
239
270
}
@@ -247,21 +278,27 @@ export class InjectivePricePusher implements IPricePusher {
247
278
} ) ;
248
279
249
280
const rs = await this . signAndBroadcastMsg ( executeMsg ) ;
250
- this . logger . info ( { hash : rs . txHash } , "Succesfully broadcasted txHash" ) ;
281
+ this . logger . info (
282
+ { hash : rs . txHash } ,
283
+ `Successfully broadcasted txHash for chunk ${ chunkIndex } ` ,
284
+ ) ;
251
285
} catch ( err : any ) {
252
286
if ( err . message . match ( / a c c o u n t i n j [ a - z A - Z 0 - 9 ] + n o t f o u n d / ) !== null ) {
253
- this . logger . error ( err , " Account not found" ) ;
287
+ this . logger . error ( err , ` Account not found for chunk ${ chunkIndex } ` ) ;
254
288
throw new Error ( "Please check the mnemonic" ) ;
255
289
}
256
290
257
291
if (
258
292
err . message . match ( / i n s u f f i c i e n t / ) !== null &&
259
293
err . message . match ( / f u n d s / ) !== null
260
294
) {
261
- this . logger . error ( err , " Insufficient funds" ) ;
295
+ this . logger . error ( err , ` Insufficient funds for chunk ${ chunkIndex } ` ) ;
262
296
throw new Error ( "Insufficient funds" ) ;
263
297
}
264
- this . logger . error ( err , "Error executing messages" ) ;
298
+ this . logger . error (
299
+ err ,
300
+ `Error executing messages for chunk ${ chunkIndex } ` ,
301
+ ) ;
265
302
}
266
303
}
267
304
}
0 commit comments