Skip to content

Commit

Permalink
feat: Support async connect a wallet (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenty22 authored Sep 6, 2024
1 parent b6b08e5 commit 69e6f4e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-pianos-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@node-real/walletkit': patch
---

Support async connect a wallet
3 changes: 1 addition & 2 deletions packages/walletkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
"@solana/wallet-adapter-react": "^0",
"@solana/wallet-adapter-wallets": "^0",
"@solana/web3.js": "^1",
"qrcode": "^1.5.3",
"@twa-dev/sdk": "^7.8.0"
"qrcode": "^1.5.3"
},
"devDependencies": {
"@solana/wallet-adapter-react": "^0.15.35",
Expand Down
34 changes: 28 additions & 6 deletions packages/walletkit/src/evm/hooks/useConnectWallet.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
import { useEvmConnect } from '@/evm/hooks/useEvmConnect';
import { useEvmConnect, UseEvmConnectReturnType } from '@/evm/hooks/useEvmConnect';
import { useWalletKit } from '../../core/providers/WalletKitProvider/context';
import { useIsConnected } from './useIsConnected';

type ConnectOptions = Partial<Parameters<UseEvmConnectReturnType['connect']>[0]> & {
walletId: string;
};

export function useConnectWallet() {
const { log, evmConfig } = useWalletKit();

const { connect, connectors } = useEvmConnect();
const { connect, connectAsync, connectors } = useEvmConnect();
const isConnected = useIsConnected();

return {
connect(options: { walletId: string; initialChainId?: number }) {
const { walletId, initialChainId } = options;
connect(options: ConnectOptions) {
const { walletId, ...restOptions } = options;
const wallet = evmConfig?.wallets.find((item) => item.id === walletId);

if (!wallet) {
log(`wallet not found, walletId: ${walletId}`);
} else {
const connector = connectors.find((item) => item.id === walletId);
if (connector && wallet.isInstalled())
if (connector && wallet.isInstalled() && !isConnected)
connect({
connector,
chainId: initialChainId,
...restOptions,
});
}
},

async connectAsync(options: ConnectOptions) {
const { walletId, ...restOptions } = options;
const wallet = evmConfig?.wallets.find((item) => item.id === walletId);

if (!wallet) {
log(`wallet not found, walletId: ${walletId}`);
} else {
const connector = connectors.find((item) => item.id === walletId);
if (connector && wallet.isInstalled() && !isConnected)
await connectAsync({
connector,
...restOptions,
});
}
},
Expand Down
18 changes: 0 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 69e6f4e

Please sign in to comment.