otx-swap 1.2.7 → 1.2.8

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 CHANGED
@@ -30,26 +30,34 @@ npm install react react-dom
30
30
  ## Quick Start
31
31
 
32
32
  ```tsx
33
- import { SwapWidget } from "otx-swap";
34
- import type { ExternalWalletClient } from "otx-swap";
33
+ import { useState, useEffect, useMemo } from "react";
34
+ import { SwapWidget, createEvmWalletClientAsync } from "otx-swap";
35
+ import type { ExternalWalletClient, EvmWalletAccount, Eip1193Provider } from "otx-swap";
35
36
 
36
37
  function App() {
37
- // Build wallet client from your wallet provider
38
- const walletClient: ExternalWalletClient = {
39
- evm: {
40
- address: "0x...",
41
- publicKey: "0x...",
42
- chain: { id: 1, name: "Ethereum", type: "EVM" },
43
- sendTransaction: async (tx) => {
44
- // Your wallet's sendTransaction implementation
45
- return txHash;
46
- },
47
- getChainId: async () => 1,
48
- switchChain: async (chainId) => {
49
- // Your wallet's switchChain implementation
50
- },
51
- },
52
- };
38
+ const [evmWallet, setEvmWallet] = useState<EvmWalletAccount | null>(null);
39
+
40
+ // Create wallet client from EIP-1193 provider (MetaMask, WalletConnect, etc.)
41
+ useEffect(() => {
42
+ const initWallet = async () => {
43
+ if (window.ethereum) {
44
+ try {
45
+ const wallet = await createEvmWalletClientAsync({
46
+ provider: window.ethereum as Eip1193Provider,
47
+ });
48
+ setEvmWallet(wallet);
49
+ } catch (error) {
50
+ console.error("Failed to create wallet:", error);
51
+ }
52
+ }
53
+ };
54
+ initWallet();
55
+ }, []);
56
+
57
+ const walletClient = useMemo((): ExternalWalletClient => {
58
+ if (!evmWallet) return {};
59
+ return { evm: evmWallet };
60
+ }, [evmWallet]);
53
61
 
54
62
  return (
55
63
  <SwapWidget
@@ -234,11 +242,107 @@ interface WalletAccount {
234
242
  }
235
243
  ```
236
244
 
245
+ ### EVM Helpers
246
+
247
+ We provide two helper functions to convert any EIP-1193 provider to the required wallet interface:
248
+
249
+ #### createEvmWalletClientAsync (Recommended)
250
+
251
+ Async version that automatically fetches `address` and `chainId` from the provider if not provided:
252
+
253
+ ```tsx
254
+ import { createEvmWalletClientAsync } from "otx-swap";
255
+ import type { EvmWalletAccount, Eip1193Provider } from "otx-swap";
256
+
257
+ // Simplest usage - just pass the provider
258
+ const evmWallet = await createEvmWalletClientAsync({
259
+ provider: window.ethereum as Eip1193Provider,
260
+ });
261
+
262
+ // Or with optional overrides
263
+ const evmWallet = await createEvmWalletClientAsync({
264
+ provider: window.ethereum as Eip1193Provider,
265
+ chainName: "Ethereum", // Optional
266
+ });
267
+ ```
268
+
269
+ #### createEvmWalletClient (Sync)
270
+
271
+ Sync version that requires all parameters:
272
+
273
+ ```tsx
274
+ import { createEvmWalletClient } from "otx-swap";
275
+ import type { ExternalWalletClient } from "otx-swap";
276
+
277
+ const walletClient: ExternalWalletClient = {
278
+ evm: createEvmWalletClient({
279
+ provider: window.ethereum, // Any EIP-1193 provider
280
+ address: "0x...", // Required
281
+ chainId: 1, // Required
282
+ chainName: "Ethereum", // Optional
283
+ }),
284
+ };
285
+ ```
286
+
237
287
  ### Integration Examples
238
288
 
239
- #### With wagmi (EVM)
289
+ #### With wagmi (EVM) - Recommended
290
+
291
+ Using `createEvmWalletClientAsync` with wagmi's connector provider:
240
292
 
