@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,166 @@
|
|
|
1
|
+
# Recipe: Migrate a Single-Chain Connect Button
|
|
2
|
+
|
|
3
|
+
Migrates the most common pattern: one button per chain that lists connectors, lets the user pick one, and shows the connected address. Self-contained — apply this recipe without reading other files.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## When to use this recipe
|
|
8
|
+
|
|
9
|
+
Apply when the user's v1 code uses any of:
|
|
10
|
+
|
|
11
|
+
- `useXConnectors('<chain>')` (positional)
|
|
12
|
+
- `useXAccount('<chain>')` (positional)
|
|
13
|
+
- `useXConnect()` + `useXDisconnect()` for a per-chain button
|
|
14
|
+
|
|
15
|
+
If the user has a multi-chain modal (chain picker → connector picker), use [`multi-chain-modal.md`](./multi-chain-modal.md) instead.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Before (v1)
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
'use client';
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
useXConnectors,
|
|
26
|
+
useXConnect,
|
|
27
|
+
useXAccount,
|
|
28
|
+
useXDisconnect,
|
|
29
|
+
} from '@sodax/wallet-sdk-react';
|
|
30
|
+
|
|
31
|
+
export function EvmConnectButton() {
|
|
32
|
+
const connectors = useXConnectors('EVM');
|
|
33
|
+
const { mutateAsync: connect, isPending } = useXConnect();
|
|
34
|
+
const { address } = useXAccount('EVM');
|
|
35
|
+
const disconnect = useXDisconnect();
|
|
36
|
+
|
|
37
|
+
if (address) {
|
|
38
|
+
return (
|
|
39
|
+
<div>
|
|
40
|
+
Connected: {address}
|
|
41
|
+
<button onClick={() => disconnect('EVM')}>Disconnect</button>
|
|
42
|
+
</div>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<div>
|
|
48
|
+
{connectors.map((connector) => (
|
|
49
|
+
<button
|
|
50
|
+
key={connector.id}
|
|
51
|
+
disabled={isPending}
|
|
52
|
+
onClick={() => connect(connector)}
|
|
53
|
+
>
|
|
54
|
+
Connect with {connector.name}
|
|
55
|
+
</button>
|
|
56
|
+
))}
|
|
57
|
+
</div>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## After (v2)
|
|
65
|
+
|
|
66
|
+
```tsx
|
|
67
|
+
'use client';
|
|
68
|
+
|
|
69
|
+
import {
|
|
70
|
+
useXConnectors,
|
|
71
|
+
useXConnect,
|
|
72
|
+
useXAccount,
|
|
73
|
+
useXDisconnect,
|
|
74
|
+
} from '@sodax/wallet-sdk-react';
|
|
75
|
+
|
|
76
|
+
export function EvmConnectButton() {
|
|
77
|
+
const connectors = useXConnectors({ xChainType: 'EVM' });
|
|
78
|
+
const { mutateAsync: connect, isPending } = useXConnect();
|
|
79
|
+
const { address } = useXAccount({ xChainType: 'EVM' });
|
|
80
|
+
const disconnect = useXDisconnect();
|
|
81
|
+
|
|
82
|
+
if (address) {
|
|
83
|
+
return (
|
|
84
|
+
<div>
|
|
85
|
+
Connected: {address}
|
|
86
|
+
<button onClick={() => disconnect({ xChainType: 'EVM' })}>Disconnect</button>
|
|
87
|
+
</div>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<div>
|
|
93
|
+
{connectors.map((connector) => (
|
|
94
|
+
<button
|
|
95
|
+
key={connector.id}
|
|
96
|
+
disabled={isPending}
|
|
97
|
+
onClick={() => connect(connector)}
|
|
98
|
+
>
|
|
99
|
+
Connect with {connector.name}
|
|
100
|
+
</button>
|
|
101
|
+
))}
|
|
102
|
+
</div>
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## What changed
|
|
110
|
+
|
|
111
|
+
| Line | v1 | v2 |
|
|
112
|
+
|---|---|---|
|
|
113
|
+
| `useXConnectors` | `useXConnectors('EVM')` | `useXConnectors({ xChainType: 'EVM' })` |
|
|
114
|
+
| `useXAccount` | `useXAccount('EVM')` | `useXAccount({ xChainType: 'EVM' })` |
|
|
115
|
+
| `useXConnect` mutation arg | pass `connector` to `mutateAsync` | unchanged |
|
|
116
|
+
| `useXDisconnect` returned callback | `disconnect('EVM')` | `disconnect({ xChainType: 'EVM' })` |
|
|
117
|
+
|
|
118
|
+
The mechanical changes are the **three hook usages** above. The `useXDisconnect` callback shape changed in v2 — see [`../reference/hooks.md`](../reference/hooks.md) § `useXDisconnect`.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## Variations
|
|
123
|
+
|
|
124
|
+
### Per-chain-id (e.g. one connect button per EVM network)
|
|
125
|
+
|
|
126
|
+
If v1 used `useXAccount('0x1.eth')`:
|
|
127
|
+
|
|
128
|
+
```tsx
|
|
129
|
+
// v1 ❌
|
|
130
|
+
const { address } = useXAccount('0x1.eth');
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Use the typed chain key in v2:
|
|
134
|
+
|
|
135
|
+
```tsx
|
|
136
|
+
// v2 ✅
|
|
137
|
+
import { ChainKeys } from '@sodax/types';
|
|
138
|
+
const { address } = useXAccount({ xChainId: ChainKeys.ETHEREUM_MAINNET });
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
> ⚠️ If the v1 button is "connect to Ethereum", remember v2 treats EVM as a **single connection across every configured EVM network**. A per-Ethereum button is rarely what users want — consider replacing with a single "EVM" button + `useEvmSwitchChain` for network switching. See [`../breaking-changes.md`](../breaking-changes.md) §7.
|
|
142
|
+
|
|
143
|
+
### Sui / Solana / etc.
|
|
144
|
+
|
|
145
|
+
Replace `'EVM'` with the chain type the button targets — the migration is mechanical:
|
|
146
|
+
|
|
147
|
+
```tsx
|
|
148
|
+
const connectors = useXConnectors({ xChainType: 'SUI' });
|
|
149
|
+
const { address } = useXAccount({ xChainType: 'SUI' });
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## Verification
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
# 1. Type check
|
|
158
|
+
pnpm checkTs
|
|
159
|
+
|
|
160
|
+
# 2. Confirm no positional hook args remain in this file
|
|
161
|
+
grep -nE "useXAccount\(['\"]" <file>
|
|
162
|
+
grep -nE "useXConnectors\(['\"]" <file>
|
|
163
|
+
# expect empty
|
|
164
|
+
|
|
165
|
+
# 3. Manual — load page, click connect, confirm address renders
|
|
166
|
+
```
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
# Recipe: Migrate a Multi-Chain Wallet Modal
|
|
2
|
+
|
|
3
|
+
Replaces a hand-rolled v1 modal (chain picker → connector picker → connect) with v2's headless `useWalletModal` state machine. Self-contained — apply this recipe without reading other files.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## When to use this recipe
|
|
8
|
+
|
|
9
|
+
Apply when the user's v1 code:
|
|
10
|
+
|
|
11
|
+
- Maintains its own state for "which chain is the user picking"
|
|
12
|
+
- Calls `useXConnectors(<chain>)` per chain in a loop
|
|
13
|
+
- Calls `useXConnect` directly from a click handler
|
|
14
|
+
- Tracks a `connecting` / `success` / `error` state by hand
|
|
15
|
+
|
|
16
|
+
v2 ships these primitives so the consumer focuses only on UI rendering.
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Before (v1, simplified)
|
|
21
|
+
|
|
22
|
+
```diff
|
|
23
|
+
- 'use client';
|
|
24
|
+
-
|
|
25
|
+
- import { useState } from 'react';
|
|
26
|
+
- import {
|
|
27
|
+
- useXConnectors,
|
|
28
|
+
- useXConnect,
|
|
29
|
+
- useXDisconnect,
|
|
30
|
+
- type ChainType,
|
|
31
|
+
- } from '@sodax/wallet-sdk-react';
|
|
32
|
+
-
|
|
33
|
+
- const SUPPORTED_CHAINS: ChainType[] = ['EVM', 'SUI', 'SOLANA', 'ICON'];
|
|
34
|
+
-
|
|
35
|
+
- export function WalletModal({ open, onClose }: { open: boolean; onClose: () => void }) {
|
|
36
|
+
- const [pickedChain, setPickedChain] = useState<ChainType | null>(null);
|
|
37
|
+
- const [connectingId, setConnectingId] = useState<string | null>(null);
|
|
38
|
+
- const [error, setError] = useState<Error | null>(null);
|
|
39
|
+
-
|
|
40
|
+
- const evmConnectors = useXConnectors('EVM');
|
|
41
|
+
- const suiConnectors = useXConnectors('SUI');
|
|
42
|
+
- const solanaConnectors = useXConnectors('SOLANA');
|
|
43
|
+
- const iconConnectors = useXConnectors('ICON');
|
|
44
|
+
-
|
|
45
|
+
- const { mutateAsync: connect } = useXConnect();
|
|
46
|
+
- const disconnect = useXDisconnect();
|
|
47
|
+
-
|
|
48
|
+
- const connectorsByChain: Record<ChainType, ReturnType<typeof useXConnectors>> = {
|
|
49
|
+
- EVM: evmConnectors,
|
|
50
|
+
- SUI: suiConnectors,
|
|
51
|
+
- SOLANA: solanaConnectors,
|
|
52
|
+
- ICON: iconConnectors,
|
|
53
|
+
- };
|
|
54
|
+
-
|
|
55
|
+
- if (!open) return null;
|
|
56
|
+
-
|
|
57
|
+
- if (!pickedChain) {
|
|
58
|
+
- return (
|
|
59
|
+
- <Dialog onClose={onClose}>
|
|
60
|
+
- <h2>Select a chain</h2>
|
|
61
|
+
- {SUPPORTED_CHAINS.map((chain) => (
|
|
62
|
+
- <button key={chain} onClick={() => setPickedChain(chain)}>
|
|
63
|
+
- {chain}
|
|
64
|
+
- </button>
|
|
65
|
+
- ))}
|
|
66
|
+
- </Dialog>
|
|
67
|
+
- );
|
|
68
|
+
- }
|
|
69
|
+
-
|
|
70
|
+
- const connectors = connectorsByChain[pickedChain] ?? [];
|
|
71
|
+
-
|
|
72
|
+
- return (
|
|
73
|
+
- <Dialog onClose={onClose}>
|
|
74
|
+
- <button onClick={() => setPickedChain(null)}>← back</button>
|
|
75
|
+
- <h2>Connect to {pickedChain}</h2>
|
|
76
|
+
- {error && <div>Error: {error.message}</div>}
|
|
77
|
+
- {connectors.map((connector) => (
|
|
78
|
+
- <button
|
|
79
|
+
- key={connector.id}
|
|
80
|
+
- disabled={connectingId !== null}
|
|
81
|
+
- onClick={async () => {
|
|
82
|
+
- setConnectingId(connector.id);
|
|
83
|
+
- setError(null);
|
|
84
|
+
- try {
|
|
85
|
+
- await connect(connector);
|
|
86
|
+
- onClose();
|
|
87
|
+
- } catch (err) {
|
|
88
|
+
- setError(err as Error);
|
|
89
|
+
- } finally {
|
|
90
|
+
- setConnectingId(null);
|
|
91
|
+
- }
|
|
92
|
+
- }}
|
|
93
|
+
- >
|
|
94
|
+
- {connector.name}
|
|
95
|
+
- {connectingId === connector.id && ' (connecting...)'}
|
|
96
|
+
- </button>
|
|
97
|
+
- ))}
|
|
98
|
+
- </Dialog>
|
|
99
|
+
- );
|
|
100
|
+
- }
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
(Most v1 modals look like this — manual chain list, manual loop over connectors, manual try/catch for state.)
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## After (v2)
|
|
108
|
+
|
|
109
|
+
```tsx
|
|
110
|
+
'use client';
|
|
111
|
+
|
|
112
|
+
import {
|
|
113
|
+
useWalletModal,
|
|
114
|
+
useChainGroups,
|
|
115
|
+
sortConnectors,
|
|
116
|
+
} from '@sodax/wallet-sdk-react';
|
|
117
|
+
|
|
118
|
+
export function WalletModal({ open, onClose }: { open: boolean; onClose: () => void }) {
|
|
119
|
+
const modal = useWalletModal({
|
|
120
|
+
onConnected: () => onClose(),
|
|
121
|
+
});
|
|
122
|
+
const chainGroups = useChainGroups();
|
|
123
|
+
|
|
124
|
+
if (!open) return null;
|
|
125
|
+
|
|
126
|
+
switch (modal.state.kind) {
|
|
127
|
+
case 'closed':
|
|
128
|
+
case 'chainSelect':
|
|
129
|
+
return (
|
|
130
|
+
<Dialog onClose={onClose}>
|
|
131
|
+
<h2>Select a chain</h2>
|
|
132
|
+
{chainGroups.map((group) => (
|
|
133
|
+
<button
|
|
134
|
+
key={group.id}
|
|
135
|
+
onClick={() => modal.openChain(group.xChainType)}
|
|
136
|
+
>
|
|
137
|
+
{group.label}
|
|
138
|
+
</button>
|
|
139
|
+
))}
|
|
140
|
+
</Dialog>
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
case 'walletSelect': {
|
|
144
|
+
const sorted = sortConnectors(modal.state.connectors, {
|
|
145
|
+
preferred: ['MetaMask', 'Phantom'],
|
|
146
|
+
});
|
|
147
|
+
return (
|
|
148
|
+
<Dialog onClose={onClose}>
|
|
149
|
+
<button onClick={() => modal.back()}>← back</button>
|
|
150
|
+
<h2>Connect to {modal.state.xChainType}</h2>
|
|
151
|
+
{sorted.map((connector) => (
|
|
152
|
+
<button
|
|
153
|
+
key={connector.id}
|
|
154
|
+
disabled={!connector.isInstalled}
|
|
155
|
+
onClick={() => modal.connect(connector)}
|
|
156
|
+
>
|
|
157
|
+
{connector.name}
|
|
158
|
+
{!connector.isInstalled && (
|
|
159
|
+
<a href={connector.installUrl} target="_blank" rel="noreferrer">
|
|
160
|
+
Install
|
|
161
|
+
</a>
|
|
162
|
+
)}
|
|
163
|
+
</button>
|
|
164
|
+
))}
|
|
165
|
+
</Dialog>
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
case 'connecting':
|
|
170
|
+
return (
|
|
171
|
+
<Dialog onClose={onClose}>
|
|
172
|
+
<p>Connecting to {modal.state.connector.name}…</p>
|
|
173
|
+
</Dialog>
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
case 'error':
|
|
177
|
+
return (
|
|
178
|
+
<Dialog onClose={onClose}>
|
|
179
|
+
<p>Error: {modal.state.error.message}</p>
|
|
180
|
+
<button onClick={() => modal.retry()}>Retry</button>
|
|
181
|
+
</Dialog>
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
case 'success':
|
|
185
|
+
return null; // onConnected handler closes
|
|
186
|
+
|
|
187
|
+
default:
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## What changed
|
|
196
|
+
|
|
197
|
+
| Concern | v1 (manual) | v2 (primitive) |
|
|
198
|
+
|---|---|---|
|
|
199
|
+
| Track which chain is picked | `useState<ChainType \| null>` | `useWalletModal().state.kind === 'walletSelect'` |
|
|
200
|
+
| List enabled chains | hardcoded `SUPPORTED_CHAINS` | `useChainGroups()` (driven by `walletConfig`) |
|
|
201
|
+
| List connectors per chain | one `useXConnectors` per chain | `modal.state.connectors` when `kind === 'walletSelect'` |
|
|
202
|
+
| Connecting / error state | `useState` for `connectingId`, `error` | `modal.state.kind` with discriminated union |
|
|
203
|
+
| Retry on error | re-trigger click manually | `modal.retry()` |
|
|
204
|
+
| Filter to installed wallets | n/a | `connector.isInstalled` + `connector.installUrl` |
|
|
205
|
+
| Sort connectors | manual `array.sort` | `sortConnectors(xs, { preferred })` |
|
|
206
|
+
| Close on success | manual `onClose()` after `connect` | `useWalletModal({ onConnected })` callback |
|
|
207
|
+
| EVM treated as one | per-network confusion | `useChainGroups` collapses EVM into one row |
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
## Migration steps
|
|
212
|
+
|
|
213
|
+
1. **Find the v1 modal file.** Search for `useState<ChainType` or multiple `useXConnectors` calls in the same component.
|
|
214
|
+
2. **Replace state-tracking with `useWalletModal`.** Remove `useState` for picked chain, connectingId, error.
|
|
215
|
+
3. **Replace hardcoded chain list with `useChainGroups`.** Use the `xChainType` field on each group entry.
|
|
216
|
+
4. **Switch on `modal.state.kind`.** Render branches: `'closed'` / `'chainSelect'` / `'walletSelect'` / `'connecting'` / `'error'` / `'success'`.
|
|
217
|
+
5. **Use connector metadata.** `connector.isInstalled` / `connector.installUrl` for install CTA. `sortConnectors` to rank installed wallets first.
|
|
218
|
+
6. **Keep your existing `<Dialog>` primitive.** v2 is **headless** — bring your own UI components.
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
## Verification
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
# 1. Type check
|
|
226
|
+
pnpm checkTs
|
|
227
|
+
|
|
228
|
+
# 2. Confirm no manual state is tracking modal flow
|
|
229
|
+
grep -nE "useState<ChainType" <user-modal-file>
|
|
230
|
+
grep -nE "useState.*connectingId|useState.*pickedChain" <user-modal-file>
|
|
231
|
+
# expect empty
|
|
232
|
+
|
|
233
|
+
# 3. Manual — open modal, walk through chain → wallet → connecting → success
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
## Edge cases
|
|
239
|
+
|
|
240
|
+
- **Modal opens on a specific chain (skip chain select).** Call `modal.openChain('EVM')` directly in your trigger button instead of going through chain select. The state machine starts at `'walletSelect'` for that chain.
|
|
241
|
+
- **App needs to know which chain the user picked even before connecting.** Read `modal.state.xChainType` inside the `'walletSelect'` / `'connecting'` branches.
|
|
242
|
+
- **Multiple modal trigger buttons (e.g. chain-specific CTAs).** Reuse one `useWalletModal` instance — do not create one per button.
|
|
243
|
+
|
|
244
|
+
For a full integration walkthrough (without v1 baggage), see [`../../integration/recipes/multi-chain-modal.md`](../../integration/recipes/multi-chain-modal.md).
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# Recipe: Migrate Next.js SSR Setup
|
|
2
|
+
|
|
3
|
+
Migrates the SSR-specific provider setup. Both `initialState` and `reconnectOnMount` move **into the `EVM` slot** of the new `config` prop — they are not removed in v2. Self-contained — apply this recipe without reading other files.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## When to use this recipe
|
|
8
|
+
|
|
9
|
+
Apply when the user's v1 code:
|
|
10
|
+
|
|
11
|
+
- Passes `initialState={...}` to `SodaxWalletProvider`
|
|
12
|
+
- Has a server component or page that calls `cookieToInitialState` (wagmi)
|
|
13
|
+
- Sets `options.wagmi.ssr: true` and/or `options.wagmi.reconnectOnMount`
|
|
14
|
+
|
|
15
|
+
If the project is **not** Next.js (Vite, CRA), only the prop-shape rewrite applies; `initialState` plumbing is not needed.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Before (v1)
|
|
20
|
+
|
|
21
|
+
```diff
|
|
22
|
+
- // app/layout.tsx — v1 ❌
|
|
23
|
+
- import { headers } from 'next/headers';
|
|
24
|
+
- import { cookieToInitialState } from 'wagmi';
|
|
25
|
+
- import { SodaxWalletProvider } from '@sodax/wallet-sdk-react';
|
|
26
|
+
- import { createWagmiConfig } from '@sodax/wallet-sdk-react'; // hypothetical helper consumers used
|
|
27
|
+
-
|
|
28
|
+
- const rpcConfig = {
|
|
29
|
+
- 'sonic': 'https://rpc.soniclabs.com',
|
|
30
|
+
- '0x1.eth': 'https://ethereum-rpc.publicnode.com',
|
|
31
|
+
- };
|
|
32
|
+
-
|
|
33
|
+
- export default async function RootLayout({ children }: { children: React.ReactNode }) {
|
|
34
|
+
- const headersList = await headers();
|
|
35
|
+
- const cookie = headersList.get('cookie');
|
|
36
|
+
-
|
|
37
|
+
- // Build wagmi config the same way the package built it internally to derive initialState
|
|
38
|
+
- const wagmiConfig = createWagmiConfig(rpcConfig, { ssr: true });
|
|
39
|
+
- const initialState = cookieToInitialState(wagmiConfig, cookie);
|
|
40
|
+
-
|
|
41
|
+
- return (
|
|
42
|
+
- <html>
|
|
43
|
+
- <body>
|
|
44
|
+
- <SodaxWalletProvider
|
|
45
|
+
- rpcConfig={rpcConfig}
|
|
46
|
+
- options={{ wagmi: { ssr: true, reconnectOnMount: true } }}
|
|
47
|
+
- initialState={initialState}
|
|
48
|
+
- >
|
|
49
|
+
- {children}
|
|
50
|
+
- </SodaxWalletProvider>
|
|
51
|
+
- </body>
|
|
52
|
+
- </html>
|
|
53
|
+
- );
|
|
54
|
+
- }
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## After (v2)
|
|
60
|
+
|
|
61
|
+
```tsx
|
|
62
|
+
// app/layout.tsx — v2 ✅
|
|
63
|
+
import { headers } from 'next/headers';
|
|
64
|
+
import { cookieToInitialState } from 'wagmi';
|
|
65
|
+
import { ChainKeys } from '@sodax/types';
|
|
66
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
67
|
+
import { SodaxWalletProvider, type SodaxWalletConfig } from '@sodax/wallet-sdk-react';
|
|
68
|
+
import { createWagmiConfig } from '@sodax/wallet-sdk-react/xchains/evm';
|
|
69
|
+
|
|
70
|
+
const walletConfig: SodaxWalletConfig = {
|
|
71
|
+
EVM: {
|
|
72
|
+
ssr: true,
|
|
73
|
+
reconnectOnMount: true,
|
|
74
|
+
chains: {
|
|
75
|
+
[ChainKeys.SONIC_MAINNET]: { rpcUrl: 'https://rpc.soniclabs.com' },
|
|
76
|
+
[ChainKeys.ETHEREUM_MAINNET]: { rpcUrl: 'https://ethereum-rpc.publicnode.com' },
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
// Add other chain slots as needed
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const queryClient = new QueryClient();
|
|
83
|
+
|
|
84
|
+
export default async function RootLayout({ children }: { children: React.ReactNode }) {
|
|
85
|
+
const cookie = (await headers()).get('cookie');
|
|
86
|
+
// Re-derive initialState from cookies to avoid disconnect-flash on first render.
|
|
87
|
+
const wagmiConfig = createWagmiConfig(walletConfig.EVM!);
|
|
88
|
+
const initialState = cookieToInitialState(wagmiConfig, cookie);
|
|
89
|
+
|
|
90
|
+
const evmConfig = { ...walletConfig.EVM!, initialState };
|
|
91
|
+
const config: SodaxWalletConfig = { ...walletConfig, EVM: evmConfig };
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<html>
|
|
95
|
+
<body>
|
|
96
|
+
<QueryClientProvider client={queryClient}>
|
|
97
|
+
<SodaxWalletProvider config={config}>{children}</SodaxWalletProvider>
|
|
98
|
+
</QueryClientProvider>
|
|
99
|
+
</body>
|
|
100
|
+
</html>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
If you don't need to avoid the first-render disconnect flash, drop the cookie / `initialState` plumbing entirely — `EVM.ssr: true` alone is enough for the typical SSR app.
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## What changed
|
|
110
|
+
|
|
111
|
+
| Concern | v1 | v2 |
|
|
112
|
+
|---|---|---|
|
|
113
|
+
| `rpcConfig` flat dict | top-level prop | nested under `EVM.chains[<ChainKey>].rpcUrl` |
|
|
114
|
+
| `options.wagmi.ssr` | nested under `options.wagmi` | `EVM.ssr: true` |
|
|
115
|
+
| `options.wagmi.reconnectOnMount` | nested under `options.wagmi` | `EVM.reconnectOnMount` (still supported, default `false`) |
|
|
116
|
+
| `initialState` | top-level prop | `EVM.initialState` (still supported — pass `cookieToInitialState(...)` for Next.js cookie hydration) |
|
|
117
|
+
| `QueryClient` | created internally by `SodaxWalletProvider` | created by caller, wrapped with `QueryClientProvider` |
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Migration steps
|
|
122
|
+
|
|
123
|
+
1. **Move per-chain RPC URLs.** Move `rpcConfig['sonic']` etc. into `EVM.chains[ChainKeys.SONIC_MAINNET].rpcUrl`. Use `ChainKeys` constants from `@sodax/types`.
|
|
124
|
+
2. **Collapse `options.wagmi.*` into `EVM.*`.** `options.wagmi.ssr` → `EVM.ssr`, `options.wagmi.reconnectOnMount` → `EVM.reconnectOnMount`.
|
|
125
|
+
3. **Move `initialState` into `EVM.initialState`.** v2 still accepts wagmi cookie state; the prop name and location changed, not the feature.
|
|
126
|
+
4. **Add `QueryClientProvider`.** Create a `QueryClient` (top-level module constant — singleton across renders) and wrap `SodaxWalletProvider` with `<QueryClientProvider>`.
|
|
127
|
+
5. **Make sure `@tanstack/react-query` is a direct dep.** Add it if missing:
|
|
128
|
+
```bash
|
|
129
|
+
pnpm add @tanstack/react-query
|
|
130
|
+
```
|
|
131
|
+
6. **Confirm consumer components are `'use client'`.** Hooks (`useXAccount`, `useXConnect`, etc.) only run on the client — any component that calls them needs the directive.
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## Verification
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
# 1. Type check
|
|
139
|
+
pnpm checkTs
|
|
140
|
+
|
|
141
|
+
# 2. No top-level v1 props remain on SodaxWalletProvider
|
|
142
|
+
grep -rnE "SodaxWalletProvider[^>]*\b(rpcConfig|options|initialState)\s*=" <user-src>
|
|
143
|
+
# expect empty
|
|
144
|
+
|
|
145
|
+
# 3. EVM.ssr: true is set
|
|
146
|
+
grep -rnE "EVM:\s*\{[^}]*\bssr:\s*true" <user-src>
|
|
147
|
+
# expect at least one match
|
|
148
|
+
|
|
149
|
+
# 4. QueryClientProvider wraps SodaxWalletProvider
|
|
150
|
+
# (manual — open layout.tsx, confirm nesting)
|
|
151
|
+
|
|
152
|
+
# 5. Manual — pnpm build && pnpm start, load page, confirm no hydration mismatch warnings in console
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## Common pitfalls
|
|
158
|
+
|
|
159
|
+
- **Forgetting `'use client'` on consumer components.** If you call `useXAccount` / `useXConnect` in a Server Component, you'll get "useEffect cannot be called from a server component". Wrap the consumer in a client component.
|
|
160
|
+
- **Creating `QueryClient` inside the layout component.** Each render creates a new client and React Query loses its cache. Define `queryClient` as a **module-level constant**, or use `useState(() => new QueryClient())` inside a client component.
|
|
161
|
+
- **Pages Router project.** This recipe assumes App Router. For Pages Router, mount the providers in `_app.tsx` instead — the API is the same.
|
|
162
|
+
- **Dynamic RPC URLs from env vars.** If `rpcUrl` comes from `process.env.NEXT_PUBLIC_*`, ensure the env var is available at module init time (it is, by Next.js convention). Don't compute the config inside a hook — `SodaxWalletProvider` freezes config on first render, so config must be stable.
|