diff --git a/packages/sdk/src/client/ClientAppChain.ts b/packages/sdk/src/client/ClientAppChain.ts index bc1b1c63d..e46414d1b 100644 --- a/packages/sdk/src/client/ClientAppChain.ts +++ b/packages/sdk/src/client/ClientAppChain.ts @@ -19,18 +19,12 @@ import { } from "@proto-kit/protocol"; import { DummyStateService, - NetworkStateQuery, - NetworkStateTransportModule, Query, - QueryBuilderFactory, - QueryTransportModule, Sequencer, UnsignedTransaction, AppChain, AppChainModule, MinimalAppChainDefinition, - BlockExplorerQuery, - BlockExplorerTransportModule, } from "@proto-kit/sequencer"; import { container } from "tsyringe"; import { Field, PublicKey, UInt64 } from "o1js"; @@ -42,6 +36,7 @@ import { GraphqlTransactionSender } from "../graphql/GraphqlTransactionSender"; import { Signer } from "../transaction/InMemorySigner"; import { AppChainTransaction } from "../transaction/AppChainTransaction"; import { TransactionSender } from "../transaction/InMemoryTransactionSender"; +import { QueryService } from "../query/QueryService"; export type InferModules>> = Container extends TypedClass @@ -192,49 +187,15 @@ export class ClientAppChain< return transaction; } - public get query(): { - runtime: Query< - RuntimeModule, - InferModules - >; - protocol: Query< - ProtocolModule, - InferModules - >; - network: NetworkStateQuery; - explorer: BlockExplorerQuery; - } { - const queryTransportModule = this.container.resolve( - "QueryTransportModule" - ); - - const networkStateTransportModule = - this.container.resolve( - "NetworkStateTransportModule" - ); - - const blockExplorerTransportModule = - this.container.resolve( - "BlockExplorerTransportModule" - ); - - const network = new NetworkStateQuery(networkStateTransportModule); - const explorer = new BlockExplorerQuery(blockExplorerTransportModule); - - return { - runtime: QueryBuilderFactory.fromRuntime( - this.runtime, - queryTransportModule - ), - - protocol: QueryBuilderFactory.fromProtocol( - this.protocol, - queryTransportModule - ), - - network, - - explorer, - }; + public get query(): QueryService< + InferModules, + InferModules + > { + return this.container.resolve< + QueryService< + InferModules, + InferModules + > + >(QueryService); } } diff --git a/packages/sdk/src/query/QueryService.ts b/packages/sdk/src/query/QueryService.ts new file mode 100644 index 000000000..e49f750fc --- /dev/null +++ b/packages/sdk/src/query/QueryService.ts @@ -0,0 +1,105 @@ +import { inject, injectable, Lifecycle, scoped } from "tsyringe"; +import { + Runtime, + RuntimeModule, + RuntimeModulesRecord, +} from "@proto-kit/module"; +import { + MandatoryProtocolModulesRecord, + Protocol, + ProtocolModule, + ProtocolModulesRecord, +} from "@proto-kit/protocol"; +import { + BlockExplorerQuery, + BlockExplorerTransportModule, + NetworkStateQuery, + NetworkStateTransportModule, + Query, + QueryBuilderFactory, + QueryTransportModule, +} from "@proto-kit/sequencer"; + +@scoped(Lifecycle.ContainerScoped) +@injectable() +export class QueryService< + RuntimeModules extends RuntimeModulesRecord, + ProtocolModules extends ProtocolModulesRecord & + MandatoryProtocolModulesRecord, +> { + // Lazily initialized query instances + private RuntimeQuery?: Query, RuntimeModules>; + + private ProtocolQuery?: Query, ProtocolModules>; + + private NetworkQuery?: NetworkStateQuery; + + private ExplorerQuery?: BlockExplorerQuery; + + public constructor( + @inject("Runtime") + private readonly runtimeInstance: Runtime, + @inject("Protocol") + private readonly protocolInstance: Protocol, + @inject("QueryTransportModule", { isOptional: true }) + private readonly queryTransport: QueryTransportModule, + @inject("NetworkStateTransportModule", { isOptional: true }) + private readonly networkStateTransport: NetworkStateTransportModule, + @inject("BlockExplorerTransportModule", { isOptional: true }) + private readonly blockExplorerTransport: BlockExplorerTransportModule + ) {} + + /** + * Getter of query module for runtime modules. + * If not initialized before, it is initialized. + * @returns A {@link Query} module for runtime module. + */ + public get runtime(): Query, RuntimeModules> { + if (this.RuntimeQuery === undefined) { + this.RuntimeQuery = QueryBuilderFactory.fromRuntime( + this.runtimeInstance, + this.queryTransport + ); + } + return this.RuntimeQuery; + } + + /** + * Getter of query module for protocol. + * If not initialized before, it is initialized. + * @returns A {@link Query} module for protocol module. + */ + public get protocol(): Query, ProtocolModules> { + if (this.ProtocolQuery === undefined) { + this.ProtocolQuery = QueryBuilderFactory.fromProtocol( + this.protocolInstance, + this.queryTransport + ); + } + return this.ProtocolQuery; + } + + /** + * Getter of network state query module. + * If not initialized before, it is initialized. + * @returns A {@link NetworkStateQuery} module. + */ + public get network(): NetworkStateQuery { + if (this.NetworkQuery === undefined) { + this.NetworkQuery = new NetworkStateQuery(this.networkStateTransport); + } + return this.NetworkQuery; + } + + /** + * Getter of block explorer query module. + * If not initialized before, it is initialized. + * @returns A {@link BlockExplorerQuery} module. + */ + public get explorer(): BlockExplorerQuery { + if (this.ExplorerQuery === undefined) { + this.ExplorerQuery = new BlockExplorerQuery(this.blockExplorerTransport); + } + return this.ExplorerQuery; + } +}