Skip to content

Commit 7a0550e

Browse files
committed
fix: allow setting up Solana hooks and composables
1 parent dc1f31c commit 7a0550e

File tree

7 files changed

+38
-29
lines changed

7 files changed

+38
-29
lines changed

demo/vue-app-new/src/MainView.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ const configs = computed<Web3AuthContextConfig>(() => {
272272
<div class="flex flex-col items-center justify-center">
273273
<main class="relative flex flex-col lg:h-[calc(100dvh_-_110px)]">
274274
<AppSettings />
275-
<AppDashboard :chains="options.chains || []" />
275+
<AppDashboard />
276276
</main>
277277
</div>
278278
</SolanaProvider>

demo/vue-app-new/src/components/AppDashboard.vue

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
useWeb3AuthUser,
1515
1616
} from "@web3auth/modal/vue";
17-
import { CONNECTOR_INITIAL_AUTHENTICATION_MODE, type CustomChainConfig } from "@web3auth/no-modal";
17+
import { CONNECTOR_INITIAL_AUTHENTICATION_MODE } from "@web3auth/no-modal";
1818
import { useI18n } from "petite-vue-i18n";
1919
2020
import { useSignMessage as useSolanaSignMessage, useSolanaWallet, useSolanaClient } from "@web3auth/modal/vue/solana";
@@ -30,7 +30,7 @@ import {
3030
import { getCapabilities, getCallsStatus, sendCalls, showCallsStatus } from "@wagmi/core";
3131
import { parseEther } from "viem";
3232
import { createWalletTransactionSigner, toAddress } from "@solana/client";
33-
import { address as solanaAddress, } from "@solana/kit";
33+
import { address as solanaAddress } from "@solana/kit";
3434
import { getTransferSolInstruction } from "@solana-program/system";
3535
import { computed, ref, watch } from "vue";
3636
import { getPrivateKey, sendEth, sendEthWithSmartAccount, signTransaction as signEthTransaction } from "../services/ethHandlers";
@@ -40,10 +40,6 @@ const { t } = useI18n({ useScope: "global" });
4040
4141
const formData = formDataStore;
4242
43-
const props = defineProps<{
44-
chains: CustomChainConfig[];
45-
}>();
46-
4743
const { isConnected, connection, web3Auth, isMFAEnabled, isAuthorized } = useWeb3Auth();
4844
const { userInfo, loading: userInfoLoading } = useWeb3AuthUser();
4945
const { enableMFA } = useEnableMFA();
@@ -68,7 +64,7 @@ const balance = useBalance({
6864
const config = useConfig();
6965
const trackedCallsId = ref<string | undefined>();
7066
71-
const { accounts: solanaAccounts, getPrivateKey: getSolanaPrivateKey } = useSolanaWallet();
67+
const { accounts: solanaAccounts, getPrivateKey: getSolanaPrivateKey, solanaWallet } = useSolanaWallet();
7268
const solanaClient = useSolanaClient();
7369
const { signMessage: signSolanaMessage } = useSolanaSignMessage();
7470
@@ -350,7 +346,7 @@ const onSignSolMessage = async () => {
350346
const onGetSolBalance = async () => {
351347
const client = solanaClient.value;
352348
if (!client) throw new Error("Solana client not available");
353-
const account = solanaAccounts.value?.[0];
349+
const account = solanaWallet.value?.accounts[0]?.address;
354350
if (!account) throw new Error("No account connected");
355351
356352
try {
@@ -362,6 +358,20 @@ const onGetSolBalance = async () => {
362358
}
363359
};
364360
361+
const onGetSolChain = async () => {
362+
const w3a = web3Auth.value;
363+
if (!w3a) {
364+
printToConsole("Solana chain", "Web3Auth not initialized");
365+
return;
366+
}
367+
const current = w3a.currentChain;
368+
const chain =
369+
current?.chainNamespace === CHAIN_NAMESPACES.SOLANA
370+
? current
371+
: w3a.coreOptions.chains?.find((c) => c.chainNamespace === CHAIN_NAMESPACES.SOLANA);
372+
printToConsole("Solana chain", chain ?? null);
373+
};
374+
365375
const onGetSolPrivateKey = async () => {
366376
try {
367377
const privateKey = await getSolanaPrivateKey();
@@ -372,7 +382,7 @@ const onGetSolPrivateKey = async () => {
372382
};
373383
374384
// EVM-only: wagmi switchChain does not change Solana cluster; only show when multiple EIP-155 chains are configured.
375-
const eip155Chains = computed(() => props.chains.filter((c) => c.chainNamespace === CHAIN_NAMESPACES.EIP155));
385+
const eip155Chains = computed(() => web3Auth.value?.coreOptions.chains?.filter((c) => c.chainNamespace === CHAIN_NAMESPACES.EIP155) || []);
376386
377387
const canSwitchEvmChain = computed(() => {
378388
if (eip155Chains.value.length < 2) return false;
@@ -515,6 +525,7 @@ const onSwitchChain = async () => {
515525
<div class="mb-2 text-xl font-bold leading-tight text-left">Solana Transaction</div>
516526
<Button block size="xs" pill class="mb-2" @click="onGetSolPrivateKey">{{ t("app.buttons.btnGetPrivateKey") }}</Button>
517527
<Button block size="xs" pill class="mb-2" @click="onGetSolBalance">{{ t("app.buttons.btnGetBalance") }}</Button>
528+
<Button block size="xs" pill class="mb-2" @click="onGetSolChain">{{ t("app.buttons.btnGetCurrentSolanaChain") }}</Button>
518529
<Button block size="xs" pill class="mb-2" @click="onSignSolMessage">{{ t("app.buttons.btnSignMessage") }}</Button>
519530
<Button block size="xs" pill class="mb-2" @click="onSignAndSendTransaction">
520531
{{ t("app.buttons.btnSignAndSendTransaction") }}

demo/vue-app-new/src/translations/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@
6666
"btnSendAATx": "Send ETH with Smart Account",
6767
"btnSignEthMessage": "Sign ETH Message",
6868
"btnGetConnectedChainId": "Get Connected Chain ID",
69+
"btnGetCurrentSolanaChain": "Get Current Chain",
6970
"btnAddChain": "Add Chain",
7071
"btnSwitchChain": "Switch Chain",
72+
"btnSwitchToSolana": "Switch to Solana",
73+
"btnSwitchToEvm": "Switch to EVM",
7174
"btnSwitchChainNamespace": "Switch Chain Namespace",
7275
"btnSignAndSendTransaction": "Sign and Send Transaction",
7376
"btnSignTransaction": "Sign Transaction",

packages/modal/src/vue/solana/composables/useSolanaWallet.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { createSolanaRpc, type Rpc, type SolanaRpcApi } from "@solana/kit";
22
import type { Wallet } from "@wallet-standard/base";
3-
import { CHAIN_NAMESPACES, WALLET_CONNECTORS } from "@web3auth/no-modal";
3+
import { WALLET_CONNECTORS } from "@web3auth/no-modal";
44
import { SOLANA_METHOD_TYPES } from "@web3auth/ws-embed";
5-
import { computed, Ref, ref, ShallowRef, shallowRef, watch } from "vue";
5+
import { Ref, ref, ShallowRef, shallowRef, watch } from "vue";
66

7-
import { useChain, useWeb3Auth } from "../../composables";
7+
import { useWeb3Auth } from "../../composables";
88

99
export type IUseSolanaWallet = {
1010
accounts: Ref<string[] | null>;
@@ -27,15 +27,11 @@ export type IUseSolanaWallet = {
2727

2828
export const useSolanaWallet = (): IUseSolanaWallet => {
2929
const { connection, web3Auth } = useWeb3Auth();
30-
const { chainNamespace } = useChain();
3130
const accounts = ref<string[] | null>(null);
3231
const solanaWallet = shallowRef<Wallet | null>(null);
3332
const rpc = shallowRef<Rpc<SolanaRpcApi> | null>(null);
3433

35-
const isSolana = computed(() => chainNamespace.value === CHAIN_NAMESPACES.SOLANA);
36-
3734
const setupWallet = () => {
38-
if (!isSolana.value) return;
3935
const wallet = connection.value?.solanaWallet ?? null;
4036
if (!wallet) return;
4137
solanaWallet.value = wallet;
@@ -65,9 +61,9 @@ export const useSolanaWallet = (): IUseSolanaWallet => {
6561
};
6662

6763
watch(
68-
[connection, chainNamespace],
69-
([newConnection, newChainNamespace]) => {
70-
if (!newConnection?.solanaWallet || newChainNamespace !== CHAIN_NAMESPACES.SOLANA) {
64+
[connection],
65+
([newConnection]) => {
66+
if (!newConnection?.solanaWallet) {
7167
if (solanaWallet.value) resetWallet();
7268
return;
7369
}

packages/modal/src/vue/solana/provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export const SolanaProvider = defineComponent({
4040
return;
4141
}
4242

43-
const chainConfig = web3Auth.value.currentChain;
44-
if (chainConfig.chainNamespace !== CHAIN_NAMESPACES.SOLANA) return;
43+
const chainConfig = web3Auth.value.coreOptions.chains.find((c) => c.chainNamespace === CHAIN_NAMESPACES.SOLANA);
44+
if (!chainConfig) return;
4545

4646
const prevClient = clientRef.value;
4747
try {

packages/no-modal/src/connectors/metamask-connector/metamaskConnector.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,10 @@ class MetaMaskConnector extends BaseConnector<void> {
430430
public async switchChain(params: { chainId: string }, init = false): Promise<void> {
431431
super.checkSwitchChainRequirements(params, init);
432432

433-
const namespaces = new Set(this.coreOptions.chains.map((c) => c.chainNamespace));
434-
if (namespaces.size > 1) {
435-
throw WalletLoginError.unsupportedOperation(
436-
"switchChain is not supported when multiple chain namespaces are configured. Use connection.ethereumProvider and connection.solanaWallet directly."
437-
);
433+
const targetChainConfig = this.coreOptions.chains.find((c) => c.chainId === params.chainId);
434+
if (targetChainConfig?.chainNamespace === CHAIN_NAMESPACES.SOLANA) {
435+
// no need to switch chain for Solana
436+
return;
438437
}
439438

440439
await this.ensureInitialized();

packages/no-modal/src/vue/solana/provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export const SolanaProvider = defineComponent({
4141
return;
4242
}
4343

44-
const chainConfig = web3Auth.value.currentChain;
45-
if (chainConfig.chainNamespace !== CHAIN_NAMESPACES.SOLANA) return;
44+
const chainConfig = web3Auth.value.coreOptions.chains.find((c) => c.chainNamespace === CHAIN_NAMESPACES.SOLANA);
45+
if (!chainConfig) return;
4646

4747
const prevClient = clientRef.value;
4848
try {

0 commit comments

Comments
 (0)