11// Copyright 2020-2025 SubQuery Pte Ltd authors & contributors
22// SPDX-License-Identifier: GPL-3.0
33
4- import { Inject , Injectable } from '@nestjs/common' ;
5- import { EventEmitter2 } from '@nestjs/event-emitter' ;
6- import { StellarProjectNetworkConfig } from '@subql/common-stellar' ;
7- import {
8- ApiService ,
9- ConnectionPoolService ,
10- getLogger ,
11- IBlock ,
12- exitWithError ,
13- NodeConfig ,
14- } from '@subql/node-core' ;
15- import { IEndpointConfig } from '@subql/types-core' ;
16- import {
17- StellarBlockWrapper ,
18- SubqlDatasource ,
19- IStellarEndpointConfig ,
20- } from '@subql/types-stellar' ;
21- import {
22- SubqueryProject ,
23- dsHasSorobanEventHandler ,
24- } from '../configure/SubqueryProject' ;
25- import { StellarApiConnection } from './api.connection' ;
26- import { StellarApi } from './api.stellar' ;
4+ import { Inject , Injectable } from '@nestjs/common' ;
5+ import { EventEmitter2 } from '@nestjs/event-emitter' ;
6+ import { StellarProjectNetworkConfig } from '@subql/common-stellar' ;
7+ import { ApiService , ConnectionPoolService , getLogger , IBlock , exitWithError , NodeConfig } from '@subql/node-core' ;
8+ import { IEndpointConfig } from '@subql/types-core' ;
9+ import { StellarBlockWrapper , SubqlDatasource , IStellarEndpointConfig } from '@subql/types-stellar' ;
10+ import { SubqueryProject , dsHasSorobanEventHandler } from '../configure/SubqueryProject' ;
11+ import { StellarApiConnection } from './api.connection' ;
12+ import { StellarApi } from './api.stellar' ;
2713import SafeStellarProvider from './safe-api' ;
28- import { SorobanServer } from './soroban.server' ;
14+ import { SorobanServer } from './soroban.server' ;
2915
3016const logger = getLogger ( 'api' ) ;
3117
@@ -37,10 +23,7 @@ export class StellarApiService extends ApiService<
3723 StellarApiConnection ,
3824 IStellarEndpointConfig
3925> {
40- private constructor (
41- connectionPoolService : ConnectionPoolService < StellarApiConnection > ,
42- eventEmitter : EventEmitter2 ,
43- ) {
26+ private constructor ( connectionPoolService : ConnectionPoolService < StellarApiConnection > , eventEmitter : EventEmitter2 ) {
4427 super ( connectionPoolService , eventEmitter ) ;
4528 }
4629
@@ -52,15 +35,12 @@ export class StellarApiService extends ApiService<
5235 ) : Promise < StellarApiService > {
5336 let network : StellarProjectNetworkConfig ;
5437
55- const apiService = new StellarApiService (
56- connectionPoolService ,
57- eventEmitter ,
58- ) ;
38+ const apiService = new StellarApiService ( connectionPoolService , eventEmitter ) ;
5939
6040 try {
6141 network = project . network ;
6242 } catch ( e ) {
63- exitWithError ( new Error ( `Failed to init api` , { cause : e } ) , logger ) ;
43+ exitWithError ( new Error ( `Failed to init api` , { cause : e } ) , logger ) ;
6444 }
6545
6646 if ( nodeConfig . primaryNetworkEndpoint ) {
@@ -77,18 +57,13 @@ export class StellarApiService extends ApiService<
7757
7858 // TOOD if project upgrades introduces new datasoruces this wont work
7959 if (
80- dsHasSorobanEventHandler ( [
81- ...project . dataSources ,
82- ...( project . templates as SubqlDatasource [ ] ) ,
83- ] ) &&
60+ dsHasSorobanEventHandler ( [ ...project . dataSources , ...( project . templates as SubqlDatasource [ ] ) ] ) &&
8461 ! sorobanEndpoint
8562 ) {
86- throw new Error (
87- `Soroban network endpoint must be provided for network. chainId="${ project . network . chainId } "` ,
88- ) ;
63+ throw new Error ( `Soroban network endpoint must be provided for network. chainId="${ project . network . chainId } "` ) ;
8964 }
9065
91- const { protocol } = new URL ( sorobanEndpoint ) ;
66+ const { protocol} = new URL ( sorobanEndpoint ) ;
9267 const protocolStr = protocol . replace ( ':' , '' ) ;
9368
9469 const sorobanClient = sorobanEndpoint
@@ -98,12 +73,7 @@ export class StellarApiService extends ApiService<
9873 : undefined ;
9974
10075 await apiService . createConnections ( network , ( endpoint , config ) =>
101- StellarApiConnection . create (
102- endpoint ,
103- apiService . fetchBlockBatches . bind ( apiService ) ,
104- sorobanClient ,
105- config ,
106- ) ,
76+ StellarApiConnection . create ( endpoint , apiService . fetchBlockBatches . bind ( apiService ) , sorobanClient , config ) ,
10777 ) ;
10878
10979 return apiService ;
@@ -120,9 +90,7 @@ export class StellarApiService extends ApiService<
12090 get : ( target , prop , receiver ) => {
12191 const originalMethod = target [ prop as keyof SafeStellarProvider ] ;
12292 if ( typeof originalMethod === 'function' ) {
123- return async (
124- ...args : any [ ]
125- ) : Promise < ReturnType < typeof originalMethod > > => {
93+ return async ( ...args : any [ ] ) : Promise < ReturnType < typeof originalMethod > > => {
12694 let retries = 0 ;
12795 let currentApi = target ;
12896 let throwingError : Error | undefined ;
@@ -131,18 +99,14 @@ export class StellarApiService extends ApiService<
13199 try {
132100 return await originalMethod . apply ( currentApi , args ) ;
133101 } catch ( error : any ) {
134- logger . warn (
135- `Request failed with api at height ${ height } (retry ${ retries } ): ${ error . message } ` ,
136- ) ;
102+ logger . warn ( `Request failed with api at height ${ height } (retry ${ retries } ): ${ error . message } ` ) ;
137103 throwingError = error ;
138104 currentApi = this . unsafeApi . getSafeApi ( height ) ;
139105 retries ++ ;
140106 }
141107 }
142108
143- logger . error (
144- `Maximum retries (${ maxRetries } ) exceeded for api at height ${ height } ` ,
145- ) ;
109+ logger . error ( `Maximum retries (${ maxRetries } ) exceeded for api at height ${ height } ` ) ;
146110 if ( ! throwingError ) {
147111 throw new Error ( 'Failed to make request, maximum retries failed' ) ;
148112 }
@@ -156,10 +120,7 @@ export class StellarApiService extends ApiService<
156120 return new Proxy ( this . unsafeApi . getSafeApi ( height ) , handler ) ;
157121 }
158122
159- private async fetchBlockBatches (
160- api : StellarApi ,
161- batch : number [ ] ,
162- ) : Promise < IBlock < StellarBlockWrapper > [ ] > {
123+ private async fetchBlockBatches ( api : StellarApi , batch : number [ ] ) : Promise < IBlock < StellarBlockWrapper > [ ] > {
163124 return api . fetchBlocks ( batch ) ;
164125 }
165126}
0 commit comments