From 30e1ad7318ab640e648eb5a6df930f51dc93d353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ti=E1=BA=BFn=20Nguy=E1=BB=85n=20Kh=E1=BA=AFc?= Date: Tue, 11 Jun 2024 16:56:05 +1200 Subject: [PATCH] refactor: remove dependency on internal `PAPI` API --- .../polkadot-api-npm-0.8.0-ef17b11e20.patch | 98 ----------- apps/example/package.json | 2 +- packages/core/package.json | 2 +- packages/core/src/QueryBuilder.ts | 161 +++++++----------- packages/react/package.json | 2 +- packages/types/package.json | 2 +- yarn.lock | 38 +---- 7 files changed, 73 insertions(+), 232 deletions(-) delete mode 100644 .yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch diff --git a/.yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch b/.yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch deleted file mode 100644 index ceb3bdc7..00000000 --- a/.yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch +++ /dev/null @@ -1,98 +0,0 @@ -diff --git a/dist/index.d.mts b/dist/index.d.mts -index e0f9bb0c3068ca6fa0672be77d03173eb429eb7f..a1b5f19a70513e724bfc3d9f9c80bce4f87342ab 100644 ---- a/dist/index.d.mts -+++ b/dist/index.d.mts -@@ -129,7 +129,7 @@ declare const compatibilityHelper: (runtimeApi: RuntimeApi, getDescriptorChecksu - }; - type CompatibilityHelper = ReturnType; - --interface ConstantEntry { -+export interface ConstantEntry { - /** - * Constants are simple key-value structures found in the runtime metadata. - * -@@ -215,7 +215,7 @@ type CallOptions$1 = Partial<{ - signal: AbortSignal; - }>; - type WithCallOptions$1> = Args["length"] extends 0 ? [options?: CallOptions$1] : [...args: Args, options?: CallOptions$1]; --interface RuntimeCall, Payload> { -+export interface RuntimeCall, Payload> { - /** - * Get `Payload` (Promise-based) for the runtime call. - * -@@ -248,7 +248,7 @@ type WithCallOptions> = [ - options?: CallOptions - ]; - type PossibleParents> = A extends [...infer Left, any] ? Left | PossibleParents : []; --type StorageEntryWithoutKeys = { -+export type StorageEntryWithoutKeys = { - /** - * `isCompatible` enables you to check whether or not the call you're trying - * to make is compatible with the descriptors you generated on dev time. -@@ -269,7 +269,7 @@ type StorageEntryWithoutKeys = { - */ - watchValue: (bestOrFinalized?: "best" | "finalized") => Observable; - }; --type StorageEntryWithKeys, Payload> = { -+export type StorageEntryWithKeys, Payload> = { - /** - * `isCompatible` enables you to check whether or not the call you're trying - * to make is compatible with the descriptors you generated on dev time. -@@ -321,7 +321,7 @@ type StorageEntryWithKeys, Payload> = { - value: NonNullable; - }>>; - }; --type StorageEntry, Payload> = Args extends [] ? StorageEntryWithoutKeys : StorageEntryWithKeys; -+export type StorageEntry, Payload> = Args extends [] ? StorageEntryWithoutKeys : StorageEntryWithKeys; - - type TxEvent = TxSigned | TxBroadcasted | TxBestBlocksState | TxFinalized; - type TxBroadcastEvent = TxSigned | TxBroadcasted | TxBestBlocksState | TxFinalized; -diff --git a/dist/index.d.ts b/dist/index.d.ts -index e0f9bb0c3068ca6fa0672be77d03173eb429eb7f..a1b5f19a70513e724bfc3d9f9c80bce4f87342ab 100644 ---- a/dist/index.d.ts -+++ b/dist/index.d.ts -@@ -129,7 +129,7 @@ declare const compatibilityHelper: (runtimeApi: RuntimeApi, getDescriptorChecksu - }; - type CompatibilityHelper = ReturnType; - --interface ConstantEntry { -+export interface ConstantEntry { - /** - * Constants are simple key-value structures found in the runtime metadata. - * -@@ -215,7 +215,7 @@ type CallOptions$1 = Partial<{ - signal: AbortSignal; - }>; - type WithCallOptions$1> = Args["length"] extends 0 ? [options?: CallOptions$1] : [...args: Args, options?: CallOptions$1]; --interface RuntimeCall, Payload> { -+export interface RuntimeCall, Payload> { - /** - * Get `Payload` (Promise-based) for the runtime call. - * -@@ -248,7 +248,7 @@ type WithCallOptions> = [ - options?: CallOptions - ]; - type PossibleParents> = A extends [...infer Left, any] ? Left | PossibleParents : []; --type StorageEntryWithoutKeys = { -+export type StorageEntryWithoutKeys = { - /** - * `isCompatible` enables you to check whether or not the call you're trying - * to make is compatible with the descriptors you generated on dev time. -@@ -269,7 +269,7 @@ type StorageEntryWithoutKeys = { - */ - watchValue: (bestOrFinalized?: "best" | "finalized") => Observable; - }; --type StorageEntryWithKeys, Payload> = { -+export type StorageEntryWithKeys, Payload> = { - /** - * `isCompatible` enables you to check whether or not the call you're trying - * to make is compatible with the descriptors you generated on dev time. -@@ -321,7 +321,7 @@ type StorageEntryWithKeys, Payload> = { - value: NonNullable; - }>>; - }; --type StorageEntry, Payload> = Args extends [] ? StorageEntryWithoutKeys : StorageEntryWithKeys; -+export type StorageEntry, Payload> = Args extends [] ? StorageEntryWithoutKeys : StorageEntryWithKeys; - - type TxEvent = TxSigned | TxBroadcasted | TxBestBlocksState | TxFinalized; - type TxBroadcastEvent = TxSigned | TxBroadcasted | TxBestBlocksState | TxFinalized; diff --git a/apps/example/package.json b/apps/example/package.json index 0c5cbb3a..f30976e8 100644 --- a/apps/example/package.json +++ b/apps/example/package.json @@ -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" }, diff --git a/packages/core/package.json b/packages/core/package.json index 82bb30c8..59f1156c 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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": { diff --git a/packages/core/src/QueryBuilder.ts b/packages/core/src/QueryBuilder.ts index 4b477b55..a5d364f6 100644 --- a/packages/core/src/QueryBuilder.ts +++ b/packages/core/src/QueryBuilder.ts @@ -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 [...infer Left, any] - ? Left | PossibleParents - : []; +type InferPapiStorageEntry = T extends { + watchValue: (...args: infer Args) => Observable; +} + ? { args: Args; payload: Payload } + : { args: unknown[]; payload: unknown }; + +type InferPapiStorageEntryWithKeys = T extends { + getEntries: (...args: infer Args) => Promise; +} + ? { args: Args; payload: Payload } + : { args: unknown[]; payload: unknown }; + +type InferPapiRuntimeCall = T extends ( + ...args: infer Args +) => Promise + ? { args: Args; payload: Payload } + : { args: unknown[]; payload: unknown }; + +type InferPapiConstantEntry = T extends { + (): Promise; + (runtime: infer _): infer Payload; +} + ? Payload + : unknown; type BaseInstruction = { instruction: T; @@ -29,13 +44,9 @@ export type ConstantFetchInstruction< export type StorageReadInstruction< TPallet extends keyof TypedApi["query"], TStorage extends keyof TypedApi["query"][TPallet], - TArguments extends - TypedApi["query"][TPallet][TStorage] extends StorageEntry< - infer Args, - any - > - ? Args - : unknown[], + TArguments extends InferPapiStorageEntry< + TypedApi["query"][TPallet][TStorage] + >["args"], TDescriptor extends ChainDefinition = ReDotDescriptor, > = BaseInstruction<"read-storage"> & { pallet: TPallet; @@ -46,13 +57,9 @@ export type StorageReadInstruction< export type StorageEntriesReadInstruction< TPallet extends keyof TypedApi["query"], TStorage extends keyof TypedApi["query"][TPallet], - TArguments extends - TypedApi["query"][TPallet][TStorage] extends StorageEntry< - infer Args, - any - > - ? Args - : unknown[], + TArguments extends InferPapiStorageEntryWithKeys< + TypedApi["query"][TPallet][TStorage] + >["args"], TDescriptor extends ChainDefinition = ReDotDescriptor, > = BaseInstruction<"read-storage-entries"> & { pallet: TPallet; @@ -63,13 +70,9 @@ export type StorageEntriesReadInstruction< export type ApiCallInstruction< TPallet extends keyof TypedApi["apis"], TApi extends keyof TypedApi["apis"][TPallet], - TArguments extends - TypedApi["apis"][TPallet][TApi] extends RuntimeCall< - infer Args, - any - > - ? Args - : unknown[], + TArguments extends InferPapiRuntimeCall< + TypedApi["apis"][TPallet][TApi] + >["args"], TDescriptor extends ChainDefinition = ReDotDescriptor, > = BaseInstruction<"call-api"> & { pallet: TPallet; @@ -95,25 +98,18 @@ export type QueryInstruction< type ConstantFetchPayload< TInstruction extends ConstantFetchInstruction, TDescriptor extends ChainDefinition = ReDotDescriptor, -> = - TypedApi["constants"][TInstruction["pallet"]][TInstruction["constant"]] extends ConstantEntry< - infer Payload - > - ? Payload - : never; +> = InferPapiConstantEntry< + TypedApi["constants"][TInstruction["pallet"]][TInstruction["constant"]] +>; type StorageReadPayload< TInstruction extends | StorageReadInstruction | MultiInstruction>, TDescriptor extends ChainDefinition = ReDotDescriptor, -> = - TypedApi["query"][TInstruction["pallet"]][TInstruction["storage"]] extends StorageEntry< - any, - infer Payload - > - ? Payload - : unknown; +> = InferPapiStorageEntry< + TypedApi["query"][TInstruction["pallet"]][TInstruction["storage"]] +>["payload"]; type StorageEntriesReadPayload< TInstruction extends StorageEntriesReadInstruction< @@ -123,29 +119,18 @@ type StorageEntriesReadPayload< TDescriptor >, TDescriptor extends ChainDefinition = ReDotDescriptor, -> = - TypedApi["query"][TInstruction["pallet"]][TInstruction["storage"]] extends StorageEntryWithKeys< - infer Args, - infer Payload - > - ? Array<{ - keyArgs: Args; - value: NonNullable; - }> - : unknown; +> = InferPapiStorageEntryWithKeys< + TypedApi["query"][TInstruction["pallet"]][TInstruction["storage"]] +>["payload"]; type ApiCallPayload< TInstruction extends | ApiCallInstruction | MultiInstruction>, TDescriptor extends ChainDefinition = ReDotDescriptor, -> = - TypedApi["apis"][TInstruction["pallet"]][TInstruction["api"]] extends RuntimeCall< - any, - infer Payload - > - ? Payload - : unknown; +> = InferPapiRuntimeCall< + TypedApi["apis"][TInstruction["pallet"]][TInstruction["api"]] +>["payload"]; export type InferInstruction< TInstruction extends QueryInstruction, @@ -218,13 +203,9 @@ export default class QueryBuilder< readStorage< TPallet extends keyof TypedApi["query"], TStorage extends keyof TypedApi["query"][TPallet], - TArguments extends - TypedApi["query"][TPallet][TStorage] extends StorageEntry< - infer Args, - any - > - ? Args - : unknown[], + TArguments extends InferPapiStorageEntry< + TypedApi["query"][TPallet][TStorage] + >["args"], >(pallet: TPallet, storage: TStorage, args: TArguments) { return new QueryBuilder([ ...this.#instructions, @@ -235,13 +216,9 @@ export default class QueryBuilder< readStorages< TPallet extends keyof TypedApi["query"], TStorage extends keyof TypedApi["query"][TPallet], - TArguments extends - TypedApi["query"][TPallet][TStorage] extends StorageEntry< - infer Args, - any - > - ? Args - : unknown[], + TArguments extends InferPapiStorageEntry< + TypedApi["query"][TPallet][TStorage] + >["args"], >(pallet: TPallet, storage: TStorage, args: TArguments[]) { return new QueryBuilder([ ...this.#instructions, @@ -252,13 +229,11 @@ export default class QueryBuilder< readStorageEntries< TPallet extends keyof TypedApi["query"], TStorage extends keyof TypedApi["query"][TPallet], - TArguments extends - TypedApi["query"][TPallet][TStorage] extends StorageEntryWithKeys< - infer Args, - any - > - ? PossibleParents - : unknown[], + TArguments extends Array< + InferPapiStorageEntryWithKeys< + TypedApi["query"][TPallet][TStorage] + >["args"] + >, >(pallet: TPallet, storage: TStorage, args: TArguments) { return new QueryBuilder([ ...this.#instructions, @@ -269,13 +244,9 @@ export default class QueryBuilder< callApi< TPallet extends keyof TypedApi["apis"], TApi extends keyof TypedApi["apis"][TPallet], - TArguments extends - TypedApi["apis"][TPallet][TApi] extends RuntimeCall< - infer Args, - any - > - ? Args - : unknown[], + TArguments extends InferPapiRuntimeCall< + TypedApi["apis"][TPallet][TApi] + >["args"], >(pallet: TPallet, api: TApi, args: TArguments) { return new QueryBuilder([ ...this.#instructions, @@ -286,13 +257,9 @@ export default class QueryBuilder< callApis< TPallet extends keyof TypedApi["apis"], TApi extends keyof TypedApi["apis"][TPallet], - TArguments extends - TypedApi["apis"][TPallet][TApi] extends RuntimeCall< - infer Args, - any - > - ? Args - : unknown[], + TArguments extends InferPapiRuntimeCall< + TypedApi["apis"][TPallet][TApi] + >["args"], >(pallet: TPallet, api: TApi, args: TArguments[]) { return new QueryBuilder([ ...this.#instructions, diff --git a/packages/react/package.json b/packages/react/package.json index 14be3227..9e459556 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -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" } diff --git a/packages/types/package.json b/packages/types/package.json index 983e8a51..2888be3f 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -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" } } diff --git a/yarn.lock b/yarn.lock index 4b9534ed..b3671d51 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3351,7 +3351,7 @@ __metadata: "@tsconfig/recommended": "npm:^1.0.6" "@tsconfig/strictest": "npm:^2.0.5" eslint: "npm:^9.4.0" - polkadot-api: "patch:polkadot-api@npm%3A0.8.0#~/.yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch" + polkadot-api: "npm:^0.8.0" typescript: "npm:^5.4.5" peerDependencies: polkadot-api: ^0.8.0 @@ -3408,7 +3408,7 @@ __metadata: "@vitejs/plugin-react": "npm:^4.2.1" date-fns: "npm:^3.6.0" eslint: "npm:^9.4.0" - polkadot-api: "patch:polkadot-api@npm%3A0.8.0#~/.yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch" + polkadot-api: "npm:^0.8.0" react: "npm:^18.3.1" react-dom: "npm:^18.3.1" typescript: "npm:^5.4.5" @@ -3428,7 +3428,7 @@ __metadata: eslint: "npm:^9.4.0" jotai: "npm:^2.8.3" jotai-scope: "npm:^0.6.0" - polkadot-api: "patch:polkadot-api@npm%3A0.8.0#~/.yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch" + polkadot-api: "npm:^0.8.0" react: "npm:^18.3.1" typescript: "npm:^5.4.5" languageName: unknown @@ -3442,7 +3442,7 @@ __metadata: "@tsconfig/recommended": "npm:^1.0.6" "@tsconfig/strictest": "npm:^2.0.5" eslint: "npm:^9.4.0" - polkadot-api: "patch:polkadot-api@npm%3A0.8.0#~/.yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch" + polkadot-api: "npm:^0.8.0" typescript: "npm:^5.4.5" languageName: unknown linkType: soft @@ -12000,7 +12000,7 @@ __metadata: languageName: node linkType: hard -"polkadot-api@npm:0.8.0": +"polkadot-api@npm:^0.8.0": version: 0.8.0 resolution: "polkadot-api@npm:0.8.0" dependencies: @@ -12028,34 +12028,6 @@ __metadata: languageName: node linkType: hard -"polkadot-api@patch:polkadot-api@npm%3A0.8.0#~/.yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch": - version: 0.8.0 - resolution: "polkadot-api@patch:polkadot-api@npm%3A0.8.0#~/.yarn/patches/polkadot-api-npm-0.8.0-ef17b11e20.patch::version=0.8.0&hash=a53a35" - dependencies: - "@polkadot-api/cli": "npm:0.5.0" - "@polkadot-api/json-rpc-provider": "npm:0.0.1" - "@polkadot-api/known-chains": "npm:0.1.5" - "@polkadot-api/logs-provider": "npm:0.0.2" - "@polkadot-api/metadata-builders": "npm:0.3.0" - "@polkadot-api/observable-client": "npm:0.3.0" - "@polkadot-api/pjs-signer": "npm:0.2.0" - "@polkadot-api/polkadot-signer": "npm:0.0.1" - "@polkadot-api/signer": "npm:0.0.1" - "@polkadot-api/sm-provider": "npm:0.1.0" - "@polkadot-api/smoldot": "npm:0.2.4" - "@polkadot-api/substrate-bindings": "npm:0.5.0" - "@polkadot-api/substrate-client": "npm:0.1.2" - "@polkadot-api/utils": "npm:0.1.0" - "@polkadot-api/ws-provider": "npm:0.1.0" - peerDependencies: - rxjs: ">=7.8.0" - bin: - papi: bin/cli.mjs - polkadot-api: bin/cli.mjs - checksum: 10c0/a322bbffbdb4a7cc23edc7138b638ef0b53aa1e98d1a821981a00ed1cdb2e0faf1b1d3c7f4e28b0ee035e27d0fd87f4cfb5d22bb038ae5fa1eb35b394d6f736a - languageName: node - linkType: hard - "possible-typed-array-names@npm:^1.0.0": version: 1.0.0 resolution: "possible-typed-array-names@npm:1.0.0"