Skip to content

Commit

Permalink
refactor: remove dependency on internal PAPI API
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Jun 11, 2024
1 parent cf7037d commit 30e1ad7
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 232 deletions.
98 changes: 0 additions & 98 deletions .yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch

This file was deleted.

2 changes: 1 addition & 1 deletion apps/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@reactive-dot/react": "workspace:^",
"date-fns": "^3.6.0",
"polkadot-api": "patch:polkadot-api@npm%3A0.8.0#~/.yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch",
"polkadot-api": "^0.8.0",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@tsconfig/recommended": "^1.0.6",
"@tsconfig/strictest": "^2.0.5",
"eslint": "^9.4.0",
"polkadot-api": "patch:polkadot-api@npm%3A0.8.0#~/.yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch",
"polkadot-api": "^0.8.0",
"typescript": "^5.4.5"
},
"peerDependencies": {
Expand Down
161 changes: 64 additions & 97 deletions packages/core/src/QueryBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { ReDotDescriptor } from "@reactive-dot/types";
import type {
TypedApi,
StorageEntry,
StorageEntryWithKeys,
ChainDefinition,
RuntimeCall,
ConstantEntry,
} from "polkadot-api";
import type { ChainDefinition, TypedApi } from "polkadot-api";
import { Observable } from "rxjs";

type PossibleParents<A extends Array<any>> = A extends [...infer Left, any]
? Left | PossibleParents<Left>
: [];
type InferPapiStorageEntry<T> = T extends {
watchValue: (...args: infer Args) => Observable<infer Payload>;
}
? { args: Args; payload: Payload }
: { args: unknown[]; payload: unknown };

type InferPapiStorageEntryWithKeys<T> = T extends {
getEntries: (...args: infer Args) => Promise<infer Payload>;
}
? { args: Args; payload: Payload }
: { args: unknown[]; payload: unknown };

type InferPapiRuntimeCall<T> = T extends (
...args: infer Args
) => Promise<infer Payload>
? { args: Args; payload: Payload }
: { args: unknown[]; payload: unknown };

type InferPapiConstantEntry<T> = T extends {
(): Promise<infer Payload>;
(runtime: infer _): infer Payload;
}
? Payload
: unknown;

type BaseInstruction<T extends string> = {
instruction: T;
Expand All @@ -29,13 +44,9 @@ export type ConstantFetchInstruction<
export type StorageReadInstruction<
TPallet extends keyof TypedApi<TDescriptor>["query"],
TStorage extends keyof TypedApi<TDescriptor>["query"][TPallet],
TArguments extends
TypedApi<TDescriptor>["query"][TPallet][TStorage] extends StorageEntry<
infer Args,
any
>
? Args
: unknown[],
TArguments extends InferPapiStorageEntry<
TypedApi<TDescriptor>["query"][TPallet][TStorage]
>["args"],
TDescriptor extends ChainDefinition = ReDotDescriptor,
> = BaseInstruction<"read-storage"> & {
pallet: TPallet;
Expand All @@ -46,13 +57,9 @@ export type StorageReadInstruction<
export type StorageEntriesReadInstruction<
TPallet extends keyof TypedApi<TDescriptor>["query"],
TStorage extends keyof TypedApi<TDescriptor>["query"][TPallet],
TArguments extends
TypedApi<TDescriptor>["query"][TPallet][TStorage] extends StorageEntry<
infer Args,
any
>
? Args
: unknown[],
TArguments extends InferPapiStorageEntryWithKeys<
TypedApi<TDescriptor>["query"][TPallet][TStorage]
>["args"],
TDescriptor extends ChainDefinition = ReDotDescriptor,
> = BaseInstruction<"read-storage-entries"> & {
pallet: TPallet;
Expand All @@ -63,13 +70,9 @@ export type StorageEntriesReadInstruction<
export type ApiCallInstruction<
TPallet extends keyof TypedApi<TDescriptor>["apis"],
TApi extends keyof TypedApi<TDescriptor>["apis"][TPallet],
TArguments extends
TypedApi<TDescriptor>["apis"][TPallet][TApi] extends RuntimeCall<
infer Args,
any
>
? Args
: unknown[],
TArguments extends InferPapiRuntimeCall<
TypedApi<TDescriptor>["apis"][TPallet][TApi]
>["args"],
TDescriptor extends ChainDefinition = ReDotDescriptor,
> = BaseInstruction<"call-api"> & {
pallet: TPallet;
Expand All @@ -95,25 +98,18 @@ export type QueryInstruction<
type ConstantFetchPayload<
TInstruction extends ConstantFetchInstruction<any, any, TDescriptor>,
TDescriptor extends ChainDefinition = ReDotDescriptor,
> =
TypedApi<TDescriptor>["constants"][TInstruction["pallet"]][TInstruction["constant"]] extends ConstantEntry<
infer Payload
>
? Payload
: never;
> = InferPapiConstantEntry<
TypedApi<TDescriptor>["constants"][TInstruction["pallet"]][TInstruction["constant"]]
>;

type StorageReadPayload<
TInstruction extends
| StorageReadInstruction<any, any, any, TDescriptor>
| MultiInstruction<StorageReadInstruction<any, any, any, TDescriptor>>,
TDescriptor extends ChainDefinition = ReDotDescriptor,
> =
TypedApi<TDescriptor>["query"][TInstruction["pallet"]][TInstruction["storage"]] extends StorageEntry<
any,
infer Payload
>
? Payload
: unknown;
> = InferPapiStorageEntry<
TypedApi<TDescriptor>["query"][TInstruction["pallet"]][TInstruction["storage"]]
>["payload"];

type StorageEntriesReadPayload<
TInstruction extends StorageEntriesReadInstruction<
Expand All @@ -123,29 +119,18 @@ type StorageEntriesReadPayload<
TDescriptor
>,
TDescriptor extends ChainDefinition = ReDotDescriptor,
> =
TypedApi<TDescriptor>["query"][TInstruction["pallet"]][TInstruction["storage"]] extends StorageEntryWithKeys<
infer Args,
infer Payload
>
? Array<{
keyArgs: Args;
value: NonNullable<Payload>;
}>
: unknown;
> = InferPapiStorageEntryWithKeys<
TypedApi<TDescriptor>["query"][TInstruction["pallet"]][TInstruction["storage"]]
>["payload"];

type ApiCallPayload<
TInstruction extends
| ApiCallInstruction<any, any, any, TDescriptor>
| MultiInstruction<ApiCallInstruction<any, any, any, TDescriptor>>,
TDescriptor extends ChainDefinition = ReDotDescriptor,
> =
TypedApi<TDescriptor>["apis"][TInstruction["pallet"]][TInstruction["api"]] extends RuntimeCall<
any,
infer Payload
>
? Payload
: unknown;
> = InferPapiRuntimeCall<
TypedApi<TDescriptor>["apis"][TInstruction["pallet"]][TInstruction["api"]]
>["payload"];

export type InferInstruction<
TInstruction extends QueryInstruction,
Expand Down Expand Up @@ -218,13 +203,9 @@ export default class QueryBuilder<
readStorage<
TPallet extends keyof TypedApi<TDescriptor>["query"],
TStorage extends keyof TypedApi<TDescriptor>["query"][TPallet],
TArguments extends
TypedApi<TDescriptor>["query"][TPallet][TStorage] extends StorageEntry<
infer Args,
any
>
? Args
: unknown[],
TArguments extends InferPapiStorageEntry<
TypedApi<TDescriptor>["query"][TPallet][TStorage]
>["args"],
>(pallet: TPallet, storage: TStorage, args: TArguments) {
return new QueryBuilder([
...this.#instructions,
Expand All @@ -235,13 +216,9 @@ export default class QueryBuilder<
readStorages<
TPallet extends keyof TypedApi<TDescriptor>["query"],
TStorage extends keyof TypedApi<TDescriptor>["query"][TPallet],
TArguments extends
TypedApi<TDescriptor>["query"][TPallet][TStorage] extends StorageEntry<
infer Args,
any
>
? Args
: unknown[],
TArguments extends InferPapiStorageEntry<
TypedApi<TDescriptor>["query"][TPallet][TStorage]
>["args"],
>(pallet: TPallet, storage: TStorage, args: TArguments[]) {
return new QueryBuilder([
...this.#instructions,
Expand All @@ -252,13 +229,11 @@ export default class QueryBuilder<
readStorageEntries<
TPallet extends keyof TypedApi<TDescriptor>["query"],
TStorage extends keyof TypedApi<TDescriptor>["query"][TPallet],
TArguments extends
TypedApi<TDescriptor>["query"][TPallet][TStorage] extends StorageEntryWithKeys<
infer Args,
any
>
? PossibleParents<Args>
: unknown[],
TArguments extends Array<
InferPapiStorageEntryWithKeys<
TypedApi<TDescriptor>["query"][TPallet][TStorage]
>["args"]
>,
>(pallet: TPallet, storage: TStorage, args: TArguments) {
return new QueryBuilder([
...this.#instructions,
Expand All @@ -269,13 +244,9 @@ export default class QueryBuilder<
callApi<
TPallet extends keyof TypedApi<TDescriptor>["apis"],
TApi extends keyof TypedApi<TDescriptor>["apis"][TPallet],
TArguments extends
TypedApi<TDescriptor>["apis"][TPallet][TApi] extends RuntimeCall<
infer Args,
any
>
? Args
: unknown[],
TArguments extends InferPapiRuntimeCall<
TypedApi<TDescriptor>["apis"][TPallet][TApi]
>["args"],
>(pallet: TPallet, api: TApi, args: TArguments) {
return new QueryBuilder([
...this.#instructions,
Expand All @@ -286,13 +257,9 @@ export default class QueryBuilder<
callApis<
TPallet extends keyof TypedApi<TDescriptor>["apis"],
TApi extends keyof TypedApi<TDescriptor>["apis"][TPallet],
TArguments extends
TypedApi<TDescriptor>["apis"][TPallet][TApi] extends RuntimeCall<
infer Args,
any
>
? Args
: unknown[],
TArguments extends InferPapiRuntimeCall<
TypedApi<TDescriptor>["apis"][TPallet][TApi]
>["args"],
>(pallet: TPallet, api: TApi, args: TArguments[]) {
return new QueryBuilder([
...this.#instructions,
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@tsconfig/strictest": "^2.0.5",
"@types/react": "^18.3.3",
"eslint": "^9.4.0",
"polkadot-api": "patch:polkadot-api@npm%3A0.8.0#~/.yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch",
"polkadot-api": "^0.8.0",
"react": "^18.3.1",
"typescript": "^5.4.5"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@tsconfig/recommended": "^1.0.6",
"@tsconfig/strictest": "^2.0.5",
"eslint": "^9.4.0",
"polkadot-api": "patch:polkadot-api@npm%3A0.8.0#~/.yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch",
"polkadot-api": "^0.8.0",
"typescript": "^5.4.5"
}
}
Loading

0 comments on commit 30e1ad7

Please sign in to comment.