@@ -7,20 +7,22 @@ import {
77} from "../interface" ;
88import { DurationInSeconds } from "../utils" ;
99import {
10+ Msgs ,
11+ Account ,
12+ TxResponse ,
13+ PrivateKey ,
14+ TxGrpcApi ,
1015 ChainGrpcAuthApi ,
1116 ChainGrpcWasmApi ,
1217 MsgExecuteContract ,
13- Msgs ,
14- PrivateKey ,
15- TxGrpcClient ,
16- TxResponse ,
1718 createTransactionFromMsg ,
1819} from "@injectivelabs/sdk-ts" ;
20+ import { splitArrayToChunks } from "@injectivelabs/utils" ;
1921import { Logger } from "pino" ;
20- import { Account } from "@injectivelabs/sdk-ts/dist/cjs/client/chain/types/auth" ;
2122
2223const DEFAULT_GAS_PRICE = 160000000 ;
2324const DEFAULT_GAS_MULTIPLIER = 1.05 ;
25+ const DEFAULT_PRICE_IDS_PROCESS_CHUNK_SIZE = - 1 ;
2426const INJECTIVE_TESTNET_CHAIN_ID = "injective-888" ;
2527
2628type PriceQueryResponse = {
@@ -90,6 +92,7 @@ type InjectiveConfig = {
9092 chainId : string ;
9193 gasMultiplier : number ;
9294 gasPrice : number ;
95+ priceIdsProcessChunkSize : number ;
9396} ;
9497export class InjectivePricePusher implements IPricePusher {
9598 private wallet : PrivateKey ;
@@ -110,6 +113,9 @@ export class InjectivePricePusher implements IPricePusher {
110113 chainId : chainConfig ?. chainId ?? INJECTIVE_TESTNET_CHAIN_ID ,
111114 gasMultiplier : chainConfig ?. gasMultiplier ?? DEFAULT_GAS_MULTIPLIER ,
112115 gasPrice : chainConfig ?. gasPrice ?? DEFAULT_GAS_PRICE ,
116+ priceIdsProcessChunkSize :
117+ chainConfig ?. priceIdsProcessChunkSize ??
118+ DEFAULT_PRICE_IDS_PROCESS_CHUNK_SIZE ,
113119 } ;
114120 }
115121
@@ -119,6 +125,7 @@ export class InjectivePricePusher implements IPricePusher {
119125
120126 private async signAndBroadcastMsg ( msg : Msgs ) : Promise < TxResponse > {
121127 const chainGrpcAuthApi = new ChainGrpcAuthApi ( this . grpcEndpoint ) ;
128+
122129 // Fetch the latest account details only if it's not stored.
123130 this . account ??= await chainGrpcAuthApi . fetchAccount (
124131 this . injectiveAddress ( ) ,
@@ -132,7 +139,7 @@ export class InjectivePricePusher implements IPricePusher {
132139 pubKey : this . wallet . toPublicKey ( ) . toBase64 ( ) ,
133140 } ) ;
134141
135- const txService = new TxGrpcClient ( this . grpcEndpoint ) ;
142+ const txService = new TxGrpcApi ( this . grpcEndpoint ) ;
136143 // simulation
137144 try {
138145 const {
@@ -207,12 +214,33 @@ export class InjectivePricePusher implements IPricePusher {
207214 if ( priceIds . length !== pubTimesToPush . length )
208215 throw new Error ( "Invalid arguments" ) ;
209216
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 > {
210234 let priceFeedUpdateObject ;
235+
211236 try {
212237 // get the latest VAAs for updatePriceFeed and then push them
213238 priceFeedUpdateObject = await this . getPriceFeedUpdateObject ( priceIds ) ;
214239 } 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+ ) ;
216244 return ;
217245 }
218246
@@ -233,7 +261,10 @@ export class InjectivePricePusher implements IPricePusher {
233261 const json = Buffer . from ( data ) . toString ( ) ;
234262 updateFeeQueryResponse = JSON . parse ( json ) ;
235263 } 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+ ) ;
237268 // Throwing an error because it is likely an RPC issue
238269 throw err ;
239270 }
@@ -247,21 +278,27 @@ export class InjectivePricePusher implements IPricePusher {
247278 } ) ;
248279
249280 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+ ) ;
251285 } catch ( err : any ) {
252286 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 } ` ) ;
254288 throw new Error ( "Please check the mnemonic" ) ;
255289 }
256290
257291 if (
258292 err . message . match ( / i n s u f f i c i e n t / ) !== null &&
259293 err . message . match ( / f u n d s / ) !== null
260294 ) {
261- this . logger . error ( err , " Insufficient funds" ) ;
295+ this . logger . error ( err , ` Insufficient funds for chunk ${ chunkIndex } ` ) ;
262296 throw new Error ( "Insufficient funds" ) ;
263297 }
264- this . logger . error ( err , "Error executing messages" ) ;
298+ this . logger . error (
299+ err ,
300+ `Error executing messages for chunk ${ chunkIndex } ` ,
301+ ) ;
265302 }
266303 }
267304}
0 commit comments