@vue-solana/vue 0.4.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,19 +1,170 @@
1
1
  export { u as useBalance } from './shared/vue.DQhHHVu6.mjs';
2
2
  export { u as useConnection } from './shared/vue.DlEAL2G8.mjs';
3
3
  export { u as useRpc } from './shared/vue.BqDzSepb.mjs';
4
- export { u as useSignAndSendTransaction } from './shared/vue.BfF-kOvb.mjs';
4
+ export { u as useSignAndSendTransaction } from './shared/vue.RQ9fETnm.mjs';
5
5
  import { s as solanaInjectionKey } from './shared/vue.1M_c5FWA.mjs';
6
6
  export { t as tryUseSolana, u as useSolana } from './shared/vue.1M_c5FWA.mjs';
7
- export { u as useTransaction } from './shared/vue.CuhLIeDx.mjs';
7
+ import { w as withTimeout } from './shared/vue.gLqkzJY4.mjs';
8
+ export { u as useTransaction } from './shared/vue.gLqkzJY4.mjs';
9
+ export { g as getConfirmedTransactionStatus, u as useTransactionConfirmation } from './shared/vue.jOsz34kz.mjs';
8
10
  export { u as useWallet } from './shared/vue.DYf_CRDv.mjs';
9
11
  export { u as useWallets } from './shared/vue.h-uS8MXN.mjs';
10
- import { isSolanaIosWalletInfo, adaptSolanaIosWallet, handleSolanaIosWalletCallback, getSolanaIosWallets } from '@vue-solana/core/ios-wallet';
11
- import { subscribeSolanaWallets, getSolanaChain, adaptSolanaStandardWallet, getRegisteredSolanaWallets } from '@vue-solana/core/wallet-standard';
12
12
  import { createSolanaContext } from '@vue-solana/core/rpc';
13
+ import { getRegisteredSolanaWallets, getSolanaChain, adaptSolanaStandardWallet, subscribeSolanaWallets } from '@vue-solana/core/wallet-standard';
13
14
  import { shallowRef, ref, triggerRef } from 'vue';
15
+ import { handleSolanaIosWalletCallback, getSolanaIosWallets, isSolanaIosWalletInfo, adaptSolanaIosWallet } from '@vue-solana/core/ios-wallet';
14
16
  import '@solana/web3-compat';
15
17
  import '@vue-solana/core/transaction';
16
18
 
