@sodax/wallet-sdk-react 1.5.6-beta → 2.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +103 -145
- package/ai-exported/AGENTS.md +122 -0
- package/ai-exported/integration/README.md +102 -0
- package/ai-exported/integration/ai-rules.md +136 -0
- package/ai-exported/integration/architecture.md +181 -0
- package/ai-exported/integration/examples/01-minimal-evm.tsx +75 -0
- package/ai-exported/integration/examples/02-multi-chain-modal.tsx +169 -0
- package/ai-exported/integration/examples/03-nextjs-app-router.tsx +99 -0
- package/ai-exported/integration/examples/04-walletconnect-setup.tsx +89 -0
- package/ai-exported/integration/examples/README.md +29 -0
- package/ai-exported/integration/recipes/batch-operations.md +223 -0
- package/ai-exported/integration/recipes/bridge-to-sdk.md +164 -0
- package/ai-exported/integration/recipes/chain-detection.md +254 -0
- package/ai-exported/integration/recipes/connect-button.md +156 -0
- package/ai-exported/integration/recipes/multi-chain-modal.md +199 -0
- package/ai-exported/integration/recipes/setup.md +158 -0
- package/ai-exported/integration/recipes/sign-message.md +137 -0
- package/ai-exported/integration/recipes/sub-path-imports.md +95 -0
- package/ai-exported/integration/recipes/switch-chain.md +141 -0
- package/ai-exported/integration/recipes/walletconnect-setup.md +139 -0
- package/ai-exported/integration/reference/api-surface.md +175 -0
- package/ai-exported/integration/reference/chain-support.md +78 -0
- package/ai-exported/integration/reference/connectors.md +74 -0
- package/ai-exported/integration/reference/hooks.md +204 -0
- package/ai-exported/integration/reference/wallet-brands.md +106 -0
- package/ai-exported/migration/README.md +49 -0
- package/ai-exported/migration/ai-rules.md +144 -0
- package/ai-exported/migration/breaking-changes.md +305 -0
- package/ai-exported/migration/checklist.md +159 -0
- package/ai-exported/migration/recipes/connect-button.md +166 -0
- package/ai-exported/migration/recipes/multi-chain-modal.md +244 -0
- package/ai-exported/migration/recipes/ssr-setup.md +162 -0
- package/ai-exported/migration/recipes/walletconnect-migration.md +168 -0
- package/ai-exported/migration/reference/components.md +73 -0
- package/ai-exported/migration/reference/config.md +307 -0
- package/ai-exported/migration/reference/hooks.md +278 -0
- package/ai-exported/migration/reference/imports.md +157 -0
- package/dist/XConnector-B9YQTVJ4.d.ts +146 -0
- package/dist/chunk-2BOUGCJ7.mjs +150 -0
- package/dist/chunk-2BOUGCJ7.mjs.map +1 -0
- package/dist/chunk-66BAUK56.mjs +202 -0
- package/dist/chunk-66BAUK56.mjs.map +1 -0
- package/dist/chunk-7ULB6DW4.mjs +102 -0
- package/dist/chunk-7ULB6DW4.mjs.map +1 -0
- package/dist/chunk-BKJB527E.mjs +125 -0
- package/dist/chunk-BKJB527E.mjs.map +1 -0
- package/dist/chunk-BXJLBR4G.mjs +88 -0
- package/dist/chunk-BXJLBR4G.mjs.map +1 -0
- package/dist/chunk-E5IAZ7E6.mjs +186 -0
- package/dist/chunk-E5IAZ7E6.mjs.map +1 -0
- package/dist/chunk-MAQ47Q52.mjs +33 -0
- package/dist/chunk-MAQ47Q52.mjs.map +1 -0
- package/dist/chunk-MXZVF5HR.mjs +34 -0
- package/dist/chunk-MXZVF5HR.mjs.map +1 -0
- package/dist/chunk-N5A2TMF6.mjs +33 -0
- package/dist/chunk-N5A2TMF6.mjs.map +1 -0
- package/dist/chunk-NY7U7OJW.mjs +64 -0
- package/dist/chunk-NY7U7OJW.mjs.map +1 -0
- package/dist/chunk-PJLEJVAU.mjs +140 -0
- package/dist/chunk-PJLEJVAU.mjs.map +1 -0
- package/dist/chunk-PLCA4ZDJ.mjs +1585 -0
- package/dist/chunk-PLCA4ZDJ.mjs.map +1 -0
- package/dist/chunk-TZMKDXFA.mjs +3 -0
- package/dist/chunk-TZMKDXFA.mjs.map +1 -0
- package/dist/chunk-X2MHIWXO.mjs +100 -0
- package/dist/chunk-X2MHIWXO.mjs.map +1 -0
- package/dist/chunk-XZ7CHO2S.mjs +41 -0
- package/dist/chunk-XZ7CHO2S.mjs.map +1 -0
- package/dist/config-OlnzyEUE.d.ts +146 -0
- package/dist/index.cjs +2784 -1594
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +768 -1498
- package/dist/index.mjs +463 -2004
- package/dist/index.mjs.map +1 -1
- package/dist/xchains/bitcoin/index.cjs +1927 -0
- package/dist/xchains/bitcoin/index.cjs.map +1 -0
- package/dist/xchains/bitcoin/index.d.ts +125 -0
- package/dist/xchains/bitcoin/index.mjs +16 -0
- package/dist/xchains/bitcoin/index.mjs.map +1 -0
- package/dist/xchains/evm/index.cjs +316 -0
- package/dist/xchains/evm/index.cjs.map +1 -0
- package/dist/xchains/evm/index.d.ts +39 -0
- package/dist/xchains/evm/index.mjs +5 -0
- package/dist/xchains/evm/index.mjs.map +1 -0
- package/dist/xchains/icon/index.cjs +311 -0
- package/dist/xchains/icon/index.cjs.map +1 -0
- package/dist/xchains/icon/index.d.ts +37 -0
- package/dist/xchains/icon/index.mjs +7 -0
- package/dist/xchains/icon/index.mjs.map +1 -0
- package/dist/xchains/injective/index.cjs +223 -0
- package/dist/xchains/injective/index.cjs.map +1 -0
- package/dist/xchains/injective/index.d.ts +35 -0
- package/dist/xchains/injective/index.mjs +5 -0
- package/dist/xchains/injective/index.mjs.map +1 -0
- package/dist/xchains/near/index.cjs +190 -0
- package/dist/xchains/near/index.cjs.map +1 -0
- package/dist/xchains/near/index.d.ts +34 -0
- package/dist/xchains/near/index.mjs +6 -0
- package/dist/xchains/near/index.mjs.map +1 -0
- package/dist/xchains/solana/index.cjs +186 -0
- package/dist/xchains/solana/index.cjs.map +1 -0
- package/dist/xchains/solana/index.d.ts +26 -0
- package/dist/xchains/solana/index.mjs +7 -0
- package/dist/xchains/solana/index.mjs.map +1 -0
- package/dist/xchains/stacks/index.cjs +240 -0
- package/dist/xchains/stacks/index.cjs.map +1 -0
- package/dist/xchains/stacks/index.d.ts +36 -0
- package/dist/xchains/stacks/index.mjs +5 -0
- package/dist/xchains/stacks/index.mjs.map +1 -0
- package/dist/xchains/stellar/index.cjs +322 -0
- package/dist/xchains/stellar/index.cjs.map +1 -0
- package/dist/xchains/stellar/index.d.ts +44 -0
- package/dist/xchains/stellar/index.mjs +6 -0
- package/dist/xchains/stellar/index.mjs.map +1 -0
- package/dist/xchains/sui/index.cjs +248 -0
- package/dist/xchains/sui/index.cjs.map +1 -0
- package/dist/xchains/sui/index.d.ts +37 -0
- package/dist/xchains/sui/index.mjs +7 -0
- package/dist/xchains/sui/index.mjs.map +1 -0
- package/docs/ADDING_A_NEW_CHAIN.md +440 -0
- package/docs/ARCHITECTURE.md +291 -0
- package/docs/BATCH_OPERATIONS.md +267 -0
- package/docs/CHAIN_DETECTION.md +216 -0
- package/docs/CONFIGURE_PROVIDER.md +360 -0
- package/docs/CONNECTORS.md +247 -0
- package/docs/CONNECT_FLOW.md +276 -0
- package/docs/EVM_SWITCH_CHAIN.md +161 -0
- package/docs/SIGN_MESSAGE.md +213 -0
- package/docs/SUB_PATH_EXPORTS.md +246 -0
- package/docs/WALLETCONNECT.md +154 -0
- package/docs/WALLET_MODAL.md +331 -0
- package/docs/WALLET_PROVIDER_BRIDGE.md +226 -0
- package/package.json +34 -9
- package/skills/SKILLS.md +84 -0
- package/skills/bridge-to-sdk.md +148 -0
- package/skills/connect-button.md +116 -0
- package/skills/evm-only-walletconnect.md +111 -0
- package/skills/multi-chain-modal.md +178 -0
- package/skills/setup.md +107 -0
- package/dist/index.d.cts +0 -1579
- package/src/Hydrate.ts +0 -65
- package/src/SodaxWalletProvider.tsx +0 -97
- package/src/actions/getXChainType.ts +0 -8
- package/src/actions/getXService.ts +0 -33
- package/src/actions/index.ts +0 -2
- package/src/assets/wallets/hana.svg +0 -6
- package/src/assets/wallets/havah.svg +0 -76
- package/src/assets/wallets/keplr.svg +0 -30
- package/src/assets/wallets/metamask.svg +0 -60
- package/src/assets/wallets/phantom.svg +0 -4
- package/src/assets/wallets/sui.svg +0 -20
- package/src/core/XConnector.ts +0 -54
- package/src/core/XService.ts +0 -85
- package/src/core/index.ts +0 -2
- package/src/hooks/index.ts +0 -11
- package/src/hooks/useEthereumChainId.ts +0 -44
- package/src/hooks/useEvmSwitchChain.ts +0 -91
- package/src/hooks/useWalletProvider.ts +0 -206
- package/src/hooks/useXAccount.ts +0 -51
- package/src/hooks/useXAccounts.ts +0 -56
- package/src/hooks/useXBalances.ts +0 -65
- package/src/hooks/useXConnect.ts +0 -118
- package/src/hooks/useXConnection.ts +0 -72
- package/src/hooks/useXConnectors.ts +0 -72
- package/src/hooks/useXDisconnect.ts +0 -73
- package/src/hooks/useXService.ts +0 -8
- package/src/hooks/useXSignMessage.ts +0 -82
- package/src/index.ts +0 -19
- package/src/types/index.ts +0 -22
- package/src/useXWagmiStore.ts +0 -116
- package/src/utils/index.ts +0 -21
- package/src/xchains/bitcoin/BitcoinXConnector.ts +0 -34
- package/src/xchains/bitcoin/BitcoinXService.ts +0 -40
- package/src/xchains/bitcoin/OKXXConnector.ts +0 -117
- package/src/xchains/bitcoin/UnisatXConnector.ts +0 -117
- package/src/xchains/bitcoin/XverseXConnector.ts +0 -232
- package/src/xchains/bitcoin/index.ts +0 -7
- package/src/xchains/bitcoin/useBitcoinXConnectors.ts +0 -14
- package/src/xchains/evm/EvmXConnector.ts +0 -27
- package/src/xchains/evm/EvmXService.ts +0 -211
- package/src/xchains/evm/index.ts +0 -3
- package/src/xchains/icon/IconHanaXConnector.ts +0 -39
- package/src/xchains/icon/IconXService.ts +0 -117
- package/src/xchains/icon/actions.ts +0 -28
- package/src/xchains/icon/iconex/index.tsx +0 -46
- package/src/xchains/icon/index.ts +0 -2
- package/src/xchains/injective/InjectiveXConnector.ts +0 -60
- package/src/xchains/injective/InjectiveXService.ts +0 -62
- package/src/xchains/injective/actions.ts +0 -32
- package/src/xchains/injective/index.ts +0 -2
- package/src/xchains/near/NearXConnector.ts +0 -42
- package/src/xchains/near/NearXService.ts +0 -46
- package/src/xchains/near/useNearXConnectors.ts +0 -23
- package/src/xchains/solana/SolanaXConnector.ts +0 -26
- package/src/xchains/solana/SolanaXService.ts +0 -46
- package/src/xchains/solana/index.ts +0 -2
- package/src/xchains/stacks/StacksXConnector.ts +0 -63
- package/src/xchains/stacks/StacksXService.ts +0 -59
- package/src/xchains/stacks/constants.ts +0 -42
- package/src/xchains/stacks/index.ts +0 -4
- package/src/xchains/stacks/useStacksXConnectors.ts +0 -7
- package/src/xchains/stellar/CustomSorobanServer.ts +0 -93
- package/src/xchains/stellar/StellarWalletsKitXConnector.ts +0 -53
- package/src/xchains/stellar/StellarXService.ts +0 -93
- package/src/xchains/stellar/actions.ts +0 -24
- package/src/xchains/stellar/index.tsx +0 -2
- package/src/xchains/stellar/useStellarXConnectors.ts +0 -21
- package/src/xchains/stellar/utils.ts +0 -49
- package/src/xchains/sui/SuiXConnector.ts +0 -28
- package/src/xchains/sui/SuiXService.ts +0 -66
- package/src/xchains/sui/index.ts +0 -2
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# Recipe: Chain & Wallet Detection
|
|
2
|
+
|
|
3
|
+
Aggregate views over the wallet store — what's enabled, what's connected, what's installed. Use these hooks to render chain pickers, "manage connections" panels, install CTAs, and hydration-safe UIs.
|
|
4
|
+
|
|
5
|
+
**Depends on:** [`setup.md`](./setup.md)
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Hooks at a glance
|
|
10
|
+
|
|
11
|
+
| Hook | Purpose |
|
|
12
|
+
|------|---------|
|
|
13
|
+
| `useEnabledChains()` | List chain types mounted in `SodaxWalletProvider` config |
|
|
14
|
+
| `useChainGroups({ order? })` | One row per enabled chain (with display + connection metadata) — for chain pickers |
|
|
15
|
+
| `useConnectedChains({ order? })` | List of currently-connected chains + hydration `status` — for "manage connections" |
|
|
16
|
+
| `useIsWalletInstalled({ connectors?, chainType? })` | Cross-chain install check — for gating "Install X" CTA |
|
|
17
|
+
|
|
18
|
+
EVM **collapses to a single row** — wagmi maintains one connection across every configured EVM network.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## `useEnabledChains` — what's mounted
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import { useEnabledChains } from '@sodax/wallet-sdk-react';
|
|
26
|
+
|
|
27
|
+
const enabled = useEnabledChains();
|
|
28
|
+
// e.g. ['EVM', 'SOLANA', 'BITCOIN']
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Returns the slot keys in `SodaxWalletConfig` (`config.EVM`, `config.SOLANA`, …), not which chains have a wallet connected. Use cases:
|
|
32
|
+
|
|
33
|
+
- Render only chain rows the dApp opted into.
|
|
34
|
+
- Cross-reference with `useXConnections()` to compute "of N enabled chains, M are connected".
|
|
35
|
+
- Drive `<Tabs>` / `<Select>` UIs without hard-coding a list.
|
|
36
|
+
|
|
37
|
+
`useChainGroups` and `useConnectedChains` already filter by `useEnabledChains` internally — reach for it directly only when you need the raw list.
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## `useChainGroups` — chain picker model
|
|
42
|
+
|
|
43
|
+
One `ChainGroup` per enabled chain type, with display metadata + connection status. Designed for the "select a chain" step in modals.
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
'use client';
|
|
47
|
+
|
|
48
|
+
import { useChainGroups } from '@sodax/wallet-sdk-react';
|
|
49
|
+
import type { ChainType } from '@sodax/types';
|
|
50
|
+
|
|
51
|
+
export function ChainPicker({ onPick }: { onPick: (c: ChainType) => void }) {
|
|
52
|
+
const groups = useChainGroups({ order: ['EVM', 'SOLANA', 'BITCOIN', 'ICON'] });
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<ul>
|
|
56
|
+
{groups.map((group) => (
|
|
57
|
+
<li key={group.chainType}>
|
|
58
|
+
<button onClick={() => onPick(group.chainType)}>
|
|
59
|
+
{group.iconUrl && <img src={group.iconUrl} alt="" width={24} height={24} />}
|
|
60
|
+
<span>{group.displayName}</span>
|
|
61
|
+
{group.isConnected && <span className="badge">Connected</span>}
|
|
62
|
+
</button>
|
|
63
|
+
</li>
|
|
64
|
+
))}
|
|
65
|
+
</ul>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### `ChainGroup` shape
|
|
71
|
+
|
|
72
|
+
| Field | Type | Source |
|
|
73
|
+
|-------|------|--------|
|
|
74
|
+
| `chainType` | `ChainType` | The slot key (`'EVM'`, `'SOLANA'`, …) |
|
|
75
|
+
| `chainIds` | `readonly SpokeChainKey[]` | All chain keys sharing this `chainType` (e.g. all 12 EVM `ChainKeys.*` for `'EVM'`) |
|
|
76
|
+
| `displayName` | `string` | Default per-chain display name |
|
|
77
|
+
| `iconUrl` | `string \| undefined` | `undefined` = SDK doesn't ship one — provide your own |
|
|
78
|
+
| `isConnected` | `boolean` | `true` when an account is connected for this chain |
|
|
79
|
+
| `account` | `XAccount \| undefined` | Connected account |
|
|
80
|
+
| `connectorId` | `string \| undefined` | Active connector id when connected |
|
|
81
|
+
|
|
82
|
+
EVM's `chainIds` lists every configured EVM `ChainKey`, but the group itself is **one row**. Per-network switching belongs to [`switch-chain.md`](./switch-chain.md), not a separate group.
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## `useConnectedChains` — connected list with hydration gate
|
|
87
|
+
|
|
88
|
+
Returns one entry per **currently-connected** chain (skipping the rest), with enriched connector metadata for "manage connections" UIs and status badges.
|
|
89
|
+
|
|
90
|
+
```tsx
|
|
91
|
+
'use client';
|
|
92
|
+
|
|
93
|
+
import { useConnectedChains } from '@sodax/wallet-sdk-react';
|
|
94
|
+
|
|
95
|
+
export function ConnectionList() {
|
|
96
|
+
const { chains, total, status } = useConnectedChains();
|
|
97
|
+
|
|
98
|
+
if (status === 'loading') return <Skeleton />;
|
|
99
|
+
if (total === 0) return <ConnectCta />;
|
|
100
|
+
|
|
101
|
+
return (
|
|
102
|
+
<ul>
|
|
103
|
+
{chains.map((chain) => (
|
|
104
|
+
<li key={chain.chainType}>
|
|
105
|
+
{chain.connectorIcon && <img src={chain.connectorIcon} alt="" />}
|
|
106
|
+
<span>{chain.connectorName ?? chain.connectorId}</span>
|
|
107
|
+
<code>{chain.account.address}</code>
|
|
108
|
+
</li>
|
|
109
|
+
))}
|
|
110
|
+
</ul>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### `ConnectedChain` shape
|
|
116
|
+
|
|
117
|
+
| Field | Type | Notes |
|
|
118
|
+
|-------|------|-------|
|
|
119
|
+
| `chainType` | `ChainType` | |
|
|
120
|
+
| `account` | `XAccount` | Always populated (only included when address is non-empty) |
|
|
121
|
+
| `connectorId` | `string` | The persisted active connector |
|
|
122
|
+
| `connectorName` | `string \| undefined` | Looked up in `xConnectorsByChain` |
|
|
123
|
+
| `connectorIcon` | `string \| undefined` | |
|
|
124
|
+
|
|
125
|
+
### Result shape
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
type UseConnectedChainsResult = {
|
|
129
|
+
chains: ConnectedChain[];
|
|
130
|
+
total: number;
|
|
131
|
+
status: 'loading' | 'ready';
|
|
132
|
+
};
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## `useIsWalletInstalled` — install detection
|
|
138
|
+
|
|
139
|
+
Read hook for gating "Connect" buttons on actual installation. Same identifier matching as `useBatchConnect` — see [`batch-operations.md`](./batch-operations.md).
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { useIsWalletInstalled } from '@sodax/wallet-sdk-react';
|
|
143
|
+
|
|
144
|
+
// True if any Hana variant is installed across all enabled chains
|
|
145
|
+
const hasHana = useIsWalletInstalled({ connectors: ['hana'] });
|
|
146
|
+
|
|
147
|
+
// True if any wallet is installed for Bitcoin specifically
|
|
148
|
+
const hasBitcoinWallet = useIsWalletInstalled({ chainType: 'BITCOIN' });
|
|
149
|
+
|
|
150
|
+
// AND filter — Hana specifically on EVM
|
|
151
|
+
const hanaOnEvm = useIsWalletInstalled({ connectors: ['hana'], chainType: 'EVM' });
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
The type union enforces at compile time that **at least one of `connectors` / `chainType`** is present — `useIsWalletInstalled({})` is a type error. `connectors: []` is explicit "match nothing" — returns `false`.
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## Hydration status — gate reload flicker
|
|
159
|
+
|
|
160
|
+
`useConnectedChains` exposes `status: 'loading' | 'ready'`. Use it to avoid the "Connect wallet" → "Connected" flash on page reload while Zustand rehydrates from `localStorage`:
|
|
161
|
+
|
|
162
|
+
```tsx
|
|
163
|
+
const { chains, status } = useConnectedChains();
|
|
164
|
+
|
|
165
|
+
// ❌ Flicker — `chains` is empty for one render before hydration completes
|
|
166
|
+
return chains.length >= 1 ? <Connected /> : <ConnectCta />;
|
|
167
|
+
|
|
168
|
+
// ✅ No flicker — wait for hydration before deciding
|
|
169
|
+
return status === 'loading'
|
|
170
|
+
? <Skeleton />
|
|
171
|
+
: chains.length >= 1 ? <Connected /> : <ConnectCta />;
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
For first-paint correctness in SSR (Next.js), prefer `useConnectedChains.status` over an ad-hoc `useEffect(() => setMounted(true), [])` pattern — it's the official hydration signal.
|
|
175
|
+
|
|
176
|
+
`useChainGroups` does **not** expose this flag — its outputs are stable across hydration because connection-status fields (`isConnected`, `account`) start as `false` / `undefined` and gain values atomically when the persist middleware finishes.
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## Display ordering
|
|
181
|
+
|
|
182
|
+
Both `useChainGroups` and `useConnectedChains` accept an `order?: readonly ChainType[]`:
|
|
183
|
+
|
|
184
|
+
1. Chains in the array render in array order.
|
|
185
|
+
2. Chains **not** in the array fall to the bottom, sorted alphabetically.
|
|
186
|
+
3. Without `order`:
|
|
187
|
+
- `useChainGroups` follows the order of slots in `walletConfig` (config object key order).
|
|
188
|
+
- `useConnectedChains` follows `ChainTypeArr` from `@sodax/types` — stable across reloads.
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
const groups = useChainGroups({ order: ['EVM', 'ICON'] });
|
|
192
|
+
// EVM → ICON → (alphabetical: BITCOIN, INJECTIVE, NEAR, SOLANA, STACKS, STELLAR, SUI)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
For UIs that must not reflow on reload (header chain list, navigation), always pass `order`.
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
## Common patterns
|
|
200
|
+
|
|
201
|
+
### Pattern 1 — header connected-account chip
|
|
202
|
+
|
|
203
|
+
```tsx
|
|
204
|
+
function HeaderAccountChip() {
|
|
205
|
+
const { chains, status } = useConnectedChains();
|
|
206
|
+
|
|
207
|
+
if (status === 'loading') return null;
|
|
208
|
+
if (chains.length === 0) return <ConnectButton />;
|
|
209
|
+
|
|
210
|
+
return <span>{chains.length} chain{chains.length !== 1 ? 's' : ''} connected</span>;
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Pattern 2 — "install Hana" CTA when not installed
|
|
215
|
+
|
|
216
|
+
```tsx
|
|
217
|
+
function HanaCta() {
|
|
218
|
+
const installed = useIsWalletInstalled({ connectors: ['hana'] });
|
|
219
|
+
return installed ? null : (
|
|
220
|
+
<a href="https://hana-wallet.com" target="_blank" rel="noreferrer">
|
|
221
|
+
Install Hana Wallet
|
|
222
|
+
</a>
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Pattern 3 — chain selector in swap form
|
|
228
|
+
|
|
229
|
+
```tsx
|
|
230
|
+
function ChainSelector({ value, onChange }: { value: ChainType; onChange: (c: ChainType) => void }) {
|
|
231
|
+
const groups = useChainGroups({ order: ['EVM', 'SOLANA', 'SUI'] });
|
|
232
|
+
return (
|
|
233
|
+
<select value={value} onChange={(e) => onChange(e.target.value as ChainType)}>
|
|
234
|
+
{groups.map((g) => (
|
|
235
|
+
<option key={g.chainType} value={g.chainType}>
|
|
236
|
+
{g.displayName} {g.isConnected ? '✓' : ''}
|
|
237
|
+
</option>
|
|
238
|
+
))}
|
|
239
|
+
</select>
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
---
|
|
245
|
+
|
|
246
|
+
## Verification
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
# 1. Type check
|
|
250
|
+
pnpm checkTs
|
|
251
|
+
|
|
252
|
+
# 2. Manual — load page on slow 3G, confirm no Connect→Connected flash on reload
|
|
253
|
+
# 3. Manual — uninstall Hana, confirm CTA appears; reinstall, confirm CTA disappears
|
|
254
|
+
```
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# Recipe: Connect Button
|
|
2
|
+
|
|
3
|
+
Single-chain connect/disconnect button — pick a connector, connect, read the account, disconnect. Self-contained.
|
|
4
|
+
|
|
5
|
+
**Depends on:** [`setup.md`](./setup.md)
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Hooks used
|
|
10
|
+
|
|
11
|
+
| Hook | Purpose |
|
|
12
|
+
|------|---------|
|
|
13
|
+
| `useXConnectors({ xChainType })` | List available connectors for the chain family |
|
|
14
|
+
| `useXConnect()` | React Query mutation — `mutate(connector)` |
|
|
15
|
+
| `useXAccount({ xChainType })` | Read connected account (always returns object — `address` is `undefined` when disconnected) |
|
|
16
|
+
| `useXDisconnect()` | Returns `(xChainType) => Promise<void>` |
|
|
17
|
+
| `sortConnectors(list, { preferred })` | Optional — rank installed/preferred wallets first |
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Connect button
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
'use client';
|
|
25
|
+
|
|
26
|
+
import {
|
|
27
|
+
useXConnectors,
|
|
28
|
+
useXConnect,
|
|
29
|
+
useXAccount,
|
|
30
|
+
useXDisconnect,
|
|
31
|
+
sortConnectors,
|
|
32
|
+
type IXConnector,
|
|
33
|
+
} from '@sodax/wallet-sdk-react';
|
|
34
|
+
|
|
35
|
+
const PREFERRED = ['hana', 'metamask'] as const;
|
|
36
|
+
|
|
37
|
+
export function EvmConnectButton() {
|
|
38
|
+
const raw = useXConnectors({ xChainType: 'EVM' });
|
|
39
|
+
const connectors = sortConnectors(raw, { preferred: PREFERRED });
|
|
40
|
+
const { mutateAsync: connect, isPending, error } = useXConnect();
|
|
41
|
+
const account = useXAccount({ xChainType: 'EVM' });
|
|
42
|
+
const disconnect = useXDisconnect();
|
|
43
|
+
|
|
44
|
+
if (account.address) {
|
|
45
|
+
return (
|
|
46
|
+
<div>
|
|
47
|
+
<code>{account.address}</code>
|
|
48
|
+
<button onClick={() => disconnect({ xChainType: 'EVM' })}>Disconnect</button>
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<div>
|
|
55
|
+
{connectors.map((connector) => (
|
|
56
|
+
<button
|
|
57
|
+
key={connector.id}
|
|
58
|
+
onClick={() => connect(connector).catch(() => {})}
|
|
59
|
+
disabled={isPending}
|
|
60
|
+
>
|
|
61
|
+
{connector.icon && <img src={connector.icon} alt="" width={20} height={20} />}
|
|
62
|
+
{connector.name}
|
|
63
|
+
{!connector.isInstalled && ' (not installed)'}
|
|
64
|
+
</button>
|
|
65
|
+
))}
|
|
66
|
+
{error && <p style={{ color: 'red' }}>{error.message}</p>}
|
|
67
|
+
</div>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Install CTA for missing wallets
|
|
75
|
+
|
|
76
|
+
```tsx
|
|
77
|
+
{connectors.map((connector) =>
|
|
78
|
+
connector.isInstalled ? (
|
|
79
|
+
<button key={connector.id} onClick={() => connect(connector)}>
|
|
80
|
+
{connector.name}
|
|
81
|
+
</button>
|
|
82
|
+
) : (
|
|
83
|
+
<a key={connector.id} href={connector.installUrl} target="_blank" rel="noreferrer">
|
|
84
|
+
Install {connector.name}
|
|
85
|
+
</a>
|
|
86
|
+
),
|
|
87
|
+
)}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
`isInstalled` reads `window.*` at render time (no extra subscription). For batch install detection across wallet brands, use `useIsWalletInstalled` — see [`batch-operations.md`](./batch-operations.md).
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## Caveat — provider-managed chains resolve with `undefined`
|
|
95
|
+
|
|
96
|
+
For EVM, Solana, and Sui, `connect(connector)` resolves with `undefined` because connection state is set asynchronously after the native SDK adapter reports `connected`. Always read the account via `useXAccount`, not the mutation's return value:
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
const { mutateAsync: connect } = useXConnect();
|
|
100
|
+
const account = useXAccount({ xChainType: 'EVM' });
|
|
101
|
+
|
|
102
|
+
await connect(connector); // resolves with undefined for EVM
|
|
103
|
+
// account.address is populated on the next render
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Non-provider chains (Bitcoin, ICON, Injective, Stellar, NEAR, Stacks) return the resolved `XAccount` from `connect()` — but reading via `useXAccount` works for both, so default to it.
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## Multiple chains, one button
|
|
111
|
+
|
|
112
|
+
For a "connect EVM + Solana + Bitcoin in one click" pattern, use [`batch-operations.md`](./batch-operations.md):
|
|
113
|
+
|
|
114
|
+
```tsx
|
|
115
|
+
import { useBatchConnect } from '@sodax/wallet-sdk-react';
|
|
116
|
+
|
|
117
|
+
const { run, status } = useBatchConnect({ connectors: ['hana'] });
|
|
118
|
+
// Connects every chain Hana supports — sequential, errors collected.
|
|
119
|
+
<button onClick={run} disabled={status === 'running'}>
|
|
120
|
+
Connect Hana on all chains
|
|
121
|
+
</button>;
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## Variations
|
|
127
|
+
|
|
128
|
+
### Per-chain-id button (e.g. one button per EVM network)
|
|
129
|
+
|
|
130
|
+
⚠️ Not recommended for EVM — wagmi treats EVM as a **single connection across every configured network**. A per-Ethereum button rarely matches user expectations. If you really need per-chain-id resolution:
|
|
131
|
+
|
|
132
|
+
```tsx
|
|
133
|
+
import { ChainKeys } from '@sodax/types';
|
|
134
|
+
|
|
135
|
+
const account = useXAccount({ xChainId: ChainKeys.ETHEREUM_MAINNET });
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Sui / Solana / etc.
|
|
139
|
+
|
|
140
|
+
Replace `'EVM'` with the chain type the button targets:
|
|
141
|
+
|
|
142
|
+
```tsx
|
|
143
|
+
const connectors = useXConnectors({ xChainType: 'SUI' });
|
|
144
|
+
const { address } = useXAccount({ xChainType: 'SUI' });
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## Verification
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
# 1. Type check
|
|
153
|
+
pnpm checkTs
|
|
154
|
+
|
|
155
|
+
# 2. Manual — load page, click connect, confirm address renders, reload page, confirm connection survives
|
|
156
|
+
```
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# Recipe: Multi-Chain Wallet Modal
|
|
2
|
+
|
|
3
|
+
Headless wallet-connect modal that walks the user through `chainSelect → walletSelect → connecting → success | error`. Pair with `useChainGroups` for the chain picker and `useXConnectors` for the wallet picker. Render-agnostic — works with any dialog/drawer/inline UI.
|
|
4
|
+
|
|
5
|
+
**Depends on:** [`setup.md`](./setup.md)
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Hooks used
|
|
10
|
+
|
|
11
|
+
| Hook | Purpose |
|
|
12
|
+
|------|---------|
|
|
13
|
+
| `useWalletModal({ onConnected? })` | State machine + actions (`open`, `close`, `back`, `selectChain`, `selectWallet`, `retry`) |
|
|
14
|
+
| `useChainGroups({ order? })` | One row per enabled chain family (EVM collapses to one row) |
|
|
15
|
+
| `useXConnectors({ xChainType })` | Wallet list for the chosen chain family |
|
|
16
|
+
| `useXAccount({ xChainType })` | Read the connected account when needed |
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Render switch
|
|
21
|
+
|
|
22
|
+
```tsx
|
|
23
|
+
'use client';
|
|
24
|
+
|
|
25
|
+
import {
|
|
26
|
+
useWalletModal,
|
|
27
|
+
useChainGroups,
|
|
28
|
+
useXConnectors,
|
|
29
|
+
type IXConnector,
|
|
30
|
+
} from '@sodax/wallet-sdk-react';
|
|
31
|
+
|
|
32
|
+
export function WalletModalRoot() {
|
|
33
|
+
const modal = useWalletModal({
|
|
34
|
+
onConnected: async (chainType, account) => {
|
|
35
|
+
console.log('connected', chainType, account.address);
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
switch (modal.state.kind) {
|
|
40
|
+
case 'closed':
|
|
41
|
+
return <button onClick={modal.open}>Connect Wallet</button>;
|
|
42
|
+
|
|
43
|
+
case 'chainSelect':
|
|
44
|
+
return <ChainPicker onPick={modal.selectChain} onClose={modal.close} />;
|
|
45
|
+
|
|
46
|
+
case 'walletSelect':
|
|
47
|
+
return (
|
|
48
|
+
<WalletPicker
|
|
49
|
+
chainType={modal.state.chainType}
|
|
50
|
+
onPick={modal.selectWallet}
|
|
51
|
+
onBack={modal.back}
|
|
52
|
+
onClose={modal.close}
|
|
53
|
+
/>
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
case 'connecting':
|
|
57
|
+
// Hide modal while wagmi's QR modal is up for WalletConnect
|
|
58
|
+
if (modal.state.connector.id === 'walletConnect') return null;
|
|
59
|
+
return (
|
|
60
|
+
<Dialog onClose={modal.close}>
|
|
61
|
+
<p>Approve in {modal.state.connector.name}…</p>
|
|
62
|
+
<button onClick={modal.back}>Cancel</button>
|
|
63
|
+
</Dialog>
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
case 'success':
|
|
67
|
+
// onConnected fired; close after a beat
|
|
68
|
+
setTimeout(modal.close, 0);
|
|
69
|
+
return null;
|
|
70
|
+
|
|
71
|
+
case 'error':
|
|
72
|
+
return (
|
|
73
|
+
<Dialog onClose={modal.close}>
|
|
74
|
+
<p>{modal.state.error.message}</p>
|
|
75
|
+
{!modal.state.connector.isInstalled && modal.state.connector.installUrl && (
|
|
76
|
+
<a href={modal.state.connector.installUrl}>Install {modal.state.connector.name}</a>
|
|
77
|
+
)}
|
|
78
|
+
<button onClick={modal.retry}>Retry</button>
|
|
79
|
+
<button onClick={modal.back}>Pick another wallet</button>
|
|
80
|
+
</Dialog>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Render `<WalletModalRoot />` once at the app root — any other component can dispatch `useWalletModal().open()` to show it.
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Chain picker
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
import { useChainGroups } from '@sodax/wallet-sdk-react';
|
|
94
|
+
import type { ChainType } from '@sodax/types';
|
|
95
|
+
|
|
96
|
+
function ChainPicker({ onPick, onClose }: { onPick: (c: ChainType) => void; onClose: () => void }) {
|
|
97
|
+
const groups = useChainGroups({ order: ['EVM', 'SOLANA', 'BITCOIN', 'ICON'] });
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<Dialog onClose={onClose}>
|
|
101
|
+
<h2>Select a chain</h2>
|
|
102
|
+
{groups.map((group) => (
|
|
103
|
+
<button key={group.chainType} onClick={() => onPick(group.chainType)}>
|
|
104
|
+
{group.iconUrl && <img src={group.iconUrl} alt="" width={24} height={24} />}
|
|
105
|
+
<span>{group.displayName}</span>
|
|
106
|
+
{group.isConnected && <span>Connected</span>}
|
|
107
|
+
</button>
|
|
108
|
+
))}
|
|
109
|
+
</Dialog>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
EVM collapses to a single group covering every configured EVM network — this matches reality (wagmi maintains one connection across all EVM chains).
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## Wallet picker
|
|
119
|
+
|
|
120
|
+
```tsx
|
|
121
|
+
import { useXConnectors, sortConnectors, type IXConnector } from '@sodax/wallet-sdk-react';
|
|
122
|
+
import type { ChainType } from '@sodax/types';
|
|
123
|
+
|
|
124
|
+
function WalletPicker({
|
|
125
|
+
chainType,
|
|
126
|
+
onPick,
|
|
127
|
+
onBack,
|
|
128
|
+
onClose,
|
|
129
|
+
}: {
|
|
130
|
+
chainType: ChainType;
|
|
131
|
+
onPick: (c: IXConnector) => void;
|
|
132
|
+
onBack: () => void;
|
|
133
|
+
onClose: () => void;
|
|
134
|
+
}) {
|
|
135
|
+
const connectors = sortConnectors(useXConnectors({ xChainType: chainType }), {
|
|
136
|
+
preferred: ['hana', 'metamask', 'phantom'],
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
<Dialog onClose={onClose}>
|
|
141
|
+
<button onClick={onBack}>← Back</button>
|
|
142
|
+
<h2>Select a wallet</h2>
|
|
143
|
+
{connectors.map((connector) => (
|
|
144
|
+
<button key={connector.id} onClick={() => onPick(connector)}>
|
|
145
|
+
{connector.icon && <img src={connector.icon} alt="" />}
|
|
146
|
+
{connector.name}
|
|
147
|
+
{!connector.isInstalled && ' (not installed)'}
|
|
148
|
+
</button>
|
|
149
|
+
))}
|
|
150
|
+
</Dialog>
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## Concurrency rules
|
|
158
|
+
|
|
159
|
+
- **Same connector double-click** → returns the same in-flight promise (no double popup).
|
|
160
|
+
- **Different connector mid-attempt** → starts a new attempt; previous one's late resolution is dropped.
|
|
161
|
+
- **`back()` / `close()` mid-attempt** → cancellation guard inside the modal layer; the wallet may still approve in the background but `success`/`error` won't fire. To roll back, call `useXDisconnect()(xChainType)` from the same handler.
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## `onConnected` is non-fatal
|
|
166
|
+
|
|
167
|
+
Throwing inside `onConnected` is logged but **does not** downgrade `success` → `error`. The connection is already persisted; the user is genuinely connected.
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## Non-modal alternative — `useConnectionFlow`
|
|
172
|
+
|
|
173
|
+
For a single-button flow without the multi-step modal:
|
|
174
|
+
|
|
175
|
+
```tsx
|
|
176
|
+
import { useConnectionFlow } from '@sodax/wallet-sdk-react';
|
|
177
|
+
|
|
178
|
+
const { status, error, connect, retry, activeConnector } = useConnectionFlow();
|
|
179
|
+
|
|
180
|
+
return (
|
|
181
|
+
<button onClick={() => connect(connector)} disabled={status === 'connecting'}>
|
|
182
|
+
{status === 'connecting' ? 'Waiting…' : 'Connect'}
|
|
183
|
+
</button>
|
|
184
|
+
);
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
`connect()` and `retry()` never throw — errors flow into `error` state.
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## Verification
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
# 1. Type check
|
|
195
|
+
pnpm checkTs
|
|
196
|
+
|
|
197
|
+
# 2. Manual — open modal, walk through chain → wallet → connecting → success → close
|
|
198
|
+
# 3. Manual — pick non-installed wallet, confirm install link appears in error state
|
|
199
|
+
```
|