@caatinga/cli 2.0.0 → 2.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.js +1 -1
- package/package.json +2 -2
- package/templates/marketplace-with-token/caatinga.template.json +1 -1
- package/templates/marketplace-with-token/package.json +3 -3
- package/templates/react-vite-counter/caatinga.template.json +1 -1
- package/templates/react-vite-counter/package.json +4 -5
- package/templates/react-vite-counter/src/App.tsx +26 -2
- package/templates/react-vite-counter/src/components/CounterCard.tsx +27 -6
- package/templates/react-vite-counter/src/components/LoadingModal.tsx +14 -0
- package/templates/react-vite-counter/src/components/WalletButton.tsx +3 -39
- package/templates/react-vite-counter/src/context/WalletContext.tsx +64 -0
- package/templates/react-vite-counter/src/stubs/hot-wallet.ts +14 -0
- package/templates/react-vite-counter/src/styles.css +51 -0
- package/templates/react-vite-counter/src/wallet.ts +2 -3
- package/templates/react-vite-counter/vite.config.ts +5 -8
- package/templates/react-vite-counter/src/hooks/useStellarWallet.ts +0 -70
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@caatinga/cli",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Caatinga CLI for building dApps on Stellar/Soroban",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"stellar",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"LICENSE"
|
|
44
44
|
],
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@caatinga/core": "^2.0.
|
|
46
|
+
"@caatinga/core": "^2.0.1",
|
|
47
47
|
"chalk": "^5.4.1",
|
|
48
48
|
"commander": "^12.1.0"
|
|
49
49
|
},
|
|
@@ -12,15 +12,15 @@
|
|
|
12
12
|
"caatinga:generate": "caatinga generate token && caatinga generate marketplace"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@caatinga/client": "^2.0.
|
|
16
|
-
"@caatinga/core": "^2.0.
|
|
15
|
+
"@caatinga/client": "^2.0.1",
|
|
16
|
+
"@caatinga/core": "^2.0.1",
|
|
17
17
|
"@vitejs/plugin-react": "^4.3.4",
|
|
18
18
|
"react": "^18.3.1",
|
|
19
19
|
"react-dom": "^18.3.1",
|
|
20
20
|
"vite": "^6.0.6"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@caatinga/cli": "^2.0.
|
|
23
|
+
"@caatinga/cli": "^2.0.1",
|
|
24
24
|
"@types/react": "^18.3.18",
|
|
25
25
|
"@types/react-dom": "^18.3.5",
|
|
26
26
|
"typescript": "^5.7.2"
|
|
@@ -12,17 +12,16 @@
|
|
|
12
12
|
"caatinga:generate": "caatinga generate counter"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@caatinga/client": "^2.0.
|
|
16
|
-
"@caatinga/core": "^2.0.
|
|
17
|
-
"@creit.tech/
|
|
15
|
+
"@caatinga/client": "^2.0.1",
|
|
16
|
+
"@caatinga/core": "^2.0.1",
|
|
17
|
+
"@creit.tech/stellar-wallets-kit": "^1.9.5",
|
|
18
18
|
"@vitejs/plugin-react": "^4.3.4",
|
|
19
19
|
"react": "^18.3.1",
|
|
20
20
|
"react-dom": "^18.3.1",
|
|
21
|
-
"stellar-wallets-kit": "github:Creit-Tech/Stellar-Wallets-Kit#v0.0.7",
|
|
22
21
|
"vite": "^6.0.6"
|
|
23
22
|
},
|
|
24
23
|
"devDependencies": {
|
|
25
|
-
"@caatinga/cli": "^2.0.
|
|
24
|
+
"@caatinga/cli": "^2.0.1",
|
|
26
25
|
"@types/react": "^18.3.18",
|
|
27
26
|
"@types/react-dom": "^18.3.5",
|
|
28
27
|
"typescript": "^5.7.2"
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { CounterCard } from "./components/CounterCard";
|
|
2
2
|
import { WalletButton } from "./components/WalletButton";
|
|
3
|
+
import { WalletProvider, useWallet } from "./context/WalletContext";
|
|
4
|
+
|
|
5
|
+
function AppBody() {
|
|
6
|
+
const { publicKey } = useWallet();
|
|
3
7
|
|
|
4
|
-
export default function App() {
|
|
5
8
|
return (
|
|
6
9
|
<main className="app-shell">
|
|
7
10
|
<header className="topbar">
|
|
@@ -12,7 +15,28 @@ export default function App() {
|
|
|
12
15
|
<WalletButton />
|
|
13
16
|
</header>
|
|
14
17
|
|
|
15
|
-
|
|
18
|
+
{publicKey ? (
|
|
19
|
+
<CounterCard />
|
|
20
|
+
) : (
|
|
21
|
+
<section className="counter-panel" aria-labelledby="connect-title">
|
|
22
|
+
<div className="counter-panel__header">
|
|
23
|
+
<div>
|
|
24
|
+
<p className="eyebrow">Get started</p>
|
|
25
|
+
<h2 id="connect-title">Connect your wallet</h2>
|
|
26
|
+
</div>
|
|
27
|
+
<span className="network-pill">testnet</span>
|
|
28
|
+
</div>
|
|
29
|
+
<p>Connect a Stellar wallet to read and update the counter contract.</p>
|
|
30
|
+
</section>
|
|
31
|
+
)}
|
|
16
32
|
</main>
|
|
17
33
|
);
|
|
18
34
|
}
|
|
35
|
+
|
|
36
|
+
export default function App() {
|
|
37
|
+
return (
|
|
38
|
+
<WalletProvider>
|
|
39
|
+
<AppBody />
|
|
40
|
+
</WalletProvider>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
2
|
import { caatingaClient } from "../caatinga.js";
|
|
3
3
|
import { CaatingaError } from "@caatinga/core/browser";
|
|
4
|
+
import { useWallet } from "../context/WalletContext.js";
|
|
5
|
+
import { LoadingModal } from "./LoadingModal.js";
|
|
4
6
|
|
|
5
7
|
function formatCaatingaError(error: unknown): string {
|
|
6
8
|
if (error instanceof CaatingaError) {
|
|
@@ -11,13 +13,22 @@ function formatCaatingaError(error: unknown): string {
|
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
export function CounterCard() {
|
|
16
|
+
const { publicKey } = useWallet();
|
|
14
17
|
const [count, setCount] = useState<number | null>(null);
|
|
15
|
-
|
|
18
|
+
// Start in the loading state: the component only mounts once a wallet is
|
|
19
|
+
// connected, and it fetches immediately — so the first paint should read as
|
|
20
|
+
// "Loading…" instead of flashing "Not loaded" while the read resolves.
|
|
21
|
+
const [loading, setLoading] = useState(true);
|
|
16
22
|
const [error, setError] = useState<string | null>(null);
|
|
17
|
-
const formattedCount = useMemo(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
const formattedCount = useMemo(() => {
|
|
24
|
+
if (count !== null) {
|
|
25
|
+
return new Intl.NumberFormat().format(count);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// While loading, the LoadingModal overlay covers this; show a neutral
|
|
29
|
+
// placeholder (not "Not loaded") so nothing flashes behind it.
|
|
30
|
+
return loading ? "—" : "Not loaded";
|
|
31
|
+
}, [count, loading]);
|
|
21
32
|
|
|
22
33
|
const refresh = useCallback(async () => {
|
|
23
34
|
setLoading(true);
|
|
@@ -34,8 +45,16 @@ export function CounterCard() {
|
|
|
34
45
|
}, []);
|
|
35
46
|
|
|
36
47
|
useEffect(() => {
|
|
48
|
+
// Reads route through the wallet for the source account, so only fetch once
|
|
49
|
+
// a wallet is connected. Avoids the "Not loaded" state firing before connect.
|
|
50
|
+
if (!publicKey) {
|
|
51
|
+
setCount(null);
|
|
52
|
+
setLoading(false);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
37
56
|
void refresh();
|
|
38
|
-
}, [refresh]);
|
|
57
|
+
}, [publicKey, refresh]);
|
|
39
58
|
|
|
40
59
|
async function increment() {
|
|
41
60
|
setLoading(true);
|
|
@@ -57,6 +76,8 @@ export function CounterCard() {
|
|
|
57
76
|
|
|
58
77
|
return (
|
|
59
78
|
<section className="counter-panel" aria-labelledby="counter-title">
|
|
79
|
+
{loading ? <LoadingModal label={count === null ? "Loading…" : "Updating…"} /> : null}
|
|
80
|
+
|
|
60
81
|
<div className="counter-panel__header">
|
|
61
82
|
<div>
|
|
62
83
|
<p className="eyebrow">Counter Contract</p>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface LoadingModalProps {
|
|
2
|
+
label?: string;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function LoadingModal({ label = "Loading…" }: LoadingModalProps) {
|
|
6
|
+
return (
|
|
7
|
+
<div className="loading-modal" role="status" aria-live="polite" aria-label={label}>
|
|
8
|
+
<div className="loading-modal__card">
|
|
9
|
+
<span className="loading-modal__spinner" aria-hidden="true" />
|
|
10
|
+
<span className="loading-modal__label">{label}</span>
|
|
11
|
+
</div>
|
|
12
|
+
</div>
|
|
13
|
+
);
|
|
14
|
+
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { useStellarWallet } from "../hooks/useStellarWallet.js";
|
|
1
|
+
import { useWallet } from "../context/WalletContext.js";
|
|
3
2
|
|
|
4
3
|
function shortenAddress(address: string): string {
|
|
5
4
|
if (address.length <= 12) {
|
|
@@ -10,49 +9,14 @@ function shortenAddress(address: string): string {
|
|
|
10
9
|
}
|
|
11
10
|
|
|
12
11
|
export function WalletButton() {
|
|
13
|
-
const {
|
|
14
|
-
publicKey,
|
|
15
|
-
selectedWallet,
|
|
16
|
-
network,
|
|
17
|
-
loading,
|
|
18
|
-
error,
|
|
19
|
-
selectWallet,
|
|
20
|
-
selectNetwork,
|
|
21
|
-
connect,
|
|
22
|
-
disconnect
|
|
23
|
-
} = useStellarWallet();
|
|
12
|
+
const { publicKey, loading, error, connect, disconnect } = useWallet();
|
|
24
13
|
|
|
25
14
|
return (
|
|
26
15
|
<div className="wallet-shell">
|
|
27
|
-
<div className="wallet-controls">
|
|
28
|
-
<label>
|
|
29
|
-
<span>Wallet</span>
|
|
30
|
-
<select
|
|
31
|
-
value={selectedWallet}
|
|
32
|
-
onChange={(event) => void selectWallet(event.target.value as WalletType)}
|
|
33
|
-
>
|
|
34
|
-
<option value={WalletType.XBULL}>xBull</option>
|
|
35
|
-
<option value={WalletType.FREIGHTER}>Freighter</option>
|
|
36
|
-
<option value={WalletType.ALBEDO}>Albedo</option>
|
|
37
|
-
<option value={WalletType.RABET}>Rabet</option>
|
|
38
|
-
<option value={WalletType.WALLET_CONNECT}>WalletConnect</option>
|
|
39
|
-
</select>
|
|
40
|
-
</label>
|
|
41
|
-
<label>
|
|
42
|
-
<span>Network</span>
|
|
43
|
-
<select
|
|
44
|
-
value={network}
|
|
45
|
-
onChange={(event) => void selectNetwork(event.target.value as WalletNetwork)}
|
|
46
|
-
>
|
|
47
|
-
<option value={WalletNetwork.TESTNET}>Testnet</option>
|
|
48
|
-
<option value={WalletNetwork.PUBLIC}>Public</option>
|
|
49
|
-
</select>
|
|
50
|
-
</label>
|
|
51
|
-
</div>
|
|
52
16
|
<button
|
|
53
17
|
className="wallet-button"
|
|
54
18
|
type="button"
|
|
55
|
-
onClick={publicKey ? disconnect : () => void connect()}
|
|
19
|
+
onClick={publicKey ? () => void disconnect() : () => void connect()}
|
|
56
20
|
disabled={loading}
|
|
57
21
|
aria-live="polite"
|
|
58
22
|
>
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
useCallback,
|
|
4
|
+
useContext,
|
|
5
|
+
useMemo,
|
|
6
|
+
useState,
|
|
7
|
+
type ReactNode
|
|
8
|
+
} from "react";
|
|
9
|
+
import { stellarWalletAdapter } from "../wallet.js";
|
|
10
|
+
|
|
11
|
+
interface WalletContextValue {
|
|
12
|
+
publicKey: string | null;
|
|
13
|
+
loading: boolean;
|
|
14
|
+
error: string | null;
|
|
15
|
+
connect: () => Promise<void>;
|
|
16
|
+
disconnect: () => Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const WalletContext = createContext<WalletContextValue | null>(null);
|
|
20
|
+
|
|
21
|
+
export function WalletProvider({ children }: { children: ReactNode }) {
|
|
22
|
+
const [publicKey, setPublicKey] = useState<string | null>(null);
|
|
23
|
+
const [loading, setLoading] = useState(false);
|
|
24
|
+
const [error, setError] = useState<string | null>(null);
|
|
25
|
+
|
|
26
|
+
const connect = useCallback(async () => {
|
|
27
|
+
setLoading(true);
|
|
28
|
+
setError(null);
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
// openModal lists only installed/available wallets and resolves with the
|
|
32
|
+
// chosen account address (rejects if the user dismisses the modal).
|
|
33
|
+
const address = await stellarWalletAdapter.openModal();
|
|
34
|
+
setPublicKey(address);
|
|
35
|
+
} catch (caught) {
|
|
36
|
+
const message = caught instanceof Error ? caught.message : String(caught);
|
|
37
|
+
setError(message);
|
|
38
|
+
} finally {
|
|
39
|
+
setLoading(false);
|
|
40
|
+
}
|
|
41
|
+
}, []);
|
|
42
|
+
|
|
43
|
+
const disconnect = useCallback(async () => {
|
|
44
|
+
await stellarWalletAdapter.disconnect();
|
|
45
|
+
setPublicKey(null);
|
|
46
|
+
setError(null);
|
|
47
|
+
}, []);
|
|
48
|
+
|
|
49
|
+
const value = useMemo<WalletContextValue>(
|
|
50
|
+
() => ({ publicKey, loading, error, connect, disconnect }),
|
|
51
|
+
[publicKey, loading, error, connect, disconnect]
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
return <WalletContext.Provider value={value}>{children}</WalletContext.Provider>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function useWallet(): WalletContextValue {
|
|
58
|
+
const context = useContext(WalletContext);
|
|
59
|
+
if (!context) {
|
|
60
|
+
throw new Error("useWallet must be used within a WalletProvider");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return context;
|
|
64
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Stub for @hot-wallet/sdk. Stellar Wallets Kit pulls this NEAR-ecosystem SDK
|
|
2
|
+
// (via its HOT Wallet module) into every import, dragging @near-js/crypto and
|
|
3
|
+
// randombytes — Node-only code that references `global` and crashes in the
|
|
4
|
+
// browser. The Caatinga adapter filters HOT Wallet out of the wallet list, so
|
|
5
|
+
// this SDK is never actually used; aliasing it here (see vite.config.ts) keeps
|
|
6
|
+
// the NEAR chain out of the bundle entirely.
|
|
7
|
+
//
|
|
8
|
+
// `HOT` is only referenced inside HotWalletModule methods, which are never
|
|
9
|
+
// reached, so a throwing stub is safe.
|
|
10
|
+
export const HOT = {
|
|
11
|
+
request(): Promise<never> {
|
|
12
|
+
return Promise.reject(new Error("HOT Wallet is not supported in this build."));
|
|
13
|
+
}
|
|
14
|
+
};
|
|
@@ -140,6 +140,7 @@ button:hover {
|
|
|
140
140
|
}
|
|
141
141
|
|
|
142
142
|
.counter-panel {
|
|
143
|
+
position: relative;
|
|
143
144
|
border: 1px solid #d9d5ca;
|
|
144
145
|
border-radius: 8px;
|
|
145
146
|
padding: clamp(20px, 5vw, 40px);
|
|
@@ -147,6 +148,56 @@ button:hover {
|
|
|
147
148
|
box-shadow: 0 18px 60px rgba(32, 35, 42, 0.08);
|
|
148
149
|
}
|
|
149
150
|
|
|
151
|
+
.loading-modal {
|
|
152
|
+
position: absolute;
|
|
153
|
+
inset: 0;
|
|
154
|
+
z-index: 5;
|
|
155
|
+
display: flex;
|
|
156
|
+
align-items: center;
|
|
157
|
+
justify-content: center;
|
|
158
|
+
border-radius: 8px;
|
|
159
|
+
background: rgba(255, 253, 247, 0.72);
|
|
160
|
+
backdrop-filter: blur(2px);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
.loading-modal__card {
|
|
164
|
+
display: flex;
|
|
165
|
+
align-items: center;
|
|
166
|
+
gap: 12px;
|
|
167
|
+
padding: 14px 20px;
|
|
168
|
+
border: 1px solid #d9d5ca;
|
|
169
|
+
border-radius: 10px;
|
|
170
|
+
background: #fffdf7;
|
|
171
|
+
box-shadow: 0 12px 40px rgba(32, 35, 42, 0.14);
|
|
172
|
+
color: #20232a;
|
|
173
|
+
font-weight: 700;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
.loading-modal__spinner {
|
|
177
|
+
width: 18px;
|
|
178
|
+
height: 18px;
|
|
179
|
+
border: 3px solid #d9d5ca;
|
|
180
|
+
border-top-color: #1d6154;
|
|
181
|
+
border-radius: 50%;
|
|
182
|
+
animation: loading-spin 0.7s linear infinite;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
.loading-modal__label {
|
|
186
|
+
font-size: 0.95rem;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
@keyframes loading-spin {
|
|
190
|
+
to {
|
|
191
|
+
transform: rotate(360deg);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
@media (prefers-reduced-motion: reduce) {
|
|
196
|
+
.loading-modal__spinner {
|
|
197
|
+
animation-duration: 1.6s;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
150
201
|
.counter-panel__header {
|
|
151
202
|
display: flex;
|
|
152
203
|
align-items: flex-start;
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
// Keep in sync with examples/counter-web/src/wallet.ts (WalletConnect defaults differ per project).
|
|
2
2
|
import {
|
|
3
3
|
createStellarWalletsKitAdapter,
|
|
4
|
+
WalletNetwork,
|
|
4
5
|
type StellarWalletsKitMetadata
|
|
5
6
|
} from "@caatinga/client/stellar-wallets-kit";
|
|
6
|
-
import { WalletNetwork, WalletType } from "stellar-wallets-kit";
|
|
7
7
|
|
|
8
8
|
export const stellarWalletAdapter = createStellarWalletsKitAdapter({
|
|
9
9
|
network: WalletNetwork.TESTNET,
|
|
10
|
-
selectedWallet: WalletType.XBULL,
|
|
11
10
|
walletConnectMetadata: getWalletConnectMetadata()
|
|
12
11
|
});
|
|
13
12
|
|
|
14
|
-
export { WalletNetwork
|
|
13
|
+
export { WalletNetwork };
|
|
15
14
|
|
|
16
15
|
function getWalletConnectMetadata(): StellarWalletsKitMetadata | undefined {
|
|
17
16
|
const projectId = import.meta.env.VITE_WALLETCONNECT_PROJECT_ID as string | undefined;
|
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
import { defineConfig } from "vite";
|
|
2
2
|
import react from "@vitejs/plugin-react";
|
|
3
|
-
import {
|
|
4
|
-
import { dirname, join } from "node:path";
|
|
5
|
-
|
|
6
|
-
const require = createRequire(import.meta.url);
|
|
7
|
-
const xbullWalletConnectRoot = dirname(
|
|
8
|
-
require.resolve("@creit-tech/xbull-wallet-connect/package.json")
|
|
9
|
-
);
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
10
4
|
|
|
11
5
|
export default defineConfig({
|
|
12
6
|
plugins: [react()],
|
|
13
7
|
resolve: {
|
|
14
8
|
alias: {
|
|
15
|
-
|
|
9
|
+
// Stellar Wallets Kit drags NEAR's @hot-wallet/sdk (Node-only crypto) into
|
|
10
|
+
// the browser bundle. The adapter filters HOT Wallet out, so stub the SDK
|
|
11
|
+
// to keep the NEAR chain out of the build. See src/stubs/hot-wallet.ts.
|
|
12
|
+
"@hot-wallet/sdk": fileURLToPath(new URL("./src/stubs/hot-wallet.ts", import.meta.url))
|
|
16
13
|
}
|
|
17
14
|
}
|
|
18
15
|
});
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { useCallback, useEffect, useState } from "react";
|
|
2
|
-
import { stellarWalletAdapter, WalletNetwork, WalletType } from "../wallet.js";
|
|
3
|
-
|
|
4
|
-
export function useStellarWallet() {
|
|
5
|
-
const [publicKey, setPublicKey] = useState<string | null>(null);
|
|
6
|
-
const [selectedWallet, setSelectedWallet] = useState<WalletType>(WalletType.XBULL);
|
|
7
|
-
const [network, setNetwork] = useState<WalletNetwork>(WalletNetwork.TESTNET);
|
|
8
|
-
const [loading, setLoading] = useState(false);
|
|
9
|
-
const [error, setError] = useState<string | null>(null);
|
|
10
|
-
|
|
11
|
-
const selectWallet = useCallback(async (wallet: WalletType) => {
|
|
12
|
-
setError(null);
|
|
13
|
-
await stellarWalletAdapter.setWallet(wallet);
|
|
14
|
-
setSelectedWallet(wallet);
|
|
15
|
-
setPublicKey(null);
|
|
16
|
-
}, []);
|
|
17
|
-
|
|
18
|
-
const selectNetwork = useCallback(async (nextNetwork: WalletNetwork) => {
|
|
19
|
-
setError(null);
|
|
20
|
-
await stellarWalletAdapter.setNetwork(nextNetwork);
|
|
21
|
-
setNetwork(nextNetwork);
|
|
22
|
-
setPublicKey(null);
|
|
23
|
-
}, []);
|
|
24
|
-
|
|
25
|
-
const connect = useCallback(async () => {
|
|
26
|
-
setLoading(true);
|
|
27
|
-
setError(null);
|
|
28
|
-
|
|
29
|
-
try {
|
|
30
|
-
if (selectedWallet === WalletType.WALLET_CONNECT) {
|
|
31
|
-
await stellarWalletAdapter.startWalletConnect();
|
|
32
|
-
await stellarWalletAdapter.connectWalletConnect();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const key = await stellarWalletAdapter.getPublicKey();
|
|
36
|
-
setPublicKey(key);
|
|
37
|
-
return key;
|
|
38
|
-
} catch (caught) {
|
|
39
|
-
const message = caught instanceof Error ? caught.message : String(caught);
|
|
40
|
-
setPublicKey(null);
|
|
41
|
-
setError(message);
|
|
42
|
-
throw caught;
|
|
43
|
-
} finally {
|
|
44
|
-
setLoading(false);
|
|
45
|
-
}
|
|
46
|
-
}, [selectedWallet]);
|
|
47
|
-
|
|
48
|
-
const disconnect = useCallback(() => {
|
|
49
|
-
setPublicKey(null);
|
|
50
|
-
setError(null);
|
|
51
|
-
}, []);
|
|
52
|
-
|
|
53
|
-
useEffect(() => {
|
|
54
|
-
stellarWalletAdapter.onWalletConnectSessionDeleted(() => {
|
|
55
|
-
setPublicKey(null);
|
|
56
|
-
});
|
|
57
|
-
}, []);
|
|
58
|
-
|
|
59
|
-
return {
|
|
60
|
-
publicKey,
|
|
61
|
-
selectedWallet,
|
|
62
|
-
network,
|
|
63
|
-
loading,
|
|
64
|
-
error,
|
|
65
|
-
selectWallet,
|
|
66
|
-
selectNetwork,
|
|
67
|
-
connect,
|
|
68
|
-
disconnect
|
|
69
|
-
};
|
|
70
|
-
}
|