miniwallet 0.1.7 → 0.1.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/dist/miniwallet.css +725 -1
- package/dist/miniwallet.js +294 -200
- package/package.json +1 -1
package/dist/miniwallet.js
CHANGED
|
@@ -1,26 +1,32 @@
|
|
|
1
1
|
import "./miniwallet.css";
|
|
2
|
-
import { createContext
|
|
3
|
-
import { jsx
|
|
2
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
4
|
//#region src/MiniWalletContext.tsx
|
|
5
|
-
var
|
|
5
|
+
var MiniWalletContext = createContext(null);
|
|
6
6
|
//#endregion
|
|
7
7
|
//#region src/utils/formatUnits.ts
|
|
8
|
-
function
|
|
9
|
-
if (typeof
|
|
10
|
-
|
|
11
|
-
if (!
|
|
12
|
-
return BigInt(
|
|
8
|
+
function parseUnitValue(value) {
|
|
9
|
+
if (typeof value === "bigint") return value;
|
|
10
|
+
const trimmed = value.trim();
|
|
11
|
+
if (!trimmed) throw new Error("value must not be empty");
|
|
12
|
+
return BigInt(trimmed);
|
|
13
13
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Convert smallest-unit amount (e.g. wei) to a human-readable decimal string.
|
|
16
|
+
*/
|
|
17
|
+
function formatUnits(value, decimals) {
|
|
18
|
+
if (!Number.isInteger(decimals) || decimals < 0) throw new Error("decimals must be a non-negative integer");
|
|
19
|
+
const amount = parseUnitValue(value);
|
|
20
|
+
if (decimals === 0) return amount.toString();
|
|
21
|
+
const divisor = 10n ** BigInt(decimals);
|
|
22
|
+
const integer = amount / divisor;
|
|
23
|
+
const fraction = amount % divisor;
|
|
24
|
+
if (fraction === 0n) return integer.toString();
|
|
25
|
+
return `${integer}.${fraction.toString().padStart(decimals, "0").replace(/0+$/, "")}`;
|
|
20
26
|
}
|
|
21
27
|
//#endregion
|
|
22
28
|
//#region src/constants.ts
|
|
23
|
-
var
|
|
29
|
+
var WALLETS = [
|
|
24
30
|
{
|
|
25
31
|
id: "metamask",
|
|
26
32
|
name: "MetaMask",
|
|
@@ -41,13 +47,16 @@ var d = [
|
|
|
41
47
|
name: "Coinbase",
|
|
42
48
|
icon: "data:image/svg+xml,%3c?xml%20version='1.0'%20standalone='no'?%3e%3c!DOCTYPE%20svg%20PUBLIC%20'-//W3C//DTD%20SVG%201.1//EN'%20'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'%3e%3csvg%20t='1783439807758'%20class='icon'%20viewBox='0%200%201024%201024'%20version='1.1'%20xmlns='http://www.w3.org/2000/svg'%20p-id='13209'%20xmlns:xlink='http://www.w3.org/1999/xlink'%20width='32'%20height='32'%3e%3cpath%20d='M1024%20186.181818a186.181818%20186.181818%200%200%200-186.181818-186.181818H186.181818a186.181818%20186.181818%200%200%200-186.181818%20186.181818v651.636364a186.181818%20186.181818%200%200%200%20186.181818%20186.181818h651.636364a186.181818%20186.181818%200%200%200%20186.181818-186.181818V186.181818z'%20fill='%232C5FF6'%20p-id='13210'%3e%3c/path%3e%3cpath%20d='M512%20870.4a358.4%20358.4%200%201%200%200-716.8%20358.4%20358.4%200%200%200%200%20716.8zM422.4%20394.984727h179.2a27.461818%2027.461818%200%200%201%2027.461818%2027.415273v179.2a27.461818%2027.461818%200%200%201-27.461818%2027.461818h-179.2a27.461818%2027.461818%200%200%201-27.461818-27.461818v-179.2a27.461818%2027.461818%200%200%201%2027.461818-27.461818z'%20fill='%23FFFFFF'%20p-id='13211'%3e%3c/path%3e%3c/svg%3e"
|
|
43
49
|
}
|
|
44
|
-
]
|
|
50
|
+
];
|
|
51
|
+
/** EIP-6963 rdns → wallet id */
|
|
52
|
+
var RDNS_TO_WALLET_ID = {
|
|
45
53
|
"io.metamask": "metamask",
|
|
46
54
|
"com.okex.wallet": "okx",
|
|
47
55
|
"com.okx.wallet": "okx",
|
|
48
56
|
"app.phantom": "phantom",
|
|
49
57
|
"com.coinbase.wallet": "coinbase"
|
|
50
|
-
}
|
|
58
|
+
};
|
|
59
|
+
var DEFAULT_CHAINS = [
|
|
51
60
|
{
|
|
52
61
|
id: 1,
|
|
53
62
|
name: "Ethereum",
|
|
@@ -69,260 +78,343 @@ var d = [
|
|
|
69
78
|
];
|
|
70
79
|
//#endregion
|
|
71
80
|
//#region src/MiniWalletProvider.tsx
|
|
72
|
-
function
|
|
73
|
-
|
|
74
|
-
|
|
81
|
+
function MiniWalletProvider({ children, chains = [] }) {
|
|
82
|
+
const [account, setAccount] = useState("");
|
|
83
|
+
const [selectedWalletId, setSelectedWalletId] = useState("");
|
|
84
|
+
const [selectedChainId, setSelectedChainId] = useState(0);
|
|
85
|
+
const [balanceValue, setBalanceValue] = useState("");
|
|
86
|
+
const [connectingWallet, setConnectingWallet] = useState(null);
|
|
87
|
+
const eip6963ProvidersRef = useRef({});
|
|
88
|
+
const [providersVersion, setProvidersVersion] = useState(0);
|
|
89
|
+
const chainsList = useMemo(() => {
|
|
90
|
+
return chains.length > 0 ? chains : DEFAULT_CHAINS;
|
|
91
|
+
}, [chains]);
|
|
92
|
+
const curWalletProvider = useMemo(() => {
|
|
93
|
+
return eip6963ProvidersRef.current[selectedWalletId] ?? null;
|
|
94
|
+
}, [providersVersion, selectedWalletId]);
|
|
95
|
+
const wallet = useMemo(() => {
|
|
96
|
+
return WALLETS.find((w) => w.id === selectedWalletId);
|
|
97
|
+
}, [selectedWalletId]);
|
|
98
|
+
const isConnected = useMemo(() => {
|
|
99
|
+
return !!account;
|
|
100
|
+
}, [account]);
|
|
101
|
+
const selectedChain = useMemo(() => {
|
|
102
|
+
return chainsList.find((c) => c.id === selectedChainId) || null;
|
|
103
|
+
}, [chainsList, selectedChainId]);
|
|
104
|
+
const balance = useMemo(() => {
|
|
105
|
+
if (!selectedChain || !balanceValue || !selectedChain?.decimals) return "0";
|
|
106
|
+
return Number(formatUnits(balanceValue, selectedChain.decimals)).toFixed(3);
|
|
107
|
+
}, [balanceValue, selectedChain]);
|
|
108
|
+
const connect = useCallback(async (w) => {
|
|
109
|
+
const { id } = w;
|
|
75
110
|
try {
|
|
76
|
-
console.log("eip6963Providers: ",
|
|
77
|
-
|
|
78
|
-
if (!
|
|
111
|
+
console.log("eip6963Providers: ", eip6963ProvidersRef.current);
|
|
112
|
+
const provider = eip6963ProvidersRef.current[id];
|
|
113
|
+
if (!provider || typeof provider.request !== "function") {
|
|
79
114
|
alert("请安装钱包");
|
|
80
115
|
return;
|
|
81
116
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
117
|
+
setConnectingWallet(w);
|
|
118
|
+
console.log("provider: ", provider);
|
|
119
|
+
const accounts = await provider.request({ method: "eth_requestAccounts" });
|
|
120
|
+
console.log("accounts: ", accounts);
|
|
121
|
+
console.log("provider.chainId: ", provider.chainId);
|
|
122
|
+
const chainId = parseInt(String(provider.chainId ?? "0x0"), 16);
|
|
123
|
+
setAccount(accounts[0]);
|
|
124
|
+
setSelectedWalletId(id);
|
|
125
|
+
setSelectedChainId(chainId);
|
|
126
|
+
localStorage.setItem("miniwallet:lastWalletId", id);
|
|
127
|
+
setConnectingWallet(null);
|
|
128
|
+
} catch (error) {
|
|
129
|
+
console.log("error: ", error);
|
|
130
|
+
const e = error;
|
|
131
|
+
const errCode = e?.code ?? "";
|
|
132
|
+
const errMsg = e?.message ?? "";
|
|
133
|
+
if (errCode === -32002) {
|
|
134
|
+
alert(`连接已发起,请在扩展中确认连接`);
|
|
92
135
|
return;
|
|
93
136
|
}
|
|
94
|
-
alert(`Connect failed${
|
|
137
|
+
alert(`Connect failed${errCode && `\n${errCode}`}${errMsg && `\n${errMsg}`}`);
|
|
138
|
+
setConnectingWallet(null);
|
|
95
139
|
}
|
|
96
|
-
}, [
|
|
140
|
+
}, []);
|
|
141
|
+
const disconnect = useCallback(async () => {
|
|
97
142
|
try {
|
|
98
|
-
if (!
|
|
143
|
+
if (!curWalletProvider || !selectedWalletId) {
|
|
99
144
|
alert("请安装钱包");
|
|
100
145
|
return;
|
|
101
146
|
}
|
|
102
|
-
|
|
147
|
+
if (selectedWalletId !== "okx") await curWalletProvider.request({
|
|
103
148
|
method: "wallet_revokePermissions",
|
|
104
149
|
params: [{ eth_accounts: {} }]
|
|
105
150
|
});
|
|
106
151
|
} catch (e) {
|
|
107
152
|
console.error("disconnect error: ", e);
|
|
108
153
|
} finally {
|
|
109
|
-
|
|
154
|
+
setAccount("");
|
|
155
|
+
setSelectedWalletId("");
|
|
156
|
+
setSelectedChainId(0);
|
|
157
|
+
localStorage.removeItem("miniwallet:lastWalletId");
|
|
110
158
|
}
|
|
111
|
-
}, [
|
|
112
|
-
|
|
159
|
+
}, [curWalletProvider, selectedWalletId]);
|
|
160
|
+
const switchChain = useCallback(async (chain) => {
|
|
161
|
+
if (!curWalletProvider) {
|
|
113
162
|
alert("钱包不存在");
|
|
114
163
|
return;
|
|
115
164
|
}
|
|
116
165
|
try {
|
|
117
|
-
await
|
|
166
|
+
await curWalletProvider.request({
|
|
118
167
|
method: "wallet_switchEthereumChain",
|
|
119
|
-
params: [{ chainId: `0x${
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
168
|
+
params: [{ chainId: `0x${chain.id.toString(16)}` }]
|
|
169
|
+
});
|
|
170
|
+
setSelectedChainId(chain.id);
|
|
171
|
+
} catch (switchError) {
|
|
172
|
+
console.log("switchError: ", switchError);
|
|
173
|
+
const e = switchError;
|
|
174
|
+
const errCode = e?.code ?? "";
|
|
175
|
+
const errMsg = e?.message ?? "";
|
|
176
|
+
alert(`Switch failed${errCode && `\n${errCode}`}${errMsg && `\n${errMsg}`}`);
|
|
125
177
|
}
|
|
126
|
-
}, [
|
|
178
|
+
}, [curWalletProvider]);
|
|
179
|
+
const getBalance = useCallback(async () => {
|
|
127
180
|
try {
|
|
128
|
-
if (!
|
|
129
|
-
|
|
181
|
+
if (!account || !curWalletProvider) return;
|
|
182
|
+
const balanceRes = await curWalletProvider.request({
|
|
130
183
|
method: "eth_getBalance",
|
|
131
|
-
params: [
|
|
184
|
+
params: [account, "latest"]
|
|
132
185
|
});
|
|
133
|
-
|
|
186
|
+
setBalanceValue(BigInt(balanceRes).toString());
|
|
134
187
|
} catch (e) {
|
|
135
188
|
console.error("getBalance error: ", e);
|
|
136
189
|
}
|
|
137
|
-
}, [
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
190
|
+
}, [account, curWalletProvider]);
|
|
191
|
+
const value = useMemo(() => ({
|
|
192
|
+
account,
|
|
193
|
+
balance,
|
|
194
|
+
isConnected,
|
|
195
|
+
connectingWallet,
|
|
196
|
+
selectedChain,
|
|
197
|
+
wallet,
|
|
198
|
+
chains: chainsList,
|
|
199
|
+
connect,
|
|
200
|
+
switchChain,
|
|
201
|
+
disconnect
|
|
148
202
|
}), [
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
203
|
+
account,
|
|
204
|
+
balance,
|
|
205
|
+
isConnected,
|
|
206
|
+
connectingWallet,
|
|
207
|
+
selectedChain,
|
|
208
|
+
wallet,
|
|
209
|
+
chainsList,
|
|
210
|
+
connect,
|
|
211
|
+
switchChain,
|
|
212
|
+
disconnect
|
|
159
213
|
]);
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
214
|
+
useEffect(() => {
|
|
215
|
+
const handler = ((event) => {
|
|
216
|
+
const { info, provider } = event.detail ?? {};
|
|
217
|
+
const walletId = RDNS_TO_WALLET_ID[info?.rdns];
|
|
218
|
+
if (!walletId || typeof provider?.request !== "function") return;
|
|
219
|
+
if (eip6963ProvidersRef.current[walletId] === provider) return;
|
|
220
|
+
eip6963ProvidersRef.current = {
|
|
221
|
+
...eip6963ProvidersRef.current,
|
|
222
|
+
[walletId]: provider
|
|
223
|
+
};
|
|
224
|
+
setProvidersVersion((v) => v + 1);
|
|
167
225
|
});
|
|
168
|
-
|
|
169
|
-
|
|
226
|
+
window.addEventListener("eip6963:announceProvider", handler);
|
|
227
|
+
window.dispatchEvent(new Event("eip6963:requestProvider"));
|
|
228
|
+
return () => {
|
|
229
|
+
window.removeEventListener("eip6963:announceProvider", handler);
|
|
170
230
|
};
|
|
171
|
-
}, [])
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
console.log("
|
|
231
|
+
}, []);
|
|
232
|
+
useEffect(() => {
|
|
233
|
+
if (selectedChainId) getBalance();
|
|
234
|
+
}, [getBalance, selectedChainId]);
|
|
235
|
+
useEffect(() => {
|
|
236
|
+
if (!isConnected || !curWalletProvider?.on) return;
|
|
237
|
+
const handleChainChanged = (chainId) => {
|
|
238
|
+
console.log("ChainChanged chainId: ", chainId);
|
|
239
|
+
setSelectedChainId(parseInt(chainId, 16));
|
|
179
240
|
};
|
|
180
|
-
|
|
181
|
-
console.log("
|
|
241
|
+
const handleAccountsChanged = (accounts) => {
|
|
242
|
+
console.log("AccountsChanged accounts: ", accounts);
|
|
243
|
+
setAccount(accounts[0] ?? "");
|
|
182
244
|
};
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
return () =>
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
245
|
+
console.log("Listen chainChanged and accountsChanged");
|
|
246
|
+
curWalletProvider.on("chainChanged", handleChainChanged);
|
|
247
|
+
curWalletProvider.on("accountsChanged", handleAccountsChanged);
|
|
248
|
+
return () => {
|
|
249
|
+
console.log("RemoveListener chainChanged and accountsChanged");
|
|
250
|
+
curWalletProvider.removeListener?.("chainChanged", handleChainChanged);
|
|
251
|
+
curWalletProvider.removeListener?.("accountsChanged", handleAccountsChanged);
|
|
252
|
+
};
|
|
253
|
+
}, [curWalletProvider, isConnected]);
|
|
254
|
+
useEffect(() => {
|
|
255
|
+
if (!account) return;
|
|
256
|
+
const timer = setInterval(getBalance, 1e4);
|
|
257
|
+
return () => clearInterval(timer);
|
|
258
|
+
}, [account, getBalance]);
|
|
259
|
+
useEffect(() => {
|
|
260
|
+
const lastWalletId = localStorage.getItem("miniwallet:lastWalletId");
|
|
261
|
+
if (!lastWalletId) return;
|
|
262
|
+
const provider = eip6963ProvidersRef.current[lastWalletId];
|
|
263
|
+
if (!provider) return;
|
|
264
|
+
let cancelled = false;
|
|
265
|
+
(async () => {
|
|
266
|
+
const accounts = await provider.request({ method: "eth_accounts" });
|
|
267
|
+
if (cancelled) return;
|
|
268
|
+
if (!accounts?.length) {
|
|
269
|
+
localStorage.removeItem("miniwallet:lastWalletId");
|
|
270
|
+
return;
|
|
201
271
|
}
|
|
202
|
-
|
|
203
|
-
|
|
272
|
+
setAccount(accounts[0]);
|
|
273
|
+
setSelectedWalletId(lastWalletId);
|
|
274
|
+
setSelectedChainId(parseInt(String(provider.chainId ?? "0x0"), 16));
|
|
275
|
+
})();
|
|
276
|
+
return () => {
|
|
277
|
+
cancelled = true;
|
|
204
278
|
};
|
|
205
|
-
}, [
|
|
206
|
-
|
|
207
|
-
|
|
279
|
+
}, [providersVersion]);
|
|
280
|
+
return /* @__PURE__ */ jsx(MiniWalletContext.Provider, {
|
|
281
|
+
value,
|
|
282
|
+
children
|
|
208
283
|
});
|
|
209
284
|
}
|
|
210
285
|
//#endregion
|
|
211
286
|
//#region src/useMiniWallet.ts
|
|
212
|
-
function
|
|
213
|
-
|
|
214
|
-
if (!
|
|
215
|
-
return
|
|
287
|
+
function useMiniWallet() {
|
|
288
|
+
const ctx = useContext(MiniWalletContext);
|
|
289
|
+
if (!ctx) throw new Error("useMiniWallet 必须在 MiniWalletProvider 内使用");
|
|
290
|
+
return ctx;
|
|
216
291
|
}
|
|
217
292
|
//#endregion
|
|
218
293
|
//#region src/icons/disconnect.svg
|
|
219
|
-
var
|
|
294
|
+
var disconnect_default = "data:image/svg+xml,%3c?xml%20version='1.0'%20standalone='no'?%3e%3c!DOCTYPE%20svg%20PUBLIC%20'-//W3C//DTD%20SVG%201.1//EN'%20'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'%3e%3csvg%20t='1783575961451'%20class='icon'%20viewBox='0%200%201024%201024'%20version='1.1'%20xmlns='http://www.w3.org/2000/svg'%20p-id='5071'%20xmlns:xlink='http://www.w3.org/1999/xlink'%20width='32'%20height='32'%3e%3cpath%20d='M512.922%2063.583c248.335%200%20449.712%20201.384%20449.712%20449.71%200%20248.333-201.377%20449.702-449.712%20449.702-248.333%200-449.71-201.369-449.71-449.702%200-248.326%20201.377-449.71%20449.71-449.71z%20m148.683%20213.634l-35.351%2061.247a206.8%20206.8%200%200%201%2034.37%2027.739c37.359%2037.351%2060.475%2088.96%2060.475%20145.955%200%2057.004-23.117%20108.607-60.475%20145.965-37.344%2037.352-88.945%2060.469-145.949%2060.469-56.987%200-108.606-23.117-145.965-60.469-37.359-37.359-60.461-88.962-60.461-145.965%200-56.995%2023.117-108.605%2060.461-145.955a209.04%20209.04%200%200%201%2027.762-23.286l-35.43-61.359a278.69%20278.69%200%200%200-42.279%2034.69c-50.156%2050.148-81.18%20119.417-81.18%20195.91%200%2076.504%2031.041%20145.773%2081.18%20195.929%2050.139%2050.139%20119.408%2081.166%20195.912%2081.166%2076.502%200%20145.771-31.026%20195.91-81.166%2050.156-50.156%2081.182-119.425%2081.182-195.929%200-76.494-31.026-145.763-81.182-195.91a277.704%20277.704%200%200%200-48.98-39.031zM473.618%20128.865v337.849h75.458V128.865h-75.458z'%20fill='%23e6523f'%20p-id='5072'%3e%3c/path%3e%3c/svg%3e";
|
|
220
295
|
//#endregion
|
|
221
296
|
//#region src/MiniWalletButton.tsx
|
|
222
|
-
function
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
297
|
+
function MiniWalletButton({ label = "Connect Wallet" }) {
|
|
298
|
+
const { account, balance, isConnected, connectingWallet, selectedChain, wallet, switchChain, chains, connect, disconnect } = useMiniWallet();
|
|
299
|
+
const [modalOpen, setModalOpen] = useState(false);
|
|
300
|
+
const [isConnectCalled, setIsConnectCalled] = useState(false);
|
|
301
|
+
const [accountModalOpen, setAccountModalOpen] = useState(false);
|
|
302
|
+
const [copyLabel, setCopyLabel] = useState("复制地址");
|
|
303
|
+
const accountText = useMemo(() => {
|
|
304
|
+
if (account) return `${account.slice(0, 6)}...${account.slice(-4)}`;
|
|
305
|
+
return "";
|
|
306
|
+
}, [account]);
|
|
307
|
+
const openModal = useCallback(() => {
|
|
308
|
+
setModalOpen(true);
|
|
309
|
+
}, []);
|
|
310
|
+
const closeModal = useCallback(() => {
|
|
311
|
+
setModalOpen(false);
|
|
312
|
+
}, []);
|
|
313
|
+
const copyAddress = useCallback(async () => {
|
|
228
314
|
try {
|
|
229
|
-
await navigator.clipboard.writeText(
|
|
230
|
-
|
|
315
|
+
await navigator.clipboard.writeText(account);
|
|
316
|
+
setCopyLabel("已复制!");
|
|
317
|
+
setTimeout(() => {
|
|
318
|
+
setCopyLabel("复制地址");
|
|
231
319
|
}, 1e3);
|
|
232
|
-
} catch (
|
|
233
|
-
console.error("copy",
|
|
320
|
+
} catch (err) {
|
|
321
|
+
console.error("copy", err);
|
|
322
|
+
}
|
|
323
|
+
}, [account]);
|
|
324
|
+
useEffect(() => {
|
|
325
|
+
if (!connectingWallet) {
|
|
326
|
+
setModalOpen(false);
|
|
327
|
+
setAccountModalOpen(false);
|
|
234
328
|
}
|
|
235
|
-
}, [
|
|
236
|
-
return
|
|
237
|
-
u || (x(!1), T(!1));
|
|
238
|
-
}, [u]), l ? /* @__PURE__ */ s("section", {
|
|
329
|
+
}, [connectingWallet]);
|
|
330
|
+
return isConnected ? /* @__PURE__ */ jsxs("section", {
|
|
239
331
|
className: "inline-flex items-center justify-center text-center",
|
|
240
332
|
children: [
|
|
241
|
-
/* @__PURE__ */
|
|
242
|
-
value:
|
|
243
|
-
onChange: (e) =>
|
|
333
|
+
/* @__PURE__ */ jsx("select", {
|
|
334
|
+
value: selectedChain?.id,
|
|
335
|
+
onChange: (e) => switchChain(chains.find((c) => c.id === Number(e.target.value))),
|
|
244
336
|
className: "bg-transparent1 mr-4 cursor-pointer rounded-sm border-none p-1 text-gray-600 outline-none hover:bg-gray-100",
|
|
245
|
-
children:
|
|
246
|
-
value:
|
|
247
|
-
children:
|
|
248
|
-
},
|
|
337
|
+
children: chains.map((chain) => /* @__PURE__ */ jsx("option", {
|
|
338
|
+
value: chain.id,
|
|
339
|
+
children: chain.name
|
|
340
|
+
}, chain.id))
|
|
249
341
|
}),
|
|
250
|
-
/* @__PURE__ */
|
|
342
|
+
/* @__PURE__ */ jsxs("div", {
|
|
251
343
|
className: "flex items-center justify-center rounded-2xl bg-white pr-3 shadow-lg inset-ring-1 shadow-gray-200 inset-ring-gray-100",
|
|
252
|
-
children: [/* @__PURE__ */
|
|
344
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
253
345
|
onClick: () => {
|
|
254
|
-
|
|
346
|
+
setAccountModalOpen(true);
|
|
255
347
|
},
|
|
256
348
|
className: "flex cursor-pointer items-center justify-center space-x-2 rounded-l-2xl py-2.5 pr-1 pl-3 hover:bg-gray-50",
|
|
257
349
|
children: [
|
|
258
|
-
|
|
259
|
-
src:
|
|
260
|
-
alt:
|
|
350
|
+
wallet && /* @__PURE__ */ jsx("img", {
|
|
351
|
+
src: wallet.icon,
|
|
352
|
+
alt: wallet.name,
|
|
261
353
|
className: "h-6 w-6"
|
|
262
354
|
}),
|
|
263
|
-
/* @__PURE__ */
|
|
355
|
+
/* @__PURE__ */ jsx("div", {
|
|
264
356
|
className: "mr-1",
|
|
265
|
-
children:
|
|
357
|
+
children: accountText
|
|
266
358
|
}),
|
|
267
|
-
/* @__PURE__ */
|
|
359
|
+
/* @__PURE__ */ jsxs("span", {
|
|
268
360
|
className: "text-sm text-gray-400",
|
|
269
361
|
children: [
|
|
270
362
|
"(",
|
|
271
|
-
|
|
363
|
+
balance,
|
|
272
364
|
" ",
|
|
273
|
-
|
|
365
|
+
selectedChain?.symbol ?? "",
|
|
274
366
|
")"
|
|
275
367
|
]
|
|
276
368
|
})
|
|
277
369
|
]
|
|
278
|
-
}), /* @__PURE__ */
|
|
370
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
279
371
|
className: "ml-1 h-6 w-6 cursor-pointer rounded-full",
|
|
280
|
-
onClick:
|
|
281
|
-
children: /* @__PURE__ */
|
|
282
|
-
src:
|
|
372
|
+
onClick: disconnect,
|
|
373
|
+
children: /* @__PURE__ */ jsx("img", {
|
|
374
|
+
src: disconnect_default,
|
|
283
375
|
alt: "disconnect",
|
|
284
376
|
className: "h-[22px] w-6"
|
|
285
377
|
})
|
|
286
378
|
})]
|
|
287
379
|
}),
|
|
288
|
-
|
|
380
|
+
accountModalOpen && /* @__PURE__ */ jsx("div", {
|
|
289
381
|
className: "z-index-10 fixed top-0 right-0 bottom-0 left-0 bg-[rgba(0,0,0,0.5)]",
|
|
290
|
-
children: /* @__PURE__ */
|
|
382
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
291
383
|
className: "absolute top-1/2 left-1/2 flex h-auto w-[380px] -translate-x-1/2 -translate-y-1/2 flex-col items-center gap-3 rounded-3xl bg-white p-10 pt-5 text-center",
|
|
292
384
|
children: [
|
|
293
|
-
/* @__PURE__ */
|
|
385
|
+
/* @__PURE__ */ jsx("div", {
|
|
294
386
|
className: "mb-[-12px] w-full text-right",
|
|
295
|
-
children: /* @__PURE__ */
|
|
387
|
+
children: /* @__PURE__ */ jsx("button", {
|
|
296
388
|
className: "mr-[-20px] cursor-pointer rounded-full p-1 hover:bg-gray-100",
|
|
297
389
|
onClick: () => {
|
|
298
|
-
|
|
390
|
+
setAccountModalOpen(false);
|
|
299
391
|
},
|
|
300
|
-
children: /* @__PURE__ */
|
|
392
|
+
children: /* @__PURE__ */ jsxs("svg", {
|
|
301
393
|
xmlns: "http://www.w3.org/2000/svg",
|
|
302
394
|
viewBox: "0 0 24 24",
|
|
303
395
|
fill: "none",
|
|
304
396
|
stroke: "currentColor",
|
|
305
397
|
strokeWidth: "2",
|
|
306
398
|
className: "h-8 w-8",
|
|
307
|
-
children: [/* @__PURE__ */
|
|
399
|
+
children: [/* @__PURE__ */ jsx("path", { d: "M18 6 6 18" }), /* @__PURE__ */ jsx("path", { d: "m6 6 12 12" })]
|
|
308
400
|
})
|
|
309
401
|
})
|
|
310
402
|
}),
|
|
311
|
-
|
|
312
|
-
src:
|
|
313
|
-
alt:
|
|
403
|
+
wallet && /* @__PURE__ */ jsx("img", {
|
|
404
|
+
src: wallet.icon,
|
|
405
|
+
alt: wallet.name,
|
|
314
406
|
className: "h-16 w-16"
|
|
315
407
|
}),
|
|
316
|
-
/* @__PURE__ */
|
|
317
|
-
/* @__PURE__ */
|
|
408
|
+
/* @__PURE__ */ jsx("p", { children: accountText }),
|
|
409
|
+
/* @__PURE__ */ jsxs("div", {
|
|
318
410
|
className: "mt-2 w-full space-x-10",
|
|
319
|
-
children: [/* @__PURE__ */
|
|
320
|
-
onClick:
|
|
411
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
412
|
+
onClick: copyAddress,
|
|
321
413
|
className: "w-[112px] cursor-pointer rounded-xl bg-gray-700 py-2 text-white shadow-md shadow-gray-500/50 outline-none",
|
|
322
|
-
children:
|
|
323
|
-
}), /* @__PURE__ */
|
|
414
|
+
children: copyLabel
|
|
415
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
324
416
|
type: "button",
|
|
325
|
-
onClick:
|
|
417
|
+
onClick: disconnect,
|
|
326
418
|
className: "w-[112px] cursor-pointer rounded-xl bg-red-400 py-2 text-white shadow-md shadow-red-500/50 outline-none",
|
|
327
419
|
children: "断开连接"
|
|
328
420
|
})]
|
|
@@ -331,82 +423,84 @@ function _({ label: e = "Connect Wallet" }) {
|
|
|
331
423
|
})
|
|
332
424
|
})
|
|
333
425
|
]
|
|
334
|
-
}) : /* @__PURE__ */
|
|
426
|
+
}) : /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("button", {
|
|
335
427
|
type: "button",
|
|
336
|
-
onClick:
|
|
428
|
+
onClick: openModal,
|
|
337
429
|
className: "cursor-pointer rounded-xl bg-blue-500 px-4 py-2 text-white shadow-lg shadow-blue-500/50",
|
|
338
|
-
children:
|
|
339
|
-
}),
|
|
430
|
+
children: label
|
|
431
|
+
}), modalOpen && /* @__PURE__ */ jsx("div", {
|
|
340
432
|
className: "z-index-10 fixed top-0 right-0 bottom-0 left-0 bg-[rgba(0,0,0,0.5)]",
|
|
341
|
-
children: /* @__PURE__ */
|
|
433
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
342
434
|
className: "absolute top-1/2 left-1/2 flex h-auto w-[380px] -translate-x-1/2 -translate-y-1/2 flex-col gap-3 rounded-3xl bg-white p-6",
|
|
343
|
-
children: [/* @__PURE__ */
|
|
435
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
344
436
|
className: "mb-2 flex items-center justify-between",
|
|
345
|
-
children: [/* @__PURE__ */
|
|
437
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
346
438
|
className: "text-2xl font-bold",
|
|
347
439
|
children: "Connect Wallet"
|
|
348
|
-
}), /* @__PURE__ */
|
|
440
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
349
441
|
type: "button",
|
|
350
442
|
"aria-label": "Close",
|
|
351
443
|
className: "cursor-pointer rounded-full p-1 hover:bg-gray-100",
|
|
352
444
|
onClick: () => {
|
|
353
|
-
|
|
445
|
+
setIsConnectCalled(false);
|
|
446
|
+
closeModal();
|
|
354
447
|
},
|
|
355
|
-
children: /* @__PURE__ */
|
|
448
|
+
children: /* @__PURE__ */ jsxs("svg", {
|
|
356
449
|
xmlns: "http://www.w3.org/2000/svg",
|
|
357
450
|
viewBox: "0 0 24 24",
|
|
358
451
|
fill: "none",
|
|
359
452
|
stroke: "currentColor",
|
|
360
453
|
strokeWidth: "2",
|
|
361
454
|
className: "h-8 w-8",
|
|
362
|
-
children: [/* @__PURE__ */
|
|
455
|
+
children: [/* @__PURE__ */ jsx("path", { d: "M18 6 6 18" }), /* @__PURE__ */ jsx("path", { d: "m6 6 12 12" })]
|
|
363
456
|
})
|
|
364
457
|
})]
|
|
365
|
-
}), /* @__PURE__ */
|
|
458
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
366
459
|
className: "relative w-full space-y-4",
|
|
367
|
-
children: [
|
|
460
|
+
children: [isConnectCalled && connectingWallet && /* @__PURE__ */ jsxs("div", {
|
|
368
461
|
className: "absolute inset-0 mb-0 flex flex-col items-center justify-center rounded-xl bg-[rgba(255,255,255,0.9)] text-black",
|
|
369
462
|
children: [
|
|
370
|
-
/* @__PURE__ */
|
|
463
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
371
464
|
"正在连接",
|
|
372
|
-
|
|
465
|
+
connectingWallet.name,
|
|
373
466
|
"..."
|
|
374
467
|
] }),
|
|
375
|
-
/* @__PURE__ */
|
|
468
|
+
/* @__PURE__ */ jsx("div", {
|
|
376
469
|
className: "my-1.5 text-xs",
|
|
377
470
|
children: "请在扩展中确认连接"
|
|
378
471
|
}),
|
|
379
|
-
/* @__PURE__ */
|
|
472
|
+
/* @__PURE__ */ jsx("img", {
|
|
380
473
|
src: "data:image/svg+xml,%3c?xml%20version='1.0'%20standalone='no'?%3e%3c!DOCTYPE%20svg%20PUBLIC%20'-//W3C//DTD%20SVG%201.1//EN'%20'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'%3e%3csvg%20t='1783646544006'%20class='icon'%20viewBox='0%200%201024%201024'%20version='1.1'%20xmlns='http://www.w3.org/2000/svg'%20p-id='78324'%20xmlns:xlink='http://www.w3.org/1999/xlink'%20width='32'%20height='32'%3e%3cpath%20d='M512%20907c-24.852%200-45-20.148-45-45s20.148-45%2045-45c168.446%200%20305-136.554%20305-305S680.446%20207%20512%20207%20207%20343.554%20207%20512c0%2024.852-20.148%2045-45%2045S117%20536.852%20117%20512c0-218.152%20176.848-395%20395-395S907%20293.848%20907%20512%20730.152%20907%20512%20907z'%20fill='%23444444'%20p-id='78325'%3e%3c/path%3e%3c/svg%3e",
|
|
381
474
|
alt: "spin",
|
|
382
475
|
className: "h-8 w-8 animate-[spin_2s_linear_infinite]"
|
|
383
476
|
})
|
|
384
477
|
]
|
|
385
|
-
}),
|
|
478
|
+
}), WALLETS.map((w) => /* @__PURE__ */ jsxs("button", {
|
|
386
479
|
type: "button",
|
|
387
480
|
onClick: () => {
|
|
388
|
-
|
|
481
|
+
setIsConnectCalled(true);
|
|
482
|
+
connect(w);
|
|
389
483
|
},
|
|
390
484
|
className: "flex w-full cursor-pointer items-center gap-2 rounded-xl border border-gray-300 px-4 py-2 hover:bg-gray-100",
|
|
391
|
-
children: [/* @__PURE__ */
|
|
392
|
-
src:
|
|
393
|
-
alt:
|
|
485
|
+
children: [/* @__PURE__ */ jsx("img", {
|
|
486
|
+
src: w.icon,
|
|
487
|
+
alt: w.name,
|
|
394
488
|
className: "h-8 w-8"
|
|
395
|
-
}), /* @__PURE__ */
|
|
489
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
396
490
|
className: "flex flex-col justify-between text-left",
|
|
397
|
-
children: [/* @__PURE__ */
|
|
491
|
+
children: [/* @__PURE__ */ jsx("p", { children: w.name }), /* @__PURE__ */ jsxs("span", {
|
|
398
492
|
className: "text-xs text-gray-500",
|
|
399
493
|
children: [
|
|
400
494
|
"Connect your ",
|
|
401
|
-
|
|
495
|
+
w.name,
|
|
402
496
|
" wallet"
|
|
403
497
|
]
|
|
404
498
|
})]
|
|
405
499
|
})]
|
|
406
|
-
},
|
|
500
|
+
}, w.id))]
|
|
407
501
|
})]
|
|
408
502
|
})
|
|
409
503
|
})] });
|
|
410
504
|
}
|
|
411
505
|
//#endregion
|
|
412
|
-
export {
|
|
506
|
+
export { MiniWalletButton, MiniWalletProvider, useMiniWallet };
|