diff --git a/packages/core/src/actions/getClient.ts b/packages/core/src/actions/getClient.ts new file mode 100644 index 00000000..97964a5c --- /dev/null +++ b/packages/core/src/actions/getClient.ts @@ -0,0 +1,16 @@ +import type { ChainConfig } from "../config.js"; +import { createClient } from "polkadot-api"; +import type { JsonRpcProvider } from "polkadot-api/ws-provider/web"; + +export async function getClient(chainConfig: ChainConfig) { + const providerOrGetter = await chainConfig.provider; + + // Hack to detect wether function is a `JsonRpcProvider` or a getter of `JsonRpcProvider` + const provider = await (providerOrGetter.length > 0 + ? (providerOrGetter as JsonRpcProvider) + : ( + providerOrGetter as Exclude + )()); + + return createClient(provider); +} diff --git a/packages/core/src/actions/index.ts b/packages/core/src/actions/index.ts index 92d600f4..052fe5ed 100644 --- a/packages/core/src/actions/index.ts +++ b/packages/core/src/actions/index.ts @@ -2,5 +2,6 @@ export { aggregateWallets } from "./aggregateWallets.js"; export { connectWallet } from "./connectWallet.js"; export { disconnectWallet } from "./disconnectWallet.js"; export { getAccounts } from "./getAccounts.js"; +export { getClient } from "./getClient.js"; export { getConnectedWallets } from "./getConnectedWallets.js"; export { preflight, query } from "./query.js"; diff --git a/packages/react/src/stores/client.ts b/packages/react/src/stores/client.ts index 1cadd2f6..c438d8d1 100644 --- a/packages/react/src/stores/client.ts +++ b/packages/react/src/stores/client.ts @@ -1,10 +1,8 @@ import { chainConfigsAtom } from "./config.js"; -import type { JsonRpcProvider } from "@polkadot-api/json-rpc-provider"; -import { ReDotError } from "@reactive-dot/core"; import type { ChainId } from "@reactive-dot/core"; +import { getClient, ReDotError } from "@reactive-dot/core"; import { atom } from "jotai"; import { atomFamily } from "jotai/utils"; -import { createClient } from "polkadot-api"; export const clientAtomFamily = atomFamily((chainId: ChainId) => atom(async (get) => { @@ -14,16 +12,7 @@ export const clientAtomFamily = atomFamily((chainId: ChainId) => throw new ReDotError(`No config provided for ${chainId}`); } - const providerOrGetter = await chainConfig.provider; - - // Hack to detect wether function is a `JsonRpcProvider` or a getter of `JsonRpcProvider` - const provider = await (providerOrGetter.length > 0 - ? (providerOrGetter as JsonRpcProvider) - : ( - providerOrGetter as Exclude - )()); - - return createClient(provider); + return getClient(chainConfig); }), );