@movebridge/react 0.0.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/dist/index.mjs ADDED
@@ -0,0 +1,703 @@
1
+ // src/context.tsx
2
+ import { createContext, useContext, useState, useEffect, useCallback } from "react";
3
+ import {
4
+ Movement,
5
+ MovementError
6
+ } from "@movebridge/core";
7
+ import { jsx } from "react/jsx-runtime";
8
+ var MovementContext = createContext(null);
9
+ function MovementProvider({
10
+ network,
11
+ autoConnect = false,
12
+ onError,
13
+ children
14
+ }) {
15
+ const [movement, setMovement] = useState(null);
16
+ const [walletState, setWalletState] = useState({
17
+ connected: false,
18
+ address: null,
19
+ publicKey: null
20
+ });
21
+ const [connecting, setConnecting] = useState(false);
22
+ const [wallets, setWallets] = useState([]);
23
+ const [currentWallet, setCurrentWallet] = useState(null);
24
+ useEffect(() => {
25
+ const sdk = new Movement({
26
+ network,
27
+ autoConnect
28
+ });
29
+ setMovement(sdk);
30
+ const detected = sdk.wallet.detectWallets();
31
+ setWallets(detected);
32
+ const handleConnect = (address) => {
33
+ setWalletState((prev) => ({
34
+ ...prev,
35
+ connected: true,
36
+ address
37
+ }));
38
+ setConnecting(false);
39
+ };
40
+ const handleDisconnect = () => {
41
+ setWalletState({
42
+ connected: false,
43
+ address: null,
44
+ publicKey: null
45
+ });
46
+ setCurrentWallet(null);
47
+ };
48
+ const handleAccountChanged = (newAddress) => {
49
+ setWalletState((prev) => ({
50
+ ...prev,
51
+ address: newAddress
52
+ }));
53
+ };
54
+ sdk.wallet.on("connect", handleConnect);
55
+ sdk.wallet.on("disconnect", handleDisconnect);
56
+ sdk.wallet.on("accountChanged", handleAccountChanged);
57
+ const state = sdk.wallet.getState();
58
+ if (state.connected) {
59
+ setWalletState(state);
60
+ setCurrentWallet(sdk.wallet.getWallet());
61
+ }
62
+ return () => {
63
+ sdk.wallet.off("connect", handleConnect);
64
+ sdk.wallet.off("disconnect", handleDisconnect);
65
+ sdk.wallet.off("accountChanged", handleAccountChanged);
66
+ sdk.events.unsubscribeAll();
67
+ };
68
+ }, [network, autoConnect]);
69
+ const connect = useCallback(
70
+ async (wallet) => {
71
+ if (!movement) return;
72
+ setConnecting(true);
73
+ try {
74
+ await movement.wallet.connect(wallet);
75
+ setCurrentWallet(wallet);
76
+ } catch (error) {
77
+ setConnecting(false);
78
+ if (error instanceof MovementError && onError) {
79
+ onError(error);
80
+ }
81
+ throw error;
82
+ }
83
+ },
84
+ [movement, onError]
85
+ );
86
+ const disconnect = useCallback(async () => {
87
+ if (!movement) return;
88
+ try {
89
+ await movement.wallet.disconnect();
90
+ } catch (error) {
91
+ if (error instanceof MovementError && onError) {
92
+ onError(error);
93
+ }
94
+ throw error;
95
+ }
96
+ }, [movement, onError]);
97
+ const value = {
98
+ movement,
99
+ network,
100
+ address: walletState.address,
101
+ connected: walletState.connected,
102
+ connecting,
103
+ wallets,
104
+ wallet: currentWallet,
105
+ connect,
106
+ disconnect,
107
+ ...onError && { onError }
108
+ };
109
+ return /* @__PURE__ */ jsx(MovementContext.Provider, { value, children });
110
+ }
111
+ function useMovementContext() {
112
+ const context = useContext(MovementContext);
113
+ if (!context) {
114
+ throw new Error("useMovementContext must be used within a MovementProvider");
115
+ }
116
+ return context;
117
+ }
118
+
119
+ // src/hooks/useMovement.ts
120
+ function useMovement() {
121
+ const { movement, network, address, connected, connecting, connect, disconnect, wallets, wallet } = useMovementContext();
122
+ return {
123
+ movement,
124
+ network,
125
+ address,
126
+ connected,
127
+ connecting,
128
+ connect,
129
+ disconnect,
130
+ wallets,
131
+ wallet
132
+ };
133
+ }
134
+
135
+ // src/hooks/useBalance.ts
136
+ import { useState as useState2, useEffect as useEffect2, useCallback as useCallback2 } from "react";
137
+ function useBalance(address) {
138
+ const { movement, address: connectedAddress, onError } = useMovementContext();
139
+ const [balance, setBalance] = useState2(null);
140
+ const [loading, setLoading] = useState2(false);
141
+ const [error, setError] = useState2(null);
142
+ const targetAddress = address ?? connectedAddress;
143
+ const fetchBalance = useCallback2(async () => {
144
+ if (!movement || !targetAddress) {
145
+ setBalance(null);
146
+ return;
147
+ }
148
+ setLoading(true);
149
+ setError(null);
150
+ try {
151
+ const result = await movement.getAccountBalance(targetAddress);
152
+ setBalance(result);
153
+ } catch (err) {
154
+ const movementError = err;
155
+ setError(movementError);
156
+ setBalance(null);
157
+ if (onError) {
158
+ onError(movementError);
159
+ }
160
+ } finally {
161
+ setLoading(false);
162
+ }
163
+ }, [movement, targetAddress, onError]);
164
+ useEffect2(() => {
165
+ let cancelled = false;
166
+ const doFetch = async () => {
167
+ if (!movement || !targetAddress) {
168
+ setBalance(null);
169
+ return;
170
+ }
171
+ setLoading(true);
172
+ setError(null);
173
+ try {
174
+ const result = await movement.getAccountBalance(targetAddress);
175
+ if (!cancelled) {
176
+ setBalance(result);
177
+ }
178
+ } catch (err) {
179
+ if (!cancelled) {
180
+ const movementError = err;
181
+ setError(movementError);
182
+ setBalance(null);
183
+ if (onError) {
184
+ onError(movementError);
185
+ }
186
+ }
187
+ } finally {
188
+ if (!cancelled) {
189
+ setLoading(false);
190
+ }
191
+ }
192
+ };
193
+ doFetch();
194
+ return () => {
195
+ cancelled = true;
196
+ };
197
+ }, [movement, targetAddress, onError]);
198
+ return {
199
+ balance,
200
+ loading,
201
+ error,
202
+ refetch: fetchBalance
203
+ };
204
+ }
205
+
206
+ // src/hooks/useContract.ts
207
+ import { useState as useState3, useMemo, useCallback as useCallback3 } from "react";
208
+ function useContract(options) {
209
+ const { movement, onError } = useMovementContext();
210
+ const [data, setData] = useState3(null);
211
+ const [loading, setLoading] = useState3(false);
212
+ const [error, setError] = useState3(null);
213
+ const contract = useMemo(() => {
214
+ if (!movement) return null;
215
+ return movement.contract(options);
216
+ }, [movement, options.address, options.module]);
217
+ const read = useCallback3(
218
+ async (functionName, args, typeArgs = []) => {
219
+ if (!contract) {
220
+ throw new Error("Contract not initialized");
221
+ }
222
+ setLoading(true);
223
+ setError(null);
224
+ try {
225
+ const result = await contract.view(functionName, args, typeArgs);
226
+ setData(result);
227
+ return result;
228
+ } catch (err) {
229
+ const movementError = err;
230
+ setError(movementError);
231
+ if (onError) {
232
+ onError(movementError);
233
+ }
234
+ throw err;
235
+ } finally {
236
+ setLoading(false);
237
+ }
238
+ },
239
+ [contract, onError]
240
+ );
241
+ const write = useCallback3(
242
+ async (functionName, args, typeArgs = []) => {
243
+ if (!contract) {
244
+ throw new Error("Contract not initialized");
245
+ }
246
+ setLoading(true);
247
+ setError(null);
248
+ try {
249
+ const txHash = await contract.call(functionName, args, typeArgs);
250
+ setData(txHash);
251
+ return txHash;
252
+ } catch (err) {
253
+ const movementError = err;
254
+ setError(movementError);
255
+ if (onError) {
256
+ onError(movementError);
257
+ }
258
+ throw err;
259
+ } finally {
260
+ setLoading(false);
261
+ }
262
+ },
263
+ [contract, onError]
264
+ );
265
+ return {
266
+ data,
267
+ loading,
268
+ error,
269
+ read,
270
+ write,
271
+ contract
272
+ };
273
+ }
274
+
275
+ // src/hooks/useTransaction.ts
276
+ import { useState as useState4, useCallback as useCallback4 } from "react";
277
+ function useTransaction() {
278
+ const { movement, onError } = useMovementContext();
279
+ const [data, setData] = useState4(null);
280
+ const [loading, setLoading] = useState4(false);
281
+ const [error, setError] = useState4(null);
282
+ const send = useCallback4(
283
+ async (options) => {
284
+ if (!movement) {
285
+ throw new Error("Movement SDK not initialized");
286
+ }
287
+ setLoading(true);
288
+ setError(null);
289
+ try {
290
+ const payload = await movement.transaction.build(options);
291
+ const hash = await movement.transaction.signAndSubmit(payload);
292
+ setData(hash);
293
+ return hash;
294
+ } catch (err) {
295
+ const movementError = err;
296
+ setError(movementError);
297
+ if (onError) {
298
+ onError(movementError);
299
+ }
300
+ throw err;
301
+ } finally {
302
+ setLoading(false);
303
+ }
304
+ },
305
+ [movement, onError]
306
+ );
307
+ const reset = useCallback4(() => {
308
+ setData(null);
309
+ setError(null);
310
+ setLoading(false);
311
+ }, []);
312
+ return {
313
+ send,
314
+ data,
315
+ loading,
316
+ error,
317
+ reset
318
+ };
319
+ }
320
+
321
+ // src/hooks/useWaitForTransaction.ts
322
+ import { useState as useState5, useEffect as useEffect3 } from "react";
323
+ function useWaitForTransaction(hash, options) {
324
+ const { movement, onError } = useMovementContext();
325
+ const [data, setData] = useState5(null);
326
+ const [loading, setLoading] = useState5(false);
327
+ const [error, setError] = useState5(null);
328
+ useEffect3(() => {
329
+ if (!hash || !movement) {
330
+ setData(null);
331
+ setLoading(false);
332
+ setError(null);
333
+ return;
334
+ }
335
+ let cancelled = false;
336
+ const waitForTx = async () => {
337
+ setLoading(true);
338
+ setError(null);
339
+ try {
340
+ const waitOptions = {};
341
+ if (options?.timeoutMs !== void 0) {
342
+ waitOptions.timeoutMs = options.timeoutMs;
343
+ }
344
+ if (options?.checkIntervalMs !== void 0) {
345
+ waitOptions.checkIntervalMs = options.checkIntervalMs;
346
+ }
347
+ const response = await movement.waitForTransaction(hash, waitOptions);
348
+ if (!cancelled) {
349
+ setData(response);
350
+ }
351
+ } catch (err) {
352
+ if (!cancelled) {
353
+ const movementError = err;
354
+ setError(movementError);
355
+ if (onError) {
356
+ onError(movementError);
357
+ }
358
+ }
359
+ } finally {
360
+ if (!cancelled) {
361
+ setLoading(false);
362
+ }
363
+ }
364
+ };
365
+ waitForTx();
366
+ return () => {
367
+ cancelled = true;
368
+ };
369
+ }, [hash, movement, options?.timeoutMs, options?.checkIntervalMs, onError]);
370
+ return {
371
+ data,
372
+ loading,
373
+ error
374
+ };
375
+ }
376
+
377
+ // src/components/WalletButton.tsx
378
+ import { useState as useState6 } from "react";
379
+
380
+ // src/components/WalletModal.tsx
381
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
382
+ var WALLET_INFO = {
383
+ petra: {
384
+ name: "Petra",
385
+ icon: "\u{1F98A}",
386
+ description: "Recommended for Movement"
387
+ },
388
+ pontem: {
389
+ name: "Pontem",
390
+ icon: "\u{1F309}",
391
+ description: "Multi-chain with Movement support"
392
+ },
393
+ nightly: {
394
+ name: "Nightly",
395
+ icon: "\u{1F319}",
396
+ description: "Movement & Aptos wallet"
397
+ }
398
+ };
399
+ function WalletModal({ open, onClose }) {
400
+ const { wallets, connect, connecting } = useMovement();
401
+ if (!open) return null;
402
+ const handleConnect = async (wallet) => {
403
+ try {
404
+ await connect(wallet);
405
+ onClose();
406
+ } catch {
407
+ }
408
+ };
409
+ const overlayStyles = {
410
+ position: "fixed",
411
+ top: 0,
412
+ left: 0,
413
+ right: 0,
414
+ bottom: 0,
415
+ backgroundColor: "rgba(0, 0, 0, 0.5)",
416
+ display: "flex",
417
+ alignItems: "center",
418
+ justifyContent: "center",
419
+ zIndex: 1e3
420
+ };
421
+ const modalStyles = {
422
+ backgroundColor: "white",
423
+ borderRadius: "12px",
424
+ padding: "24px",
425
+ minWidth: "320px",
426
+ maxWidth: "400px",
427
+ boxShadow: "0 20px 25px -5px rgba(0, 0, 0, 0.1)"
428
+ };
429
+ const headerStyles = {
430
+ display: "flex",
431
+ justifyContent: "space-between",
432
+ alignItems: "center",
433
+ marginBottom: "20px"
434
+ };
435
+ const titleStyles = {
436
+ fontSize: "18px",
437
+ fontWeight: 600,
438
+ margin: 0
439
+ };
440
+ const closeButtonStyles = {
441
+ background: "none",
442
+ border: "none",
443
+ fontSize: "24px",
444
+ cursor: "pointer",
445
+ padding: "4px",
446
+ lineHeight: 1
447
+ };
448
+ const walletButtonStyles = {
449
+ display: "flex",
450
+ alignItems: "center",
451
+ gap: "12px",
452
+ width: "100%",
453
+ padding: "12px 16px",
454
+ border: "1px solid #e5e7eb",
455
+ borderRadius: "8px",
456
+ backgroundColor: "white",
457
+ cursor: "pointer",
458
+ fontSize: "16px",
459
+ marginBottom: "8px",
460
+ transition: "all 0.2s ease"
461
+ };
462
+ const emptyStyles = {
463
+ textAlign: "center",
464
+ color: "#6b7280",
465
+ padding: "20px"
466
+ };
467
+ return /* @__PURE__ */ jsx2("div", { style: overlayStyles, onClick: onClose, children: /* @__PURE__ */ jsxs("div", { style: modalStyles, onClick: (e) => e.stopPropagation(), children: [
468
+ /* @__PURE__ */ jsxs("div", { style: headerStyles, children: [
469
+ /* @__PURE__ */ jsx2("h2", { style: titleStyles, children: "Connect Wallet" }),
470
+ /* @__PURE__ */ jsx2("button", { style: closeButtonStyles, onClick: onClose, children: "\xD7" })
471
+ ] }),
472
+ wallets.length === 0 ? /* @__PURE__ */ jsxs("div", { style: emptyStyles, children: [
473
+ /* @__PURE__ */ jsx2("p", { style: { marginBottom: "12px" }, children: "No wallets detected." }),
474
+ /* @__PURE__ */ jsxs("p", { style: { marginBottom: "16px" }, children: [
475
+ "Install a wallet that supports ",
476
+ /* @__PURE__ */ jsx2("strong", { children: "Movement Network" }),
477
+ ":"
478
+ ] }),
479
+ /* @__PURE__ */ jsxs("div", { style: { display: "flex", flexDirection: "column", gap: "8px" }, children: [
480
+ /* @__PURE__ */ jsx2(
481
+ "a",
482
+ {
483
+ href: "https://petra.app/",
484
+ target: "_blank",
485
+ rel: "noopener noreferrer",
486
+ style: {
487
+ display: "inline-block",
488
+ padding: "8px 16px",
489
+ backgroundColor: "#4f46e5",
490
+ color: "white",
491
+ borderRadius: "6px",
492
+ textDecoration: "none",
493
+ fontSize: "14px"
494
+ },
495
+ children: "\u{1F98A} Get Petra (Recommended)"
496
+ }
497
+ ),
498
+ /* @__PURE__ */ jsx2(
499
+ "a",
500
+ {
501
+ href: "https://pontem.network/pontem-wallet",
502
+ target: "_blank",
503
+ rel: "noopener noreferrer",
504
+ style: {
505
+ display: "inline-block",
506
+ padding: "8px 16px",
507
+ backgroundColor: "#6366f1",
508
+ color: "white",
509
+ borderRadius: "6px",
510
+ textDecoration: "none",
511
+ fontSize: "14px"
512
+ },
513
+ children: "\u{1F309} Get Pontem"
514
+ }
515
+ )
516
+ ] })
517
+ ] }) : /* @__PURE__ */ jsx2("div", { children: wallets.map((wallet) => /* @__PURE__ */ jsxs(
518
+ "button",
519
+ {
520
+ style: walletButtonStyles,
521
+ onClick: () => handleConnect(wallet),
522
+ disabled: connecting,
523
+ children: [
524
+ /* @__PURE__ */ jsx2("span", { style: { fontSize: "24px" }, children: WALLET_INFO[wallet].icon }),
525
+ /* @__PURE__ */ jsxs("div", { style: { textAlign: "left" }, children: [
526
+ /* @__PURE__ */ jsx2("div", { children: WALLET_INFO[wallet].name }),
527
+ /* @__PURE__ */ jsx2("div", { style: { fontSize: "12px", color: "#6b7280" }, children: WALLET_INFO[wallet].description })
528
+ ] })
529
+ ]
530
+ },
531
+ wallet
532
+ )) })
533
+ ] }) });
534
+ }
535
+
536
+ // src/components/WalletButton.tsx
537
+ import { Fragment, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
538
+ function truncateAddress(address) {
539
+ return `${address.slice(0, 6)}...${address.slice(-4)}`;
540
+ }
541
+ function WalletButton({ className = "", connectText = "Connect Wallet" }) {
542
+ const { address, connected, connecting, disconnect } = useMovement();
543
+ const [modalOpen, setModalOpen] = useState6(false);
544
+ const baseStyles = {
545
+ padding: "10px 20px",
546
+ borderRadius: "8px",
547
+ border: "none",
548
+ cursor: "pointer",
549
+ fontSize: "14px",
550
+ fontWeight: 500,
551
+ transition: "all 0.2s ease"
552
+ };
553
+ const connectedStyles = {
554
+ ...baseStyles,
555
+ backgroundColor: "#f0f0f0",
556
+ color: "#333"
557
+ };
558
+ const disconnectedStyles = {
559
+ ...baseStyles,
560
+ backgroundColor: "#6366f1",
561
+ color: "white"
562
+ };
563
+ if (connected && address) {
564
+ return /* @__PURE__ */ jsx3(
565
+ "button",
566
+ {
567
+ className,
568
+ style: connectedStyles,
569
+ onClick: disconnect,
570
+ title: "Click to disconnect",
571
+ children: truncateAddress(address)
572
+ }
573
+ );
574
+ }
575
+ return /* @__PURE__ */ jsxs2(Fragment, { children: [
576
+ /* @__PURE__ */ jsx3(
577
+ "button",
578
+ {
579
+ className,
580
+ style: disconnectedStyles,
581
+ onClick: () => setModalOpen(true),
582
+ disabled: connecting,
583
+ children: connecting ? "Connecting..." : connectText
584
+ }
585
+ ),
586
+ /* @__PURE__ */ jsx3(WalletModal, { open: modalOpen, onClose: () => setModalOpen(false) })
587
+ ] });
588
+ }
589
+
590
+ // src/components/AddressDisplay.tsx
591
+ import { useState as useState7, useCallback as useCallback5 } from "react";
592
+ import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
593
+ function truncateAddress2(address, startChars = 6, endChars = 4) {
594
+ if (address.length <= startChars + endChars + 3) {
595
+ return address;
596
+ }
597
+ return `${address.slice(0, startChars)}...${address.slice(-endChars)}`;
598
+ }
599
+ function AddressDisplay({
600
+ address,
601
+ truncate = true,
602
+ copyable = true,
603
+ className = ""
604
+ }) {
605
+ const [copied, setCopied] = useState7(false);
606
+ const handleCopy = useCallback5(async () => {
607
+ try {
608
+ await navigator.clipboard.writeText(address);
609
+ setCopied(true);
610
+ setTimeout(() => setCopied(false), 2e3);
611
+ } catch {
612
+ }
613
+ }, [address]);
614
+ const displayAddress = truncate ? truncateAddress2(address) : address;
615
+ const containerStyles = {
616
+ display: "inline-flex",
617
+ alignItems: "center",
618
+ gap: "8px",
619
+ fontFamily: "monospace",
620
+ fontSize: "14px"
621
+ };
622
+ const addressStyles = {
623
+ backgroundColor: "#f3f4f6",
624
+ padding: "4px 8px",
625
+ borderRadius: "4px"
626
+ };
627
+ const buttonStyles = {
628
+ background: "none",
629
+ border: "none",
630
+ cursor: "pointer",
631
+ padding: "4px",
632
+ fontSize: "14px",
633
+ color: copied ? "#10b981" : "#6b7280"
634
+ };
635
+ return /* @__PURE__ */ jsxs3("span", { className, style: containerStyles, title: address, children: [
636
+ /* @__PURE__ */ jsx4("span", { style: addressStyles, children: displayAddress }),
637
+ copyable && /* @__PURE__ */ jsx4("button", { style: buttonStyles, onClick: handleCopy, title: copied ? "Copied!" : "Copy address", children: copied ? "\u2713" : "\u{1F4CB}" })
638
+ ] });
639
+ }
640
+
641
+ // src/components/NetworkSwitcher.tsx
642
+ import { jsx as jsx5, jsxs as jsxs4 } from "react/jsx-runtime";
643
+ var NETWORK_INFO = {
644
+ mainnet: { name: "Mainnet", color: "#10b981" },
645
+ testnet: { name: "Testnet", color: "#f59e0b" }
646
+ };
647
+ function NetworkSwitcher({ className = "", onNetworkChange }) {
648
+ const { network } = useMovementContext();
649
+ const containerStyles = {
650
+ display: "inline-flex",
651
+ alignItems: "center",
652
+ gap: "8px"
653
+ };
654
+ const indicatorStyles = {
655
+ width: "8px",
656
+ height: "8px",
657
+ borderRadius: "50%",
658
+ backgroundColor: NETWORK_INFO[network].color
659
+ };
660
+ const selectStyles = {
661
+ padding: "6px 12px",
662
+ borderRadius: "6px",
663
+ border: "1px solid #e5e7eb",
664
+ backgroundColor: "white",
665
+ fontSize: "14px",
666
+ cursor: onNetworkChange ? "pointer" : "default"
667
+ };
668
+ if (!onNetworkChange) {
669
+ return /* @__PURE__ */ jsxs4("span", { className, style: containerStyles, children: [
670
+ /* @__PURE__ */ jsx5("span", { style: indicatorStyles }),
671
+ /* @__PURE__ */ jsx5("span", { children: NETWORK_INFO[network].name })
672
+ ] });
673
+ }
674
+ return /* @__PURE__ */ jsxs4("span", { className, style: containerStyles, children: [
675
+ /* @__PURE__ */ jsx5("span", { style: indicatorStyles }),
676
+ /* @__PURE__ */ jsxs4(
677
+ "select",
678
+ {
679
+ style: selectStyles,
680
+ value: network,
681
+ onChange: (e) => onNetworkChange(e.target.value),
682
+ children: [
683
+ /* @__PURE__ */ jsx5("option", { value: "mainnet", children: "Mainnet" }),
684
+ /* @__PURE__ */ jsx5("option", { value: "testnet", children: "Testnet" })
685
+ ]
686
+ }
687
+ )
688
+ ] });
689
+ }
690
+ export {
691
+ AddressDisplay,
692
+ MovementContext,
693
+ MovementProvider,
694
+ NetworkSwitcher,
695
+ WalletButton,
696
+ WalletModal,
697
+ useBalance,
698
+ useContract,
699
+ useMovement,
700
+ useMovementContext,
701
+ useTransaction,
702
+ useWaitForTransaction
703
+ };