241
293
  ```tsx
294
+ import { useState, useEffect, useMemo } from "react";
295
+ import { useAccount, useChainId } from "wagmi";
296
+ import { SwapWidget, createEvmWalletClientAsync } from "otx-swap";
297
+ import type { ExternalWalletClient, EvmWalletAccount, Eip1193Provider } from "otx-swap";
298
+
299
+ function SwapWithWagmi() {
300
+ const { isConnected, connector } = useAccount();
301
+ const chainId = useChainId();
302
+ const [evmWallet, setEvmWallet] = useState<EvmWalletAccount | null>(null);
303
+
304
+ // Create wallet client from provider (auto-fetches address & chainId)
305
+ useEffect(() => {
306
+ if (connector?.getProvider && isConnected) {
307
+ connector.getProvider().then(async (provider) => {
308
+ try {
309
+ const wallet = await createEvmWalletClientAsync({
310
+ provider: provider as Eip1193Provider,
311
+ });
312
+ setEvmWallet(wallet);
313
+ } catch (error) {
314
+ console.error("Failed to create wallet client:", error);
315
+ setEvmWallet(null);
316
+ }
317
+ });
318
+ } else {
319
+ setEvmWallet(null);
320
+ }
321
+ }, [connector, isConnected, chainId]); // Recreate on chain change
322
+
323
+ const walletClient = useMemo((): ExternalWalletClient => {
324
+ if (!isConnected || !evmWallet) return {};
325
+ return { evm: evmWallet };
326
+ }, [isConnected, evmWallet]);
327
+
328
+ return (
329
+ <SwapWidget
330
+ apiBaseUrl="https://api.optimex.io"
331
+ walletClient={walletClient}
332
+ onRequestWallet={() => {
333
+ // Open your wallet connect modal
334
+ }}
335
+ />
336
+ );
337
+ }
338
+ ```
339
+
340
+ #### With wagmi (EVM) - Using wagmi Hooks
341
+
342
+ Alternative approach using wagmi's transaction hooks instead of the helper function:
343
+
344
+ ```tsx
345
+ import { useMemo } from "react";
242
346
  import { useAccount, useSendTransaction, useChainId, useSwitchChain } from "wagmi";
243
347
  import { SwapWidget } from "otx-swap";
244
348
  import type { ExternalWalletClient, EvmTransactionRequest } from "otx-swap";
@@ -249,25 +353,28 @@ function SwapWithWagmi() {
249
353
  const { sendTransactionAsync } = useSendTransaction();
250
354
  const { switchChainAsync } = useSwitchChain();
251
355
 
252
- const walletClient: ExternalWalletClient = {
253
- evm: isConnected && address ? {
254
- address,
255
- publicKey: address,
256
- chain: { id: chainId, name: "Ethereum", type: "EVM" },
257
- sendTransaction: async (tx: unknown) => {
258
- const evmTx = tx as EvmTransactionRequest;
259
- return sendTransactionAsync({
260
- to: evmTx.to,
261
- value: evmTx.value,
262
- data: evmTx.data,
263
- });
264
- },
265
- getChainId: async () => chainId,
266
- switchChain: async (id) => {
267
- await switchChainAsync({ chainId: id });
356
+ const walletClient = useMemo((): ExternalWalletClient => {
357
+ if (!isConnected || !address) return {};
358
+ return {
359
+ evm: {
360
+ address,
361
+ publicKey: address,
362
+ chain: { id: chainId, name: "Ethereum", type: "EVM" },
363
+ sendTransaction: async (tx: unknown) => {
364
+ const evmTx = tx as EvmTransactionRequest;
365
+ return sendTransactionAsync({
366
+ to: evmTx.to,
367
+ value: evmTx.value,
368
+ data: evmTx.data,
369
+ });
370
+ },
371
+ getChainId: async () => chainId,
372
+ switchChain: async (id) => {
373
+ await switchChainAsync({ chainId: id });
374
+ },
268
375
  },
269
- } : undefined,
270
- };
376
+ };
377
+ }, [isConnected, address, chainId, sendTransactionAsync, switchChainAsync]);
271
378
 
272
379
  return (
273
380
  <SwapWidget
@@ -430,22 +537,42 @@ import { SwapCard, ConnectButton, useWallet } from "otx-swap";
430
537
  Full TypeScript support with exported types:
431
538
 
432
539
  ```tsx
540
+ import {
541
+ // Helper functions
542
+ createEvmWalletClient, // Sync version (requires address & chainId)
543
+ createEvmWalletClientAsync, // Async version (auto-fetches from provider)
544
+ } from "otx-swap";
545
+
433
546
  import type {
547
+ // Props
434
548
  SwapWidgetProps,
549
+
550
+ // Wallet types
435
551
  ExternalWalletClient,
436
552
  WalletAccount,
437
553
  EvmWalletAccount,
554
+ ChainInfo,
555
+ Eip1193Provider,
556
+ CreateEvmWalletClientOptions, // For sync version
557
+ CreateEvmWalletClientAsyncOptions, // For async version
558
+
559
+ // Transaction types
438
560
  EvmTransactionRequest,
439
561
  BitcoinTransactionRequest,
440
- ChainInfo,
562
+
563
+ // Data types
441
564
  Token,
442
565
  Quote,
443
566
  Trade,
444
567
  TradeState,
445
568
  NetworkType,
569
+
570
+ // Theme types
446
571
  ThemeConfig,
447
572
  PartialThemeConfig,
448
573
  ThemeColors,
574
+
575
+ // Callback types
449
576
  QuoteCallbackData,
450
577
  WalletTxCallbackData,
451
578
  SubmitTxCallbackData,