Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: substrate-connect integration #368

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/neat-fireants-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reactive-dot/core": minor
---

Added SubstrateConnect integration.
12 changes: 3 additions & 9 deletions apps/docs/react/getting-started/setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,17 @@ For more information on metadata syncing and type generation, please refer to th
// `dot` is the name we gave to `npx papi add`
import { dot } from "@polkadot-api/descriptors";
import { defineConfig } from "@reactive-dot/core";
import { createLightClientProvider } from "@reactive-dot/core/providers/light-client.js";
import { InjectedWalletProvider } from "@reactive-dot/core/wallets.js";
import { chainSpec } from "polkadot-api/chains/polkadot";
import { getSmProvider } from "polkadot-api/sm-provider";
import { startFromWorker } from "polkadot-api/smoldot/from-worker";

const smoldot = startFromWorker(
new Worker(new URL("polkadot-api/smoldot/worker", import.meta.url), {
type: "module",
}),
);
const lightClientProvider = createLightClientProvider();

export const config = defineConfig({
chains: {
// "polkadot" here can be any unique string value
polkadot: {
descriptor: dot,
provider: getSmProvider(smoldot.addChain({ chainSpec })),
provider: lightClientProvider.addRelayChain({ id: "polkadot" }),
},
},
wallets: [new InjectedWalletProvider()],
Expand Down
14 changes: 4 additions & 10 deletions apps/docs/vue/getting-started/setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,17 @@ For more information on metadata syncing and type generation, please refer to th
// `dot` is the name we gave to `npx papi add`
import { dot } from "@polkadot-api/descriptors";
import { defineConfig } from "@reactive-dot/core";
import { createLightClientProvider } from "@reactive-dot/core/providers/light-client.js";
import { InjectedWalletProvider } from "@reactive-dot/core/wallets.js";
import { chainSpec } from "polkadot-api/chains/polkadot";
import { getSmProvider } from "polkadot-api/sm-provider";
import { startFromWorker } from "polkadot-api/smoldot/from-worker";

const smoldot = startFromWorker(
new Worker(new URL("polkadot-api/smoldot/worker", import.meta.url), {
type: "module",
}),
);

const lightClientProvider = createLightClientProvider();

export const config = defineConfig({
chains: {
// "polkadot" here can be any unique string value
polkadot: {
descriptor: dot,
provider: getSmProvider(smoldot.addChain({ chainSpec })),
provider: lightClientProvider.addRelayChain({ id: "polkadot" }),
},
},
wallets: [new InjectedWalletProvider()],
Expand Down
2 changes: 1 addition & 1 deletion examples/react/.papi/descriptors/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.0-autogenerated.2857293709118977602",
"version": "0.1.0-autogenerated.14047238154392085370",
"name": "@polkadot-api/descriptors",
"files": [
"dist"
Expand Down
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions examples/react/.papi/polkadot-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
"chain": "polkadot",
"metadata": ".papi/metadata/polkadot.scale"
},
"polkadot_asset_hub": {
"chain": "polkadot_asset_hub",
"metadata": ".papi/metadata/polkadot_asset_hub.scale"
},
"polkadot_people": {
"chain": "polkadot_people",
"metadata": ".papi/metadata/polkadot_people.scale"
},
"kusama": {
"chain": "ksmcc3",
"metadata": ".papi/metadata/kusama.scale"
Expand Down
45 changes: 22 additions & 23 deletions examples/react/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
import { kusama, polkadot, westend } from "@polkadot-api/descriptors";
import {
kusama,
polkadot,
polkadot_asset_hub,
polkadot_people,
westend,
} from "@polkadot-api/descriptors";
import { defineConfig } from "@reactive-dot/core";
import { createLightClientProvider } from "@reactive-dot/core/providers/light-client.js";
import { InjectedWalletProvider } from "@reactive-dot/core/wallets.js";
import { LedgerWallet } from "@reactive-dot/wallet-ledger";
import { WalletConnect } from "@reactive-dot/wallet-walletconnect";
import { getSmProvider } from "polkadot-api/sm-provider";
import { startFromWorker } from "polkadot-api/smoldot/from-worker";

const smoldot = startFromWorker(
new Worker(new URL("polkadot-api/smoldot/worker", import.meta.url), {
type: "module",
}),
);
const lightClientProvider = createLightClientProvider();

const polkadotProvider = lightClientProvider.addRelayChain({ id: "polkadot" });

export const config = defineConfig({
chains: {
polkadot: {
descriptor: polkadot,
provider: getSmProvider(
import("polkadot-api/chains/polkadot").then(({ chainSpec }) =>
smoldot.addChain({ chainSpec }),
),
),
provider: polkadotProvider,
},
polkadot_asset_hub: {
descriptor: polkadot_asset_hub,
provider: polkadotProvider.addParachain({ id: "polkadot_asset_hub" }),
},
polkadot_people: {
descriptor: polkadot_people,
provider: polkadotProvider.addParachain({ id: "polkadot_people" }),
},
kusama: {
descriptor: kusama,
provider: getSmProvider(
import("polkadot-api/chains/ksmcc3").then(({ chainSpec }) =>
smoldot.addChain({ chainSpec }),
),
),
provider: lightClientProvider.addRelayChain({ id: "kusama" }),
},
westend: {
descriptor: westend,
provider: getSmProvider(
import("polkadot-api/chains/westend2").then(({ chainSpec }) =>
smoldot.addChain({ chainSpec }),
),
),
provider: lightClientProvider.addRelayChain({ id: "westend" }),
},
},
wallets: [
Expand Down
2 changes: 1 addition & 1 deletion examples/vue/.papi/descriptors/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.0-autogenerated.2857293709118977602",
"version": "0.1.0-autogenerated.14047238154392085370",
"name": "@polkadot-api/descriptors",
"files": [
"dist"
Expand Down
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions examples/vue/.papi/polkadot-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
"chain": "polkadot",
"metadata": ".papi/metadata/polkadot.scale"
},
"polkadot_asset_hub": {
"chain": "polkadot_asset_hub",
"metadata": ".papi/metadata/polkadot_asset_hub.scale"
},
"polkadot_people": {
"chain": "polkadot_people",
"metadata": ".papi/metadata/polkadot_people.scale"
},
"kusama": {
"chain": "ksmcc3",
"metadata": ".papi/metadata/kusama.scale"
Expand Down
66 changes: 42 additions & 24 deletions examples/vue/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,61 @@
import { kusama, polkadot, westend } from "@polkadot-api/descriptors";
import {
kusama,
polkadot,
polkadot_asset_hub,
polkadot_people,
westend,
} from "@polkadot-api/descriptors";
import { defineConfig } from "@reactive-dot/core";
import { createLightClientProvider } from "@reactive-dot/core/providers/light-client.js";
import { InjectedWalletProvider } from "@reactive-dot/core/wallets.js";
import { getSmProvider } from "polkadot-api/sm-provider";
import { startFromWorker } from "polkadot-api/smoldot/from-worker";
import { LedgerWallet } from "@reactive-dot/wallet-ledger";
import { WalletConnect } from "@reactive-dot/wallet-walletconnect";

const smoldot = startFromWorker(
new Worker(new URL("polkadot-api/smoldot/worker", import.meta.url), {
type: "module",
}),
);
const lightClientProvider = createLightClientProvider();

const polkadotProvider = lightClientProvider.addRelayChain({ id: "polkadot" });

export const config = defineConfig({
chains: {
polkadot: {
descriptor: polkadot,
provider: getSmProvider(
import("polkadot-api/chains/polkadot").then(({ chainSpec }) =>
smoldot.addChain({ chainSpec }),
),
),
provider: polkadotProvider,
},
polkadot_asset_hub: {
descriptor: polkadot_asset_hub,
provider: polkadotProvider.addParachain({ id: "polkadot_asset_hub" }),
},
polkadot_people: {
descriptor: polkadot_people,
provider: polkadotProvider.addParachain({ id: "polkadot_people" }),
},
kusama: {
descriptor: kusama,
provider: getSmProvider(
import("polkadot-api/chains/ksmcc3").then(({ chainSpec }) =>
smoldot.addChain({ chainSpec }),
),
),
provider: lightClientProvider.addRelayChain({ id: "kusama" }),
},
westend: {
descriptor: westend,
provider: getSmProvider(
import("polkadot-api/chains/westend2").then(({ chainSpec }) =>
smoldot.addChain({ chainSpec }),
),
),
provider: lightClientProvider.addRelayChain({ id: "westend" }),
},
},
wallets: [
new InjectedWalletProvider({ originName: "ReactiveDOT Vue Example" }),
new InjectedWalletProvider({ originName: "ReactiveDOT React Example" }),
new LedgerWallet(),
new WalletConnect({
projectId: "68f5b7e972a51cf379b127f51a791c34",
providerOptions: {
metadata: {
name: "ReactiveDOT example",
description: "Simple React App showcasing ReactiveDOT",
url: globalThis.location.origin,
icons: ["https://walletconnect.com/walletconnect-logo.png"],
},
},
chainIds: [
"polkadot:91b171bb158e2d3848fa23a9f1c25182", // Polkadot
"polkadot:b0a8d493285c2df73290dfb7e61f870f", // Kusama
"polkadot:e143f23803ac50e8f6f8e62695d1ce9e", // Westend
],
}),
],
});
4 changes: 3 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
],
"exports": {
".": "./build/index.js",
"./providers/light-client.js": "./build/providers/light-client/index.js",
"./wallets.js": "./build/wallets/index.js",
"./internal/maths.js": "./build/maths/index.js",
"./internal.js": "./build/internal.js"
Expand All @@ -36,7 +37,8 @@
"test": "vitest run"
},
"dependencies": {
"@reactive-dot/utils": "workspace:^"
"@reactive-dot/utils": "workspace:^",
"@substrate/smoldot-discovery": "^2.0.1"
},
"devDependencies": {
"@reactive-dot/eslint-config": "workspace:^",
Expand Down
24 changes: 20 additions & 4 deletions packages/core/src/actions/get-client.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import type { ChainConfig } from "../config.js";
import {
createClientFromLightClientProvider,
isLightClientProvider,
type LightClientProvider,
} from "../providers/light-client/provider.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;

if (isLightClientProvider(providerOrGetter)) {
return createClientFromLightClientProvider(providerOrGetter);
}

// Hack to detect wether function is a `JsonRpcProvider` or a getter of `JsonRpcProvider`
const provider = await (providerOrGetter.length > 0
? (providerOrGetter as JsonRpcProvider)
const providerOrController = await (providerOrGetter.length > 0
? (providerOrGetter as JsonRpcProvider | LightClientProvider)
: (
providerOrGetter as Exclude<typeof providerOrGetter, JsonRpcProvider>
providerOrGetter as Exclude<
typeof providerOrGetter,
JsonRpcProvider | LightClientProvider
>
)());

return createClient(provider);
if (isLightClientProvider(providerOrController)) {
return createClientFromLightClientProvider(providerOrController);
}

return createClient(providerOrController);
}
3 changes: 2 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { LightClientProvider } from "./providers/light-client/provider.js";
import type { Gettable } from "./types.js";
import type { Wallet, WalletProvider } from "./wallets/index.js";
import type { JsonRpcProvider } from "@polkadot-api/json-rpc-provider";
import type { ChainDefinition } from "polkadot-api";

export type ChainConfig = {
readonly descriptor: ChainDefinition;
readonly provider: Gettable<JsonRpcProvider>;
readonly provider: Gettable<JsonRpcProvider | LightClientProvider>;
};

export type Config<
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/providers/light-client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { createLightClientProvider } from "./provider.js";
Loading