Skip to content

Commit

Permalink
refactor: simplify WalletConnect options
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Jun 13, 2024
1 parent 5b8cdfa commit 220c943
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
18 changes: 5 additions & 13 deletions apps/example/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,11 @@ const config: Config = {
icons: ["https://walletconnect.com/walletconnect-logo.png"],
},
},
connectOptions: {
requiredNamespaces: {
polkadot: {
methods: ["polkadot_signTransaction", "polkadot_signMessage"],
chains: [
"polkadot:91b171bb158e2d3848fa23a9f1c25182", // Polkadot
"polkadot:b0a8d493285c2df73290dfb7e61f870f", // Kusama
"polkadot:e143f23803ac50e8f6f8e62695d1ce9e", // Westend
],
events: ['chainChanged", "accountsChanged'],
},
},
},
chainIds: [
"polkadot:91b171bb158e2d3848fa23a9f1c25182", // Polkadot
"polkadot:b0a8d493285c2df73290dfb7e61f870f", // Kusama
"polkadot:e143f23803ac50e8f6f8e62695d1ce9e", // Westend
],
}),
],
};
Expand Down
39 changes: 31 additions & 8 deletions packages/core/src/wallets/wallet-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
WalletConnectModal,
type WalletConnectModalConfig,
} from "@walletconnect/modal";
import type { ISignClient, SessionTypes } from "@walletconnect/types";
import type { SessionTypes, ISignClient } from "@walletconnect/types";
import {
UniversalProvider,
type IUniversalProvider,
Expand All @@ -24,7 +24,9 @@ export default class WalletConnect extends Wallet {

readonly #modal: WalletConnectModal;

readonly #connectOptions: Parameters<ISignClient["connect"]>[0];
readonly #chainIds: string[];

readonly #optionalChainIds: string[];

readonly #session = new BehaviorSubject<SessionTypes.Struct | undefined>(
undefined,
Expand All @@ -38,7 +40,8 @@ export default class WalletConnect extends Wallet {
projectId?: string;
providerOptions: Omit<UniversalProviderOpts, "projectId">;
modalOptions?: Omit<WalletConnectModalConfig, "projectId">;
connectOptions: Parameters<ISignClient["connect"]>[0];
chainIds: string[];
optionalChainIds?: string[];
}) {
super();

Expand All @@ -47,12 +50,13 @@ export default class WalletConnect extends Wallet {
? options.providerOptions
: { ...options.providerOptions, projectId: options.projectId };

this.#connectOptions = options.connectOptions;

this.#modal = new WalletConnectModal({
...(options.modalOptions ?? {}),
projectId: options.projectId,
});

this.#chainIds = options.chainIds;
this.#optionalChainIds = options.optionalChainIds ?? [];
}

override readonly connected$ = this.#session.pipe(
Expand All @@ -70,9 +74,28 @@ export default class WalletConnect extends Wallet {
throw new ReDotError("Wallet connect provider doesn't have any client");
}

const { uri, approval } = await this.#provider.client.connect(
this.#connectOptions,
);
const connectOptions: Parameters<ISignClient["connect"]>[0] = {
requiredNamespaces: {
polkadot: {
methods: ["polkadot_signTransaction", "polkadot_signMessage"],
chains: this.#chainIds,
events: ['chainChanged", "accountsChanged'],
},
},
};

if (this.#optionalChainIds.length > 0) {
connectOptions.optionalNamespaces = {
polkadot: {
methods: ["polkadot_signTransaction", "polkadot_signMessage"],
chains: this.#optionalChainIds,
events: ['chainChanged", "accountsChanged'],
},
};
}

const { uri, approval } =
await this.#provider.client.connect(connectOptions);

if (uri === undefined) {
throw new ReDotError("Client connection doesn't return any URI");
Expand Down

0 comments on commit 220c943

Please sign in to comment.