@btc-vision/walletconnect 1.9.1 → 1.9.3

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.
@@ -10,3 +10,17 @@ export interface WalletInformation {
10
10
  isInstalled: boolean;
11
11
  isConnected: boolean;
12
12
  }
13
+ export interface WalletBalance {
14
+ total: number;
15
+ confirmed: number;
16
+ unconfirmed: number;
17
+ csv75_total: number;
18
+ csv75_unlocked: number;
19
+ csv75_locked: number;
20
+ csv1_total: number;
21
+ csv1_unlocked: number;
22
+ csv1_locked: number;
23
+ p2wda_total_amount: number;
24
+ p2wda_pending_amount: number;
25
+ usd_value: string;
26
+ }
@@ -1,6 +1,6 @@
1
1
  import { Address, type Unisat, UnisatSigner } from '@btc-vision/transaction';
2
2
  import { AbstractRpcProvider } from 'opnet';
3
- import type { WalletConnectNetwork, WalletInformation } from '../types.ts';
3
+ import type { WalletBalance, WalletConnectNetwork, WalletInformation } from '../types.ts';
4
4
  import { type SupportedWallets } from '../wallets';
5
5
  export type WalletConnectContextType = {
6
6
  allWallets: WalletInformation[];
@@ -16,5 +16,6 @@ export type WalletConnectContextType = {
16
16
  disconnect: () => void;
17
17
  provider: AbstractRpcProvider | null;
18
18
  signer: UnisatSigner | null;
19
+ walletBalance: WalletBalance | null;
19
20
  };
20
21
  export declare const WalletConnectContext: import("react").Context<WalletConnectContextType | undefined>;
@@ -22,6 +22,7 @@ const WalletConnectProvider = ({ theme, children }) => {
22
22
  const [walletInstance, setWalletInstance] = useState(null);
23
23
  const [provider, setProvider] = useState(null);
24
24
  const [signer, setSigner] = useState(null);
25
+ const [walletBalance, setWalletBalance] = useState(null);
25
26
  const clearConnectError = useCallback(() => {
26
27
  if (timeoutRef.current)
27
28
  clearTimeout(timeoutRef.current);
@@ -88,6 +89,7 @@ const WalletConnectProvider = ({ theme, children }) => {
88
89
  WalletController.setChainChangedHook(chainChanged);
89
90
  WalletController.setDisconnectHook(disconnect);
90
91
  closeConnectModal();
92
+ console.log('Connected to wallet:', wallet);
91
93
  setSelectedWallet(wallet);
92
94
  localStorage.setItem('WC_SelectedWallet', wallet);
93
95
  }
@@ -132,14 +134,12 @@ const WalletConnectProvider = ({ theme, children }) => {
132
134
  void attemptReconnect();
133
135
  }, [attemptReconnect]);
134
136
  const accountsChanged = useCallback(async (accounts) => {
135
- console.log('Accounts', accounts);
136
- if (selectedWallet) {
137
- const account = accounts.length > 0 ? accounts[0] : null;
138
- setWalletAddress(account);
139
- const publicKey = account ? await WalletController.getPublicKey() : null;
140
- setPublicKey(publicKey);
141
- }
142
- }, [selectedWallet, setWalletAddress, setPublicKey]);
137
+ console.log('Account changed, updating address');
138
+ const account = accounts.length > 0 ? accounts[0] : null;
139
+ setWalletAddress(account);
140
+ const publicKey = account ? await WalletController.getPublicKey() : null;
141
+ setPublicKey(publicKey);
142
+ }, [setWalletAddress, setPublicKey]);
143
143
  const chainChanged = useCallback((network) => {
144
144
  if (selectedWallet) {
145
145
  setNetwork(network);
@@ -178,6 +178,24 @@ const WalletConnectProvider = ({ theme, children }) => {
178
178
  };
179
179
  void updateSigner();
180
180
  }, [network, publicKey]);
181
+ useEffect(() => {
182
+ const fetchBalance = async () => {
183
+ if (walletAddress && walletInstance) {
184
+ try {
185
+ const balance = (await walletInstance.getBalance());
186
+ setWalletBalance(balance);
187
+ }
188
+ catch (error) {
189
+ console.error('Error fetching balance:', error);
190
+ setWalletBalance(null);
191
+ }
192
+ }
193
+ else {
194
+ setWalletBalance(null);
195
+ }
196
+ };
197
+ void fetchBalance();
198
+ }, [walletAddress, walletInstance]);
181
199
  const currentTheme = useMemo(() => {
182
200
  const currentTheme = theme || 'light';
183
201
  return `wallet-connect-${currentTheme}-theme`;
@@ -198,6 +216,7 @@ const WalletConnectProvider = ({ theme, children }) => {
198
216
  walletInstance,
199
217
  provider,
200
218
  signer,
219
+ walletBalance,
201
220
  walletType,
202
221
  }, children: [children, modalOpen && (_jsx("div", { className: `wallet-connect-modal-backdrop ${currentTheme}`, children: _jsxs("div", { className: "wallet-connect-modal", role: "dialog", "aria-modal": "true", "aria-labelledby": "wallet-connect-modal-title", children: [_jsxs("div", { className: "wallet-connect-header", children: [_jsx("span", { children: "Connect Wallet" }), _jsx("button", { className: "close", onClick: () => closeConnectModal(), children: _jsx("span", { className: "close-icon", children: _jsx("svg", { width: "30px", height: "30px", viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg", children: _jsx("path", { className: "close-x-path", d: "M16 8L8 16M8.00001 8L16 16", stroke: "#fff", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round" }) }) }) })] }), connectError && (_jsx("div", { className: "wallet-connect-error", children: _jsx("p", { className: "error-message", children: connectError }) })), availableWallets.length > 0 ? (_jsx("div", { className: "wallet-list", children: availableWallets.map((wallet) => (_jsxs("button", { onClick: () => connectToWallet(wallet.name), disabled: connecting || !wallet.controller.isInstalled(), className: `wallet-button ${wallet.controller.isInstalled()
203
222
  ? 'wallet-installed'
package/build/types.d.ts CHANGED
@@ -10,3 +10,17 @@ export interface WalletInformation {
10
10
  isInstalled: boolean;
11
11
  isConnected: boolean;
12
12
  }
13
+ export interface WalletBalance {
14
+ total: number;
15
+ confirmed: number;
16
+ unconfirmed: number;
17
+ csv75_total: number;
18
+ csv75_unlocked: number;
19
+ csv75_locked: number;
20
+ csv1_total: number;
21
+ csv1_unlocked: number;
22
+ csv1_locked: number;
23
+ p2wda_total_amount: number;
24
+ p2wda_pending_amount: number;
25
+ usd_value: string;
26
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@btc-vision/walletconnect",
3
3
  "type": "module",
4
- "version": "1.9.1",
4
+ "version": "1.9.3",
5
5
  "author": "impredmet",
6
6
  "description": "The OP_NET Wallet Connect library helps your dApp connect to any compatible wallet.",
7
7
  "engines": {
@@ -82,7 +82,7 @@
82
82
  "react-dom": "^19.1.1",
83
83
  "stream-browserify": "^3.0.0",
84
84
  "stream-http": "^3.2.0",
85
- "typescript-eslint": "^8.43.0",
85
+ "typescript-eslint": "^8.44.0",
86
86
  "webpack-cli": "^6.0.1"
87
87
  },
88
88
  "dependencies": {
@@ -94,7 +94,7 @@
94
94
  "gulp-clean": "^0.4.0",
95
95
  "gulp-eslint-new": "^2.5.0",
96
96
  "gulp-logger-new": "^1.0.1",
97
- "opnet": "^1.6.34",
97
+ "opnet": "^1.6.35",
98
98
  "style-loader": "^4.0.0",
99
99
  "webpack": "^5.101.3"
100
100
  }
@@ -1,7 +1,7 @@
1
1
  import { Address, type Unisat, UnisatSigner } from '@btc-vision/transaction';
2
2
  import { AbstractRpcProvider } from 'opnet';
3
3
  import { createContext } from 'react';
4
- import type { WalletConnectNetwork, WalletInformation } from '../types.ts';
4
+ import type { WalletBalance, WalletConnectNetwork, WalletInformation } from '../types.ts';
5
5
  import { type SupportedWallets } from '../wallets';
6
6
 
7
7
  export type WalletConnectContextType = {
@@ -18,6 +18,7 @@ export type WalletConnectContextType = {
18
18
  disconnect: () => void;
19
19
  provider: AbstractRpcProvider | null;
20
20
  signer: UnisatSigner | null;
21
+ walletBalance: WalletBalance | null;
21
22
  };
22
23
 
23
24
  export const WalletConnectContext = createContext<WalletConnectContextType | undefined>(undefined);
@@ -2,7 +2,7 @@ import { Address, type Unisat, UnisatSigner } from '@btc-vision/transaction';
2
2
  import { AbstractRpcProvider } from 'opnet';
3
3
  import React, { type ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react';
4
4
  import { WalletConnectContext } from '../context/WalletConnectContext';
5
- import type { WalletConnectNetwork, WalletInformation } from '../types.ts';
5
+ import type { WalletBalance, WalletConnectNetwork, WalletInformation } from '../types.ts';
6
6
  import '../utils/style.css';
7
7
  import '../utils/theme.css';
8
8
  import { type SupportedWallets, WalletController } from '../wallets';
@@ -38,6 +38,7 @@ const WalletConnectProvider: React.FC<WalletConnectProviderProps> = ({ theme, ch
38
38
  const [walletInstance, setWalletInstance] = useState<Unisat | null>(null);
39
39
  const [provider, setProvider] = useState<AbstractRpcProvider | null>(null);
40
40
  const [signer, setSigner] = useState<UnisatSigner | null>(null);
41
+ const [walletBalance, setWalletBalance] = useState<WalletBalance | null>(null);
41
42
 
42
43
  const clearConnectError = useCallback(() => {
43
44
  if (timeoutRef.current) clearTimeout(timeoutRef.current);
@@ -119,6 +120,7 @@ const WalletConnectProvider: React.FC<WalletConnectProviderProps> = ({ theme, ch
119
120
  WalletController.setDisconnectHook(disconnect);
120
121
 
121
122
  closeConnectModal();
123
+ console.log('Connected to wallet:', wallet);
122
124
  setSelectedWallet(wallet);
123
125
  localStorage.setItem('WC_SelectedWallet', wallet);
124
126
  } else if (response.data && 'message' in response.data) {
@@ -172,15 +174,13 @@ const WalletConnectProvider: React.FC<WalletConnectProviderProps> = ({ theme, ch
172
174
 
173
175
  const accountsChanged = useCallback(
174
176
  async (accounts: string[]) => {
175
- console.log('Accounts', accounts);
176
- if (selectedWallet) {
177
- const account = accounts.length > 0 ? accounts[0] : null;
178
- setWalletAddress(account);
179
- const publicKey = account ? await WalletController.getPublicKey() : null;
180
- setPublicKey(publicKey);
181
- }
177
+ console.log('Account changed, updating address');
178
+ const account = accounts.length > 0 ? accounts[0] : null;
179
+ setWalletAddress(account);
180
+ const publicKey = account ? await WalletController.getPublicKey() : null;
181
+ setPublicKey(publicKey);
182
182
  },
183
- [selectedWallet, setWalletAddress, setPublicKey],
183
+ [setWalletAddress, setPublicKey],
184
184
  );
185
185
 
186
186
  const chainChanged = useCallback(
@@ -235,6 +235,23 @@ const WalletConnectProvider: React.FC<WalletConnectProviderProps> = ({ theme, ch
235
235
  void updateSigner();
236
236
  }, [network, publicKey]);
237
237
 
238
+ useEffect(() => {
239
+ const fetchBalance = async () => {
240
+ if (walletAddress && walletInstance) {
241
+ try {
242
+ const balance = (await walletInstance.getBalance()) as WalletBalance | null;
243
+ setWalletBalance(balance);
244
+ } catch (error) {
245
+ console.error('Error fetching balance:', error);
246
+ setWalletBalance(null);
247
+ }
248
+ } else {
249
+ setWalletBalance(null);
250
+ }
251
+ };
252
+ void fetchBalance();
253
+ }, [walletAddress, walletInstance]);
254
+
238
255
  const currentTheme = useMemo(() => {
239
256
  const currentTheme = theme || 'light';
240
257
  return `wallet-connect-${currentTheme}-theme`;
@@ -259,6 +276,7 @@ const WalletConnectProvider: React.FC<WalletConnectProviderProps> = ({ theme, ch
259
276
  walletInstance,
260
277
  provider,
261
278
  signer,
279
+ walletBalance,
262
280
  walletType,
263
281
  }}>
264
282
  {children}
package/src/types.ts CHANGED
@@ -12,3 +12,18 @@ export interface WalletInformation {
12
12
  isInstalled: boolean;
13
13
  isConnected: boolean;
14
14
  }
15
+
16
+ export interface WalletBalance {
17
+ total: number;
18
+ confirmed: number;
19
+ unconfirmed: number;
20
+ csv75_total: number;
21
+ csv75_unlocked: number;
22
+ csv75_locked: number;
23
+ csv1_total: number;
24
+ csv1_unlocked: number;
25
+ csv1_locked: number;
26
+ p2wda_total_amount: number;
27
+ p2wda_pending_amount: number;
28
+ usd_value: string;
29
+ }