19
+ const SELECTED_WALLET_STORAGE_KEY = "vue-solana:selected-wallet";
20
+ function readSelectedWallet() {
21
+ const storage = getLocalStorage();
22
+ if (!storage) {
23
+ return null;
24
+ }
25
+ try {
26
+ const value = storage.getItem(SELECTED_WALLET_STORAGE_KEY);
27
+ if (!value) {
28
+ return null;
29
+ }
30
+ const wallet = JSON.parse(value);
31
+ return typeof wallet.name === "string" ? {
32
+ name: wallet.name,
33
+ platform: wallet.platform,
34
+ source: wallet.source
35
+ } : null;
36
+ } catch {
37
+ return null;
38
+ }
39
+ }
40
+ function writeSelectedWallet(wallet) {
41
+ const storage = getLocalStorage();
42
+ if (!storage) {
43
+ return;
44
+ }
45
+ try {
46
+ if (wallet) {
47
+ storage.setItem(SELECTED_WALLET_STORAGE_KEY, stringifySelectedWallet(wallet));
48
+ } else {
49
+ storage.removeItem(SELECTED_WALLET_STORAGE_KEY);
50
+ }
51
+ } catch {
52
+ }
53
+ }
54
+ function stringifySelectedWallet(wallet) {
55
+ const value = { name: wallet.name };
56
+ if (wallet.platform) {
57
+ value.platform = wallet.platform;
58
+ }
59
+ if (wallet.source) {
60
+ value.source = wallet.source;
61
+ }
62
+ return JSON.stringify(value);
63
+ }
64
+ function getLocalStorage() {
65
+ if (typeof window === "undefined") {
66
+ return null;
67
+ }
68
+ try {
69
+ return window.localStorage;
70
+ } catch {
71
+ return null;
72
+ }
73
+ }
74
+
75
+ function createSolanaWalletRegistry(options) {
76
+ const adaptedWallets = /* @__PURE__ */ new WeakMap();
77
+ function getAdaptedWallet(walletInfo) {
78
+ if (isSolanaIosWalletInfo(walletInfo)) {
79
+ return adaptSolanaIosWallet(walletInfo, {
80
+ chain: getSolanaChain(options.cluster),
81
+ cluster: options.cluster,
82
+ onChange: options.onWalletChange,
83
+ ...options.iosWallet || {}
84
+ });
85
+ }
86
+ if (!isObject(walletInfo.wallet)) {
87
+ return adaptSolanaStandardWallet(walletInfo, {
88
+ chain: getSolanaChain(options.cluster),
89
+ onChange: options.onWalletChange
90
+ });
91
+ }
92
+ const cachedWallet = adaptedWallets.get(walletInfo.wallet);
93
+ if (cachedWallet) {
94
+ return cachedWallet;
95
+ }
96
+ const adaptedWallet = adaptSolanaStandardWallet(walletInfo, {
97
+ chain: getSolanaChain(options.cluster),
98
+ onChange: options.onWalletChange
99
+ });
100
+ const cachedAdapter = {
101
+ platform: walletInfo.platform,
102
+ source: walletInfo.source,
103
+ get publicKey() {
104
+ return adaptedWallet.publicKey;
105
+ },
106
+ get connected() {
107
+ return adaptedWallet.connected;
108
+ },
109
+ get connecting() {
110
+ return adaptedWallet.connecting;
111
+ },
112
+ get disconnecting() {
113
+ return adaptedWallet.disconnecting;
114
+ },
115
+ async connect() {
116
+ await adaptedWallet.connect();
117
+ await Promise.all(
118
+ Array.from(getCachedWallets()).map(
119
+ (otherWallet) => otherWallet !== cachedAdapter && otherWallet.connected ? otherWallet.disconnect() : void 0
120
+ )
121
+ );
122
+ },
123
+ disconnect: () => adaptedWallet.disconnect(),
124
+ signTransaction: adaptedWallet.signTransaction?.bind(adaptedWallet),
125
+ signAllTransactions: adaptedWallet.signAllTransactions?.bind(adaptedWallet),
126
+ signAndSendTransaction: adaptedWallet.signAndSendTransaction?.bind(adaptedWallet)
127
+ };
128
+ adaptedWallets.set(walletInfo.wallet, cachedAdapter);
129
+ return cachedAdapter;
130
+ }
131
+ function getDiscoveredWallets() {
132
+ return [
133
+ ...getRegisteredSolanaWallets(),
134
+ ...options.iosWallet === false ? [] : getSolanaIosWallets({
135
+ chains: [getSolanaChain(options.cluster)],
136
+ cluster: options.cluster,
137
+ ...options.iosWallet || {}
138
+ })
139
+ ];
140
+ }
141
+ function handleIosWalletCallback() {
142
+ try {
143
+ handleSolanaIosWalletCallback({ clearUrl: true });
144
+ } catch (cause) {
145
+ console.error("[Vue Solana] iOS wallet callback failed", cause);
146
+ }
147
+ }
148
+ function* getCachedWallets() {
149
+ for (const walletInfo of options.getWalletInfos()) {
150
+ if (isObject(walletInfo.wallet)) {
151
+ const cachedWallet = adaptedWallets.get(walletInfo.wallet);
152
+ if (cachedWallet) {
153
+ yield cachedWallet;
154
+ }
155
+ }
156
+ }
157
+ }
158
+ return {
159
+ getAdaptedWallet,
160
+ getDiscoveredWallets,
161
+ handleIosWalletCallback
162
+ };
163
+ }
164
+ function isObject(value) {
165
+ return typeof value === "object" && value !== null || typeof value === "function";
166
+ }
167
+
17
168
  const RPC_CHECK_TIMEOUT_MS = 1e4;
