Skip to content
61 changes: 11 additions & 50 deletions packages/sdk/src/client/ClientAppChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<ModuleContainer<any>>> =
Container extends TypedClass<infer Type>
Expand Down Expand Up @@ -192,49 +187,15 @@ export class ClientAppChain<
return transaction;
}

public get query(): {
runtime: Query<
RuntimeModule<unknown>,
InferModules<AppChainModules["Runtime"]>
>;
protocol: Query<
ProtocolModule<unknown>,
InferModules<AppChainModules["Protocol"]>
>;
network: NetworkStateQuery;
explorer: BlockExplorerQuery;
} {
const queryTransportModule = this.container.resolve<QueryTransportModule>(
"QueryTransportModule"
);

const networkStateTransportModule =
this.container.resolve<NetworkStateTransportModule>(
"NetworkStateTransportModule"
);

const blockExplorerTransportModule =
this.container.resolve<BlockExplorerTransportModule>(
"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<AppChainModules["Runtime"]>,
InferModules<AppChainModules["Protocol"]>
> {
return this.container.resolve<
QueryService<
InferModules<AppChainModules["Runtime"]>,
InferModules<AppChainModules["Protocol"]>
>
>(QueryService);
}
}
105 changes: 105 additions & 0 deletions packages/sdk/src/query/QueryService.ts
Original file line number Diff line number Diff line change
@@ -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<RuntimeModule<unknown>, RuntimeModules>;

private ProtocolQuery?: Query<ProtocolModule<unknown>, ProtocolModules>;

private NetworkQuery?: NetworkStateQuery;

private ExplorerQuery?: BlockExplorerQuery;

public constructor(
@inject("Runtime")
private readonly runtimeInstance: Runtime<RuntimeModules>,
@inject("Protocol")
private readonly protocolInstance: Protocol<ProtocolModules>,
@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<RuntimeModule<unknown>, 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<ProtocolModule<unknown>, 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;
}
}