@cfxdevkit/react 1.0.0
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 +33 -0
- package/dist/components/index.cjs +506 -0
- package/dist/components/index.cjs.map +1 -0
- package/dist/components/index.d.cts +4 -0
- package/dist/components/index.d.ts +4 -0
- package/dist/components/index.js +465 -0
- package/dist/components/index.js.map +1 -0
- package/dist/hooks/index.cjs +198 -0
- package/dist/hooks/index.cjs.map +1 -0
- package/dist/hooks/index.d.cts +125 -0
- package/dist/hooks/index.d.ts +125 -0
- package/dist/hooks/index.js +169 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/index-CU9Acwhq.d.cts +233 -0
- package/dist/index-CU9Acwhq.d.ts +233 -0
- package/dist/index.cjs +622 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +27 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +573 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/index.cjs +109 -0
- package/dist/providers/index.cjs.map +1 -0
- package/dist/providers/index.d.cts +77 -0
- package/dist/providers/index.d.ts +77 -0
- package/dist/providers/index.js +79 -0
- package/dist/providers/index.js.map +1 -0
- package/package.json +98 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
AccountCard: () => AccountCard,
|
|
34
|
+
ConnectButton: () => ConnectButton,
|
|
35
|
+
ContractReader: () => ContractReader,
|
|
36
|
+
ContractWriter: () => ContractWriter,
|
|
37
|
+
DevKitProvider: () => DevKitProvider,
|
|
38
|
+
SwapWidget: () => SwapWidget,
|
|
39
|
+
VERSION: () => VERSION,
|
|
40
|
+
WalletProvider: () => WalletProvider,
|
|
41
|
+
useBalance: () => useBalance,
|
|
42
|
+
useContract: () => useContract,
|
|
43
|
+
useDevKitContext: () => useDevKitContext,
|
|
44
|
+
useTransaction: () => useTransaction,
|
|
45
|
+
useWalletContext: () => useWalletContext
|
|
46
|
+
});
|
|
47
|
+
module.exports = __toCommonJS(src_exports);
|
|
48
|
+
|
|
49
|
+
// src/hooks/useBalance.ts
|
|
50
|
+
var import_react2 = require("react");
|
|
51
|
+
|
|
52
|
+
// src/providers/DevKitProvider.tsx
|
|
53
|
+
var import_react = require("react");
|
|
54
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
55
|
+
var DevKitContext = (0, import_react.createContext)(void 0);
|
|
56
|
+
function DevKitProvider({
|
|
57
|
+
apiUrl,
|
|
58
|
+
wsUrl,
|
|
59
|
+
network = "local",
|
|
60
|
+
debug = false,
|
|
61
|
+
children
|
|
62
|
+
}) {
|
|
63
|
+
const value = {
|
|
64
|
+
apiUrl,
|
|
65
|
+
wsUrl,
|
|
66
|
+
network,
|
|
67
|
+
debug
|
|
68
|
+
};
|
|
69
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DevKitContext.Provider, { value, children });
|
|
70
|
+
}
|
|
71
|
+
function useDevKitContext() {
|
|
72
|
+
const context = (0, import_react.useContext)(DevKitContext);
|
|
73
|
+
if (!context) {
|
|
74
|
+
throw new Error("useDevKitContext must be used within DevKitProvider");
|
|
75
|
+
}
|
|
76
|
+
return context;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/hooks/useBalance.ts
|
|
80
|
+
function useBalance(options) {
|
|
81
|
+
const { address, enabled = true, refreshInterval } = options;
|
|
82
|
+
useDevKitContext();
|
|
83
|
+
const [balance, setBalance] = (0, import_react2.useState)();
|
|
84
|
+
const [isLoading, setIsLoading] = (0, import_react2.useState)(false);
|
|
85
|
+
const [error, setError] = (0, import_react2.useState)();
|
|
86
|
+
const fetchBalance = (0, import_react2.useCallback)(async () => {
|
|
87
|
+
if (!address || !enabled) return;
|
|
88
|
+
setIsLoading(true);
|
|
89
|
+
setError(void 0);
|
|
90
|
+
try {
|
|
91
|
+
const mockBalance = "1000000000000000000";
|
|
92
|
+
setBalance(mockBalance);
|
|
93
|
+
} catch (err) {
|
|
94
|
+
setError(
|
|
95
|
+
err instanceof Error ? err : new Error("Failed to fetch balance")
|
|
96
|
+
);
|
|
97
|
+
} finally {
|
|
98
|
+
setIsLoading(false);
|
|
99
|
+
}
|
|
100
|
+
}, [address, enabled]);
|
|
101
|
+
(0, import_react2.useEffect)(() => {
|
|
102
|
+
void fetchBalance();
|
|
103
|
+
if (refreshInterval) {
|
|
104
|
+
const interval = setInterval(() => {
|
|
105
|
+
void fetchBalance();
|
|
106
|
+
}, refreshInterval);
|
|
107
|
+
return () => clearInterval(interval);
|
|
108
|
+
}
|
|
109
|
+
}, [fetchBalance, refreshInterval]);
|
|
110
|
+
return {
|
|
111
|
+
balance,
|
|
112
|
+
isLoading,
|
|
113
|
+
error,
|
|
114
|
+
refetch: fetchBalance
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/providers/WalletProvider.tsx
|
|
119
|
+
var import_react3 = require("react");
|
|
120
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
121
|
+
var WalletContext = (0, import_react3.createContext)(void 0);
|
|
122
|
+
function WalletProvider({ children }) {
|
|
123
|
+
const [isConnected, setIsConnected] = (0, import_react3.useState)(false);
|
|
124
|
+
const [coreAddress, setCoreAddress] = (0, import_react3.useState)();
|
|
125
|
+
const [evmAddress, setEvmAddress] = (0, import_react3.useState)();
|
|
126
|
+
const [chain, setChain] = (0, import_react3.useState)("evm");
|
|
127
|
+
const [accountIndex, setAccountIndex] = (0, import_react3.useState)();
|
|
128
|
+
const connect = async (index) => {
|
|
129
|
+
setAccountIndex(index);
|
|
130
|
+
setCoreAddress(`cfx:account${index}`);
|
|
131
|
+
setEvmAddress(`0xaccount${index}`);
|
|
132
|
+
setIsConnected(true);
|
|
133
|
+
};
|
|
134
|
+
const disconnect = () => {
|
|
135
|
+
setIsConnected(false);
|
|
136
|
+
setCoreAddress(void 0);
|
|
137
|
+
setEvmAddress(void 0);
|
|
138
|
+
setAccountIndex(void 0);
|
|
139
|
+
};
|
|
140
|
+
const switchChain = (newChain) => {
|
|
141
|
+
setChain(newChain);
|
|
142
|
+
};
|
|
143
|
+
const value = {
|
|
144
|
+
isConnected,
|
|
145
|
+
address: chain === "core" ? coreAddress : evmAddress,
|
|
146
|
+
coreAddress,
|
|
147
|
+
evmAddress,
|
|
148
|
+
chain,
|
|
149
|
+
accountIndex,
|
|
150
|
+
connect,
|
|
151
|
+
disconnect,
|
|
152
|
+
switchChain
|
|
153
|
+
};
|
|
154
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(WalletContext.Provider, { value, children });
|
|
155
|
+
}
|
|
156
|
+
function useWalletContext() {
|
|
157
|
+
const context = (0, import_react3.useContext)(WalletContext);
|
|
158
|
+
if (!context) {
|
|
159
|
+
throw new Error("useWalletContext must be used within WalletProvider");
|
|
160
|
+
}
|
|
161
|
+
return context;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// src/components/account-display/AccountCard.tsx
|
|
165
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
166
|
+
function AccountCard({
|
|
167
|
+
showBalance = true,
|
|
168
|
+
children,
|
|
169
|
+
className
|
|
170
|
+
}) {
|
|
171
|
+
const { isConnected, coreAddress, evmAddress } = useWalletContext();
|
|
172
|
+
const { balance: coreBalance, isLoading: isLoadingCore } = useBalance({
|
|
173
|
+
address: coreAddress,
|
|
174
|
+
chain: "core",
|
|
175
|
+
enabled: showBalance && isConnected
|
|
176
|
+
});
|
|
177
|
+
const { balance: evmBalance, isLoading: isLoadingEvm } = useBalance({
|
|
178
|
+
address: evmAddress,
|
|
179
|
+
chain: "evm",
|
|
180
|
+
enabled: showBalance && isConnected
|
|
181
|
+
});
|
|
182
|
+
const renderProps = {
|
|
183
|
+
isConnected,
|
|
184
|
+
coreAddress,
|
|
185
|
+
evmAddress,
|
|
186
|
+
coreBalance,
|
|
187
|
+
evmBalance,
|
|
188
|
+
isLoadingBalance: isLoadingCore || isLoadingEvm
|
|
189
|
+
};
|
|
190
|
+
if (typeof children === "function") {
|
|
191
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: children(renderProps) });
|
|
192
|
+
}
|
|
193
|
+
if (!isConnected) {
|
|
194
|
+
return children ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className, children }) : null;
|
|
195
|
+
}
|
|
196
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: className || "p-4 bg-gray-100 rounded-lg space-y-2", children: [
|
|
197
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { children: [
|
|
198
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "font-semibold", children: "Core Space:" }),
|
|
199
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "ml-2 font-mono text-sm", children: coreAddress }),
|
|
200
|
+
showBalance && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "ml-2 text-gray-600", children: isLoadingCore ? "Loading..." : `${coreBalance} CFX` })
|
|
201
|
+
] }),
|
|
202
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { children: [
|
|
203
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "font-semibold", children: "eSpace:" }),
|
|
204
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "ml-2 font-mono text-sm", children: evmAddress }),
|
|
205
|
+
showBalance && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "ml-2 text-gray-600", children: isLoadingEvm ? "Loading..." : `${evmBalance} CFX` })
|
|
206
|
+
] })
|
|
207
|
+
] });
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// src/components/connect-wallet/ConnectButton.tsx
|
|
211
|
+
var import_react4 = __toESM(require("react"), 1);
|
|
212
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
213
|
+
function ConnectButton({
|
|
214
|
+
accountIndex = 0,
|
|
215
|
+
onConnect,
|
|
216
|
+
onDisconnect,
|
|
217
|
+
children,
|
|
218
|
+
className
|
|
219
|
+
}) {
|
|
220
|
+
const {
|
|
221
|
+
isConnected,
|
|
222
|
+
address,
|
|
223
|
+
connect: contextConnect,
|
|
224
|
+
disconnect: contextDisconnect
|
|
225
|
+
} = useWalletContext();
|
|
226
|
+
const [isLoading, setIsLoading] = import_react4.default.useState(false);
|
|
227
|
+
const connect = async () => {
|
|
228
|
+
setIsLoading(true);
|
|
229
|
+
try {
|
|
230
|
+
await contextConnect(accountIndex);
|
|
231
|
+
onConnect?.();
|
|
232
|
+
} catch (error) {
|
|
233
|
+
console.error("Connection failed:", error);
|
|
234
|
+
} finally {
|
|
235
|
+
setIsLoading(false);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
const disconnect = () => {
|
|
239
|
+
contextDisconnect();
|
|
240
|
+
onDisconnect?.();
|
|
241
|
+
};
|
|
242
|
+
const renderProps = {
|
|
243
|
+
isConnected,
|
|
244
|
+
address,
|
|
245
|
+
connect,
|
|
246
|
+
disconnect,
|
|
247
|
+
isLoading
|
|
248
|
+
};
|
|
249
|
+
if (typeof children === "function") {
|
|
250
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_jsx_runtime4.Fragment, { children: children(renderProps) });
|
|
251
|
+
}
|
|
252
|
+
if (children) {
|
|
253
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
254
|
+
"button",
|
|
255
|
+
{
|
|
256
|
+
onClick: isConnected ? disconnect : connect,
|
|
257
|
+
disabled: isLoading,
|
|
258
|
+
className,
|
|
259
|
+
type: "button",
|
|
260
|
+
children
|
|
261
|
+
}
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
265
|
+
"button",
|
|
266
|
+
{
|
|
267
|
+
onClick: isConnected ? disconnect : connect,
|
|
268
|
+
disabled: isLoading,
|
|
269
|
+
className: className || "px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 disabled:opacity-50",
|
|
270
|
+
type: "button",
|
|
271
|
+
children: isLoading ? "Connecting..." : isConnected ? `Connected: ${address?.slice(0, 6)}...${address?.slice(-4)}` : "Connect Wallet"
|
|
272
|
+
}
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// src/components/contract/ContractReader.tsx
|
|
277
|
+
var import_react6 = require("react");
|
|
278
|
+
|
|
279
|
+
// src/hooks/useContract.ts
|
|
280
|
+
var import_react5 = require("react");
|
|
281
|
+
function useContract() {
|
|
282
|
+
useDevKitContext();
|
|
283
|
+
const { accountIndex } = useWalletContext();
|
|
284
|
+
const [isLoading, setIsLoading] = (0, import_react5.useState)(false);
|
|
285
|
+
const [error, setError] = (0, import_react5.useState)();
|
|
286
|
+
const read = async (_options) => {
|
|
287
|
+
setIsLoading(true);
|
|
288
|
+
setError(void 0);
|
|
289
|
+
try {
|
|
290
|
+
return {};
|
|
291
|
+
} catch (err) {
|
|
292
|
+
const errorObj = err instanceof Error ? err : new Error("Contract read failed");
|
|
293
|
+
setError(errorObj);
|
|
294
|
+
throw errorObj;
|
|
295
|
+
} finally {
|
|
296
|
+
setIsLoading(false);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
const write = async (_options) => {
|
|
300
|
+
if (accountIndex === void 0) {
|
|
301
|
+
throw new Error("Wallet not connected");
|
|
302
|
+
}
|
|
303
|
+
setIsLoading(true);
|
|
304
|
+
setError(void 0);
|
|
305
|
+
try {
|
|
306
|
+
const hash = `0x${Array.from(
|
|
307
|
+
{ length: 64 },
|
|
308
|
+
() => Math.floor(Math.random() * 16).toString(16)
|
|
309
|
+
).join("")}`;
|
|
310
|
+
return hash;
|
|
311
|
+
} catch (err) {
|
|
312
|
+
const errorObj = err instanceof Error ? err : new Error("Contract write failed");
|
|
313
|
+
setError(errorObj);
|
|
314
|
+
throw errorObj;
|
|
315
|
+
} finally {
|
|
316
|
+
setIsLoading(false);
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
return {
|
|
320
|
+
read,
|
|
321
|
+
write,
|
|
322
|
+
isLoading,
|
|
323
|
+
error
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// src/components/contract/ContractReader.tsx
|
|
328
|
+
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
329
|
+
function ContractReader({
|
|
330
|
+
address,
|
|
331
|
+
abi,
|
|
332
|
+
functionName,
|
|
333
|
+
chain,
|
|
334
|
+
children,
|
|
335
|
+
className
|
|
336
|
+
}) {
|
|
337
|
+
const { read: readContract, isLoading, error } = useContract();
|
|
338
|
+
const [result, setResult] = (0, import_react6.useState)();
|
|
339
|
+
const read = async (args) => {
|
|
340
|
+
const data = await readContract({
|
|
341
|
+
address,
|
|
342
|
+
abi,
|
|
343
|
+
functionName,
|
|
344
|
+
args,
|
|
345
|
+
chain
|
|
346
|
+
});
|
|
347
|
+
setResult(data);
|
|
348
|
+
};
|
|
349
|
+
const renderProps = {
|
|
350
|
+
read,
|
|
351
|
+
result,
|
|
352
|
+
isLoading,
|
|
353
|
+
error
|
|
354
|
+
};
|
|
355
|
+
if (typeof children === "function") {
|
|
356
|
+
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_jsx_runtime5.Fragment, { children: children(renderProps) });
|
|
357
|
+
}
|
|
358
|
+
return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { className: className || "p-4 border rounded", children: [
|
|
359
|
+
/* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("h3", { className: "font-semibold mb-2", children: [
|
|
360
|
+
"Read: ",
|
|
361
|
+
functionName
|
|
362
|
+
] }),
|
|
363
|
+
/* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
364
|
+
"button",
|
|
365
|
+
{
|
|
366
|
+
onClick: () => read(),
|
|
367
|
+
disabled: isLoading,
|
|
368
|
+
className: "px-3 py-1 bg-blue-500 text-white rounded disabled:opacity-50",
|
|
369
|
+
type: "button",
|
|
370
|
+
children: isLoading ? "Reading..." : "Read"
|
|
371
|
+
}
|
|
372
|
+
),
|
|
373
|
+
result && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { className: "mt-2", children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("pre", { className: "bg-gray-100 p-2 rounded text-sm", children: JSON.stringify(result, null, 2) }) }),
|
|
374
|
+
error && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("p", { className: "mt-2 text-red-500", children: error.message }),
|
|
375
|
+
children
|
|
376
|
+
] });
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// src/components/contract/ContractWriter.tsx
|
|
380
|
+
var import_react7 = require("react");
|
|
381
|
+
var import_jsx_runtime6 = require("react/jsx-runtime");
|
|
382
|
+
function ContractWriter({
|
|
383
|
+
address,
|
|
384
|
+
abi,
|
|
385
|
+
functionName,
|
|
386
|
+
chain,
|
|
387
|
+
onSuccess,
|
|
388
|
+
children,
|
|
389
|
+
className
|
|
390
|
+
}) {
|
|
391
|
+
const { write: writeContract, isLoading, error } = useContract();
|
|
392
|
+
const [hash, setHash] = (0, import_react7.useState)();
|
|
393
|
+
const write = async (args, value) => {
|
|
394
|
+
const txHash = await writeContract({
|
|
395
|
+
address,
|
|
396
|
+
abi,
|
|
397
|
+
functionName,
|
|
398
|
+
args,
|
|
399
|
+
chain,
|
|
400
|
+
value
|
|
401
|
+
});
|
|
402
|
+
setHash(txHash);
|
|
403
|
+
onSuccess?.(txHash);
|
|
404
|
+
};
|
|
405
|
+
const reset = () => {
|
|
406
|
+
setHash(void 0);
|
|
407
|
+
};
|
|
408
|
+
const renderProps = {
|
|
409
|
+
write,
|
|
410
|
+
hash,
|
|
411
|
+
isLoading,
|
|
412
|
+
error,
|
|
413
|
+
reset
|
|
414
|
+
};
|
|
415
|
+
if (typeof children === "function") {
|
|
416
|
+
return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_jsx_runtime6.Fragment, { children: children(renderProps) });
|
|
417
|
+
}
|
|
418
|
+
return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: className || "p-4 border rounded", children: [
|
|
419
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("h3", { className: "font-semibold mb-2", children: [
|
|
420
|
+
"Write: ",
|
|
421
|
+
functionName
|
|
422
|
+
] }),
|
|
423
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
424
|
+
"button",
|
|
425
|
+
{
|
|
426
|
+
onClick: () => write(),
|
|
427
|
+
disabled: isLoading,
|
|
428
|
+
className: "px-3 py-1 bg-green-500 text-white rounded disabled:opacity-50",
|
|
429
|
+
type: "button",
|
|
430
|
+
children: isLoading ? "Sending..." : "Execute"
|
|
431
|
+
}
|
|
432
|
+
),
|
|
433
|
+
hash && /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "mt-2", children: [
|
|
434
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("p", { className: "text-sm text-gray-600", children: "Transaction Hash:" }),
|
|
435
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("p", { className: "font-mono text-xs break-all", children: hash }),
|
|
436
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
437
|
+
"button",
|
|
438
|
+
{
|
|
439
|
+
onClick: reset,
|
|
440
|
+
className: "mt-1 text-sm text-blue-500 hover:underline",
|
|
441
|
+
type: "button",
|
|
442
|
+
children: "Reset"
|
|
443
|
+
}
|
|
444
|
+
)
|
|
445
|
+
] }),
|
|
446
|
+
error && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("p", { className: "mt-2 text-red-500", children: error.message }),
|
|
447
|
+
children
|
|
448
|
+
] });
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// src/components/swap/SwapWidget.tsx
|
|
452
|
+
var import_react8 = require("react");
|
|
453
|
+
var import_jsx_runtime7 = require("react/jsx-runtime");
|
|
454
|
+
function SwapWidget({
|
|
455
|
+
defaultSlippage = 0.5,
|
|
456
|
+
onSuccess,
|
|
457
|
+
children,
|
|
458
|
+
className
|
|
459
|
+
}) {
|
|
460
|
+
useDevKitContext();
|
|
461
|
+
const { isConnected, accountIndex } = useWalletContext();
|
|
462
|
+
const [quote, setQuote] = (0, import_react8.useState)();
|
|
463
|
+
const [isLoadingQuote, setIsLoadingQuote] = (0, import_react8.useState)(false);
|
|
464
|
+
const [isExecutingSwap, setIsExecutingSwap] = (0, import_react8.useState)(false);
|
|
465
|
+
const [error, setError] = (0, import_react8.useState)();
|
|
466
|
+
const [hash, setHash] = (0, import_react8.useState)();
|
|
467
|
+
const [currentQuoteParams, setCurrentQuoteParams] = (0, import_react8.useState)();
|
|
468
|
+
const getQuote = async (tokenIn, tokenOut, amountIn) => {
|
|
469
|
+
if (!isConnected) {
|
|
470
|
+
setError(new Error("Wallet not connected"));
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
setIsLoadingQuote(true);
|
|
474
|
+
setError(void 0);
|
|
475
|
+
try {
|
|
476
|
+
const mockQuote = {
|
|
477
|
+
amountIn,
|
|
478
|
+
amountOut: (parseFloat(amountIn) * 0.997).toString(),
|
|
479
|
+
// 0.3% fee
|
|
480
|
+
amountOutMin: (parseFloat(amountIn) * 0.997 * 0.995).toString(),
|
|
481
|
+
// With slippage
|
|
482
|
+
priceImpact: "0.3",
|
|
483
|
+
slippage: defaultSlippage
|
|
484
|
+
};
|
|
485
|
+
setQuote(mockQuote);
|
|
486
|
+
setCurrentQuoteParams({ tokenIn, tokenOut, amountIn });
|
|
487
|
+
} catch (err) {
|
|
488
|
+
setError(err instanceof Error ? err : new Error("Failed to get quote"));
|
|
489
|
+
} finally {
|
|
490
|
+
setIsLoadingQuote(false);
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
const executeSwap = async () => {
|
|
494
|
+
if (!quote || !currentQuoteParams || !isConnected || accountIndex === void 0) {
|
|
495
|
+
setError(new Error("Missing required data for swap"));
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
setIsExecutingSwap(true);
|
|
499
|
+
setError(void 0);
|
|
500
|
+
try {
|
|
501
|
+
const mockHash = `0x${Array.from(
|
|
502
|
+
{ length: 64 },
|
|
503
|
+
() => Math.floor(Math.random() * 16).toString(16)
|
|
504
|
+
).join("")}`;
|
|
505
|
+
setHash(mockHash);
|
|
506
|
+
onSuccess?.(mockHash);
|
|
507
|
+
} catch (err) {
|
|
508
|
+
setError(err instanceof Error ? err : new Error("Swap failed"));
|
|
509
|
+
} finally {
|
|
510
|
+
setIsExecutingSwap(false);
|
|
511
|
+
}
|
|
512
|
+
};
|
|
513
|
+
const renderProps = {
|
|
514
|
+
getQuote,
|
|
515
|
+
executeSwap,
|
|
516
|
+
quote,
|
|
517
|
+
isLoadingQuote,
|
|
518
|
+
isExecutingSwap,
|
|
519
|
+
error,
|
|
520
|
+
hash
|
|
521
|
+
};
|
|
522
|
+
if (typeof children === "function") {
|
|
523
|
+
return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_jsx_runtime7.Fragment, { children: children(renderProps) });
|
|
524
|
+
}
|
|
525
|
+
return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: className || "p-4 border rounded-lg", children: [
|
|
526
|
+
/* @__PURE__ */ (0, import_jsx_runtime7.jsx)("h2", { className: "text-xl font-bold mb-4", children: "Swap (Swappi)" }),
|
|
527
|
+
!isConnected && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("p", { className: "text-gray-600", children: "Connect wallet to start swapping" }),
|
|
528
|
+
quote && /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "mt-4 p-3 bg-gray-100 rounded", children: [
|
|
529
|
+
/* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("p", { children: [
|
|
530
|
+
"Amount In: ",
|
|
531
|
+
quote.amountIn
|
|
532
|
+
] }),
|
|
533
|
+
/* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("p", { children: [
|
|
534
|
+
"Amount Out: ",
|
|
535
|
+
quote.amountOut
|
|
536
|
+
] }),
|
|
537
|
+
/* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("p", { children: [
|
|
538
|
+
"Min Amount: ",
|
|
539
|
+
quote.amountOutMin
|
|
540
|
+
] }),
|
|
541
|
+
/* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("p", { children: [
|
|
542
|
+
"Slippage: ",
|
|
543
|
+
quote.slippage,
|
|
544
|
+
"%"
|
|
545
|
+
] })
|
|
546
|
+
] }),
|
|
547
|
+
hash && /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "mt-4 p-3 bg-green-100 rounded", children: [
|
|
548
|
+
/* @__PURE__ */ (0, import_jsx_runtime7.jsx)("p", { className: "text-sm font-semibold", children: "Swap Successful!" }),
|
|
549
|
+
/* @__PURE__ */ (0, import_jsx_runtime7.jsx)("p", { className: "text-xs font-mono break-all", children: hash })
|
|
550
|
+
] }),
|
|
551
|
+
error && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { className: "mt-4 p-3 bg-red-100 rounded", children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("p", { className: "text-red-700", children: error.message }) }),
|
|
552
|
+
children
|
|
553
|
+
] });
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// src/hooks/useTransaction.ts
|
|
557
|
+
var import_react9 = require("react");
|
|
558
|
+
function useTransaction() {
|
|
559
|
+
useDevKitContext();
|
|
560
|
+
const { accountIndex } = useWalletContext();
|
|
561
|
+
const [isLoading, setIsLoading] = (0, import_react9.useState)(false);
|
|
562
|
+
const [error, setError] = (0, import_react9.useState)();
|
|
563
|
+
const [transaction, setTransaction] = (0, import_react9.useState)();
|
|
564
|
+
const send = async (_options) => {
|
|
565
|
+
if (accountIndex === void 0) {
|
|
566
|
+
throw new Error("Wallet not connected");
|
|
567
|
+
}
|
|
568
|
+
setIsLoading(true);
|
|
569
|
+
setError(void 0);
|
|
570
|
+
try {
|
|
571
|
+
const result = {
|
|
572
|
+
hash: `0x${Array.from(
|
|
573
|
+
{ length: 64 },
|
|
574
|
+
() => Math.floor(Math.random() * 16).toString(16)
|
|
575
|
+
).join("")}`,
|
|
576
|
+
status: "pending"
|
|
577
|
+
};
|
|
578
|
+
setTransaction(result);
|
|
579
|
+
setTimeout(() => {
|
|
580
|
+
setTransaction({ ...result, status: "success" });
|
|
581
|
+
}, 2e3);
|
|
582
|
+
return result;
|
|
583
|
+
} catch (err) {
|
|
584
|
+
const errorObj = err instanceof Error ? err : new Error("Transaction failed");
|
|
585
|
+
setError(errorObj);
|
|
586
|
+
throw errorObj;
|
|
587
|
+
} finally {
|
|
588
|
+
setIsLoading(false);
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
const reset = () => {
|
|
592
|
+
setTransaction(void 0);
|
|
593
|
+
setError(void 0);
|
|
594
|
+
};
|
|
595
|
+
return {
|
|
596
|
+
send,
|
|
597
|
+
isLoading,
|
|
598
|
+
error,
|
|
599
|
+
transaction,
|
|
600
|
+
reset
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// src/index.ts
|
|
605
|
+
var VERSION = "1.0.0";
|
|
606
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
607
|
+
0 && (module.exports = {
|
|
608
|
+
AccountCard,
|
|
609
|
+
ConnectButton,
|
|
610
|
+
ContractReader,
|
|
611
|
+
ContractWriter,
|
|
612
|
+
DevKitProvider,
|
|
613
|
+
SwapWidget,
|
|
614
|
+
VERSION,
|
|
615
|
+
WalletProvider,
|
|
616
|
+
useBalance,
|
|
617
|
+
useContract,
|
|
618
|
+
useDevKitContext,
|
|
619
|
+
useTransaction,
|
|
620
|
+
useWalletContext
|
|
621
|
+
});
|
|
622
|
+
//# sourceMappingURL=index.cjs.map
|