@luno-kit/react 0.0.1
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/index.cjs +1177 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +256 -0
- package/dist/index.d.ts +256 -0
- package/dist/index.js +1143 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1177 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var zustand = require('zustand');
|
|
5
|
+
var core = require('@luno-kit/core');
|
|
6
|
+
var dedot = require('dedot');
|
|
7
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
8
|
+
var reactQuery = require('@tanstack/react-query');
|
|
9
|
+
var utils = require('dedot/utils');
|
|
10
|
+
|
|
11
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
|
+
|
|
13
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
14
|
+
|
|
15
|
+
// src/types/state.ts
|
|
16
|
+
var ConnectionStatus = /* @__PURE__ */ ((ConnectionStatus2) => {
|
|
17
|
+
ConnectionStatus2["Disconnected"] = "disconnected";
|
|
18
|
+
ConnectionStatus2["Connecting"] = "connecting";
|
|
19
|
+
ConnectionStatus2["Disconnecting"] = "disconnecting";
|
|
20
|
+
ConnectionStatus2["Connected"] = "connected";
|
|
21
|
+
return ConnectionStatus2;
|
|
22
|
+
})(ConnectionStatus || {});
|
|
23
|
+
|
|
24
|
+
// src/constants.ts
|
|
25
|
+
var PERSIST_KEY = {
|
|
26
|
+
LAST_CONNECTOR_ID: "lastConnectorId",
|
|
27
|
+
LAST_CHAIN_ID: "lastChainId",
|
|
28
|
+
LAST_SELECTED_ACCOUNT_INFO: "lastSelectedAccountInfo"
|
|
29
|
+
};
|
|
30
|
+
var createApi = async ({
|
|
31
|
+
config,
|
|
32
|
+
chainId
|
|
33
|
+
}) => {
|
|
34
|
+
const chainConfig = config.chains.find((c) => c.genesisHash === chainId);
|
|
35
|
+
const transportConfig = config.transports[chainId];
|
|
36
|
+
if (!chainConfig || !transportConfig) {
|
|
37
|
+
throw new Error(`Configuration missing for chainId: ${chainId}`);
|
|
38
|
+
}
|
|
39
|
+
const provider = core.wsProvider(transportConfig);
|
|
40
|
+
const apiOptions = {
|
|
41
|
+
provider,
|
|
42
|
+
cacheMetadata: config.cacheMetadata,
|
|
43
|
+
metadata: config.metadata,
|
|
44
|
+
scaledResponses: {
|
|
45
|
+
...config.scaledResponses,
|
|
46
|
+
...config.customTypes
|
|
47
|
+
},
|
|
48
|
+
runtimeApis: config.runtimeApis,
|
|
49
|
+
cacheStorage: config.cacheStorage
|
|
50
|
+
};
|
|
51
|
+
const newApi = new dedot.LegacyClient(apiOptions);
|
|
52
|
+
try {
|
|
53
|
+
await newApi.connect();
|
|
54
|
+
const actualGenesisHash = await newApi.rpc.chain_getBlockHash(0);
|
|
55
|
+
if (actualGenesisHash !== chainId) {
|
|
56
|
+
await newApi.disconnect();
|
|
57
|
+
throw new Error(
|
|
58
|
+
`Chain genesis hash mismatch. Expected: ${chainId}, Got: ${actualGenesisHash}. This might indicate connecting to the wrong network or incorrect chain configuration.`
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return newApi;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
throw new Error(`Failed to connect to ${chainConfig.name}: ${error?.message || error}`);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// src/utils/dispatchError.ts
|
|
68
|
+
function getReadableDispatchError(api, dispatchError) {
|
|
69
|
+
if (dispatchError.type === "Module") {
|
|
70
|
+
try {
|
|
71
|
+
const errorMeta = api.registry.findErrorMeta(dispatchError);
|
|
72
|
+
if (errorMeta) {
|
|
73
|
+
const { docs, name, pallet, fields, fieldCodecs } = errorMeta;
|
|
74
|
+
return `[useSendTransaction]: ${pallet}.${name} error, ${docs.join(" ")}`;
|
|
75
|
+
}
|
|
76
|
+
return `[useSendTransaction]: Module Error (index: ${dispatchError.value.index}, error: ${dispatchError.value.error})`;
|
|
77
|
+
} catch (e) {
|
|
78
|
+
const { value: { error, index } } = dispatchError;
|
|
79
|
+
return `[useSendTransaction]: Module Error (index: ${index}, error: ${error}) - Failed to decode: ${e instanceof Error ? e.message : "Unknown error"}`;
|
|
80
|
+
}
|
|
81
|
+
} else if (dispatchError.type === "Token") {
|
|
82
|
+
return `[useSendTransaction]: Token Error: ${dispatchError.value}`;
|
|
83
|
+
}
|
|
84
|
+
if ("value" in dispatchError) {
|
|
85
|
+
return `[useSendTransaction]: ${dispatchError.type} Error: ${dispatchError.value}`;
|
|
86
|
+
}
|
|
87
|
+
return `[useSendTransaction]: Error: ${dispatchError.type}`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/store/createLunoStore.ts
|
|
91
|
+
var activeConnectorUnsubscribeFunctions = [];
|
|
92
|
+
var cleanupActiveConnectorListeners = () => {
|
|
93
|
+
activeConnectorUnsubscribeFunctions.forEach((unsub) => {
|
|
94
|
+
try {
|
|
95
|
+
unsub();
|
|
96
|
+
} catch (e) {
|
|
97
|
+
console.warn("[LunoStore] Error during listener cleanup:", e);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
activeConnectorUnsubscribeFunctions = [];
|
|
101
|
+
};
|
|
102
|
+
var useLunoStore = zustand.create((set, get) => ({
|
|
103
|
+
config: void 0,
|
|
104
|
+
status: "disconnected" /* Disconnected */,
|
|
105
|
+
activeConnector: void 0,
|
|
106
|
+
accounts: [],
|
|
107
|
+
account: void 0,
|
|
108
|
+
currentChainId: void 0,
|
|
109
|
+
currentChain: void 0,
|
|
110
|
+
currentApi: void 0,
|
|
111
|
+
isApiReady: false,
|
|
112
|
+
apiError: null,
|
|
113
|
+
_setConfig: async (newConfig) => {
|
|
114
|
+
cleanupActiveConnectorListeners();
|
|
115
|
+
let storedChainId = null;
|
|
116
|
+
try {
|
|
117
|
+
storedChainId = await newConfig.storage.getItem(PERSIST_KEY.LAST_CHAIN_ID);
|
|
118
|
+
} catch (e) {
|
|
119
|
+
console.warn("[LunoStore] Failed to read stored chain ID from storage:", e);
|
|
120
|
+
}
|
|
121
|
+
const normalizedStoredChainId = storedChainId?.toLowerCase();
|
|
122
|
+
const initialChainId = normalizedStoredChainId && newConfig.chains.some((c) => c.genesisHash.toLowerCase() === normalizedStoredChainId) ? normalizedStoredChainId : newConfig.chains[0]?.genesisHash;
|
|
123
|
+
const initialChain = initialChainId ? newConfig.chains.find((c) => c.genesisHash.toLowerCase() === initialChainId) : void 0;
|
|
124
|
+
set({
|
|
125
|
+
config: newConfig,
|
|
126
|
+
status: "disconnected" /* Disconnected */,
|
|
127
|
+
activeConnector: void 0,
|
|
128
|
+
accounts: [],
|
|
129
|
+
currentChainId: initialChainId,
|
|
130
|
+
currentChain: initialChain
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
_setApi: (apiInstance) => {
|
|
134
|
+
set({ currentApi: apiInstance });
|
|
135
|
+
},
|
|
136
|
+
_setIsApiReady: (isReady) => {
|
|
137
|
+
set({ isApiReady: isReady });
|
|
138
|
+
},
|
|
139
|
+
setAccount: async (accountOrPublicKey) => {
|
|
140
|
+
if (!accountOrPublicKey) return;
|
|
141
|
+
const { accounts, config } = get();
|
|
142
|
+
const targetPublicKey = typeof accountOrPublicKey === "string" ? accountOrPublicKey.toLowerCase() : accountOrPublicKey.publicKey?.toLowerCase();
|
|
143
|
+
const nextAccount = accounts.find((acc) => acc.publicKey?.toLowerCase() === targetPublicKey);
|
|
144
|
+
if (!nextAccount) {
|
|
145
|
+
throw new Error("[LunoStore] setAccount: The provided account or address is not in the current accounts list. Ignored.");
|
|
146
|
+
}
|
|
147
|
+
set({ account: nextAccount });
|
|
148
|
+
if (config) {
|
|
149
|
+
try {
|
|
150
|
+
const accountInfo = {
|
|
151
|
+
publicKey: nextAccount.publicKey,
|
|
152
|
+
address: nextAccount.address,
|
|
153
|
+
name: nextAccount.name,
|
|
154
|
+
source: nextAccount.meta.source
|
|
155
|
+
};
|
|
156
|
+
await config.storage.setItem(PERSIST_KEY.LAST_SELECTED_ACCOUNT_INFO, JSON.stringify(accountInfo));
|
|
157
|
+
console.log(`[LunoStore] Persisted selected account: ${nextAccount.address}`);
|
|
158
|
+
} catch (e) {
|
|
159
|
+
console.error("[LunoStore] Failed to persist selected account:", e);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
connect: async (connectorId, targetChainId) => {
|
|
164
|
+
const config = get().config;
|
|
165
|
+
if (!config) {
|
|
166
|
+
set({ status: "disconnected" /* Disconnected */ });
|
|
167
|
+
throw new Error("[LunoStore] LunoConfig has not been initialized. Cannot connect.");
|
|
168
|
+
}
|
|
169
|
+
const connector = config.connectors.find((c) => c.id === connectorId);
|
|
170
|
+
if (!connector) {
|
|
171
|
+
set({ status: "disconnected" /* Disconnected */ });
|
|
172
|
+
throw new Error(`[LunoStore] Connector with ID "${connectorId}" not found in LunoConfig.`);
|
|
173
|
+
}
|
|
174
|
+
set({ status: "connecting" /* Connecting */ });
|
|
175
|
+
const previouslyActiveConnector = get().activeConnector;
|
|
176
|
+
if (previouslyActiveConnector && previouslyActiveConnector.id !== connector.id) {
|
|
177
|
+
console.log(`[LunoStore] Switching connector. Cleaning up listeners for old connector: ${previouslyActiveConnector.id}`);
|
|
178
|
+
cleanupActiveConnectorListeners();
|
|
179
|
+
} else if (previouslyActiveConnector && previouslyActiveConnector.id === connector.id) {
|
|
180
|
+
console.log(`[LunoStore] Attempting to reconnect with the same connector: ${connector.id}. Cleaning up existing listeners.`);
|
|
181
|
+
cleanupActiveConnectorListeners();
|
|
182
|
+
}
|
|
183
|
+
try {
|
|
184
|
+
const handleAccountsChanged = async (newAccounts) => {
|
|
185
|
+
console.log(`[LunoStore] accountsChanged event from ${connector.name}:`, newAccounts);
|
|
186
|
+
newAccounts.forEach((acc) => {
|
|
187
|
+
if (!acc.publicKey) {
|
|
188
|
+
console.warn(`[LunoStore] Account ${acc.address} (from ${connector.name}) is missing publicKey.`);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
let selectedAccount2 = newAccounts[0];
|
|
192
|
+
try {
|
|
193
|
+
const storedAccountJson = await config.storage.getItem(PERSIST_KEY.LAST_SELECTED_ACCOUNT_INFO);
|
|
194
|
+
if (storedAccountJson) {
|
|
195
|
+
const storedAccount = JSON.parse(storedAccountJson);
|
|
196
|
+
const restoredAccount = newAccounts.find(
|
|
197
|
+
(acc) => storedAccount.publicKey && acc.publicKey?.toLowerCase() === storedAccount.publicKey.toLowerCase() || core.isSameAddress(acc.address, storedAccount.address)
|
|
198
|
+
);
|
|
199
|
+
if (restoredAccount) {
|
|
200
|
+
selectedAccount2 = restoredAccount;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
} catch (e) {
|
|
204
|
+
console.warn("[LunoStore] Failed to restore account during accountsChanged:", e);
|
|
205
|
+
}
|
|
206
|
+
set({ accounts: newAccounts, account: selectedAccount2 });
|
|
207
|
+
};
|
|
208
|
+
const handleDisconnect = () => {
|
|
209
|
+
console.log(`[LunoStore] disconnect event from ${connector.name}`);
|
|
210
|
+
if (get().activeConnector?.id === connector.id) {
|
|
211
|
+
cleanupActiveConnectorListeners();
|
|
212
|
+
try {
|
|
213
|
+
config.storage.removeItem(PERSIST_KEY.LAST_CONNECTOR_ID);
|
|
214
|
+
config.storage.removeItem(PERSIST_KEY.LAST_CHAIN_ID);
|
|
215
|
+
console.log("[LunoStore] Removed persisted connection info from storage due to disconnect event.");
|
|
216
|
+
} catch (e) {
|
|
217
|
+
console.error("[LunoStore] Failed to remove connection info from storage:", e);
|
|
218
|
+
}
|
|
219
|
+
set({
|
|
220
|
+
status: "disconnected" /* Disconnected */,
|
|
221
|
+
activeConnector: void 0,
|
|
222
|
+
accounts: []
|
|
223
|
+
});
|
|
224
|
+
} else {
|
|
225
|
+
console.warn(`[LunoStore] Received disconnect event from an inactive connector ${connector.name}. Ignored.`);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
connector.on("accountsChanged", handleAccountsChanged);
|
|
229
|
+
activeConnectorUnsubscribeFunctions.push(() => connector.off("accountsChanged", handleAccountsChanged));
|
|
230
|
+
connector.on("disconnect", handleDisconnect);
|
|
231
|
+
activeConnectorUnsubscribeFunctions.push(() => connector.off("disconnect", handleDisconnect));
|
|
232
|
+
const chainIdToUse = targetChainId || get().currentChainId || config.chains[0]?.genesisHash;
|
|
233
|
+
const accountsFromWallet = await connector.connect(config.appName, config.chains, chainIdToUse);
|
|
234
|
+
accountsFromWallet.forEach((acc) => {
|
|
235
|
+
if (!acc.publicKey) {
|
|
236
|
+
console.error(`[LunoStore] CRITICAL WARNING: Account ${acc.address} from connector ${connector.name} was returned without a publicKey! SS58 address formatting will fail.`);
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
let selectedAccount = accountsFromWallet[0];
|
|
240
|
+
try {
|
|
241
|
+
const storedAccountJson = await config.storage.getItem(PERSIST_KEY.LAST_SELECTED_ACCOUNT_INFO);
|
|
242
|
+
if (storedAccountJson) {
|
|
243
|
+
const storedAccount = JSON.parse(storedAccountJson);
|
|
244
|
+
const restoredAccount = accountsFromWallet.find(
|
|
245
|
+
(acc) => storedAccount.publicKey && acc.publicKey?.toLowerCase() === storedAccount.publicKey.toLowerCase() || core.isSameAddress(acc.address, storedAccount.address)
|
|
246
|
+
);
|
|
247
|
+
if (restoredAccount) {
|
|
248
|
+
selectedAccount = restoredAccount;
|
|
249
|
+
console.log(`[LunoStore] Restored previously selected account: ${selectedAccount.address}`);
|
|
250
|
+
} else {
|
|
251
|
+
console.log("[LunoStore] Previously selected account not found in current accounts list, using first account");
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
} catch (e) {
|
|
255
|
+
console.warn("[LunoStore] Failed to restore selected account from storage:", e);
|
|
256
|
+
}
|
|
257
|
+
set({
|
|
258
|
+
activeConnector: connector,
|
|
259
|
+
accounts: accountsFromWallet,
|
|
260
|
+
status: "connected" /* Connected */,
|
|
261
|
+
account: selectedAccount
|
|
262
|
+
});
|
|
263
|
+
try {
|
|
264
|
+
config.storage.setItem(PERSIST_KEY.LAST_CONNECTOR_ID, connector.id);
|
|
265
|
+
console.log(`[LunoStore] Persisted connectorId: ${connector.id}`);
|
|
266
|
+
} catch (e) {
|
|
267
|
+
console.error("[LunoStore] Failed to persist connectorId to storage:", e);
|
|
268
|
+
}
|
|
269
|
+
const currentStoreChainId = get().currentChainId;
|
|
270
|
+
const chainIdToSet = targetChainId || currentStoreChainId || config.chains[0]?.genesisHash;
|
|
271
|
+
if (chainIdToSet) {
|
|
272
|
+
const newChain = config.chains.find((c) => c.genesisHash === chainIdToSet);
|
|
273
|
+
if (newChain) {
|
|
274
|
+
if (chainIdToSet !== currentStoreChainId || !get().currentApi) {
|
|
275
|
+
set({
|
|
276
|
+
currentChainId: chainIdToSet,
|
|
277
|
+
currentChain: newChain,
|
|
278
|
+
currentApi: void 0
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
try {
|
|
282
|
+
config.storage.setItem(PERSIST_KEY.LAST_CHAIN_ID, chainIdToSet);
|
|
283
|
+
console.log(`[LunoStore] Persisted chainId: ${chainIdToSet}`);
|
|
284
|
+
} catch (e) {
|
|
285
|
+
console.error("[LunoStore] Failed to persist chainId to storage:", e);
|
|
286
|
+
}
|
|
287
|
+
} else {
|
|
288
|
+
console.warn(`[LunoStore] After connection, target chain ID "${chainIdToSet}" was not found in config. Current chain state might not have changed. Not persisting chainId.`);
|
|
289
|
+
}
|
|
290
|
+
} else {
|
|
291
|
+
console.warn(`[LunoStore] Could not determine target chain ID after connection. Please check config.`);
|
|
292
|
+
}
|
|
293
|
+
} catch (err) {
|
|
294
|
+
cleanupActiveConnectorListeners();
|
|
295
|
+
set({
|
|
296
|
+
status: "disconnected" /* Disconnected */,
|
|
297
|
+
activeConnector: void 0,
|
|
298
|
+
accounts: []
|
|
299
|
+
});
|
|
300
|
+
throw new Error(`[LunoStore] Error connecting with ${connector.name}: ${err?.message || err}`);
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
disconnect: async () => {
|
|
304
|
+
const { activeConnector, status, config } = get();
|
|
305
|
+
if (!activeConnector || status === "disconnecting" /* Disconnecting */ || status === "disconnected" /* Disconnected */) {
|
|
306
|
+
console.log("[LunoStore] No active connector or already disconnected/disconnecting. Disconnect action aborted.");
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
set({ status: "disconnecting" /* Disconnecting */ });
|
|
310
|
+
try {
|
|
311
|
+
await activeConnector.disconnect();
|
|
312
|
+
if (config) {
|
|
313
|
+
try {
|
|
314
|
+
console.log("[LunoStore] Attempting to remove persisted connection info due to user disconnect action...");
|
|
315
|
+
await config.storage.removeItem(PERSIST_KEY.LAST_CONNECTOR_ID);
|
|
316
|
+
await config.storage.removeItem(PERSIST_KEY.LAST_CHAIN_ID);
|
|
317
|
+
await config.storage.removeItem(PERSIST_KEY.LAST_SELECTED_ACCOUNT_INFO);
|
|
318
|
+
console.log("[LunoStore] Removed persisted connection info from storage.");
|
|
319
|
+
} catch (e) {
|
|
320
|
+
console.error("[LunoStore] Failed to remove connection info from storage during disconnect action:", e);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
cleanupActiveConnectorListeners();
|
|
324
|
+
set({ status: "disconnected" /* Disconnected */, activeConnector: void 0, accounts: [], account: void 0 });
|
|
325
|
+
if (get().status !== "disconnected" /* Disconnected */) {
|
|
326
|
+
console.warn("[LunoStore] disconnect method called, but status is not yet 'disconnected' (event handler might be delayed or did not fire). Check connector events.");
|
|
327
|
+
}
|
|
328
|
+
} catch (err) {
|
|
329
|
+
set({ status: "connected" /* Connected */ });
|
|
330
|
+
throw new Error(`[LunoStore] Error disconnecting from ${activeConnector.name}: ${err?.message || err}`);
|
|
331
|
+
}
|
|
332
|
+
},
|
|
333
|
+
switchChain: async (newChainId) => {
|
|
334
|
+
const { config, currentChainId, currentApi, activeConnector, account, accounts } = get();
|
|
335
|
+
if (!config) {
|
|
336
|
+
throw new Error("[LunoStore] LunoConfig has not been initialized. Cannot switch chain.");
|
|
337
|
+
}
|
|
338
|
+
if (newChainId === currentChainId) {
|
|
339
|
+
console.log(`[LunoStore] Already on chain ${newChainId}. No switch needed.`);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const newChain = config.chains.find((c) => c.genesisHash === newChainId);
|
|
343
|
+
if (!newChain) {
|
|
344
|
+
throw new Error(`[LunoStore] Chain with ID "${newChainId}" not found in LunoConfig.`);
|
|
345
|
+
}
|
|
346
|
+
const prevAccountIndex = accounts.findIndex((acc) => acc.address === account.address);
|
|
347
|
+
const newAccounts = await activeConnector.updateAccountsForChain(newChainId);
|
|
348
|
+
set({ accounts: newAccounts, account: newAccounts[prevAccountIndex] });
|
|
349
|
+
try {
|
|
350
|
+
try {
|
|
351
|
+
if (currentApi && currentApi.status === "connected") {
|
|
352
|
+
await currentApi.disconnect();
|
|
353
|
+
}
|
|
354
|
+
} catch (e) {
|
|
355
|
+
console.warn("[LunoStore] Failed to disconnect from previous chain:", e);
|
|
356
|
+
}
|
|
357
|
+
console.log(`[LunoStore] Attempting to switch chain to ${newChain.name} (ID: ${newChainId})`);
|
|
358
|
+
set({
|
|
359
|
+
currentChainId: newChainId,
|
|
360
|
+
currentChain: newChain,
|
|
361
|
+
currentApi: void 0,
|
|
362
|
+
isApiReady: false,
|
|
363
|
+
apiError: null
|
|
364
|
+
});
|
|
365
|
+
const newApi = await createApi({ config, chainId: newChainId });
|
|
366
|
+
set({
|
|
367
|
+
currentApi: newApi,
|
|
368
|
+
isApiReady: true
|
|
369
|
+
});
|
|
370
|
+
await config.storage.setItem(PERSIST_KEY.LAST_CHAIN_ID, newChainId);
|
|
371
|
+
} catch (e) {
|
|
372
|
+
set({
|
|
373
|
+
apiError: e,
|
|
374
|
+
isApiReady: false
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
_setApiError: (err) => {
|
|
379
|
+
set({ apiError: err });
|
|
380
|
+
}
|
|
381
|
+
}));
|
|
382
|
+
var LunoContext = React__default.default.createContext({});
|
|
383
|
+
var useIsInitialized = () => {
|
|
384
|
+
const isInitialized = React.useRef(false);
|
|
385
|
+
return {
|
|
386
|
+
isInitialized: isInitialized.current,
|
|
387
|
+
markAsInitialized: () => {
|
|
388
|
+
isInitialized.current = true;
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
};
|
|
392
|
+
var LunoProvider = ({ config: configFromProps, children }) => {
|
|
393
|
+
const {
|
|
394
|
+
_setConfig,
|
|
395
|
+
_setApi,
|
|
396
|
+
_setIsApiReady,
|
|
397
|
+
_setApiError,
|
|
398
|
+
setAccount,
|
|
399
|
+
currentChainId,
|
|
400
|
+
config: configInStore,
|
|
401
|
+
currentApi,
|
|
402
|
+
connect,
|
|
403
|
+
status,
|
|
404
|
+
activeConnector,
|
|
405
|
+
accounts,
|
|
406
|
+
account,
|
|
407
|
+
currentChain,
|
|
408
|
+
isApiReady,
|
|
409
|
+
apiError,
|
|
410
|
+
disconnect,
|
|
411
|
+
switchChain
|
|
412
|
+
} = useLunoStore();
|
|
413
|
+
const { markAsInitialized, isInitialized } = useIsInitialized();
|
|
414
|
+
const clearApiState = React.useCallback(() => {
|
|
415
|
+
_setApi(void 0);
|
|
416
|
+
_setIsApiReady(false);
|
|
417
|
+
}, [_setApi, _setIsApiReady]);
|
|
418
|
+
React.useEffect(() => {
|
|
419
|
+
if (configFromProps) {
|
|
420
|
+
console.log("[LunoProvider] Setting config to store:", configFromProps);
|
|
421
|
+
_setConfig(configFromProps);
|
|
422
|
+
}
|
|
423
|
+
}, [configFromProps]);
|
|
424
|
+
React.useEffect(() => {
|
|
425
|
+
if (isInitialized) return;
|
|
426
|
+
if (!configFromProps || !currentChainId) {
|
|
427
|
+
if (currentApi && currentApi.status === "connected") {
|
|
428
|
+
currentApi.disconnect().catch(console.error);
|
|
429
|
+
}
|
|
430
|
+
clearApiState();
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
const chainConfig = configFromProps.chains.find(
|
|
434
|
+
(c) => c.genesisHash === currentChainId
|
|
435
|
+
);
|
|
436
|
+
const transportConfig = configFromProps.transports[currentChainId];
|
|
437
|
+
if (!chainConfig || !transportConfig) {
|
|
438
|
+
if (currentApi && currentApi.status === "connected") {
|
|
439
|
+
currentApi.disconnect().catch(console.error);
|
|
440
|
+
}
|
|
441
|
+
clearApiState();
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
if (currentApi && currentApi.status === "connected") {
|
|
445
|
+
console.log("[LunoProvider]: Disconnecting API from previous render cycle:", currentApi.runtimeVersion.specName);
|
|
446
|
+
currentApi.disconnect().catch((e) => console.error("[LunoProvider] Error disconnecting previous API:", e));
|
|
447
|
+
}
|
|
448
|
+
clearApiState();
|
|
449
|
+
createApi({ config: configFromProps, chainId: currentChainId }).then((api) => {
|
|
450
|
+
_setApi(api);
|
|
451
|
+
_setIsApiReady(true);
|
|
452
|
+
}).catch((e) => {
|
|
453
|
+
clearApiState();
|
|
454
|
+
_setApiError(e);
|
|
455
|
+
}).finally(() => markAsInitialized());
|
|
456
|
+
}, [configFromProps, currentChainId]);
|
|
457
|
+
React.useEffect(() => {
|
|
458
|
+
const performAutoConnect = async () => {
|
|
459
|
+
if (!configFromProps.autoConnect) {
|
|
460
|
+
console.log("[LunoProvider]: AutoConnect disabled or config not set.");
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
if (!configFromProps.storage) {
|
|
464
|
+
console.warn("[LunoProvider]: AutoConnect Storage not available, cannot auto-connect.");
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
try {
|
|
468
|
+
const lastConnectorId = await configFromProps.storage.getItem(PERSIST_KEY.LAST_CONNECTOR_ID);
|
|
469
|
+
const lastChainId = await configFromProps.storage.getItem(PERSIST_KEY.LAST_CHAIN_ID);
|
|
470
|
+
if (lastConnectorId && lastChainId) {
|
|
471
|
+
console.log(`[LunoProvider]: AutoConnect Found persisted session: Connector ID "${lastConnectorId}", Chain ID "${lastChainId}"`);
|
|
472
|
+
await connect(lastConnectorId, lastChainId);
|
|
473
|
+
} else {
|
|
474
|
+
console.log("[LunoProvider]: AutoConnect No persisted session found or missing data.");
|
|
475
|
+
}
|
|
476
|
+
} catch (error) {
|
|
477
|
+
console.error("[LunoProvider]: AutoConnect Error during auto-connect process:", error);
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
if (configFromProps) {
|
|
481
|
+
performAutoConnect();
|
|
482
|
+
}
|
|
483
|
+
}, [configFromProps]);
|
|
484
|
+
React.useEffect(() => {
|
|
485
|
+
if (isApiReady && currentApi && currentChain && currentChain.ss58Format !== void 0 && currentChain.ss58Format !== null) {
|
|
486
|
+
try {
|
|
487
|
+
const apiSs58 = currentApi.consts.system.ss58Prefix;
|
|
488
|
+
if (apiSs58 !== null && apiSs58 !== void 0 && apiSs58 !== currentChain.ss58Format) {
|
|
489
|
+
console.error(
|
|
490
|
+
`[LunoProvider]: SS58 Format Mismatch for chain "${currentChain.name}" (genesisHash: ${currentChain.genesisHash}):
|
|
491
|
+
- Configured SS58Format: ${currentChain.ss58Format}
|
|
492
|
+
- Node Runtime SS58Format: ${apiSs58}
|
|
493
|
+
Please verify your Luno configuration for this chain to ensure correct address display and interaction.`
|
|
494
|
+
);
|
|
495
|
+
} else if (apiSs58 === null || apiSs58 === void 0) {
|
|
496
|
+
console.warn(
|
|
497
|
+
`[LunoProvider]: Could not determine SS58 format from the API for chain "${currentChain.name}". Cannot validate configured SS58Format (${currentChain.ss58Format}). The application will use the configured value.`
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
} catch (e) {
|
|
501
|
+
console.error(
|
|
502
|
+
`[LunoProvider]: Error retrieving SS58 format from API for chain "${currentChain.name}" while attempting validation:`,
|
|
503
|
+
e
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}, [isApiReady, currentApi, currentChain]);
|
|
508
|
+
const contextValue = React.useMemo(() => ({
|
|
509
|
+
config: configInStore,
|
|
510
|
+
status,
|
|
511
|
+
activeConnector,
|
|
512
|
+
accounts,
|
|
513
|
+
account,
|
|
514
|
+
setAccount,
|
|
515
|
+
currentChainId,
|
|
516
|
+
currentChain,
|
|
517
|
+
currentApi,
|
|
518
|
+
isApiReady,
|
|
519
|
+
connect,
|
|
520
|
+
disconnect,
|
|
521
|
+
switchChain,
|
|
522
|
+
apiError
|
|
523
|
+
}), [
|
|
524
|
+
configInStore,
|
|
525
|
+
status,
|
|
526
|
+
activeConnector,
|
|
527
|
+
accounts,
|
|
528
|
+
account,
|
|
529
|
+
currentChainId,
|
|
530
|
+
currentChain,
|
|
531
|
+
currentApi,
|
|
532
|
+
isApiReady,
|
|
533
|
+
apiError,
|
|
534
|
+
connect,
|
|
535
|
+
disconnect,
|
|
536
|
+
switchChain,
|
|
537
|
+
setAccount
|
|
538
|
+
]);
|
|
539
|
+
return /* @__PURE__ */ jsxRuntime.jsx(LunoContext.Provider, { value: contextValue, children });
|
|
540
|
+
};
|
|
541
|
+
var useLuno = () => {
|
|
542
|
+
const context = React.useContext(LunoContext);
|
|
543
|
+
if (context === void 0) {
|
|
544
|
+
throw new Error("useLuno must be used within a LunoProvider");
|
|
545
|
+
}
|
|
546
|
+
return context;
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
// src/hooks/useApi.ts
|
|
550
|
+
var useApi = () => {
|
|
551
|
+
const { currentApi, isApiReady, apiError } = useLuno();
|
|
552
|
+
return {
|
|
553
|
+
api: currentApi,
|
|
554
|
+
isApiReady,
|
|
555
|
+
apiError
|
|
556
|
+
};
|
|
557
|
+
};
|
|
558
|
+
var useAccount = () => {
|
|
559
|
+
const { account, currentChain } = useLuno();
|
|
560
|
+
const formattedAccount = React.useMemo(() => {
|
|
561
|
+
if (!account) return;
|
|
562
|
+
if (!currentChain || currentChain?.ss58Format === void 0 || !account?.publicKey) return account;
|
|
563
|
+
try {
|
|
564
|
+
const newAddress = core.convertAddress(account.address, currentChain.ss58Format);
|
|
565
|
+
return {
|
|
566
|
+
...account,
|
|
567
|
+
address: newAddress
|
|
568
|
+
};
|
|
569
|
+
} catch (error) {
|
|
570
|
+
console.error(`[useAccount]: Failed to re-format address for account with publicKey ${account.publicKey}:`, error);
|
|
571
|
+
return { ...account };
|
|
572
|
+
}
|
|
573
|
+
}, [account, currentChain, currentChain?.ss58Format]);
|
|
574
|
+
return {
|
|
575
|
+
account: formattedAccount,
|
|
576
|
+
address: formattedAccount?.address
|
|
577
|
+
};
|
|
578
|
+
};
|
|
579
|
+
var useAccounts = () => {
|
|
580
|
+
const { accounts, setAccount, currentChain } = useLuno();
|
|
581
|
+
const formattedAccounts = React.useMemo(() => {
|
|
582
|
+
if (!currentChain || currentChain?.ss58Format === void 0) return accounts ?? [];
|
|
583
|
+
return (accounts || []).map((acc) => {
|
|
584
|
+
if (!acc.publicKey) {
|
|
585
|
+
console.warn(`[useAccounts]: Account ${acc.name || acc.address} is missing publicKey. Cannot re-format address.`);
|
|
586
|
+
return acc;
|
|
587
|
+
}
|
|
588
|
+
try {
|
|
589
|
+
const newAddress = core.convertAddress(acc.address, currentChain.ss58Format);
|
|
590
|
+
return {
|
|
591
|
+
...acc,
|
|
592
|
+
address: newAddress
|
|
593
|
+
};
|
|
594
|
+
} catch (error) {
|
|
595
|
+
console.error(`[useAccounts]: Failed to re-format address for account with publicKey ${acc.publicKey}:`, error);
|
|
596
|
+
return { ...acc };
|
|
597
|
+
}
|
|
598
|
+
});
|
|
599
|
+
}, [accounts, currentChain, currentChain?.ss58Format]);
|
|
600
|
+
return { accounts: formattedAccounts, selectAccount: setAccount };
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
// src/hooks/useActiveConnector.ts
|
|
604
|
+
var useActiveConnector = () => {
|
|
605
|
+
const { activeConnector } = useLuno();
|
|
606
|
+
return activeConnector;
|
|
607
|
+
};
|
|
608
|
+
var defaultTransform = (data) => data;
|
|
609
|
+
var useSubscription = ({ queryKey: userQueryKey, factory, params, options = {} }) => {
|
|
610
|
+
const [error, setError] = React.useState(void 0);
|
|
611
|
+
const { currentApi, isApiReady } = useLuno();
|
|
612
|
+
const queryClient = reactQuery.useQueryClient();
|
|
613
|
+
const {
|
|
614
|
+
enabled = true,
|
|
615
|
+
transform = defaultTransform,
|
|
616
|
+
defaultValue
|
|
617
|
+
} = options;
|
|
618
|
+
const isSubscribed = React.useRef(false);
|
|
619
|
+
const resolvedParams = React.useMemo(() => {
|
|
620
|
+
if (!params || !currentApi || !isApiReady) return void 0;
|
|
621
|
+
return typeof params === "function" ? [params(currentApi)] : params;
|
|
622
|
+
}, [params, currentApi, isApiReady]);
|
|
623
|
+
const queryKey = React.useMemo(() => [
|
|
624
|
+
userQueryKey,
|
|
625
|
+
resolvedParams,
|
|
626
|
+
currentApi?.genesisHash
|
|
627
|
+
], [userQueryKey, resolvedParams, currentApi?.genesisHash]);
|
|
628
|
+
React.useEffect(() => {
|
|
629
|
+
if (!enabled || !factory || !currentApi || !resolvedParams || !isApiReady || isSubscribed.current) {
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
let unsubscribe = null;
|
|
633
|
+
isSubscribed.current = true;
|
|
634
|
+
try {
|
|
635
|
+
const factoryFn = factory(currentApi);
|
|
636
|
+
const boundFn = typeof factoryFn === "function" ? factoryFn.bind(currentApi) : factoryFn;
|
|
637
|
+
const callback = (result) => {
|
|
638
|
+
try {
|
|
639
|
+
const transformedData = transform(result);
|
|
640
|
+
queryClient.setQueryData(queryKey, transformedData);
|
|
641
|
+
setError(void 0);
|
|
642
|
+
} catch (err) {
|
|
643
|
+
setError(new Error(`[useSubscription]: ${err}`));
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
boundFn(...resolvedParams, callback).then((unsub) => {
|
|
647
|
+
unsubscribe = unsub;
|
|
648
|
+
}).catch((err) => {
|
|
649
|
+
setError(new Error(`[useSubscription]: ${err}`));
|
|
650
|
+
});
|
|
651
|
+
} catch (err) {
|
|
652
|
+
setError(new Error(`[useSubscription]: ${err}`));
|
|
653
|
+
}
|
|
654
|
+
return () => {
|
|
655
|
+
if (unsubscribe) {
|
|
656
|
+
unsubscribe();
|
|
657
|
+
isSubscribed.current = false;
|
|
658
|
+
setError(void 0);
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
}, [JSON.stringify(queryKey), enabled, queryClient]);
|
|
662
|
+
const { data, isLoading } = reactQuery.useQuery({
|
|
663
|
+
queryKey,
|
|
664
|
+
queryFn: () => {
|
|
665
|
+
return Promise.resolve(defaultValue);
|
|
666
|
+
},
|
|
667
|
+
enabled: false,
|
|
668
|
+
initialData: defaultValue
|
|
669
|
+
});
|
|
670
|
+
return {
|
|
671
|
+
data,
|
|
672
|
+
error,
|
|
673
|
+
isLoading: !!(enabled && isApiReady && !data)
|
|
674
|
+
};
|
|
675
|
+
};
|
|
676
|
+
var DEFAULT_TOKEN_DECIMALS = 10;
|
|
677
|
+
var transformBalance = (results, chainProperties) => {
|
|
678
|
+
const accountInfo = results[0];
|
|
679
|
+
const locks = results[1];
|
|
680
|
+
const free = accountInfo.data.free;
|
|
681
|
+
const reserved = accountInfo.data.reserved;
|
|
682
|
+
const frozen = accountInfo.data.frozen;
|
|
683
|
+
const total = BigInt(free) + BigInt(reserved);
|
|
684
|
+
const transferable = free > frozen ? BigInt(free) - BigInt(frozen) : 0n;
|
|
685
|
+
return {
|
|
686
|
+
free,
|
|
687
|
+
total,
|
|
688
|
+
reserved,
|
|
689
|
+
transferable,
|
|
690
|
+
formattedTransferable: core.formatBalance(transferable, chainProperties.tokenDecimals),
|
|
691
|
+
formattedTotal: core.formatBalance(total, chainProperties.tokenDecimals),
|
|
692
|
+
locks: locks.map((lock) => ({
|
|
693
|
+
id: lock.id,
|
|
694
|
+
amount: lock.amount,
|
|
695
|
+
reason: lock.reasons,
|
|
696
|
+
lockHuman: core.formatBalance(lock.amount, chainProperties.tokenDecimals)
|
|
697
|
+
}))
|
|
698
|
+
};
|
|
699
|
+
};
|
|
700
|
+
var useBalance = ({ address }) => {
|
|
701
|
+
const { currentApi, isApiReady, currentChain } = useLuno();
|
|
702
|
+
return useSubscription({
|
|
703
|
+
queryKey: "/native-balance",
|
|
704
|
+
factory: (api) => api.queryMulti,
|
|
705
|
+
params: (api) => [
|
|
706
|
+
{ fn: api.query.system.account, args: [address] },
|
|
707
|
+
{ fn: api.query.balances.locks, args: [address] }
|
|
708
|
+
],
|
|
709
|
+
options: {
|
|
710
|
+
enabled: !!currentApi && isApiReady && !!address,
|
|
711
|
+
transform: (results) => {
|
|
712
|
+
const chainProperties = {
|
|
713
|
+
tokenDecimals: currentChain?.nativeCurrency?.decimals ?? DEFAULT_TOKEN_DECIMALS,
|
|
714
|
+
tokenSymbol: currentChain?.nativeCurrency?.symbol,
|
|
715
|
+
ss58Format: currentChain?.ss58Format
|
|
716
|
+
};
|
|
717
|
+
return transformBalance(results, chainProperties);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
});
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
// src/hooks/useBlockNumber.ts
|
|
724
|
+
var useBlockNumber = () => {
|
|
725
|
+
const { currentApi, isApiReady } = useLuno();
|
|
726
|
+
const transform = (blockNumberBn) => {
|
|
727
|
+
return blockNumberBn;
|
|
728
|
+
};
|
|
729
|
+
return useSubscription({
|
|
730
|
+
queryKey: "/block-number",
|
|
731
|
+
factory: (api) => api.query.system.number,
|
|
732
|
+
params: [],
|
|
733
|
+
options: {
|
|
734
|
+
enabled: !!currentApi && isApiReady,
|
|
735
|
+
transform
|
|
736
|
+
}
|
|
737
|
+
});
|
|
738
|
+
};
|
|
739
|
+
|
|
740
|
+
// src/hooks/useChain.ts
|
|
741
|
+
var useChain = () => {
|
|
742
|
+
const { currentChain, currentChainId } = useLuno();
|
|
743
|
+
return {
|
|
744
|
+
chain: currentChain,
|
|
745
|
+
chainId: currentChainId
|
|
746
|
+
};
|
|
747
|
+
};
|
|
748
|
+
|
|
749
|
+
// src/hooks/useChains.ts
|
|
750
|
+
var useChains = () => {
|
|
751
|
+
const { config } = useLuno();
|
|
752
|
+
return config?.chains ? [...config.chains] : [];
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
// src/hooks/useConnectors.ts
|
|
756
|
+
var useConnectors = () => {
|
|
757
|
+
const { config } = useLuno();
|
|
758
|
+
return config?.connectors ? [...config.connectors] : [];
|
|
759
|
+
};
|
|
760
|
+
|
|
761
|
+
// src/hooks/useConfig.ts
|
|
762
|
+
var useConfig = () => {
|
|
763
|
+
const { config } = useLuno();
|
|
764
|
+
return config;
|
|
765
|
+
};
|
|
766
|
+
function useLunoMutation(mutationFn, hookLevelOptions) {
|
|
767
|
+
const tanstackMutationHookOptions = {};
|
|
768
|
+
if (hookLevelOptions?.onSuccess) {
|
|
769
|
+
tanstackMutationHookOptions.onSuccess = hookLevelOptions.onSuccess;
|
|
770
|
+
}
|
|
771
|
+
if (hookLevelOptions?.onError) {
|
|
772
|
+
tanstackMutationHookOptions.onError = hookLevelOptions.onError;
|
|
773
|
+
}
|
|
774
|
+
if (hookLevelOptions?.onSettled) {
|
|
775
|
+
tanstackMutationHookOptions.onSettled = hookLevelOptions.onSettled;
|
|
776
|
+
}
|
|
777
|
+
const mutation = reactQuery.useMutation({
|
|
778
|
+
mutationFn,
|
|
779
|
+
retry: false,
|
|
780
|
+
throwOnError: false,
|
|
781
|
+
...tanstackMutationHookOptions
|
|
782
|
+
});
|
|
783
|
+
return {
|
|
784
|
+
mutate: (variables, callTimeOptions) => {
|
|
785
|
+
mutation.mutate(variables, callTimeOptions);
|
|
786
|
+
},
|
|
787
|
+
mutateAsync: (variables, callTimeOptions) => {
|
|
788
|
+
return mutation.mutateAsync(variables, callTimeOptions);
|
|
789
|
+
},
|
|
790
|
+
data: mutation.data,
|
|
791
|
+
error: mutation.error,
|
|
792
|
+
isError: mutation.isError,
|
|
793
|
+
isIdle: mutation.isIdle,
|
|
794
|
+
isPending: mutation.isPending,
|
|
795
|
+
isSuccess: mutation.isSuccess,
|
|
796
|
+
reset: mutation.reset,
|
|
797
|
+
status: mutation.status,
|
|
798
|
+
variables: mutation.variables
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
// src/hooks/useConnect.ts
|
|
803
|
+
var useConnect = (hookLevelConfig) => {
|
|
804
|
+
const { connect, config, activeConnector, status } = useLuno();
|
|
805
|
+
const connectFn = async (variables) => {
|
|
806
|
+
await connect(variables.connectorId, variables.targetChainId);
|
|
807
|
+
};
|
|
808
|
+
const mutationResult = useLunoMutation(connectFn, hookLevelConfig);
|
|
809
|
+
return {
|
|
810
|
+
connect: mutationResult.mutate,
|
|
811
|
+
connectAsync: mutationResult.mutateAsync,
|
|
812
|
+
connectors: config?.connectors ? [...config.connectors] : [],
|
|
813
|
+
activeConnector,
|
|
814
|
+
status,
|
|
815
|
+
data: mutationResult.data,
|
|
816
|
+
error: mutationResult.error,
|
|
817
|
+
isError: mutationResult.isError,
|
|
818
|
+
isIdle: mutationResult.isIdle,
|
|
819
|
+
isPending: mutationResult.isPending,
|
|
820
|
+
isSuccess: mutationResult.isSuccess,
|
|
821
|
+
reset: mutationResult.reset,
|
|
822
|
+
variables: mutationResult.variables
|
|
823
|
+
};
|
|
824
|
+
};
|
|
825
|
+
|
|
826
|
+
// src/hooks/useDisconnect.ts
|
|
827
|
+
var useDisconnect = (hookLevelConfig) => {
|
|
828
|
+
const { disconnect, status } = useLuno();
|
|
829
|
+
const disconnectFn = async () => {
|
|
830
|
+
await disconnect();
|
|
831
|
+
};
|
|
832
|
+
const mutationResult = useLunoMutation(disconnectFn, hookLevelConfig);
|
|
833
|
+
return {
|
|
834
|
+
disconnect: (options) => mutationResult.mutate(void 0, options),
|
|
835
|
+
disconnectAsync: (options) => mutationResult.mutateAsync(void 0, options),
|
|
836
|
+
status,
|
|
837
|
+
data: mutationResult.data,
|
|
838
|
+
error: mutationResult.error,
|
|
839
|
+
isError: mutationResult.isError,
|
|
840
|
+
isIdle: mutationResult.isIdle,
|
|
841
|
+
isPending: mutationResult.isPending,
|
|
842
|
+
isSuccess: mutationResult.isSuccess,
|
|
843
|
+
reset: mutationResult.reset,
|
|
844
|
+
variables: mutationResult.variables
|
|
845
|
+
};
|
|
846
|
+
};
|
|
847
|
+
var useGenesisHash = () => {
|
|
848
|
+
const { currentApi, currentChainId, isApiReady } = useLuno();
|
|
849
|
+
const { data, isLoading, error } = reactQuery.useQuery({
|
|
850
|
+
queryKey: ["/genesis-hash", currentChainId],
|
|
851
|
+
queryFn: async () => {
|
|
852
|
+
return await currentApi.rpc.chain_getBlockHash(0);
|
|
853
|
+
},
|
|
854
|
+
enabled: !!currentApi && isApiReady && !!currentChainId,
|
|
855
|
+
staleTime: Infinity,
|
|
856
|
+
gcTime: Infinity,
|
|
857
|
+
retry: false
|
|
858
|
+
});
|
|
859
|
+
return { data, isLoading, error };
|
|
860
|
+
};
|
|
861
|
+
var useRuntimeVersion = () => {
|
|
862
|
+
const { currentApi, isApiReady, currentChainId } = useLuno();
|
|
863
|
+
return reactQuery.useQuery({
|
|
864
|
+
queryKey: ["luno", "runtimeVersion", currentChainId],
|
|
865
|
+
queryFn: async () => {
|
|
866
|
+
return await currentApi.getRuntimeVersion();
|
|
867
|
+
},
|
|
868
|
+
enabled: !!currentApi && isApiReady && !!currentChainId
|
|
869
|
+
});
|
|
870
|
+
};
|
|
871
|
+
function useSendTransaction(hookLevelConfig) {
|
|
872
|
+
const { account, activeConnector, currentApi, isApiReady } = useLuno();
|
|
873
|
+
const [txStatus, setTxStatus] = React.useState("idle");
|
|
874
|
+
const [detailedTxStatus, setDetailedTxStatus] = React.useState("idle");
|
|
875
|
+
const [txError, setTxError] = React.useState(null);
|
|
876
|
+
const sendTransactionFn = React.useCallback(async (variables) => {
|
|
877
|
+
if (!currentApi || !isApiReady) {
|
|
878
|
+
throw new Error("[useSendTransaction]: Polkadot API is not ready.");
|
|
879
|
+
}
|
|
880
|
+
if (!activeConnector) {
|
|
881
|
+
throw new Error("[useSendTransaction]: No active connector found.");
|
|
882
|
+
}
|
|
883
|
+
if (!account || !account.address || !account.meta?.source) {
|
|
884
|
+
throw new Error(
|
|
885
|
+
"[useSendTransaction]: No active account, address, or account metadata (source) found."
|
|
886
|
+
);
|
|
887
|
+
}
|
|
888
|
+
if (!variables.extrinsic) {
|
|
889
|
+
throw new Error("[useSendTransaction]: No extrinsic provided to send.");
|
|
890
|
+
}
|
|
891
|
+
const signer = await activeConnector.getSigner();
|
|
892
|
+
if (!signer) {
|
|
893
|
+
throw new Error("[useSendTransaction]: Could not retrieve signer from the injector.");
|
|
894
|
+
}
|
|
895
|
+
setTxStatus("signing");
|
|
896
|
+
setDetailedTxStatus("idle");
|
|
897
|
+
return new Promise((resolve, reject) => {
|
|
898
|
+
let unsubscribe;
|
|
899
|
+
variables.extrinsic.signAndSend(
|
|
900
|
+
account.address,
|
|
901
|
+
{ signer },
|
|
902
|
+
({ status, dispatchError, events, dispatchInfo, txHash, txIndex }) => {
|
|
903
|
+
const resolveAndUnsubscribe = (receipt) => {
|
|
904
|
+
if (unsubscribe) unsubscribe();
|
|
905
|
+
resolve(receipt);
|
|
906
|
+
};
|
|
907
|
+
const rejectAndUnsubscribe = (error) => {
|
|
908
|
+
if (unsubscribe) unsubscribe();
|
|
909
|
+
setTxError(error);
|
|
910
|
+
reject(error);
|
|
911
|
+
};
|
|
912
|
+
switch (status.type) {
|
|
913
|
+
case "Broadcasting":
|
|
914
|
+
setDetailedTxStatus("broadcasting");
|
|
915
|
+
break;
|
|
916
|
+
case "BestChainBlockIncluded":
|
|
917
|
+
setDetailedTxStatus("inBlock");
|
|
918
|
+
break;
|
|
919
|
+
case "Finalized":
|
|
920
|
+
setTxStatus("success");
|
|
921
|
+
setDetailedTxStatus("finalized");
|
|
922
|
+
if (dispatchError) {
|
|
923
|
+
resolveAndUnsubscribe({
|
|
924
|
+
transactionHash: txHash,
|
|
925
|
+
blockHash: status.value?.blockHash,
|
|
926
|
+
blockNumber: status.value?.blockNumber,
|
|
927
|
+
events,
|
|
928
|
+
status: "failed",
|
|
929
|
+
dispatchError,
|
|
930
|
+
errorMessage: getReadableDispatchError(currentApi, dispatchError),
|
|
931
|
+
dispatchInfo
|
|
932
|
+
});
|
|
933
|
+
} else {
|
|
934
|
+
resolveAndUnsubscribe({
|
|
935
|
+
transactionHash: txHash,
|
|
936
|
+
blockHash: status.value?.blockHash,
|
|
937
|
+
blockNumber: status.value?.blockNumber,
|
|
938
|
+
events,
|
|
939
|
+
status: "success",
|
|
940
|
+
dispatchError: void 0,
|
|
941
|
+
errorMessage: void 0,
|
|
942
|
+
dispatchInfo
|
|
943
|
+
});
|
|
944
|
+
}
|
|
945
|
+
break;
|
|
946
|
+
case "Invalid":
|
|
947
|
+
setTxStatus("failed");
|
|
948
|
+
setDetailedTxStatus("invalid");
|
|
949
|
+
rejectAndUnsubscribe(new Error(`Transaction invalid: ${txHash}`));
|
|
950
|
+
break;
|
|
951
|
+
case "Drop":
|
|
952
|
+
setTxStatus("failed");
|
|
953
|
+
setDetailedTxStatus("dropped");
|
|
954
|
+
rejectAndUnsubscribe(new Error(`Transaction dropped: ${txHash}`));
|
|
955
|
+
break;
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
).then((unsub) => {
|
|
959
|
+
unsubscribe = unsub;
|
|
960
|
+
}).catch((error) => {
|
|
961
|
+
setTxStatus("failed");
|
|
962
|
+
console.error("[useSendTransaction]: Error in signAndSend promise:", error?.message || error);
|
|
963
|
+
setTxError(error);
|
|
964
|
+
reject(error);
|
|
965
|
+
});
|
|
966
|
+
});
|
|
967
|
+
}, [currentApi, isApiReady, activeConnector, account, setTxStatus, setDetailedTxStatus]);
|
|
968
|
+
const mutationResult = useLunoMutation(sendTransactionFn, hookLevelConfig);
|
|
969
|
+
return {
|
|
970
|
+
sendTransaction: mutationResult.mutate,
|
|
971
|
+
sendTransactionAsync: mutationResult.mutateAsync,
|
|
972
|
+
data: mutationResult.data,
|
|
973
|
+
error: txError || mutationResult.error,
|
|
974
|
+
isError: Boolean(txError) || mutationResult.isError,
|
|
975
|
+
isIdle: mutationResult.isIdle,
|
|
976
|
+
isPending: mutationResult.isPending,
|
|
977
|
+
isSuccess: mutationResult.isSuccess,
|
|
978
|
+
reset: mutationResult.reset,
|
|
979
|
+
status: mutationResult.status,
|
|
980
|
+
variables: mutationResult.variables,
|
|
981
|
+
txStatus,
|
|
982
|
+
detailedStatus: detailedTxStatus
|
|
983
|
+
};
|
|
984
|
+
}
|
|
985
|
+
function useSendTransactionHash(hookLevelConfig) {
|
|
986
|
+
const { account, activeConnector, currentApi, isApiReady } = useLuno();
|
|
987
|
+
const [txError, setTxError] = React.useState(null);
|
|
988
|
+
const sendTransactionFn = React.useCallback(async (variables) => {
|
|
989
|
+
if (!currentApi || !isApiReady) {
|
|
990
|
+
throw new Error("[useSendTransactionHash]: Polkadot API is not ready.");
|
|
991
|
+
}
|
|
992
|
+
if (!activeConnector) {
|
|
993
|
+
throw new Error("[useSendTransactionHash]: No active connector found.");
|
|
994
|
+
}
|
|
995
|
+
if (!account || !account.address || !account.meta?.source) {
|
|
996
|
+
throw new Error(
|
|
997
|
+
"[useSendTransactionHash]: No active account, address, or account metadata (source) found."
|
|
998
|
+
);
|
|
999
|
+
}
|
|
1000
|
+
if (!variables.extrinsic) {
|
|
1001
|
+
throw new Error("[useSendTransactionHash]: No extrinsic provided to send.");
|
|
1002
|
+
}
|
|
1003
|
+
const signer = await activeConnector.getSigner();
|
|
1004
|
+
if (!signer) {
|
|
1005
|
+
throw new Error("[useSendTransactionHash]: Could not retrieve signer from the injector.");
|
|
1006
|
+
}
|
|
1007
|
+
try {
|
|
1008
|
+
const txHash = await variables.extrinsic.signAndSend(account.address, { signer }).catch((e) => {
|
|
1009
|
+
throw e;
|
|
1010
|
+
});
|
|
1011
|
+
return txHash;
|
|
1012
|
+
} catch (error) {
|
|
1013
|
+
setTxError(error);
|
|
1014
|
+
throw error;
|
|
1015
|
+
}
|
|
1016
|
+
}, [currentApi, isApiReady, activeConnector, account]);
|
|
1017
|
+
const mutationResult = useLunoMutation(sendTransactionFn, hookLevelConfig);
|
|
1018
|
+
return {
|
|
1019
|
+
sendTransaction: mutationResult.mutate,
|
|
1020
|
+
sendTransactionAsync: mutationResult.mutateAsync,
|
|
1021
|
+
data: mutationResult.data,
|
|
1022
|
+
error: txError || mutationResult.error,
|
|
1023
|
+
isError: Boolean(txError) || mutationResult.isError,
|
|
1024
|
+
isIdle: mutationResult.isIdle,
|
|
1025
|
+
isPending: mutationResult.isPending,
|
|
1026
|
+
isSuccess: mutationResult.isSuccess,
|
|
1027
|
+
reset: mutationResult.reset,
|
|
1028
|
+
status: mutationResult.status,
|
|
1029
|
+
variables: mutationResult.variables
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
// src/hooks/useSignMessage.ts
|
|
1034
|
+
function useSignMessage(hookLevelConfig) {
|
|
1035
|
+
const { activeConnector, account, accounts } = useLuno();
|
|
1036
|
+
const mutationFn = async (variables) => {
|
|
1037
|
+
if (!activeConnector) {
|
|
1038
|
+
throw new Error("[useSignMessage]: No active connector found to sign the message.");
|
|
1039
|
+
}
|
|
1040
|
+
if (!account || !account.address || !account.meta?.source) {
|
|
1041
|
+
throw new Error("[useSignMessage]: No address provided for signing.");
|
|
1042
|
+
}
|
|
1043
|
+
if (!accounts.some((acc) => acc.address === account.address)) {
|
|
1044
|
+
throw new Error(`[useSignMessage]: Address ${account.address} is not managed by ${activeConnector.id}.`);
|
|
1045
|
+
}
|
|
1046
|
+
if (!variables.message) {
|
|
1047
|
+
throw new Error("[useSignMessage]: No message provided for signing.");
|
|
1048
|
+
}
|
|
1049
|
+
const signatureString = await activeConnector.signMessage(
|
|
1050
|
+
variables.message,
|
|
1051
|
+
account.address
|
|
1052
|
+
);
|
|
1053
|
+
if (!signatureString) {
|
|
1054
|
+
throw new Error(
|
|
1055
|
+
"[useSignMessage]: Signature was not obtained. The user may have cancelled the request or the connector failed."
|
|
1056
|
+
);
|
|
1057
|
+
}
|
|
1058
|
+
return {
|
|
1059
|
+
signature: signatureString,
|
|
1060
|
+
rawMessage: variables.message,
|
|
1061
|
+
addressUsed: account.address
|
|
1062
|
+
};
|
|
1063
|
+
};
|
|
1064
|
+
const mutationResult = useLunoMutation(mutationFn, hookLevelConfig);
|
|
1065
|
+
return {
|
|
1066
|
+
signMessage: mutationResult.mutate,
|
|
1067
|
+
signMessageAsync: mutationResult.mutateAsync,
|
|
1068
|
+
data: mutationResult.data,
|
|
1069
|
+
error: mutationResult.error,
|
|
1070
|
+
isError: mutationResult.isError,
|
|
1071
|
+
isIdle: mutationResult.isIdle,
|
|
1072
|
+
isPending: mutationResult.isPending,
|
|
1073
|
+
isSuccess: mutationResult.isSuccess,
|
|
1074
|
+
reset: mutationResult.reset,
|
|
1075
|
+
status: mutationResult.status,
|
|
1076
|
+
variables: mutationResult.variables
|
|
1077
|
+
};
|
|
1078
|
+
}
|
|
1079
|
+
var DEFAULT_SS58_FORMAT = 42;
|
|
1080
|
+
var useSs58Format = () => {
|
|
1081
|
+
const { currentApi, isApiReady, currentChain } = useLuno();
|
|
1082
|
+
const configuredSs58Fallback = currentChain?.ss58Format !== void 0 ? currentChain.ss58Format : DEFAULT_SS58_FORMAT;
|
|
1083
|
+
return React.useMemo(() => {
|
|
1084
|
+
if (currentApi && isApiReady) {
|
|
1085
|
+
let ss58 = void 0;
|
|
1086
|
+
try {
|
|
1087
|
+
const format = currentApi.consts.system.ss58Prefix;
|
|
1088
|
+
ss58 = utils.isNumber(format) ? format : DEFAULT_SS58_FORMAT;
|
|
1089
|
+
} catch (e) {
|
|
1090
|
+
console.error("[useSs58Format] Error fetching chainSS58:", e);
|
|
1091
|
+
ss58 = configuredSs58Fallback;
|
|
1092
|
+
}
|
|
1093
|
+
return { data: ss58, isLoading: false };
|
|
1094
|
+
} else {
|
|
1095
|
+
return { data: void 0, isLoading: true };
|
|
1096
|
+
}
|
|
1097
|
+
}, [currentApi, isApiReady, configuredSs58Fallback]);
|
|
1098
|
+
};
|
|
1099
|
+
|
|
1100
|
+
// src/hooks/useStatus.ts
|
|
1101
|
+
var useStatus = () => {
|
|
1102
|
+
const { status } = useLuno();
|
|
1103
|
+
return status;
|
|
1104
|
+
};
|
|
1105
|
+
|
|
1106
|
+
// src/hooks/useSwitchChain.ts
|
|
1107
|
+
var useSwitchChain = (hookLevelConfig) => {
|
|
1108
|
+
const { switchChain: storeSwitchChain, config, currentChain, currentChainId } = useLuno();
|
|
1109
|
+
const switchChainFn = async (variables) => {
|
|
1110
|
+
await storeSwitchChain(variables.chainId);
|
|
1111
|
+
};
|
|
1112
|
+
const mutationResult = useLunoMutation(switchChainFn, hookLevelConfig);
|
|
1113
|
+
return {
|
|
1114
|
+
switchChain: mutationResult.mutate,
|
|
1115
|
+
switchChainAsync: mutationResult.mutateAsync,
|
|
1116
|
+
chains: config?.chains ? [...config.chains] : [],
|
|
1117
|
+
currentChain,
|
|
1118
|
+
currentChainId,
|
|
1119
|
+
data: mutationResult.data,
|
|
1120
|
+
error: mutationResult.error,
|
|
1121
|
+
isError: mutationResult.isError,
|
|
1122
|
+
isIdle: mutationResult.isIdle,
|
|
1123
|
+
isPending: mutationResult.isPending,
|
|
1124
|
+
isSuccess: mutationResult.isSuccess,
|
|
1125
|
+
reset: mutationResult.reset,
|
|
1126
|
+
variables: mutationResult.variables
|
|
1127
|
+
};
|
|
1128
|
+
};
|
|
1129
|
+
var useSigner = () => {
|
|
1130
|
+
const { activeConnector } = useLuno();
|
|
1131
|
+
const { account } = useAccount();
|
|
1132
|
+
const [signer, setSigner] = React.useState(void 0);
|
|
1133
|
+
const [isLoading, setIsLoading] = React.useState(false);
|
|
1134
|
+
React.useEffect(() => {
|
|
1135
|
+
if (!activeConnector || !account?.address) {
|
|
1136
|
+
setSigner(void 0);
|
|
1137
|
+
setIsLoading(false);
|
|
1138
|
+
return;
|
|
1139
|
+
}
|
|
1140
|
+
setIsLoading(true);
|
|
1141
|
+
activeConnector.getSigner().then((signer2) => setSigner(signer2)).catch(() => setSigner(void 0)).finally(() => setIsLoading(false));
|
|
1142
|
+
}, [activeConnector, account?.address]);
|
|
1143
|
+
return { signer, isLoading };
|
|
1144
|
+
};
|
|
1145
|
+
|
|
1146
|
+
exports.ConnectionStatus = ConnectionStatus;
|
|
1147
|
+
exports.LunoProvider = LunoProvider;
|
|
1148
|
+
exports.useAccount = useAccount;
|
|
1149
|
+
exports.useAccounts = useAccounts;
|
|
1150
|
+
exports.useActiveConnector = useActiveConnector;
|
|
1151
|
+
exports.useApi = useApi;
|
|
1152
|
+
exports.useBalance = useBalance;
|
|
1153
|
+
exports.useBlockNumber = useBlockNumber;
|
|
1154
|
+
exports.useChain = useChain;
|
|
1155
|
+
exports.useChains = useChains;
|
|
1156
|
+
exports.useConfig = useConfig;
|
|
1157
|
+
exports.useConnect = useConnect;
|
|
1158
|
+
exports.useConnectors = useConnectors;
|
|
1159
|
+
exports.useDisconnect = useDisconnect;
|
|
1160
|
+
exports.useGenesisHash = useGenesisHash;
|
|
1161
|
+
exports.useRuntimeVersion = useRuntimeVersion;
|
|
1162
|
+
exports.useSendTransaction = useSendTransaction;
|
|
1163
|
+
exports.useSendTransactionHash = useSendTransactionHash;
|
|
1164
|
+
exports.useSignMessage = useSignMessage;
|
|
1165
|
+
exports.useSigner = useSigner;
|
|
1166
|
+
exports.useSs58Format = useSs58Format;
|
|
1167
|
+
exports.useStatus = useStatus;
|
|
1168
|
+
exports.useSubscription = useSubscription;
|
|
1169
|
+
exports.useSwitchChain = useSwitchChain;
|
|
1170
|
+
Object.keys(core).forEach(function (k) {
|
|
1171
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
1172
|
+
enumerable: true,
|
|
1173
|
+
get: function () { return core[k]; }
|
|
1174
|
+
});
|
|
1175
|
+
});
|
|
1176
|
+
//# sourceMappingURL=index.cjs.map
|
|
1177
|
+
//# sourceMappingURL=index.cjs.map
|