18
169
  function createSolanaPlugin(options = {}) {
19
170
  return {
@@ -25,9 +176,15 @@ function createSolanaPlugin(options = {}) {
25
176
  const status = ref("idle");
26
177
  const error = ref(null);
27
178
  const latestBlockhash = ref(null);
28
- const adaptedWallets = /* @__PURE__ */ new WeakMap();
179
+ const walletRegistry = createSolanaWalletRegistry({
180
+ cluster: context.cluster,
181
+ iosWallet: options.iosWallet,
182
+ getWalletInfos: () => wallets.value,
183
+ onWalletChange: () => triggerRef(wallet)
184
+ });
29
185
  let unsubscribeWallets = null;
30
186
  let mobileWalletRegistrationPromise = null;
187
+ let attemptedAutoConnectWallet = null;
31
188
  let rpcCheckId = 0;
32
189
  async function checkConnection() {
33
190
  const checkId = ++rpcCheckId;
@@ -66,17 +223,28 @@ function createSolanaPlugin(options = {}) {
66
223
  }
67
224
  function refreshWallets() {
68
225
  unsubscribeWallets ??= subscribeSolanaWallets(refreshWallets);
69
- handleIosWalletCallback();
70
- wallets.value = getDiscoveredWallets();
226
+ walletRegistry.handleIosWalletCallback();
227
+ wallets.value = walletRegistry.getDiscoveredWallets();
228
+ let restoredWallet = null;
71
229
  if (selectedWallet.value) {
72
230
  selectedWallet.value = wallets.value.find((nextWallet) => isSameWallet(nextWallet, selectedWallet.value)) ?? null;
73
231
  if (!selectedWallet.value) {
74
232
  wallet.value = options.wallet ?? null;
75
233
  }
234
+ } else if (!options.wallet) {
235
+ const persistedWallet = readSelectedWallet();
236
+ restoredWallet = persistedWallet ? wallets.value.find((nextWallet) => isSameWallet(nextWallet, persistedWallet)) ?? null : null;
237
+ if (restoredWallet) {
238
+ selectedWallet.value = restoredWallet;
239
+ wallet.value = walletRegistry.getAdaptedWallet(restoredWallet);
240
+ }
76
241
  }
77
242
  if (options.mobileWallet !== false) {
78
243
  registerMobileWallets();
79
244
  }
245
+ if (restoredWallet) {
246
+ autoConnectWallet(restoredWallet);
247
+ }
80
248
  }
81
249
  function registerMobileWallets() {
82
250
  mobileWalletRegistrationPromise ??= import('@vue-solana/core/mobile-wallet').then(({ registerSolanaMobileWallet }) => {
@@ -84,7 +252,7 @@ function createSolanaPlugin(options = {}) {
84
252
  chains: [getSolanaChain(context.cluster)],
85
253
  ...options.mobileWallet || {}
86
254
  });
87
- wallets.value = getDiscoveredWallets();
255
+ refreshWallets();
88
256
  }).catch((cause) => {
89
257
  console.error("[Vue Solana] Mobile wallet registration failed", cause);
90
258
  }).finally(() => {
@@ -93,88 +261,24 @@ function createSolanaPlugin(options = {}) {
93
261
  }
94
262
  function selectWallet(nextWallet) {
95
263
  selectedWallet.value = nextWallet;
96
- wallet.value = nextWallet ? getAdaptedWallet(nextWallet) : options.wallet ?? null;
264
+ wallet.value = nextWallet ? walletRegistry.getAdaptedWallet(nextWallet) : options.wallet ?? null;
265
+ writeSelectedWallet(nextWallet);
97
266
  }
98
- function getAdaptedWallet(walletInfo) {
99
- if (isSolanaIosWalletInfo(walletInfo)) {
100
- return adaptSolanaIosWallet(walletInfo, {
101
- chain: getSolanaChain(context.cluster),
102
- cluster: context.cluster,
103
- onChange: () => triggerRef(wallet),
104
- ...options.iosWallet || {}
105
- });
267
+ function autoConnectWallet(walletInfo) {
268
+ if (!options.autoConnect) {
269
+ return;
106
270
  }
107
- if (!isObject(walletInfo.wallet)) {
108
- return adaptSolanaStandardWallet(walletInfo, {
109
- chain: getSolanaChain(context.cluster),
110
- onChange: () => triggerRef(wallet)
111
- });
112
- }
113
- const cachedWallet = adaptedWallets.get(walletInfo.wallet);
114
- if (cachedWallet) {
115
- return cachedWallet;
271
+ const activeWallet = wallet.value;
272
+ const storageValue = stringifySelectedWallet(walletInfo);
273
+ if (!activeWallet || activeWallet.connected || activeWallet.connecting || attemptedAutoConnectWallet === storageValue) {
274
+ return;
116
275
  }
117
- const adaptedWallet = adaptSolanaStandardWallet(walletInfo, {
118
- chain: getSolanaChain(context.cluster),
119
- onChange: () => triggerRef(wallet)
276
+ attemptedAutoConnectWallet = storageValue;
277
+ void activeWallet.connect().catch((cause) => {
278
+ console.error("[Vue Solana] Wallet auto-connect failed", cause);
279
+ }).finally(() => {
280
+ triggerRef(wallet);
120
281
  });
121
- const cachedAdapter = {
122
- platform: walletInfo.platform,
123
- source: walletInfo.source,
124
- get publicKey() {
125
- return adaptedWallet.publicKey;
126
- },
127
- get connected() {
128
- return adaptedWallet.connected;
129
- },
130
- get connecting() {
131
- return adaptedWallet.connecting;
132
- },
133
- get disconnecting() {
134
- return adaptedWallet.disconnecting;
135
- },
136
- async connect() {
137
- await adaptedWallet.connect();
138
- await Promise.all(
139
- Array.from(getCachedWallets()).map(
140
- (otherWallet) => otherWallet !== cachedAdapter && otherWallet.connected ? otherWallet.disconnect() : void 0
141
- )
142
- );
143
- },
144
- disconnect: () => adaptedWallet.disconnect(),
145
- signTransaction: adaptedWallet.signTransaction?.bind(adaptedWallet),
146
- signAllTransactions: adaptedWallet.signAllTransactions?.bind(adaptedWallet),
147
- signAndSendTransaction: adaptedWallet.signAndSendTransaction?.bind(adaptedWallet)
148
- };
149
- adaptedWallets.set(walletInfo.wallet, cachedAdapter);
150
- return cachedAdapter;
151
- }
152
- function getDiscoveredWallets() {
153
- return [
154
- ...getRegisteredSolanaWallets(),
155
- ...options.iosWallet === false ? [] : getSolanaIosWallets({
156
- chains: [getSolanaChain(context.cluster)],
157
- cluster: context.cluster,
158
- ...options.iosWallet || {}
159
- })
160
- ];
161
- }
162
- function handleIosWalletCallback() {
163
- try {
164
- handleSolanaIosWalletCallback({ clearUrl: true });
165
- } catch (cause) {
166
- console.error("[Vue Solana] iOS wallet callback failed", cause);
167
- }
168
- }
169
- function* getCachedWallets() {
170
- for (const walletInfo of wallets.value) {
171
- if (isObject(walletInfo.wallet)) {
172
- const cachedWallet = adaptedWallets.get(walletInfo.wallet);
173
- if (cachedWallet) {
174
- yield cachedWallet;
175
- }
176
- }
177
- }
178
282
  }
179
283
  const vueContext = {
180
284
  ...context,
@@ -190,36 +294,26 @@ function createSolanaPlugin(options = {}) {
190
294
  setWallet(nextWallet) {
191
295
  selectedWallet.value = null;
192
296
  wallet.value = nextWallet;
297
+ writeSelectedWallet(null);
193
298
  }
194
299
  };
195
300
  app.provide(solanaInjectionKey, vueContext);
196
301
  if (typeof window !== "undefined") {
197
302
  window.setTimeout(() => {
303
+ try {
304
+ refreshWallets();
305
+ } catch (cause) {
306
+ console.error("[Vue Solana] Wallet refresh failed", cause);
307
+ }
198
308
  void checkConnection();
199
309
  }, 0);
200
310
  }
201
311
  }
202
312
  };
203
313
  }
204
- function isObject(value) {
205
- return typeof value === "object" && value !== null || typeof value === "function";
206
- }
207
314
  function isSameWallet(wallet, selectedWallet) {
208
315
  return wallet.name === selectedWallet?.name && wallet.source === selectedWallet.source && wallet.platform === selectedWallet.platform;
209
316
  }
210
317
  const VueSolana = createSolanaPlugin;
211
- function withTimeout(promise, timeoutMs, message) {
212
- let timeoutId;
213
- const timeout = new Promise((_, reject) => {
214
- timeoutId = setTimeout(() => {
215
- reject(new Error(message));
216
- }, timeoutMs);
217
- });
218
- return Promise.race([promise, timeout]).finally(() => {
219
- if (timeoutId) {
220
- clearTimeout(timeoutId);
221
- }
222
- });
223
- }
224
318
 
225
319
  export { VueSolana, createSolanaPlugin, solanaInjectionKey };
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ const transaction = require('@vue-solana/core/transaction');
4
+ const vue = require('vue');
5
+ const useConnection = require('./vue.CwhEmATP.cjs');
6
+ const useWallet = require('./vue.CwPjPFN6.cjs');
7
+ const useTransactionConfirmation = require('./vue.DHVYJk8P.cjs');
8
+
9
+ const SIGN_AND_SEND_TIMEOUT_MS = 12e4;
10
+ function useSignAndSendTransaction() {
11
+ const connection = useConnection.useConnection();
12
+ const { wallet } = useWallet.useWallet();
13
+ const signature = vue.ref(null);
14
+ const confirmation = vue.ref(null);
15
+ const status = vue.ref("idle");
16
+ const loading = vue.ref(false);
17
+ const error = vue.ref(null);
18
+ let executionId = 0;
19
+ async function execute(transaction$1, options) {
20
+ const currentExecutionId = ++executionId;
21
+ const { confirm, confirmation: confirmationOptions, ...sendOptions } = options ?? {};
22
+ const transactionOptions = Object.keys(sendOptions).length > 0 ? sendOptions : void 0;
23
+ status.value = "sending";
24
+ loading.value = true;
25
+ error.value = null;
26
+ confirmation.value = null;
27
+ try {
28
+ if (!wallet.value) {
29
+ throw new Error("No Solana wallet is configured");
30
+ }
31
+ const nextSignature = await withWalletTransactionTimeout(
32
+ transaction.signAndSendTransaction(connection, wallet.value, transaction$1, transactionOptions)
33
+ );
34
+ if (currentExecutionId === executionId) {
35
+ signature.value = nextSignature;
36
+ status.value = confirm ? "confirming" : "sent";
37
+ }
38
+ if (!confirm) {
39
+ return nextSignature;
40
+ }
41
+ const nextConfirmation = await transaction.confirmTransactionSignature(
42
+ connection,
43
+ nextSignature,
44
+ confirmationOptions
45
+ );
46
+ if (currentExecutionId === executionId) {
47
+ confirmation.value = nextConfirmation;
48
+ status.value = useTransactionConfirmation.getConfirmedTransactionStatus(nextConfirmation);
49
+ }
50
+ return nextSignature;
51
+ } catch (cause) {
52
+ if (currentExecutionId === executionId) {
53
+ error.value = cause;
54
+ status.value = "error";
55
+ }
56
+ throw cause;
57
+ } finally {
58
+ if (currentExecutionId === executionId) {
59
+ loading.value = false;
60
+ }
61
+ }
62
+ }
63
+ return {
64
+ signature,
65
+ confirmation,
66
+ status,
67
+ loading,
68
+ error,
69
+ execute
70
+ };
71
+ }
72
+ async function withWalletTransactionTimeout(promise) {
73
+ let timeoutId;
74
+ try {
75
+ const timeout = new Promise((_, reject) => {
76
+ timeoutId = setTimeout(() => {
77
+ reject(
78
+ new Error(
79
+ "Wallet transaction did not return a result. Check your wallet or explorer for the final status."
80
+ )
81
+ );
82
+ }, SIGN_AND_SEND_TIMEOUT_MS);
83
+ });
84
+ return await Promise.race([promise, timeout]);
85
+ } finally {
86
+ if (timeoutId) {
87
+ clearTimeout(timeoutId);
88
+ }
89
+ }
90
+ }
91
+
92
+ exports.useSignAndSendTransaction = useSignAndSendTransaction;
@@ -0,0 +1,72 @@
1
+ 'use strict';
2
+
3
+ const transaction = require('@vue-solana/core/transaction');
4
+ const vue = require('vue');
5
+ const useConnection = require('./vue.CwhEmATP.cjs');
6
+
7
+ function getConfirmedTransactionStatus(confirmation) {
8
+ if (confirmation.commitment === "finalized") {
9
+ return "finalized";
10
+ }
11
+ return confirmation.commitment === "processed" ? "processed" : "confirmed";
12
+ }
13
+ function useTransactionConfirmation(defaultOptions = {}) {
14
+ const connection = useConnection.useConnection();
15
+ const signature = vue.ref(null);
16
+ const confirmation = vue.ref(null);
17
+ const status = vue.ref("idle");
18
+ const loading = vue.ref(false);
19
+ const error = vue.ref(null);
20
+ let executionId = 0;
21
+ async function confirm(nextSignature, options = {}) {
22
+ const currentExecutionId = ++executionId;
23
+ const confirmationOptions = { ...defaultOptions, ...options };
24
+ signature.value = nextSignature;
25
+ confirmation.value = null;
26
+ status.value = "confirming";
27
+ loading.value = true;
28
+ error.value = null;
29
+ try {
30
+ const nextConfirmation = await transaction.confirmTransactionSignature(
31
+ connection,
32
+ nextSignature,
33
+ confirmationOptions
34
+ );
35
+ if (currentExecutionId === executionId) {
36
+ confirmation.value = nextConfirmation;
37
+ status.value = getConfirmedTransactionStatus(nextConfirmation);
38
+ }
39
+ return nextConfirmation;
40
+ } catch (cause) {
41
+ if (currentExecutionId === executionId) {
42
+ error.value = cause;
43
+ status.value = "error";
44
+ }
45
+ throw cause;
46
+ } finally {
47
+ if (currentExecutionId === executionId) {
48
+ loading.value = false;
49
+ }
50
+ }
51
+ }
52
+ function reset() {
53
+ executionId += 1;
54
+ signature.value = null;
55
+ confirmation.value = null;
56
+ status.value = "idle";
57
+ loading.value = false;
58
+ error.value = null;
59
+ }
60
+ return {
61
+ signature,
62
+ confirmation,
63
+ status,
64
+ loading,
65
+ error,
66
+ confirm,
67
+ reset
68
+ };
69
+ }
70
+
71
+ exports.getConfirmedTransactionStatus = getConfirmedTransactionStatus;
72
+ exports.useTransactionConfirmation = useTransactionConfirmation;
@@ -2,6 +2,25 @@
2
2
 
3
3
  const vue = require('vue');
4
4
 
5
+ async function withTimeout(promise, timeoutMs, message) {
6
+ if (!timeoutMs) {
7
+ return promise;
8
+ }
9
+ let timeoutId;
10
+ try {
11
+ const timeout = new Promise((_, reject) => {
12
+ timeoutId = setTimeout(() => {
13
+ reject(new Error(message));
14
+ }, timeoutMs);
15
+ });
16
+ return await Promise.race([promise, timeout]);
17
+ } finally {
18
+ if (timeoutId) {
19
+ clearTimeout(timeoutId);
20
+ }
21
+ }
22
+ }
23
+
5
24
  function useTransaction(handler, options = {}) {
6
25
  const signature = vue.ref(null);
7
26
  const loading = vue.ref(false);
@@ -39,21 +58,6 @@ function useTransaction(handler, options = {}) {
39
58
  execute
40
59
  };
41
60
  }
42
- function withTimeout(promise, timeoutMs, message) {
43
- if (!timeoutMs) {
44
- return promise;
45
- }
46
- let timeoutId;
47
- const timeout = new Promise((_, reject) => {
48
- timeoutId = setTimeout(() => {
49
- reject(new Error(message));
50
- }, timeoutMs);
51
- });
52
- return Promise.race([promise, timeout]).finally(() => {
53
- if (timeoutId) {
54
- clearTimeout(timeoutId);
55
- }
56
- });
57
- }
58
61
 
59
62
  exports.useTransaction = useTransaction;
63
+ exports.withTimeout = withTimeout;
@@ -0,0 +1,90 @@
1
+ import { signAndSendTransaction, confirmTransactionSignature } from '@vue-solana/core/transaction';
2
+ import { ref } from 'vue';
3
+ import { u as useConnection } from './vue.DlEAL2G8.mjs';
4
+ import { u as useWallet } from './vue.DYf_CRDv.mjs';
5
+ import { g as getConfirmedTransactionStatus } from './vue.jOsz34kz.mjs';
6
+
7
+ const SIGN_AND_SEND_TIMEOUT_MS = 12e4;
8
+ function useSignAndSendTransaction() {
9
+ const connection = useConnection();
10
+ const { wallet } = useWallet();
11
+ const signature = ref(null);
12
+ const confirmation = ref(null);
13
+ const status = ref("idle");
14
+ const loading = ref(false);
15
+ const error = ref(null);
16
+ let executionId = 0;
17
+ async function execute(transaction, options) {
18
+ const currentExecutionId = ++executionId;
19
+ const { confirm, confirmation: confirmationOptions, ...sendOptions } = options ?? {};
20
+ const transactionOptions = Object.keys(sendOptions).length > 0 ? sendOptions : void 0;
21
+ status.value = "sending";
22
+ loading.value = true;
23
+ error.value = null;
24
+ confirmation.value = null;
25
+ try {
26
+ if (!wallet.value) {
27
+ throw new Error("No Solana wallet is configured");
28
+ }
29
+ const nextSignature = await withWalletTransactionTimeout(
30
+ signAndSendTransaction(connection, wallet.value, transaction, transactionOptions)
31
+ );
32
+ if (currentExecutionId === executionId) {
33
+ signature.value = nextSignature;
34
+ status.value = confirm ? "confirming" : "sent";
35
+ }
36
+ if (!confirm) {
37
+ return nextSignature;
38
+ }
39
+ const nextConfirmation = await confirmTransactionSignature(
40
+ connection,
41
+ nextSignature,
42
+ confirmationOptions
43
+ );
44
+ if (currentExecutionId === executionId) {
45
+ confirmation.value = nextConfirmation;
46
+ status.value = getConfirmedTransactionStatus(nextConfirmation);
47
+ }
48
+ return nextSignature;
49
+ } catch (cause) {
50
+ if (currentExecutionId === executionId) {
51
+ error.value = cause;
52
+ status.value = "error";
53
+ }
54
+ throw cause;
55
+ } finally {
56
+ if (currentExecutionId === executionId) {
57
+ loading.value = false;
58
+ }
59
+ }
60
+ }
61
+ return {
62
+ signature,
63
+ confirmation,
64
+ status,
65
+ loading,
66
+ error,
67
+ execute
68
+ };
69
+ }
70
+ async function withWalletTransactionTimeout(promise) {
71
+ let timeoutId;
72
+ try {
73
+ const timeout = new Promise((_, reject) => {
74
+ timeoutId = setTimeout(() => {
75
+ reject(
76
+ new Error(
77
+ "Wallet transaction did not return a result. Check your wallet or explorer for the final status."
78
+ )
79
+ );
80
+ }, SIGN_AND_SEND_TIMEOUT_MS);
81
+ });
82
+ return await Promise.race([promise, timeout]);
83
+ } finally {
84
+ if (timeoutId) {
85
+ clearTimeout(timeoutId);
86
+ }
87
+ }
88
+ }
89
+
90
+ export { useSignAndSendTransaction as u };