Skip to content

Commit

Permalink
refactor: extract client creation logic to core
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Jul 15, 2024
1 parent b680b9d commit 8a39645
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
16 changes: 16 additions & 0 deletions packages/core/src/actions/getClient.ts
Original file line number Diff line number Diff line change
@@ -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<typeof providerOrGetter, JsonRpcProvider>
)());

return createClient(provider);
}
1 change: 1 addition & 0 deletions packages/core/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
15 changes: 2 additions & 13 deletions packages/react/src/stores/client.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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<typeof providerOrGetter, JsonRpcProvider>
)());

return createClient(provider);
return getClient(chainConfig);
}),
);

Expand Down

0 comments on commit 8a39645

Please sign in to comment.