@taskon/widget-react 0.0.1-beta.6 → 0.0.1-beta.7
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 +48 -43
- package/dist/EligibilityInfo.css +2 -33
- package/dist/TaskOnProvider.css +287 -0
- package/dist/ThemeProvider.css +227 -0
- package/dist/UserCenterWidget2.css +32 -290
- package/dist/WidgetShell.css +0 -227
- package/dist/chunks/{CommunityTaskList-Hde2OKHH.js → CommunityTaskList-D0uVD8wD.js} +37 -58
- package/dist/chunks/{EligibilityInfo-BV0Z2TgY.js → EligibilityInfo-Cf6hx9-a.js} +17 -209
- package/dist/chunks/{LeaderboardWidget-BNGRD5Bu.js → LeaderboardWidget-DyoiiNS6.js} +10 -9
- package/dist/chunks/{PageBuilder-C5DSHiW9.js → PageBuilder-DoAFPm6-.js} +5 -5
- package/dist/chunks/{Quest-DG9zfXJo.js → Quest-ySZlYd4u.js} +6 -11
- package/dist/chunks/TaskOnProvider-CxtFIs3n.js +2072 -0
- package/dist/chunks/{WidgetShell-D7yC894Y.js → ThemeProvider-CulHkqqY.js} +1354 -617
- package/dist/chunks/UserCenterWidget-BJsc_GSZ.js +3246 -0
- package/dist/chunks/{UserCenterWidget-D5ttw4hO.js → UserCenterWidget-STq8kpV4.js} +162 -365
- package/dist/chunks/WidgetShell-8xn-Jivw.js +659 -0
- package/dist/chunks/useIsMobile-D6Ybur-6.js +30 -0
- package/dist/chunks/useToast-BGJhd3BX.js +93 -0
- package/dist/community-task.js +1 -1
- package/dist/core.d.ts +9 -15
- package/dist/core.js +3 -3
- package/dist/index.d.ts +64 -15
- package/dist/index.js +15 -10
- package/dist/leaderboard.js +1 -1
- package/dist/page-builder.js +1 -1
- package/dist/quest.js +1 -1
- package/dist/user-center.js +1 -1
- package/package.json +1 -1
- package/dist/chunks/TaskOnProvider-BhamHIyY.js +0 -1260
- package/dist/chunks/ThemeProvider-mXLdLSkq.js +0 -1397
- package/dist/chunks/UserCenterWidget-jDO5zTN1.js +0 -3297
- package/dist/chunks/useToast-CaRkylKe.js +0 -304
|
@@ -1,304 +0,0 @@
|
|
|
1
|
-
import { useContext, createContext, useMemo, useState, useCallback } from "react";
|
|
2
|
-
const defaultWalletContext = {
|
|
3
|
-
// EVM state
|
|
4
|
-
evmAdapter: null,
|
|
5
|
-
evmAddress: null,
|
|
6
|
-
evmChainId: null,
|
|
7
|
-
isEvmConnected: false,
|
|
8
|
-
// Detection status
|
|
9
|
-
isDetecting: true,
|
|
10
|
-
// Actions (no-op by default)
|
|
11
|
-
connectEvm: async () => null,
|
|
12
|
-
disconnectEvm: async () => {
|
|
13
|
-
},
|
|
14
|
-
signEvmMessage: async () => null
|
|
15
|
-
};
|
|
16
|
-
const WalletContext = createContext(defaultWalletContext);
|
|
17
|
-
function useWallet() {
|
|
18
|
-
const context = useContext(WalletContext);
|
|
19
|
-
return context;
|
|
20
|
-
}
|
|
21
|
-
function useEvmWallet() {
|
|
22
|
-
const context = useWallet();
|
|
23
|
-
return useMemo(
|
|
24
|
-
() => ({
|
|
25
|
-
adapter: context.evmAdapter,
|
|
26
|
-
address: context.evmAddress,
|
|
27
|
-
chainId: context.evmChainId,
|
|
28
|
-
isConnected: context.isEvmConnected,
|
|
29
|
-
connect: context.connectEvm,
|
|
30
|
-
disconnect: context.disconnectEvm,
|
|
31
|
-
signMessage: context.signEvmMessage
|
|
32
|
-
}),
|
|
33
|
-
[
|
|
34
|
-
context.evmAdapter,
|
|
35
|
-
context.evmAddress,
|
|
36
|
-
context.evmChainId,
|
|
37
|
-
context.isEvmConnected,
|
|
38
|
-
context.connectEvm,
|
|
39
|
-
context.disconnectEvm,
|
|
40
|
-
context.signEvmMessage
|
|
41
|
-
]
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
let ethersModule = null;
|
|
45
|
-
const interfaceCache = /* @__PURE__ */ new Map();
|
|
46
|
-
async function loadEthers() {
|
|
47
|
-
if (ethersModule) {
|
|
48
|
-
return ethersModule;
|
|
49
|
-
}
|
|
50
|
-
try {
|
|
51
|
-
ethersModule = await import("ethers");
|
|
52
|
-
return ethersModule;
|
|
53
|
-
} catch {
|
|
54
|
-
throw new Error(
|
|
55
|
-
"ethers.js is required for contract invocation. Please install ethers@^6.0.0"
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
async function getInterface(abi) {
|
|
60
|
-
const { ethers } = await loadEthers();
|
|
61
|
-
const cacheKey = JSON.stringify(abi);
|
|
62
|
-
let iface = interfaceCache.get(cacheKey);
|
|
63
|
-
if (!iface) {
|
|
64
|
-
iface = new ethers.Interface(abi);
|
|
65
|
-
interfaceCache.set(cacheKey, iface);
|
|
66
|
-
}
|
|
67
|
-
return iface;
|
|
68
|
-
}
|
|
69
|
-
function getEthereumProvider() {
|
|
70
|
-
if (typeof window === "undefined") return null;
|
|
71
|
-
return window.ethereum ?? null;
|
|
72
|
-
}
|
|
73
|
-
function buildEthereumAdapter(provider, initialAddress = null, initialChainId = null) {
|
|
74
|
-
let currentAddress = initialAddress;
|
|
75
|
-
let currentChainId = initialChainId;
|
|
76
|
-
const adapter = {
|
|
77
|
-
/**
|
|
78
|
-
* Connect wallet
|
|
79
|
-
*/
|
|
80
|
-
connect: async () => {
|
|
81
|
-
const accounts = await provider.request({
|
|
82
|
-
method: "eth_requestAccounts"
|
|
83
|
-
});
|
|
84
|
-
if (!accounts || accounts.length === 0) {
|
|
85
|
-
throw new Error("No accounts found");
|
|
86
|
-
}
|
|
87
|
-
const address = accounts[0];
|
|
88
|
-
if (!address) {
|
|
89
|
-
throw new Error("No accounts found");
|
|
90
|
-
}
|
|
91
|
-
currentAddress = address;
|
|
92
|
-
const chainIdHex = await provider.request({
|
|
93
|
-
method: "eth_chainId"
|
|
94
|
-
});
|
|
95
|
-
currentChainId = parseInt(chainIdHex, 16);
|
|
96
|
-
return address;
|
|
97
|
-
},
|
|
98
|
-
/**
|
|
99
|
-
* Disconnect from wallet
|
|
100
|
-
* Note: Most wallets don't support programmatic disconnect, can only clear local state
|
|
101
|
-
*/
|
|
102
|
-
disconnect: async () => {
|
|
103
|
-
currentAddress = null;
|
|
104
|
-
currentChainId = null;
|
|
105
|
-
},
|
|
106
|
-
/**
|
|
107
|
-
* Sign message (personal_sign)
|
|
108
|
-
*/
|
|
109
|
-
signMessage: async (message) => {
|
|
110
|
-
if (!currentAddress) {
|
|
111
|
-
throw new Error("Wallet not connected");
|
|
112
|
-
}
|
|
113
|
-
const signature = await provider.request({
|
|
114
|
-
method: "personal_sign",
|
|
115
|
-
params: [message, currentAddress]
|
|
116
|
-
});
|
|
117
|
-
return signature;
|
|
118
|
-
},
|
|
119
|
-
/**
|
|
120
|
-
* Get current address
|
|
121
|
-
*/
|
|
122
|
-
getAddress: () => currentAddress,
|
|
123
|
-
/**
|
|
124
|
-
* Get current chain ID
|
|
125
|
-
*/
|
|
126
|
-
getChainId: () => currentChainId,
|
|
127
|
-
/**
|
|
128
|
-
* Switch network
|
|
129
|
-
*/
|
|
130
|
-
switchNetwork: async (chainId) => {
|
|
131
|
-
const chainIdHex = `0x${chainId.toString(16)}`;
|
|
132
|
-
try {
|
|
133
|
-
await provider.request({
|
|
134
|
-
method: "wallet_switchEthereumChain",
|
|
135
|
-
params: [{ chainId: chainIdHex }]
|
|
136
|
-
});
|
|
137
|
-
currentChainId = chainId;
|
|
138
|
-
} catch (error) {
|
|
139
|
-
const err = error;
|
|
140
|
-
if (err.code === 4902) {
|
|
141
|
-
throw new Error("Chain not found in wallet. Please add the network manually.");
|
|
142
|
-
}
|
|
143
|
-
throw error;
|
|
144
|
-
}
|
|
145
|
-
},
|
|
146
|
-
/**
|
|
147
|
-
* Get wallet's native token balance (for gas estimation)
|
|
148
|
-
* @returns Balance in ETH (string, formatted)
|
|
149
|
-
*/
|
|
150
|
-
getBalance: async () => {
|
|
151
|
-
if (!currentAddress) {
|
|
152
|
-
throw new Error("Wallet not connected");
|
|
153
|
-
}
|
|
154
|
-
const balanceHex = await provider.request({
|
|
155
|
-
method: "eth_getBalance",
|
|
156
|
-
params: [currentAddress, "latest"]
|
|
157
|
-
});
|
|
158
|
-
const balanceWei = BigInt(balanceHex);
|
|
159
|
-
const balanceEth = Number(balanceWei) / 1e18;
|
|
160
|
-
return balanceEth.toString();
|
|
161
|
-
},
|
|
162
|
-
/**
|
|
163
|
-
* Invoke a smart contract method
|
|
164
|
-
*
|
|
165
|
-
* 使用 ethers.js 编码合约调用,支持缓存优化:
|
|
166
|
-
* - ethers 模块只加载一次
|
|
167
|
-
* - 相同 ABI 的 Interface 实例会被缓存复用
|
|
168
|
-
*
|
|
169
|
-
* @param params - Contract invocation parameters
|
|
170
|
-
* @returns Transaction hash
|
|
171
|
-
*/
|
|
172
|
-
invokeContract: async (params) => {
|
|
173
|
-
var _a;
|
|
174
|
-
if (!currentAddress) {
|
|
175
|
-
throw new Error("Wallet not connected");
|
|
176
|
-
}
|
|
177
|
-
const { contract, abi, method, params: methodParams, chainId, value } = params;
|
|
178
|
-
if (chainId && currentChainId !== chainId) {
|
|
179
|
-
await ((_a = adapter.switchNetwork) == null ? void 0 : _a.call(adapter, chainId));
|
|
180
|
-
}
|
|
181
|
-
let encodedData;
|
|
182
|
-
try {
|
|
183
|
-
const iface = await getInterface(abi);
|
|
184
|
-
encodedData = iface.encodeFunctionData(method, methodParams);
|
|
185
|
-
} catch (error) {
|
|
186
|
-
if (error instanceof Error && error.message.includes("ethers.js is required")) {
|
|
187
|
-
throw error;
|
|
188
|
-
}
|
|
189
|
-
throw new Error(
|
|
190
|
-
`Failed to encode contract call "${method}": ${error instanceof Error ? error.message : String(error)}`
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
const txParams = {
|
|
194
|
-
from: currentAddress,
|
|
195
|
-
to: contract,
|
|
196
|
-
data: encodedData
|
|
197
|
-
};
|
|
198
|
-
if (value) {
|
|
199
|
-
txParams.value = `0x${BigInt(value).toString(16)}`;
|
|
200
|
-
}
|
|
201
|
-
const txHash = await provider.request({
|
|
202
|
-
method: "eth_sendTransaction",
|
|
203
|
-
params: [txParams]
|
|
204
|
-
});
|
|
205
|
-
return txHash;
|
|
206
|
-
},
|
|
207
|
-
/**
|
|
208
|
-
* Listen to account changes
|
|
209
|
-
*/
|
|
210
|
-
onAccountChange: (callback) => {
|
|
211
|
-
var _a;
|
|
212
|
-
const handler = (accounts) => {
|
|
213
|
-
const accountList = accounts;
|
|
214
|
-
currentAddress = accountList[0] ?? null;
|
|
215
|
-
callback(currentAddress);
|
|
216
|
-
};
|
|
217
|
-
(_a = provider.on) == null ? void 0 : _a.call(provider, "accountsChanged", handler);
|
|
218
|
-
return () => {
|
|
219
|
-
var _a2;
|
|
220
|
-
return (_a2 = provider.removeListener) == null ? void 0 : _a2.call(provider, "accountsChanged", handler);
|
|
221
|
-
};
|
|
222
|
-
},
|
|
223
|
-
/**
|
|
224
|
-
* Listen to chain changes
|
|
225
|
-
*/
|
|
226
|
-
onChainChange: (callback) => {
|
|
227
|
-
var _a;
|
|
228
|
-
const handler = (chainIdHex) => {
|
|
229
|
-
currentChainId = parseInt(chainIdHex, 16);
|
|
230
|
-
callback(currentChainId);
|
|
231
|
-
};
|
|
232
|
-
(_a = provider.on) == null ? void 0 : _a.call(provider, "chainChanged", handler);
|
|
233
|
-
return () => {
|
|
234
|
-
var _a2;
|
|
235
|
-
return (_a2 = provider.removeListener) == null ? void 0 : _a2.call(provider, "chainChanged", handler);
|
|
236
|
-
};
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
return adapter;
|
|
240
|
-
}
|
|
241
|
-
function createEthereumAdapter() {
|
|
242
|
-
const provider = getEthereumProvider();
|
|
243
|
-
if (!provider) return null;
|
|
244
|
-
return buildEthereumAdapter(provider);
|
|
245
|
-
}
|
|
246
|
-
function createEthereumAdapterFromProvider(provider, options = {}) {
|
|
247
|
-
return buildEthereumAdapter(
|
|
248
|
-
provider,
|
|
249
|
-
options.address ?? null,
|
|
250
|
-
options.chainId ?? null
|
|
251
|
-
);
|
|
252
|
-
}
|
|
253
|
-
const ToastContext = createContext(null);
|
|
254
|
-
function useToast() {
|
|
255
|
-
const context = useContext(ToastContext);
|
|
256
|
-
if (!context) {
|
|
257
|
-
throw new Error("useToast must be used within TaskOnProvider");
|
|
258
|
-
}
|
|
259
|
-
return context;
|
|
260
|
-
}
|
|
261
|
-
let toastId = 0;
|
|
262
|
-
function generateId() {
|
|
263
|
-
return `toast-${++toastId}-${Date.now()}`;
|
|
264
|
-
}
|
|
265
|
-
function useToastState() {
|
|
266
|
-
const [toasts, setToasts] = useState([]);
|
|
267
|
-
const showToast = useCallback(
|
|
268
|
-
(message, type = "info", duration) => {
|
|
269
|
-
const newToast = {
|
|
270
|
-
id: generateId(),
|
|
271
|
-
message,
|
|
272
|
-
type,
|
|
273
|
-
duration
|
|
274
|
-
};
|
|
275
|
-
setToasts((prev) => [...prev, newToast]);
|
|
276
|
-
},
|
|
277
|
-
[]
|
|
278
|
-
);
|
|
279
|
-
const removeToast = useCallback((id) => {
|
|
280
|
-
setToasts((prev) => prev.filter((t) => t.id !== id));
|
|
281
|
-
}, []);
|
|
282
|
-
const toast = {
|
|
283
|
-
success: (message, duration) => showToast(message, "success", duration),
|
|
284
|
-
error: (message, duration) => showToast(message, "error", duration),
|
|
285
|
-
warning: (message, duration) => showToast(message, "warning", duration),
|
|
286
|
-
info: (message, duration) => showToast(message, "info", duration)
|
|
287
|
-
};
|
|
288
|
-
return {
|
|
289
|
-
toasts,
|
|
290
|
-
showToast,
|
|
291
|
-
removeToast,
|
|
292
|
-
toast
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
export {
|
|
296
|
-
ToastContext as T,
|
|
297
|
-
WalletContext as W,
|
|
298
|
-
useEvmWallet as a,
|
|
299
|
-
useToastState as b,
|
|
300
|
-
createEthereumAdapter as c,
|
|
301
|
-
useToast as d,
|
|
302
|
-
createEthereumAdapterFromProvider as e,
|
|
303
|
-
useWallet as u
|
|
304
|
-
};
|