@sodax/wallet-sdk-react 2.0.0-rc.10 → 2.0.0-rc.11

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.
@@ -1,226 +0,0 @@
1
- # Wallet Provider Bridge
2
-
3
- `@sodax/wallet-sdk-react` is the bridge between the connected browser wallet and the chain-agnostic SDK call surface in `@sodax/sdk`. After a user connects a wallet, `useWalletProvider` returns a typed `IXxxWalletProvider` (from `@sodax/sdk`) that you pass directly into any SDK method — the SDK signs and broadcasts via that provider.
4
-
5
- The canonical interfaces are defined in [`@sodax/sdk`](https://github.com/icon-project/sodax-sdks/blob/main/packages/sdk/src/index.ts) (`IEvmWalletProvider`, `ISolanaWalletProvider`, …) and re-exported through `@sodax/wallet-sdk-core`.
6
-
7
- ## Table of contents
8
-
9
- 1. [Why a bridge layer](#why-a-bridge-layer)
10
- 2. [`useWalletProvider` — typed provider for one chain](#usewalletprovider--typed-provider-for-one-chain)
11
- 3. [End-to-end example](#end-to-end-example)
12
- 4. [Service hooks (`useXService` / `useXServices`)](#service-hooks-usexservice--usexservices)
13
- 5. [How wallet providers are populated](#how-wallet-providers-are-populated)
14
- 6. [Disabled chains return `undefined`](#disabled-chains-return-undefined)
15
- 7. [Bypassing the bridge — when to skip `useWalletProvider`](#bypassing-the-bridge--when-to-skip-usewalletprovider)
16
-
17
- ---
18
-
19
- ## Why a bridge layer
20
-
21
- `@sodax/sdk` is wallet-library-agnostic — it accepts any object implementing the per-chain `IXxxWalletProvider` interface (see [`packages/sdk/docs/WALLET_PROVIDERS.md`](https://github.com/icon-project/sodax-sdks/blob/main/packages/sdk/docs/WALLET_PROVIDERS.md)). The implementation can come from:
22
-
23
- - **Browser dApp** — `@sodax/wallet-sdk-react` Hydrators wrap `wagmi` / `@solana/wallet-adapter` / `@mysten/dapp-kit` etc. into provider instances and store them in the Zustand store. `useWalletProvider` reads them out.
24
- - **Server / script / bot** — `@sodax/wallet-sdk-core` exposes the same provider classes (`EvmWalletProvider`, `SolanaWalletProvider`, …) constructed directly from a private key. No React, no wallet-sdk-react.
25
-
26
- `useWalletProvider` is the React-side bridge. It hides the per-chain construction details and gives you a typed handle that fits the SDK call slot exactly:
27
-
28
- ```typescript
29
- // SDK call shape (signed mode)
30
- sodax.swaps.swap({
31
- params: { srcChainKey: ChainKeys.BSC_MAINNET, /* ... */ },
32
- walletProvider, // must satisfy IEvmWalletProvider for BSC
33
- });
34
- ```
35
-
36
- `useWalletProvider({ xChainId: ChainKeys.BSC_MAINNET })` returns exactly `IEvmWalletProvider | undefined` — TypeScript narrows the return type from the chain key.
37
-
38
- ---
39
-
40
- ## `useWalletProvider` — typed provider for one chain
41
-
42
- Two overloads, mutually exclusive: pass `xChainId` (a `SpokeChainKey`) **or** `xChainType` (a `ChainType`), never both.
43
-
44
- ### By chain id — narrowest typing
45
-
46
- ```typescript
47
- import { useWalletProvider } from '@sodax/wallet-sdk-react';
48
- import { ChainKeys } from '@sodax/types';
49
-
50
- const walletProvider = useWalletProvider({ xChainId: ChainKeys.BSC_MAINNET });
51
- // walletProvider: IEvmWalletProvider | undefined
52
- ```
53
-
54
- The chain id (`BSC_MAINNET`, `SOLANA_MAINNET`, etc.) resolves to its family at compile time via `GetChainType<S>` and the return type is the matching `IXxxWalletProvider`.
55
-
56
- ### By chain type — family-level typing
57
-
58
- ```typescript
59
- const evmProvider = useWalletProvider({ xChainType: 'EVM' });
60
- // evmProvider: IEvmWalletProvider | undefined
61
-
62
- const solProvider = useWalletProvider({ xChainType: 'SOLANA' });
63
- // solProvider: ISolanaWalletProvider | undefined
64
- ```
65
-
66
- Use this when the surrounding component is family-level (e.g. an EVM dashboard that doesn't care which specific EVM chain is active). For EVM specifically, one wagmi connection covers all configured EVM networks — the same provider is returned for every `xChainId` in the EVM family.
67
-
68
- ### No-arg form
69
-
70
- ```typescript
71
- const wp = useWalletProvider();
72
- // wp: undefined — no chain specified
73
- ```
74
-
75
- Both fields are optional but at least one must be set for the hook to return anything.
76
-
77
- ---
78
-
79
- ## End-to-end example
80
-
81
- Connect once, then drive an SDK call with the resulting provider:
82
-
83
- ```tsx
84
- import { useWalletProvider, useXAccount } from '@sodax/wallet-sdk-react';
85
- import { useSodaxContext } from '@sodax/dapp-kit'; // or hold a Sodax instance directly
86
- import { ChainKeys } from '@sodax/types';
87
- import type { CreateIntentParams } from '@sodax/sdk';
88
-
89
- function SwapButton({ params }: { params: CreateIntentParams<typeof ChainKeys.BSC_MAINNET> }) {
90
- const walletProvider = useWalletProvider({ xChainId: ChainKeys.BSC_MAINNET });
91
- const account = useXAccount({ xChainId: ChainKeys.BSC_MAINNET });
92
- const { sodax } = useSodaxContext();
93
-
94
- const handleSwap = async () => {
95
- if (!walletProvider) return;
96
- const result = await sodax.swaps.swap({
97
- params,
98
- walletProvider, // typed as IEvmWalletProvider — matches BSC src chain
99
- });
100
- if (!result.ok) {
101
- console.error('swap failed:', result.error);
102
- return;
103
- }
104
- console.log('swap submitted:', result.value);
105
- };
106
-
107
- return (
108
- <button onClick={handleSwap} disabled={!walletProvider || !account.address}>
109
- Swap
110
- </button>
111
- );
112
- }
113
- ```
114
-
115
- The same pattern works for every SDK feature service — `sodax.bridge.bridge`, `sodax.moneyMarket.supply`, `sodax.staking.stake`, `sodax.dex.deposit`, etc. See [`packages/sdk/docs/`](https://github.com/icon-project/sodax-sdks/blob/main/packages/sdk/docs/) for per-feature method signatures.
116
-
117
- ---
118
-
119
- ## Service hooks (`useXService` / `useXServices`)
120
-
121
- Lower-level than `useWalletProvider` — these expose the chain's `XService` instance, used for:
122
-
123
- - Reading per-chain balances (`xService.getBalance(address, xToken)`)
124
- - Listing connectors (`xService.getXConnectors()`)
125
- - Looking up a connector by id (`xService.getXConnectorById(id)`)
126
- - Custom integrations against the `IXService` contract
127
-
128
- ```typescript
129
- import { useXService, useXServices } from '@sodax/wallet-sdk-react';
130
-
131
- const evmService = useXService({ xChainType: 'EVM' });
132
- // evmService: XService | undefined — chain-specific instance (EvmXService)
133
-
134
- const allServices = useXServices();
135
- // allServices: Partial<Record<ChainType, XService>>
136
- ```
137
-
138
- For typed access, depend on the public [`IXService`](https://github.com/icon-project/sodax-sdks/blob/main/packages/wallet-sdk-react/src/types/interfaces.ts) interface rather than the concrete `XService` class. Concrete classes (`EvmXService`, `BitcoinXService`, …) are not exported from the package barrel; if you need a concrete class for `instanceof`, use the deep-import sub-path — see [`CONNECTORS.md`](https://github.com/icon-project/sodax-sdks/blob/main/packages/wallet-sdk-react/docs/CONNECTORS.md).
139
-
140
- ---
141
-
142
- ## How wallet providers are populated
143
-
144
- The store's `walletProviders` map is populated by two distinct mechanisms depending on chain type:
145
-
146
- ### Provider-managed chains (EVM, Solana, Sui)
147
-
148
- The chain's **Hydrator** is the sole writer. It subscribes to the native SDK hooks (e.g. `useAccount` from wagmi) and writes a fresh `EvmWalletProvider` (from `@sodax/wallet-sdk-core`) into the store every time the underlying client changes:
149
-
150
- ```
151
- wagmi connection observed → EvmHydrator constructs EvmWalletProvider({ walletClient, publicClient })
152
-
153
-
154
- store.walletProviders.EVM = provider
155
- ```
156
-
157
- `useWalletProvider({ xChainType: 'EVM' })` reads that slot — no chain-specific switch case in user code.
158
-
159
- ### Non-provider chains (Bitcoin, ICON, Injective, Stellar, NEAR, Stacks)
160
-
161
- The provider is created as a side-effect of `setXConnection()` inside the store. When `useXConnect` resolves a successful connection, the store's setter constructs the chain-specific provider (`BitcoinWalletProvider`, `IconWalletProvider`, …) and writes it to `walletProviders`.
162
-
163
- In both cases, the bridge layer owns the construction — consumers never call `new EvmWalletProvider(...)` themselves in dApp code.
164
-
165
- ---
166
-
167
- ## Disabled chains return `undefined`
168
-
169
- If `xChainType` resolves to a chain that isn't enabled in `SodaxWalletProvider` config, `useWalletProvider` returns `undefined` and logs a one-time warning per chain:
170
-
171
- ```
172
- [useWalletProvider] chain "BITCOIN" is not enabled in SodaxWalletProvider config.chains — returning undefined
173
- ```
174
-
175
- This is by design — `useWalletProvider` is meant to be called unconditionally inside components, and chains can be disabled without changing the call sites. Always null-check the return value:
176
-
177
- ```typescript
178
- const walletProvider = useWalletProvider({ xChainId: ChainKeys.BITCOIN_MAINNET });
179
- if (!walletProvider) return null; // chain disabled, or no wallet connected for that chain yet
180
- ```
181
-
182
- The warning fires only once per chain per session — repeated disabled-chain reads don't spam the console.
183
-
184
- ---
185
-
186
- ## Bypassing the bridge — when to skip `useWalletProvider`
187
-
188
- Skip the bridge in two cases:
189
-
190
- **Server / Node.js scripts** — no React, no wallet-sdk-react. Use `@sodax/wallet-sdk-core` directly:
191
-
192
- ```typescript
193
- import { Sodax } from '@sodax/sdk';
194
- import { EvmWalletProvider } from '@sodax/wallet-sdk-core';
195
- import { ChainKeys } from '@sodax/types';
196
-
197
- const sodax = new Sodax();
198
- const walletProvider = new EvmWalletProvider({
199
- privateKey: process.env.PRIVATE_KEY!,
200
- chainId: ChainKeys.BSC_MAINNET,
201
- rpcUrl: 'https://bsc-dataseed.binance.org',
202
- });
203
- const result = await sodax.swaps.swap({ params: /* ... */, walletProvider });
204
- ```
205
-
206
- **Raw transactions** — when you only need unsigned transaction data (for gas estimation, manual relay, or external signing). SDK methods that accept `raw: true` don't need a wallet provider:
207
-
208
- ```typescript
209
- const result = await sodax.swaps.createIntent({
210
- params,
211
- raw: true, // no walletProvider — returns unsigned tx
212
- });
213
- ```
214
-
215
- See [`packages/sdk/docs/SWAPS.md`](https://github.com/icon-project/sodax-sdks/blob/main/packages/sdk/docs/SWAPS.md) for the full raw-vs-signed matrix per method.
216
-
217
- ---
218
-
219
- ## Related docs
220
-
221
- - [Configure SodaxWalletProvider](https://github.com/icon-project/sodax-sdks/blob/main/packages/wallet-sdk-react/docs/CONFIGURE_PROVIDER.md) — chain-type slots and per-chain wallet defaults
222
- - [Connect Flow](https://github.com/icon-project/sodax-sdks/blob/main/packages/wallet-sdk-react/docs/CONNECT_FLOW.md) — discover, connect, read, disconnect
223
- - [Connectors](https://github.com/icon-project/sodax-sdks/blob/main/packages/wallet-sdk-react/docs/CONNECTORS.md) — `IXConnector`, deep imports, custom connectors
224
- - [SDK Wallet Providers Reference](https://github.com/icon-project/sodax-sdks/blob/main/packages/sdk/docs/WALLET_PROVIDERS.md) — `IXxxWalletProvider` interfaces, custom implementations
225
- - [SDK Swaps](https://github.com/icon-project/sodax-sdks/blob/main/packages/sdk/docs/SWAPS.md) — example consumer of a wallet provider
226
- - [`@sodax/wallet-sdk-core`](https://github.com/icon-project/sodax-sdks/blob/main/packages/wallet-sdk-core/README.md) — Node-side provider construction