@dappworks/kit 0.4.230 → 0.4.231
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/chunk-NO4O46V2.mjs +1052 -0
- package/dist/chunk-NO4O46V2.mjs.map +1 -0
- package/dist/index.d.mts +19 -3
- package/dist/index.mjs +5 -1
- package/dist/wallet.mjs +26 -701
- package/dist/wallet.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-DRTTNPZI.mjs +0 -298
- package/dist/chunk-DRTTNPZI.mjs.map +0 -1
|
@@ -0,0 +1,1052 @@
|
|
|
1
|
+
import { AIem } from './chunk-NPQ7GCFM.mjs';
|
|
2
|
+
import { DialogStore } from './chunk-GT4R5SDI.mjs';
|
|
3
|
+
import { ToastPlugin } from './chunk-IMOLRP7I.mjs';
|
|
4
|
+
import { RootStore, rootStore } from './chunk-XSGTWROT.mjs';
|
|
5
|
+
import { helper } from './chunk-R4N52NI2.mjs';
|
|
6
|
+
import { __spreadProps, __spreadValues } from './chunk-R4SQKVDQ.mjs';
|
|
7
|
+
import { observable, makeAutoObservable } from 'mobx';
|
|
8
|
+
import BigNumber2 from 'bignumber.js';
|
|
9
|
+
import React2, { useEffect } from 'react';
|
|
10
|
+
import { useRouter } from 'next/router';
|
|
11
|
+
import { observer } from 'mobx-react-lite';
|
|
12
|
+
import { toHex, createWalletClient, custom, publicActions } from 'viem';
|
|
13
|
+
import EventEmitter from 'events';
|
|
14
|
+
import { useAccount, useSwitchChain, useConnect, useDisconnect, useWalletClient } from 'wagmi';
|
|
15
|
+
import { getDefaultConfig, useConnectModal } from '@rainbow-me/rainbowkit';
|
|
16
|
+
import { Icon } from '@iconify/react';
|
|
17
|
+
import { iopayWallet, metaMaskWallet, walletConnectWallet, okxWallet, binanceWallet, safeWallet } from '@rainbow-me/rainbowkit/wallets';
|
|
18
|
+
import SafeAppsSDK, { TransactionStatus } from '@safe-global/safe-apps-sdk';
|
|
19
|
+
|
|
20
|
+
//@ts-ignore
|
|
21
|
+
observable();
|
|
22
|
+
var PromiseHook = class {
|
|
23
|
+
static Get(cls) {
|
|
24
|
+
try {
|
|
25
|
+
return async ({ args, select, id, unselect }) => {
|
|
26
|
+
let instance;
|
|
27
|
+
if (!id || !this.entities[id]) {
|
|
28
|
+
instance = new cls(args);
|
|
29
|
+
instance.refresh = async () => {
|
|
30
|
+
const hooks = Object.entries(instance).filter((i) => {
|
|
31
|
+
if (!this.isPromiseHook(instance[i[0]]))
|
|
32
|
+
return false;
|
|
33
|
+
if (i[1].lazy)
|
|
34
|
+
return false;
|
|
35
|
+
if (select && !select[i[0]])
|
|
36
|
+
return false;
|
|
37
|
+
if (unselect && unselect[i[0]])
|
|
38
|
+
return false;
|
|
39
|
+
return true;
|
|
40
|
+
}).map((i) => {
|
|
41
|
+
const hook = instance[i[0]];
|
|
42
|
+
return hook;
|
|
43
|
+
});
|
|
44
|
+
await Promise.all(hooks.map((i) => i.call()));
|
|
45
|
+
};
|
|
46
|
+
if (id) {
|
|
47
|
+
this.entities[id] = instance;
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
instance = this.entities[id];
|
|
51
|
+
}
|
|
52
|
+
await instance.refresh();
|
|
53
|
+
return instance;
|
|
54
|
+
};
|
|
55
|
+
} catch (e) {
|
|
56
|
+
throw e;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
static isPromiseHook(target) {
|
|
60
|
+
return (target == null ? void 0 : target._type) == "promiseHook";
|
|
61
|
+
}
|
|
62
|
+
//ttl : ms
|
|
63
|
+
static wrap({ func, defaultValue, lazy }) {
|
|
64
|
+
let context;
|
|
65
|
+
const call = (args) => {
|
|
66
|
+
context.loading = true;
|
|
67
|
+
if (!context._call) {
|
|
68
|
+
context._call = func(args).then((i) => {
|
|
69
|
+
context.value = i;
|
|
70
|
+
context.loading = false;
|
|
71
|
+
context._call = null;
|
|
72
|
+
return i;
|
|
73
|
+
}).catch((i) => {
|
|
74
|
+
console.error(i, func);
|
|
75
|
+
context.value = defaultValue;
|
|
76
|
+
context._call = null;
|
|
77
|
+
context.loading = false;
|
|
78
|
+
throw i;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
return context._call;
|
|
82
|
+
};
|
|
83
|
+
const get = async (args) => {
|
|
84
|
+
if (!context.value) {
|
|
85
|
+
return call(args);
|
|
86
|
+
}
|
|
87
|
+
return context.value;
|
|
88
|
+
};
|
|
89
|
+
if (!context) {
|
|
90
|
+
context = observable({
|
|
91
|
+
_type: "promiseHook",
|
|
92
|
+
_value: defaultValue,
|
|
93
|
+
get value() {
|
|
94
|
+
return context["_value"];
|
|
95
|
+
},
|
|
96
|
+
set value(val) {
|
|
97
|
+
context["_value"] = val;
|
|
98
|
+
},
|
|
99
|
+
get,
|
|
100
|
+
lazy,
|
|
101
|
+
loading: false,
|
|
102
|
+
call,
|
|
103
|
+
defaultValue,
|
|
104
|
+
toJSON() {
|
|
105
|
+
return context.value;
|
|
106
|
+
},
|
|
107
|
+
toString() {
|
|
108
|
+
return context.value;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return context;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
PromiseHook.entities = observable([]);
|
|
116
|
+
var BigNumberState = class {
|
|
117
|
+
constructor(args) {
|
|
118
|
+
this.value = new BigNumber2(0);
|
|
119
|
+
this.loading = false;
|
|
120
|
+
this.decimals = 18;
|
|
121
|
+
this.fixed = 6;
|
|
122
|
+
Object.assign(this, args);
|
|
123
|
+
makeAutoObservable(this, {
|
|
124
|
+
getFormat: false
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
get format() {
|
|
128
|
+
if (this.loading)
|
|
129
|
+
return "...";
|
|
130
|
+
return this.getFormat();
|
|
131
|
+
}
|
|
132
|
+
getFormat({ decimals = this.decimals, fixed = this.fixed } = {}) {
|
|
133
|
+
if (this.loading)
|
|
134
|
+
return "...";
|
|
135
|
+
if (this.formatter)
|
|
136
|
+
return this.formatter(this);
|
|
137
|
+
return helper.number.toPrecisionFloor(new BigNumber2(this.value).dividedBy(10 ** decimals).toFixed(), {
|
|
138
|
+
decimals: fixed
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
setDecimals(decimals) {
|
|
142
|
+
this.decimals = decimals;
|
|
143
|
+
}
|
|
144
|
+
setValue(value) {
|
|
145
|
+
this.value = value;
|
|
146
|
+
this.setLoading(false);
|
|
147
|
+
}
|
|
148
|
+
setLoading(val) {
|
|
149
|
+
this.loading = val;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
var BigNumberInputState = class {
|
|
153
|
+
constructor(args) {
|
|
154
|
+
this.value = new BigNumber2(0);
|
|
155
|
+
this.format = "";
|
|
156
|
+
this.loading = false;
|
|
157
|
+
this.decimals = 18;
|
|
158
|
+
Object.assign(this, args);
|
|
159
|
+
makeAutoObservable(this);
|
|
160
|
+
}
|
|
161
|
+
get noDecimasValue() {
|
|
162
|
+
return new BigNumber2(this.value).dividedBy(10 ** this.decimals);
|
|
163
|
+
}
|
|
164
|
+
setValue(value) {
|
|
165
|
+
this.value = new BigNumber2(value);
|
|
166
|
+
this.format = helper.number.warpBigNumber(this.value.toFixed(), this.decimals, { format: "0.000", fallback: "" }).format;
|
|
167
|
+
this.setLoading(false);
|
|
168
|
+
}
|
|
169
|
+
setFormat(val) {
|
|
170
|
+
this.format = val;
|
|
171
|
+
this.value = new BigNumber2(val).multipliedBy(10 ** this.decimals);
|
|
172
|
+
}
|
|
173
|
+
setLoading(val) {
|
|
174
|
+
this.loading = val;
|
|
175
|
+
}
|
|
176
|
+
setDecimals(val) {
|
|
177
|
+
this.decimals = val;
|
|
178
|
+
}
|
|
179
|
+
get isZero() {
|
|
180
|
+
return this.value.isZero() && this.format == "";
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
var StorageState = class _StorageState {
|
|
184
|
+
constructor(args) {
|
|
185
|
+
this.value = null;
|
|
186
|
+
this.default = null;
|
|
187
|
+
Object.assign(this, args);
|
|
188
|
+
makeAutoObservable(this);
|
|
189
|
+
this.load();
|
|
190
|
+
}
|
|
191
|
+
static safeParse(val) {
|
|
192
|
+
try {
|
|
193
|
+
return JSON.parse(val);
|
|
194
|
+
} catch (error) {
|
|
195
|
+
return val;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
load() {
|
|
199
|
+
var _a;
|
|
200
|
+
try {
|
|
201
|
+
if (typeof window == "undefined")
|
|
202
|
+
return;
|
|
203
|
+
const value = (_a = window == null ? void 0 : window.localStorage) == null ? void 0 : _a.getItem(this.key);
|
|
204
|
+
this.value = _StorageState.safeParse(value);
|
|
205
|
+
if (this.value == null) {
|
|
206
|
+
this.value = this.default;
|
|
207
|
+
}
|
|
208
|
+
return this.value;
|
|
209
|
+
} catch (error) {
|
|
210
|
+
console.error(error);
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
save(value) {
|
|
215
|
+
try {
|
|
216
|
+
if (typeof window == "undefined")
|
|
217
|
+
return;
|
|
218
|
+
if (value !== null || value !== void 0) {
|
|
219
|
+
this.value = value;
|
|
220
|
+
}
|
|
221
|
+
window == null ? void 0 : window.localStorage.setItem(this.key, JSON.stringify(value));
|
|
222
|
+
} catch (error) {
|
|
223
|
+
console.error(error);
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
setValue(value) {
|
|
228
|
+
this.save(value);
|
|
229
|
+
}
|
|
230
|
+
clear() {
|
|
231
|
+
try {
|
|
232
|
+
if (typeof window == "undefined")
|
|
233
|
+
return;
|
|
234
|
+
window == null ? void 0 : window.localStorage.removeItem(this.key);
|
|
235
|
+
} catch (error) {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
var StorageListState = class _StorageListState {
|
|
241
|
+
constructor(args) {
|
|
242
|
+
this.list = [];
|
|
243
|
+
Object.assign(this, args);
|
|
244
|
+
makeAutoObservable(this);
|
|
245
|
+
this.load();
|
|
246
|
+
}
|
|
247
|
+
static safeParse(val) {
|
|
248
|
+
try {
|
|
249
|
+
return JSON.parse(val);
|
|
250
|
+
} catch (error) {
|
|
251
|
+
return val;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
load() {
|
|
255
|
+
try {
|
|
256
|
+
const value = localStorage.getItem(this.key);
|
|
257
|
+
if (value) {
|
|
258
|
+
this.list = _StorageListState.safeParse(value);
|
|
259
|
+
}
|
|
260
|
+
return _StorageListState.safeParse(value);
|
|
261
|
+
} catch (error) {
|
|
262
|
+
console.error(error);
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
push(value) {
|
|
267
|
+
this.list.push(value);
|
|
268
|
+
this.save();
|
|
269
|
+
}
|
|
270
|
+
remove(index) {
|
|
271
|
+
this.list.splice(index, 1);
|
|
272
|
+
this.save();
|
|
273
|
+
}
|
|
274
|
+
save() {
|
|
275
|
+
try {
|
|
276
|
+
localStorage.setItem(this.key, JSON.stringify(this.list));
|
|
277
|
+
} catch (error) {
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
clear() {
|
|
281
|
+
try {
|
|
282
|
+
localStorage.removeItem(this.key);
|
|
283
|
+
this.list = [];
|
|
284
|
+
} catch (error) {
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
// store/standard/ObjectPool.ts
|
|
290
|
+
var _ObjectPool = class _ObjectPool {
|
|
291
|
+
static get(key, func) {
|
|
292
|
+
if (!_ObjectPool.pool[key]) {
|
|
293
|
+
_ObjectPool.pool[key] = func();
|
|
294
|
+
}
|
|
295
|
+
return _ObjectPool.pool[key];
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
_ObjectPool.pool = {};
|
|
299
|
+
var ObjectPool = _ObjectPool;
|
|
300
|
+
var WalletConfigStore = class {
|
|
301
|
+
constructor(args) {
|
|
302
|
+
this.sid = "WalletConfigStore";
|
|
303
|
+
this.autoObservable = true;
|
|
304
|
+
this.appName = "Dappkit";
|
|
305
|
+
this.projectId = "b69e844f38265667350efd78e3e1a5fb";
|
|
306
|
+
this.defaultChainId = 4689;
|
|
307
|
+
this.updateTicker = 1;
|
|
308
|
+
this.walletUpdateTick = 0;
|
|
309
|
+
this.isConnect = false;
|
|
310
|
+
this.isInSafeApp = false;
|
|
311
|
+
// This mode to resolve and walletClient and nextui in some extreme cases cause page infinite redraw bugs
|
|
312
|
+
this.compatibleMode = true;
|
|
313
|
+
Object.assign(this, args);
|
|
314
|
+
}
|
|
315
|
+
set(params) {
|
|
316
|
+
Object.assign(this, params);
|
|
317
|
+
this.updateTicker += 1;
|
|
318
|
+
}
|
|
319
|
+
get reconnectOnMount() {
|
|
320
|
+
if (!this.compatibleMode)
|
|
321
|
+
return true;
|
|
322
|
+
if (!this.isConnect && this.walletUpdateTick == 0) {
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
if (!this.isConnect && this.walletUpdateTick != 0) {
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
if (this.isConnect) {
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
get rainbowKitConfig() {
|
|
333
|
+
var _a;
|
|
334
|
+
return ObjectPool.get(`rainbowKitConfig-${(_a = this.supportedChains) == null ? void 0 : _a.map((i) => i.id).join("-")}`, () => {
|
|
335
|
+
return getDefaultConfig({
|
|
336
|
+
appName: this.appName,
|
|
337
|
+
projectId: this.projectId,
|
|
338
|
+
//@ts-ignore
|
|
339
|
+
chains: this.supportedChains,
|
|
340
|
+
wallets: [
|
|
341
|
+
{
|
|
342
|
+
groupName: "Recommended",
|
|
343
|
+
wallets: [iopayWallet, metaMaskWallet]
|
|
344
|
+
},
|
|
345
|
+
{
|
|
346
|
+
groupName: "Others",
|
|
347
|
+
wallets: [metaMaskWallet, walletConnectWallet, iopayWallet, okxWallet, binanceWallet, safeWallet]
|
|
348
|
+
}
|
|
349
|
+
]
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
// module/Wallet/walletPluginStore.tsx
|
|
356
|
+
var defaultRPCList = [
|
|
357
|
+
{ name: "https://babel-api.fastblocks.io", latency: 0, height: 0, custom: false },
|
|
358
|
+
{ name: "https://babel-api.mainnet.iotex.one", latency: 0, height: 0 },
|
|
359
|
+
{ name: "https://babel-api.mainnet.iotex.io", latency: 0, height: 0 },
|
|
360
|
+
{ name: "https://iotex-network.rpc.thirdweb.com", latency: 0, height: 0 },
|
|
361
|
+
{ name: "https://iotexrpc.com", latency: 0, height: 0 },
|
|
362
|
+
{ name: "https://iotex.api.onfinality.io/public", latency: 0, height: 0 },
|
|
363
|
+
{ name: "https://rpc.ankr.com/iotex", latency: 0, height: 0 }
|
|
364
|
+
];
|
|
365
|
+
var WalletRpcStore = class {
|
|
366
|
+
constructor() {
|
|
367
|
+
this.sid = "WalletPluginStore";
|
|
368
|
+
this.autoObservable = true;
|
|
369
|
+
this.curRpc = new StorageState({ default: "https://babel-api.mainnet.iotex.one", key: "curRPC-v2", value: "https://babel-api.mainnet.iotex.one" });
|
|
370
|
+
this.isAutoSelectRpc = new StorageState({ key: "isAutoSelectRpc", default: true });
|
|
371
|
+
this.customRpc = "";
|
|
372
|
+
this.rpcList = new StorageState({ key: "customRpcList-v2", default: defaultRPCList, value: [] });
|
|
373
|
+
this.showCustomRpc = false;
|
|
374
|
+
}
|
|
375
|
+
get currentRpc() {
|
|
376
|
+
var _a, _b;
|
|
377
|
+
console.log((_a = this.rpcList.value) == null ? void 0 : _a.find((i) => i.name == this.curRpc));
|
|
378
|
+
return ((_b = this.rpcList.value) == null ? void 0 : _b.find((i) => i.name == this.curRpc.value)) || null;
|
|
379
|
+
}
|
|
380
|
+
addCustomRpc() {
|
|
381
|
+
const item = { name: this.customRpc, latency: 0, height: 0, custom: true };
|
|
382
|
+
if (defaultRPCList.find((i) => i.name === item.name)) {
|
|
383
|
+
return RootStore.Get(ToastPlugin).error("Rpc already exists");
|
|
384
|
+
}
|
|
385
|
+
if (this.rpcList.value) {
|
|
386
|
+
this.rpcList.save([...this.rpcList.value, item]);
|
|
387
|
+
this.refresh();
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
this.rpcList.save([...defaultRPCList, item]);
|
|
391
|
+
this.refresh();
|
|
392
|
+
}
|
|
393
|
+
async addToMetamask(url) {
|
|
394
|
+
var _a;
|
|
395
|
+
try {
|
|
396
|
+
if (typeof window == "undefined")
|
|
397
|
+
return;
|
|
398
|
+
await ((_a = window == null ? void 0 : window.ethereum) == null ? void 0 : _a.request({
|
|
399
|
+
method: "wallet_addEthereumChain",
|
|
400
|
+
params: [{
|
|
401
|
+
chainId: `0x${4689 .toString(16)}`,
|
|
402
|
+
chainName: "IoTeX Mainnet",
|
|
403
|
+
nativeCurrency: {
|
|
404
|
+
name: "IoTeX",
|
|
405
|
+
symbol: "IOTX",
|
|
406
|
+
decimals: 18
|
|
407
|
+
},
|
|
408
|
+
rpcUrls: [url],
|
|
409
|
+
blockExplorerUrls: ["https://iotexscan.io"]
|
|
410
|
+
}]
|
|
411
|
+
}));
|
|
412
|
+
RootStore.Get(ToastPlugin).success("Network added");
|
|
413
|
+
console.log("Network added");
|
|
414
|
+
} catch (error) {
|
|
415
|
+
console.error("Failed to add network", error);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
async switchOrAddChain(chainId) {
|
|
419
|
+
return new Promise(async (resolve, reject) => {
|
|
420
|
+
try {
|
|
421
|
+
await window.ethereum.request({
|
|
422
|
+
method: "wallet_switchEthereumChain",
|
|
423
|
+
params: [{ chainId: toHex(chainId) }]
|
|
424
|
+
});
|
|
425
|
+
resolve(true);
|
|
426
|
+
} catch (switchError) {
|
|
427
|
+
if (switchError.code === 4902) {
|
|
428
|
+
try {
|
|
429
|
+
await this.addToMetamaskById(chainId);
|
|
430
|
+
} catch (addError) {
|
|
431
|
+
reject(addError);
|
|
432
|
+
}
|
|
433
|
+
} else {
|
|
434
|
+
reject(switchError);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
async addToMetamaskById(id) {
|
|
440
|
+
return new Promise(async (resolve, reject) => {
|
|
441
|
+
try {
|
|
442
|
+
if (typeof window == "undefined")
|
|
443
|
+
return;
|
|
444
|
+
const chain = RootStore.Get(WalletConfigStore).supportedChains.find((i) => i.id == id);
|
|
445
|
+
const res = await window.ethereum.request({
|
|
446
|
+
method: "wallet_addEthereumChain",
|
|
447
|
+
params: [{
|
|
448
|
+
"chainId": toHex(chain.id),
|
|
449
|
+
"chainName": chain.name,
|
|
450
|
+
"nativeCurrency": {
|
|
451
|
+
"name": chain.nativeCurrency.name,
|
|
452
|
+
"symbol": chain.nativeCurrency.symbol,
|
|
453
|
+
"decimals": chain.nativeCurrency.decimals
|
|
454
|
+
},
|
|
455
|
+
"rpcUrls": JSON.parse(JSON.stringify(chain.rpcUrls.default.http)),
|
|
456
|
+
"blockExplorerUrls": [chain.blockExplorers.default.url]
|
|
457
|
+
}]
|
|
458
|
+
});
|
|
459
|
+
console.log(res);
|
|
460
|
+
resolve(res);
|
|
461
|
+
} catch (switchError) {
|
|
462
|
+
reject(false);
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
refresh() {
|
|
467
|
+
this.showCustomRpc = false;
|
|
468
|
+
this.customRpc = "";
|
|
469
|
+
setTimeout(() => {
|
|
470
|
+
this.testRpc();
|
|
471
|
+
}, 500);
|
|
472
|
+
}
|
|
473
|
+
latencyColor(latency) {
|
|
474
|
+
if (latency < 0) {
|
|
475
|
+
return "text-red-500";
|
|
476
|
+
}
|
|
477
|
+
if (latency < 1) {
|
|
478
|
+
return "text-green-500";
|
|
479
|
+
}
|
|
480
|
+
if (latency < 2) {
|
|
481
|
+
return "text-yellow-500";
|
|
482
|
+
}
|
|
483
|
+
return "text-red-500";
|
|
484
|
+
}
|
|
485
|
+
async testRpcFunction(url) {
|
|
486
|
+
const start = performance.now();
|
|
487
|
+
try {
|
|
488
|
+
const response = await fetch(url, {
|
|
489
|
+
method: "POST",
|
|
490
|
+
headers: {
|
|
491
|
+
"Content-Type": "application/json"
|
|
492
|
+
},
|
|
493
|
+
body: JSON.stringify({
|
|
494
|
+
jsonrpc: "2.0",
|
|
495
|
+
method: "eth_getBlockByNumber",
|
|
496
|
+
params: ["latest", false],
|
|
497
|
+
id: 1
|
|
498
|
+
})
|
|
499
|
+
});
|
|
500
|
+
if (!response.ok) {
|
|
501
|
+
throw new Error(`Error from server: ${response.status}`);
|
|
502
|
+
}
|
|
503
|
+
const res = await response.json();
|
|
504
|
+
console.log(res);
|
|
505
|
+
const end = performance.now();
|
|
506
|
+
return { url, lentency: Number(helper.number.numberFormat((end - start) / 1e3, "0.000", { fallback: "-1" })), height: parseInt(res.result.number, 16) };
|
|
507
|
+
} catch (error) {
|
|
508
|
+
console.error("RPC Latency Test Failed:", error);
|
|
509
|
+
return { url, lentency: -1, height: -1 };
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
testRpc() {
|
|
513
|
+
var _a;
|
|
514
|
+
(_a = this.rpcList) == null ? void 0 : _a.value.forEach((i) => {
|
|
515
|
+
this.testRpcFunction(i.name).then((res) => {
|
|
516
|
+
i.latency = res.lentency;
|
|
517
|
+
i.height = res.height;
|
|
518
|
+
});
|
|
519
|
+
});
|
|
520
|
+
this.rpcList.save(this.rpcList.value);
|
|
521
|
+
}
|
|
522
|
+
removeRpc(name) {
|
|
523
|
+
this.rpcList.save(this.rpcList.value.filter((i) => i.name !== name));
|
|
524
|
+
}
|
|
525
|
+
scoreIcon(score) {
|
|
526
|
+
if (score < 0) {
|
|
527
|
+
return /* @__PURE__ */ React2.createElement(Icon, { icon: "codicon:error", width: "18", height: "18", style: { color: "#FF0000" } });
|
|
528
|
+
}
|
|
529
|
+
if (score < 1) {
|
|
530
|
+
return /* @__PURE__ */ React2.createElement(Icon, { icon: "icon-park-solid:check-one", width: "18", height: "18", style: { color: "#289726" } });
|
|
531
|
+
}
|
|
532
|
+
if (score < 2) {
|
|
533
|
+
return /* @__PURE__ */ React2.createElement(Icon, { icon: "bxs:error", width: "18", height: "18", style: { color: "#FFA500" } });
|
|
534
|
+
}
|
|
535
|
+
return /* @__PURE__ */ React2.createElement(Icon, { icon: "codicon:error", width: "18", height: "18", style: { color: "#FF0000" } });
|
|
536
|
+
}
|
|
537
|
+
get wallet() {
|
|
538
|
+
return RootStore.Get(WalletStore);
|
|
539
|
+
}
|
|
540
|
+
// debounceAutoSelectRpc = pDebounce(this.autoSelectRpc, 3000)
|
|
541
|
+
async autoSelectRpc() {
|
|
542
|
+
console.log("autoSelectRpc");
|
|
543
|
+
for (let i = 0; i < this.rpcList.value.length; i++) {
|
|
544
|
+
const item = this.rpcList.value[i];
|
|
545
|
+
const res = await this.testRpcFunction(item.name);
|
|
546
|
+
if (res.lentency != -1 || res.height > 0) {
|
|
547
|
+
this.curRpc.save(item.name);
|
|
548
|
+
break;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
var WalletHistoryStore = class {
|
|
554
|
+
constructor() {
|
|
555
|
+
this.sid = "WalletHistoryStore";
|
|
556
|
+
this.isRender = false;
|
|
557
|
+
this.autoObservable = true;
|
|
558
|
+
this.history = new StorageState({ value: [], key: "history" });
|
|
559
|
+
}
|
|
560
|
+
set(params) {
|
|
561
|
+
Object.assign(this, params);
|
|
562
|
+
}
|
|
563
|
+
get historyList() {
|
|
564
|
+
if (this.isRender) {
|
|
565
|
+
return this.history.value;
|
|
566
|
+
}
|
|
567
|
+
return [];
|
|
568
|
+
}
|
|
569
|
+
recordHistory(item) {
|
|
570
|
+
let value = this.history.load();
|
|
571
|
+
if (!value) {
|
|
572
|
+
value = [item];
|
|
573
|
+
} else {
|
|
574
|
+
value = [...value, item];
|
|
575
|
+
}
|
|
576
|
+
this.history.setValue(value);
|
|
577
|
+
}
|
|
578
|
+
updateHistoryStatusByTx(tx, status) {
|
|
579
|
+
let value = this.history.load();
|
|
580
|
+
if (!value) {
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
value = value.map((i) => {
|
|
584
|
+
if (i.tx == tx) {
|
|
585
|
+
i.status = status;
|
|
586
|
+
return i;
|
|
587
|
+
}
|
|
588
|
+
if (!tx) {
|
|
589
|
+
i.status = status;
|
|
590
|
+
}
|
|
591
|
+
return i;
|
|
592
|
+
});
|
|
593
|
+
this.history.setValue(value);
|
|
594
|
+
}
|
|
595
|
+
clearHistory() {
|
|
596
|
+
this.history.setValue(null);
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
var SuccessTxDialog = observer((props) => {
|
|
600
|
+
return /* @__PURE__ */ React2.createElement("div", { className: "flex-col gap-4 py-10" }, /* @__PURE__ */ React2.createElement("div", { className: "w-full flex items-center justify-center" }, /* @__PURE__ */ React2.createElement(Icon, { icon: "icon-park-solid:check-one", width: "48", height: "48", className: "text-green-500" })), /* @__PURE__ */ React2.createElement("div", { className: "text-2xl font-[900] text-center mt-4" }, props.msg), /* @__PURE__ */ React2.createElement(
|
|
601
|
+
"div",
|
|
602
|
+
{
|
|
603
|
+
className: "flex items-center justify-center text-green-500 text-sm mt-6 gap-2 cursor-pointer hover:text-green-600 transition",
|
|
604
|
+
onClick: (e) => window.open(`https://iotexscan.io/tx/${props.hash}`, "_blank")
|
|
605
|
+
},
|
|
606
|
+
"View on IoTeXScan ",
|
|
607
|
+
/* @__PURE__ */ React2.createElement(Icon, { icon: "material-symbols:chip-extraction-rounded", width: "18", height: "18" })
|
|
608
|
+
));
|
|
609
|
+
});
|
|
610
|
+
var ShowSuccessTxDialog = ({ msg, hash }) => {
|
|
611
|
+
RootStore.Get(DialogStore).setData({
|
|
612
|
+
title: "",
|
|
613
|
+
content: /* @__PURE__ */ React2.createElement(SuccessTxDialog, { msg, hash }),
|
|
614
|
+
isOpen: true
|
|
615
|
+
});
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
// module/Wallet/index.tsx
|
|
619
|
+
var WalletStore = class _WalletStore {
|
|
620
|
+
constructor(args) {
|
|
621
|
+
this.sid = "wallet";
|
|
622
|
+
this.autoObservable = true;
|
|
623
|
+
this.account = null;
|
|
624
|
+
this.isSuccessDialogOpen = false;
|
|
625
|
+
this.isConnect = false;
|
|
626
|
+
this.event = new EventEmitter();
|
|
627
|
+
this.updateTicker = 0;
|
|
628
|
+
this.addressMode = "0x";
|
|
629
|
+
this.isIoTeXChain = false;
|
|
630
|
+
this.balance = PromiseHook.wrap({
|
|
631
|
+
func: async () => {
|
|
632
|
+
var _a;
|
|
633
|
+
if (!this.publicClient || !this.account)
|
|
634
|
+
return helper.number.warpBigNumber("0");
|
|
635
|
+
const balance = await this.publicClient.getBalance({
|
|
636
|
+
address: this.account
|
|
637
|
+
});
|
|
638
|
+
if (balance) {
|
|
639
|
+
return helper.number.warpBigNumber((_a = balance == null ? void 0 : balance.toString()) != null ? _a : "0");
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
Object.assign(this, args);
|
|
644
|
+
}
|
|
645
|
+
// isInSafeApp = false;
|
|
646
|
+
get isInSafeApp() {
|
|
647
|
+
return RootStore.Get(WalletConfigStore).isInSafeApp;
|
|
648
|
+
}
|
|
649
|
+
setAddressMode(mode) {
|
|
650
|
+
this.addressMode = mode;
|
|
651
|
+
localStorage.setItem("addressMode", mode);
|
|
652
|
+
}
|
|
653
|
+
get accountFormat() {
|
|
654
|
+
return this.account ? helper.address.convertAddress(this.addressMode, this.account) : "-";
|
|
655
|
+
}
|
|
656
|
+
get connectAccountEllipsisFormat() {
|
|
657
|
+
return this.account ? helper.string.truncate(helper.address.convertAddress(this.addressMode, this.account), 11, "...") : "-";
|
|
658
|
+
}
|
|
659
|
+
get accountEllipsisFormat() {
|
|
660
|
+
return this.account ? helper.string.truncate(helper.address.convertAddress(this.addressMode, this.account), 16, "...") : "-";
|
|
661
|
+
}
|
|
662
|
+
get supportedChains() {
|
|
663
|
+
return RootStore.Get(WalletConfigStore).supportedChains;
|
|
664
|
+
}
|
|
665
|
+
use() {
|
|
666
|
+
const { chain, address, isConnected } = useAccount();
|
|
667
|
+
const { switchChainAsync } = useSwitchChain();
|
|
668
|
+
const { openConnectModal } = useConnectModal();
|
|
669
|
+
const { connect } = useConnect();
|
|
670
|
+
const { disconnect } = useDisconnect();
|
|
671
|
+
const walletConfigStore = RootStore.Get(WalletConfigStore);
|
|
672
|
+
this.set({
|
|
673
|
+
//@ts-ignore
|
|
674
|
+
connect,
|
|
675
|
+
// @ts-ignore
|
|
676
|
+
// walletClient,
|
|
677
|
+
openConnectModal,
|
|
678
|
+
switchChain: switchChainAsync,
|
|
679
|
+
disconnect
|
|
680
|
+
});
|
|
681
|
+
useEffect(() => {
|
|
682
|
+
RootStore.Get(WalletHistoryStore).set({ isRender: true });
|
|
683
|
+
this.set({
|
|
684
|
+
isConnect: isConnected,
|
|
685
|
+
account: address,
|
|
686
|
+
chain
|
|
687
|
+
});
|
|
688
|
+
if ((chain == null ? void 0 : chain.id) == 4689) {
|
|
689
|
+
this.isIoTeXChain = true;
|
|
690
|
+
} else {
|
|
691
|
+
this.isIoTeXChain = false;
|
|
692
|
+
}
|
|
693
|
+
walletConfigStore.set({
|
|
694
|
+
isConnect: isConnected
|
|
695
|
+
});
|
|
696
|
+
if (this.account) {
|
|
697
|
+
this.updateTicker++;
|
|
698
|
+
walletConfigStore.set({
|
|
699
|
+
walletUpdateTick: this.updateTicker
|
|
700
|
+
});
|
|
701
|
+
this.event.emit("walletAccount:ready");
|
|
702
|
+
}
|
|
703
|
+
this.useWalletClientWithCompatibleMode();
|
|
704
|
+
}, [address, isConnected, chain]);
|
|
705
|
+
useEffect(() => {
|
|
706
|
+
setTimeout(() => {
|
|
707
|
+
this.balance.call();
|
|
708
|
+
}, 1500);
|
|
709
|
+
}, [this.updateTicker]);
|
|
710
|
+
}
|
|
711
|
+
useWalletClientWithCompatibleMode() {
|
|
712
|
+
if (RootStore.Get(WalletConfigStore).compatibleMode) {
|
|
713
|
+
if (this.account) {
|
|
714
|
+
this.walletClient = createWalletClient({
|
|
715
|
+
account: this.account,
|
|
716
|
+
chain: this.chain,
|
|
717
|
+
transport: custom(window.ethereum)
|
|
718
|
+
}).extend(publicActions);
|
|
719
|
+
}
|
|
720
|
+
console.log(this.walletClient, "this.walletClientxxx");
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
useWalletClientWithoutCompatibleMode() {
|
|
724
|
+
const { data: walletClient } = useWalletClient();
|
|
725
|
+
this.set({
|
|
726
|
+
walletClient
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
//always return or return default chain
|
|
730
|
+
get publicClient() {
|
|
731
|
+
if (this.chain && this.supportedChains.some((i) => i.id === this.chain.id)) {
|
|
732
|
+
if (this.chain.id == 4689) {
|
|
733
|
+
return AIem.PubClient("4689", { rpcUrls: { default: { http: [RootStore.Get(WalletRpcStore).curRpc.value] } }, multicall: true });
|
|
734
|
+
}
|
|
735
|
+
return AIem.PubClient(this.chain.id.toString());
|
|
736
|
+
} else {
|
|
737
|
+
return AIem.PubClient("4689");
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
set(args) {
|
|
741
|
+
Object.assign(this, args);
|
|
742
|
+
}
|
|
743
|
+
toJSON() {
|
|
744
|
+
const { account } = this;
|
|
745
|
+
return { account };
|
|
746
|
+
}
|
|
747
|
+
async prepare(chainId) {
|
|
748
|
+
const walletRpcStore = RootStore.Get(WalletRpcStore);
|
|
749
|
+
return new Promise(async (res, rej) => {
|
|
750
|
+
var _a, _b;
|
|
751
|
+
try {
|
|
752
|
+
if (this.account) {
|
|
753
|
+
if ((!chainId || Number((_a = this.chain) == null ? void 0 : _a.id) == Number(chainId)) && this.walletClient) {
|
|
754
|
+
res(this);
|
|
755
|
+
}
|
|
756
|
+
const interval = setInterval(() => {
|
|
757
|
+
var _a2;
|
|
758
|
+
if (((_a2 = this.chain) == null ? void 0 : _a2.id) == chainId) {
|
|
759
|
+
try {
|
|
760
|
+
console.log("useWalletClientWithCompatibleMode");
|
|
761
|
+
this.useWalletClientWithCompatibleMode();
|
|
762
|
+
res(this);
|
|
763
|
+
} catch (error) {
|
|
764
|
+
console.log(error, "myerror");
|
|
765
|
+
rej(error);
|
|
766
|
+
}
|
|
767
|
+
clearInterval(interval);
|
|
768
|
+
}
|
|
769
|
+
}, 1e3);
|
|
770
|
+
if (chainId != ((_b = this.chain) == null ? void 0 : _b.id)) {
|
|
771
|
+
try {
|
|
772
|
+
walletRpcStore.switchOrAddChain(chainId != null ? chainId : 4689);
|
|
773
|
+
} catch (error) {
|
|
774
|
+
rej(error);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
} else {
|
|
778
|
+
this.openConnectModal();
|
|
779
|
+
const interval = setInterval(() => {
|
|
780
|
+
if (this.account) {
|
|
781
|
+
clearInterval(interval);
|
|
782
|
+
res(this);
|
|
783
|
+
}
|
|
784
|
+
}, 1e3);
|
|
785
|
+
}
|
|
786
|
+
} catch (error) {
|
|
787
|
+
rej(error);
|
|
788
|
+
}
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
async waitForTransactionReceipt({ hash }) {
|
|
792
|
+
if (this.isInSafeApp) {
|
|
793
|
+
const sdk = new SafeAppsSDK();
|
|
794
|
+
while (true) {
|
|
795
|
+
const queued = await sdk.txs.getBySafeTxHash(hash);
|
|
796
|
+
if (queued.txStatus === TransactionStatus.AWAITING_CONFIRMATIONS || queued.txStatus === TransactionStatus.AWAITING_EXECUTION || queued.txStatus === TransactionStatus.CANCELLED) {
|
|
797
|
+
await new Promise((resolve) => setTimeout(resolve, 2e3));
|
|
798
|
+
} else {
|
|
799
|
+
return this.publicClient.waitForTransactionReceipt({ hash: queued.txHash });
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
} else {
|
|
803
|
+
return this.publicClient.waitForTransactionReceipt({ hash });
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
static async SendTx(...args) {
|
|
807
|
+
return RootStore.Get(_WalletStore).sendTx(...args);
|
|
808
|
+
}
|
|
809
|
+
static async SendRawTx(...args) {
|
|
810
|
+
return RootStore.Get(_WalletStore).sendRawTx(...args);
|
|
811
|
+
}
|
|
812
|
+
async sendTx({
|
|
813
|
+
chainId,
|
|
814
|
+
tx,
|
|
815
|
+
autoAlert = true,
|
|
816
|
+
loadingText,
|
|
817
|
+
successText,
|
|
818
|
+
onError,
|
|
819
|
+
historyItem,
|
|
820
|
+
showSuccessDialog = false
|
|
821
|
+
}) {
|
|
822
|
+
var _a, _b, _c;
|
|
823
|
+
const toast = RootStore.Get(ToastPlugin);
|
|
824
|
+
let hash;
|
|
825
|
+
const historyStore = RootStore.Get(WalletHistoryStore);
|
|
826
|
+
try {
|
|
827
|
+
if (loadingText)
|
|
828
|
+
toast.loading(loadingText);
|
|
829
|
+
if (!chainId)
|
|
830
|
+
throw new Error("chainId, address, data is required");
|
|
831
|
+
await RootStore.Get(_WalletStore).prepare(Number(chainId));
|
|
832
|
+
hash = await tx();
|
|
833
|
+
if (historyItem) {
|
|
834
|
+
historyStore.recordHistory(__spreadProps(__spreadValues({}, historyItem), { tx: hash, timestamp: Date.now(), status: "loading", chainId: Number(chainId) }));
|
|
835
|
+
}
|
|
836
|
+
const receipt = await this.waitForTransactionReceipt({ hash });
|
|
837
|
+
if (receipt.status == "success") {
|
|
838
|
+
toast.dismiss();
|
|
839
|
+
toast.success("The transaction was successful");
|
|
840
|
+
if (historyItem) {
|
|
841
|
+
if (showSuccessDialog) {
|
|
842
|
+
ShowSuccessTxDialog({ msg: historyItem.msg, hash });
|
|
843
|
+
}
|
|
844
|
+
historyStore.updateHistoryStatusByTx(hash, "success");
|
|
845
|
+
}
|
|
846
|
+
} else {
|
|
847
|
+
toast.dismiss();
|
|
848
|
+
toast.error("The transaction failed");
|
|
849
|
+
historyStore.updateHistoryStatusByTx(hash, "fail");
|
|
850
|
+
}
|
|
851
|
+
if (successText)
|
|
852
|
+
toast.success(successText);
|
|
853
|
+
this.updateTicker++;
|
|
854
|
+
return receipt;
|
|
855
|
+
} catch (error) {
|
|
856
|
+
console.log(error);
|
|
857
|
+
toast.dismiss();
|
|
858
|
+
if (autoAlert) {
|
|
859
|
+
const msg = /reason="[A-Za-z0-9_ :"]*/g.exec(error == null ? void 0 : error.message);
|
|
860
|
+
console.log("sendTx", error == null ? void 0 : error.message);
|
|
861
|
+
if (((_a = error == null ? void 0 : error.message) == null ? void 0 : _a.includes("rejected")) || String(error).toLowerCase().includes("rejected") || String(error).toLowerCase().includes("denied")) {
|
|
862
|
+
toast.error("User rejected transaction");
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
if (((_b = error == null ? void 0 : error.message) == null ? void 0 : _b.includes("The Transaction may not be processed on a block yet")) || ((_c = error == null ? void 0 : error.message) == null ? void 0 : _c.includes("could not be found"))) {
|
|
866
|
+
if (historyItem) {
|
|
867
|
+
historyStore.updateHistoryStatusByTx(hash, "success");
|
|
868
|
+
}
|
|
869
|
+
toast.success("The transaction was successful");
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
if (msg) {
|
|
873
|
+
onError == null ? void 0 : onError(new Error(msg || "Transaction failed"));
|
|
874
|
+
} else {
|
|
875
|
+
if (error == null ? void 0 : error.message.includes("viem")) {
|
|
876
|
+
const messageArr = error == null ? void 0 : error.message.split("\n");
|
|
877
|
+
console.log("messageArr---", messageArr);
|
|
878
|
+
if (messageArr.length > 0) {
|
|
879
|
+
onError == null ? void 0 : onError(new Error(messageArr[0] || "Transaction failed"));
|
|
880
|
+
}
|
|
881
|
+
} else {
|
|
882
|
+
onError == null ? void 0 : onError(new Error((error == null ? void 0 : error.message) || "Transaction failed"));
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
} else {
|
|
886
|
+
onError == null ? void 0 : onError(error);
|
|
887
|
+
throw error;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
async sendRawTx({
|
|
892
|
+
chainId,
|
|
893
|
+
address,
|
|
894
|
+
data,
|
|
895
|
+
value = "0",
|
|
896
|
+
autoAlert = true,
|
|
897
|
+
onSended,
|
|
898
|
+
onSuccess,
|
|
899
|
+
onError,
|
|
900
|
+
historyItem,
|
|
901
|
+
loadingText,
|
|
902
|
+
showSuccessDialog = false
|
|
903
|
+
}) {
|
|
904
|
+
var _a, _b, _c, _d;
|
|
905
|
+
chainId = Number(chainId);
|
|
906
|
+
const toast = RootStore.Get(ToastPlugin);
|
|
907
|
+
try {
|
|
908
|
+
if (!chainId || !address)
|
|
909
|
+
throw new Error("chainId, address, is required");
|
|
910
|
+
await RootStore.Get(_WalletStore).prepare(chainId);
|
|
911
|
+
if (loadingText)
|
|
912
|
+
toast.loading(loadingText);
|
|
913
|
+
const historyStore = RootStore.Get(WalletHistoryStore);
|
|
914
|
+
const hash = await this.walletClient.sendTransaction({
|
|
915
|
+
account: this.account,
|
|
916
|
+
to: address,
|
|
917
|
+
data,
|
|
918
|
+
value: value ? BigInt(value) : void 0
|
|
919
|
+
});
|
|
920
|
+
let receipt = await this.waitForTransactionReceipt({ hash });
|
|
921
|
+
if (historyItem) {
|
|
922
|
+
historyStore.recordHistory(__spreadProps(__spreadValues({}, historyItem), { tx: receipt.transactionHash, timestamp: Date.now(), status: "loading", chainId: Number(chainId) }));
|
|
923
|
+
}
|
|
924
|
+
onSended ? onSended({ res: receipt }) : null;
|
|
925
|
+
if (receipt.status == "success") {
|
|
926
|
+
if (historyItem) {
|
|
927
|
+
if (showSuccessDialog) {
|
|
928
|
+
ShowSuccessTxDialog({ msg: historyItem.msg, hash });
|
|
929
|
+
}
|
|
930
|
+
historyStore.updateHistoryStatusByTx(receipt.transactionHash, "success");
|
|
931
|
+
}
|
|
932
|
+
onSuccess && onSuccess({ res: receipt });
|
|
933
|
+
toast.dismiss();
|
|
934
|
+
toast.success("The transaction was successful");
|
|
935
|
+
} else {
|
|
936
|
+
if (historyItem) {
|
|
937
|
+
historyStore.updateHistoryStatusByTx(receipt.transactionHash, "fail");
|
|
938
|
+
}
|
|
939
|
+
onError && (onError == null ? void 0 : onError(new Error("The transaction failed")));
|
|
940
|
+
toast.dismiss();
|
|
941
|
+
toast.error("The transaction failed");
|
|
942
|
+
}
|
|
943
|
+
return receipt;
|
|
944
|
+
} catch (error) {
|
|
945
|
+
console.log(error, "xty");
|
|
946
|
+
toast.dismiss();
|
|
947
|
+
onError == null ? void 0 : onError(error);
|
|
948
|
+
console.log(error.message);
|
|
949
|
+
/reason="[A-Za-z0-9_ :"]*/g.exec(error == null ? void 0 : error.message);
|
|
950
|
+
if (((_a = error == null ? void 0 : error.message) == null ? void 0 : _a.includes("rejected")) || ((_b = error == null ? void 0 : error.message) == null ? void 0 : _b.includes("cancel")) || String(error).toLowerCase().includes("rejected") || String(error).toLowerCase().includes("user denied")) {
|
|
951
|
+
autoAlert && toast.error("User rejected transaction");
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
if ((_c = error == null ? void 0 : error.message) == null ? void 0 : _c.includes("Price slippage check")) {
|
|
955
|
+
autoAlert && toast.error("The latest pool price has changed, please try to increase the slippage tolerance or reload the page.");
|
|
956
|
+
return;
|
|
957
|
+
}
|
|
958
|
+
if (error == null ? void 0 : error.message.includes("viem")) {
|
|
959
|
+
const messageArr = error == null ? void 0 : error.message.split("\n");
|
|
960
|
+
console.log("messageArr---", messageArr);
|
|
961
|
+
if (messageArr.length > 0) {
|
|
962
|
+
onError == null ? void 0 : onError(new Error((_d = messageArr[0]) != null ? _d : "Transaction failed"));
|
|
963
|
+
return;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
if (!autoAlert) {
|
|
967
|
+
onError == null ? void 0 : onError(error);
|
|
968
|
+
throw error;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
};
|
|
973
|
+
var AutoMan = class {
|
|
974
|
+
constructor() {
|
|
975
|
+
this.sid = "AutoMan";
|
|
976
|
+
this.stype = "Plugin";
|
|
977
|
+
this.provider = observer(() => {
|
|
978
|
+
const wallet = RootStore.Get(WalletStore);
|
|
979
|
+
const router = useRouter();
|
|
980
|
+
useEffect(() => {
|
|
981
|
+
console.log("automan proverider");
|
|
982
|
+
const stores = Object.values(rootStore.instance).filter((i) => i._active > 0);
|
|
983
|
+
const promise = stores.reduce((p, c) => {
|
|
984
|
+
const promise2 = Object.values(c).filter((i) => !!(i == null ? void 0 : i.call));
|
|
985
|
+
return [...p, ...promise2];
|
|
986
|
+
}, []);
|
|
987
|
+
const autoUpdate = promise.filter((i) => i.autoUpdate);
|
|
988
|
+
console.log({ autoUpdate });
|
|
989
|
+
autoUpdate.forEach((i) => !i.loading.value && i.call());
|
|
990
|
+
}, [wallet.updateTicker, router.asPath]);
|
|
991
|
+
return /* @__PURE__ */ React2.createElement(React2.Fragment, null);
|
|
992
|
+
});
|
|
993
|
+
}
|
|
994
|
+
static use(stores, promises) {
|
|
995
|
+
useEffect(() => {
|
|
996
|
+
Object.keys(stores).forEach((storeName) => {
|
|
997
|
+
const store = stores[storeName];
|
|
998
|
+
Object.keys(store).forEach((key) => {
|
|
999
|
+
const i = store[key];
|
|
1000
|
+
if (!!(i == null ? void 0 : i.getOrCall) && (i.autoInit || !!(promises == null ? void 0 : promises[storeName][key]))) {
|
|
1001
|
+
if (!store._active)
|
|
1002
|
+
store._active = 0;
|
|
1003
|
+
store._active += 1;
|
|
1004
|
+
i.getOrCall();
|
|
1005
|
+
}
|
|
1006
|
+
});
|
|
1007
|
+
});
|
|
1008
|
+
return () => {
|
|
1009
|
+
Object.values(stores).forEach((store) => {
|
|
1010
|
+
if (!store._active)
|
|
1011
|
+
store._active = 0;
|
|
1012
|
+
store._active -= 1;
|
|
1013
|
+
if (store.autoClean !== false && !store._active > 0) {
|
|
1014
|
+
Object.entries(store).filter(([key, i]) => !!(i == null ? void 0 : i.value)).forEach(([key, i]) => {
|
|
1015
|
+
console.log("clean", key);
|
|
1016
|
+
i.value = null;
|
|
1017
|
+
});
|
|
1018
|
+
}
|
|
1019
|
+
});
|
|
1020
|
+
if (!!promises) {
|
|
1021
|
+
Object.entries(promises).forEach(([storaName, promise]) => {
|
|
1022
|
+
const store = stores[storaName];
|
|
1023
|
+
Object.keys(promise).forEach((key) => {
|
|
1024
|
+
const promise2 = store[key];
|
|
1025
|
+
console.log("clean", key);
|
|
1026
|
+
if (!promise2)
|
|
1027
|
+
return;
|
|
1028
|
+
if (!promise2._active)
|
|
1029
|
+
promise2._active = 0;
|
|
1030
|
+
promise2._active -= 1;
|
|
1031
|
+
if (!promise2._active > 0) {
|
|
1032
|
+
promise2.value = null;
|
|
1033
|
+
}
|
|
1034
|
+
});
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
};
|
|
1038
|
+
}, []);
|
|
1039
|
+
return stores;
|
|
1040
|
+
}
|
|
1041
|
+
};
|
|
1042
|
+
var AppProvider = observer(({ children }) => {
|
|
1043
|
+
const rootStore2 = RootStore.init();
|
|
1044
|
+
return /* @__PURE__ */ React2.createElement(React2.Fragment, null, rootStore2.providers.map((store) => {
|
|
1045
|
+
const Component = store.provider;
|
|
1046
|
+
return /* @__PURE__ */ React2.createElement(Component, { rootStore: rootStore2, key: store.sid });
|
|
1047
|
+
}), children && children);
|
|
1048
|
+
});
|
|
1049
|
+
|
|
1050
|
+
export { AppProvider, AutoMan, BigNumberInputState, BigNumberState, ObjectPool, PromiseHook, StorageListState, StorageState, WalletConfigStore, WalletHistoryStore, WalletRpcStore, WalletStore };
|
|
1051
|
+
//# sourceMappingURL=out.js.map
|
|
1052
|
+
//# sourceMappingURL=chunk-NO4O46V2.mjs.map
|