Skip to content

Commit

Permalink
fix: incorrect query at block hash or tag (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
tien authored Jul 26, 2024
1 parent 6c91896 commit 18cb167
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 45 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-ladybugs-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reactive-dot/core": patch
---

Fix query crashing when specifying the `at` option.
21 changes: 14 additions & 7 deletions packages/core/src/actions/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,29 @@ export function query<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (api.apis[instruction.pallet]![instruction.api] as any)(
...instruction.args,
{ signal: options?.signal },
{ signal: options?.signal, at: instruction.at },
);
case "read-storage":
return (
case "read-storage": {
const storageEntry = api.query[instruction.pallet]![
instruction.storage
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(api.query[instruction.pallet]![instruction.storage] as any).watchValue(
...instruction.args,
)
);
] as any;

return instruction.at?.startsWith("0x")
? storageEntry.getValue(...instruction.args, { at: instruction.at })
: storageEntry.watchValue(
...instruction.args,
...[instruction.at].filter((x) => x !== undefined),
);
}
case "read-storage-entries":
return (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(api.query[instruction.pallet]![instruction.storage] as any).getEntries(
...instruction.args,
{
signal: options?.signal,
at: instruction.at,
},
)
);
Expand Down
75 changes: 37 additions & 38 deletions packages/core/src/query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ type PapiCallOptions = Partial<{
signal: AbortSignal;
}>;

type CallOptions = Omit<PapiCallOptions, "signal">;

type OmitCallOptions<T extends readonly unknown[]> = T extends [
infer Head,
...infer Tail,
Expand All @@ -20,20 +18,20 @@ type OmitCallOptions<T extends readonly unknown[]> = T extends [
: [];

type InferPapiStorageEntry<T> = T extends {
watchValue: (...args: [...infer Args, infer At]) => infer Response;
watchValue: (...args: [...infer Args, infer _]) => infer Response;
}
? { args: Args; options: { at: At }; response: Response }
: { args: unknown[]; options: unknown; response: unknown };
? { args: Args; response: Response }
: { args: unknown[]; response: unknown };

type InferPapiStorageEntries<T> = T extends {
getEntries: (...args: infer Args) => infer Response;
}
? { args: OmitCallOptions<Args>; options: CallOptions; response: Response }
: { args: unknown[]; options: unknown; response: unknown };
? { args: OmitCallOptions<Args>; response: Response }
: { args: unknown[]; response: unknown };

type InferPapiRuntimeCall<T> = T extends (...args: infer Args) => infer Response
? { args: OmitCallOptions<Args>; options: CallOptions; response: Response }
: { args: unknown[]; options: unknown; response: unknown };
? { args: OmitCallOptions<Args>; response: Response }
: { args: unknown[]; response: unknown };

type InferPapiConstantEntry<T> = T extends {
(): Promise<infer Payload>;
Expand All @@ -42,6 +40,8 @@ type InferPapiConstantEntry<T> = T extends {
? Promise<Payload>
: unknown;

type At = "best" | "finalized" | `0x${string}`;

type BaseInstruction<T extends string> = {
instruction: T;
};
Expand All @@ -66,6 +66,7 @@ export type StorageReadInstruction<
pallet: TPallet;
storage: TStorage;
args: TArguments;
at: At | undefined;
};

export type StorageEntriesReadInstruction<
Expand All @@ -79,6 +80,7 @@ export type StorageEntriesReadInstruction<
pallet: TPallet;
storage: TStorage;
args: TArguments;
at: At | undefined;
};

export type ApiCallInstruction<
Expand All @@ -92,6 +94,7 @@ export type ApiCallInstruction<
pallet: TPallet;
api: TApi;
args: TArguments;
at: At | undefined;
};

export type MultiInstruction<TInstruction extends BaseInstruction<string>> =
Expand Down Expand Up @@ -253,15 +256,18 @@ export class Query<
TArguments extends InferPapiStorageEntry<
TypedApi<TDescriptor>["query"][TPallet][TStorage]
>["args"],
TOptions extends InferPapiStorageEntry<
TypedApi<TDescriptor>["query"][TPallet][TStorage]
>["options"],
>(pallet: TPallet, storage: TStorage, args: TArguments, options?: TOptions) {
>(
pallet: TPallet,
storage: TStorage,
args: TArguments,
options?: { at?: At },
) {
return this.#append({
instruction: "read-storage",
pallet,
storage,
args: options === undefined ? args : [...args, options],
args,
at: options?.at,
});
}

Expand All @@ -271,21 +277,18 @@ export class Query<
TArguments extends InferPapiStorageEntry<
TypedApi<TDescriptor>["query"][TPallet][TStorage]
>["args"],
TOptions extends InferPapiStorageEntry<
TypedApi<TDescriptor>["query"][TPallet][TStorage]
>["options"],
>(
pallet: TPallet,
storage: TStorage,
args: TArguments[],
options?: TOptions,
options?: { at?: At },
) {
return this.#append({
instruction: "read-storage",
pallet,
storage,
args:
options === undefined ? args : args.map((args) => [...args, options]),
args,
at: options?.at,
multi: true,
});
}
Expand All @@ -296,17 +299,18 @@ export class Query<
TArguments extends InferPapiStorageEntries<
TypedApi<TDescriptor>["query"][TPallet][TStorage]
>["args"],
TOptions extends Array<
InferPapiStorageEntries<
TypedApi<TDescriptor>["query"][TPallet][TStorage]
>["options"]
>,
>(pallet: TPallet, storage: TStorage, args: TArguments, options?: TOptions) {
>(
pallet: TPallet,
storage: TStorage,
args: TArguments,
options?: { at?: At },
) {
return this.#append({
instruction: "read-storage-entries",
pallet,
storage,
args: options === undefined ? args : [...args, options],
args,
at: options?.at,
});
}

Expand All @@ -316,15 +320,13 @@ export class Query<
TArguments extends InferPapiRuntimeCall<
TypedApi<TDescriptor>["apis"][TPallet][TApi]
>["args"],
TOptions extends InferPapiRuntimeCall<
TypedApi<TDescriptor>["apis"][TPallet][TApi]
>["options"],
>(pallet: TPallet, api: TApi, args: TArguments, options?: TOptions) {
>(pallet: TPallet, api: TApi, args: TArguments, options?: { at?: At }) {
return this.#append({
instruction: "call-api",
pallet,
api,
args: options === undefined ? args : [...args, options],
args,
at: options?.at,
});
}

Expand All @@ -334,16 +336,13 @@ export class Query<
TArguments extends InferPapiRuntimeCall<
TypedApi<TDescriptor>["apis"][TPallet][TApi]
>["args"],
TOptions extends InferPapiRuntimeCall<
TypedApi<TDescriptor>["apis"][TPallet][TApi]
>["options"],
>(pallet: TPallet, api: TApi, args: TArguments[], options?: TOptions) {
>(pallet: TPallet, api: TApi, args: TArguments[], options?: { at?: At }) {
return this.#append({
instruction: "call-api",
pallet,
api,
args:
options === undefined ? args : args.map((args) => [...args, options]),
args,
at: options?.at,
multi: true,
});
}
Expand Down

0 comments on commit 18cb167

Please sign in to comment.