@use-stellar/vue 0.0.0-stage → 0.2.3
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/LICENSE +134 -0
- package/README.md +187 -2
- package/dist/chunk-7ROX7ZPL.mjs +123 -0
- package/dist/chunk-7ROX7ZPL.mjs.map +1 -0
- package/dist/index.d.mts +212 -0
- package/dist/index.d.ts +212 -0
- package/dist/index.js +829 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +713 -0
- package/dist/index.mjs.map +1 -0
- package/dist/nuxt/module.d.mts +15 -0
- package/dist/nuxt/module.d.ts +15 -0
- package/dist/nuxt/module.js +29 -0
- package/dist/nuxt/module.js.map +1 -0
- package/dist/nuxt/module.mjs +23 -0
- package/dist/nuxt/module.mjs.map +1 -0
- package/dist/nuxt/runtime/plugin.d.mts +24 -0
- package/dist/nuxt/runtime/plugin.d.ts +24 -0
- package/dist/nuxt/runtime/plugin.js +140 -0
- package/dist/nuxt/runtime/plugin.js.map +1 -0
- package/dist/nuxt/runtime/plugin.mjs +28 -0
- package/dist/nuxt/runtime/plugin.mjs.map +1 -0
- package/dist/plugin-bXIRDH8_.d.mts +84 -0
- package/dist/plugin-bXIRDH8_.d.ts +84 -0
- package/dist/test-utils.d.mts +27 -0
- package/dist/test-utils.d.ts +27 -0
- package/dist/test-utils.js +97 -0
- package/dist/test-utils.js.map +1 -0
- package/dist/test-utils.mjs +22 -0
- package/dist/test-utils.mjs.map +1 -0
- package/package.json +132 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship submitted to the
|
|
45
|
+
Licensor for inclusion in the Work by the copyright owner or by an
|
|
46
|
+
individual or Legal Entity authorized to submit on behalf of the
|
|
47
|
+
copyright owner.
|
|
48
|
+
|
|
49
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
50
|
+
whom a Contribution has been received by the Licensor and subsequently
|
|
51
|
+
incorporated within the Work.
|
|
52
|
+
|
|
53
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
54
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
55
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
56
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
57
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
58
|
+
Work and such Derivative Works in Source or Object form.
|
|
59
|
+
|
|
60
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
61
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
62
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
63
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
64
|
+
and otherwise transfer the Work.
|
|
65
|
+
|
|
66
|
+
4. Redistribution. You may reproduce and distribute copies of the Work
|
|
67
|
+
or Derivative Works thereof in any medium, with or without
|
|
68
|
+
modifications, and in Source or Object form, provided that You meet
|
|
69
|
+
the following conditions:
|
|
70
|
+
|
|
71
|
+
(a) You must give any other recipients of the Work or Derivative Works
|
|
72
|
+
a copy of this License; and
|
|
73
|
+
|
|
74
|
+
(b) You must cause any modified files to carry prominent notices
|
|
75
|
+
stating that You changed the files; and
|
|
76
|
+
|
|
77
|
+
(c) You must retain, in the Source form of any Derivative Works that
|
|
78
|
+
You distribute, all copyright, patent, trademark, and attribution
|
|
79
|
+
notices from the Source form of the Work; and
|
|
80
|
+
|
|
81
|
+
(d) If the Work includes a "NOTICE" text file, You must include a
|
|
82
|
+
readable copy of the attribution notices contained within such
|
|
83
|
+
NOTICE file.
|
|
84
|
+
|
|
85
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
86
|
+
any Contribution submitted for inclusion in the Work shall be under
|
|
87
|
+
the terms and conditions of this License, without any additional
|
|
88
|
+
terms or conditions.
|
|
89
|
+
|
|
90
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
91
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
92
|
+
|
|
93
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed
|
|
94
|
+
to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
95
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
96
|
+
implied, including, without limitation, any warranties or conditions
|
|
97
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
98
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
99
|
+
appropriateness of using or redistributing the Work.
|
|
100
|
+
|
|
101
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
102
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
103
|
+
unless required by applicable law, shall any Contributor be liable
|
|
104
|
+
to You for damages, including any direct, indirect, special,
|
|
105
|
+
incidental, or exemplary damages of any character arising as a
|
|
106
|
+
result of this License or out of the use or inability to use the
|
|
107
|
+
Work.
|
|
108
|
+
|
|
109
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
110
|
+
the Work or Derivative Works thereof, You may choose to offer, and
|
|
111
|
+
charge a fee for, acceptance of support, warranty, indemnity, or
|
|
112
|
+
other liability obligations consistent with this License. However,
|
|
113
|
+
in accepting such obligations, You may act only on Your own behalf
|
|
114
|
+
and on Your sole responsibility, not on behalf of any other
|
|
115
|
+
Contributor, and only if You agree to indemnify, defend, and hold
|
|
116
|
+
each Contributor harmless for any liability incurred by, or claims
|
|
117
|
+
asserted against, such Contributor by reason of your accepting any
|
|
118
|
+
such warranty or additional liability.
|
|
119
|
+
|
|
120
|
+
END OF TERMS AND CONDITIONS
|
|
121
|
+
|
|
122
|
+
Copyright 2024 israelolrunfemi
|
|
123
|
+
|
|
124
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
125
|
+
you may not use this file except in compliance with the License.
|
|
126
|
+
You may obtain a copy of the License at
|
|
127
|
+
|
|
128
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
129
|
+
|
|
130
|
+
Unless required by applicable law or agreed to in writing, software
|
|
131
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
132
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
133
|
+
See the License for the specific language governing permissions and
|
|
134
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,188 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @use-stellar/vue
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Vue 3 composables for the Stellar network, built on [`use-stellar`](https://www.npmjs.com/package/use-stellar).
|
|
4
|
+
|
|
5
|
+
The composables return Vue refs and clean up with their effect scope. Everything underneath — Horizon and Soroban fetchers, transaction building, fee policy, error codes, the query cache — is the same React-free code the React hooks use, imported from `use-stellar/core`. React is never loaded.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @use-stellar/vue use-stellar vue
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`use-stellar` and `vue` (3.3 or later) are peer dependencies: your app has one copy of each, shared with anything else that uses them.
|
|
14
|
+
|
|
15
|
+
Install the SDK of the wallet you will sign with, exactly as for the React package — for example `npm install @stellar/freighter-api`. See [Supported wallets](https://github.com/RaceeyXo/use-stellar/tree/main/packages/core#supported-wallets).
|
|
16
|
+
|
|
17
|
+
## Quick start
|
|
18
|
+
|
|
19
|
+
Install the plugin once. It defaults to testnet.
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
// main.ts
|
|
23
|
+
import { createApp } from "vue"
|
|
24
|
+
import { createStellarPlugin } from "@use-stellar/vue"
|
|
25
|
+
import App from "./App.vue"
|
|
26
|
+
|
|
27
|
+
createApp(App)
|
|
28
|
+
.use(createStellarPlugin({ network: "testnet" }))
|
|
29
|
+
.mount("#app")
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Then use the composables in any component:
|
|
33
|
+
|
|
34
|
+
```vue
|
|
35
|
+
<script setup lang="ts">
|
|
36
|
+
import { ref } from "vue"
|
|
37
|
+
import { useBalance } from "@use-stellar/vue"
|
|
38
|
+
|
|
39
|
+
const address = ref("G...")
|
|
40
|
+
const { balance, loading, error } = useBalance({ address })
|
|
41
|
+
</script>
|
|
42
|
+
|
|
43
|
+
<template>
|
|
44
|
+
<p v-if="loading">Loading…</p>
|
|
45
|
+
<p v-else-if="error">{{ error.message }}</p>
|
|
46
|
+
<p v-else>{{ balance }} XLM</p>
|
|
47
|
+
</template>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Every option accepts a plain value, a ref, or a getter, and changes are picked up reactively.
|
|
51
|
+
|
|
52
|
+
### Plugin options
|
|
53
|
+
|
|
54
|
+
| Option | Default | Description |
|
|
55
|
+
| --------------- | ---------------------------------------- | ---------------------------------------------------------------------------- |
|
|
56
|
+
| `network` | `"testnet"` | `"testnet"`, `"mainnet"` or `"futurenet"`. |
|
|
57
|
+
| `networkConfig` | — | Override `horizonUrl`, `sorobanUrl` or `networkPassphrase` for that network. |
|
|
58
|
+
| `queryConfig` | `{ staleTime: 30_000, gcTime: 300_000 }` | Cache timings in milliseconds, shared by every composable. |
|
|
59
|
+
| `walletAddress` | `null` | The address read composables default to when given none. |
|
|
60
|
+
|
|
61
|
+
## Composables
|
|
62
|
+
|
|
63
|
+
### Reading
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
// XLM, or any asset with `asset: { code, issuer }`
|
|
67
|
+
const { balance, balances, loading, error, refetch } = useBalance({ address })
|
|
68
|
+
|
|
69
|
+
// Claimable balances; defaults to the runtime's wallet address
|
|
70
|
+
const { balances } = useClaimableBalance({ address })
|
|
71
|
+
|
|
72
|
+
// Paginated payments
|
|
73
|
+
const { payments, hasNext, fetchNext } = usePayments({ address, limit: 20 })
|
|
74
|
+
|
|
75
|
+
// The same, filtered on Horizon by direction and asset
|
|
76
|
+
const { payments: received } = usePaymentHistory({ address, direction: "incoming" })
|
|
77
|
+
|
|
78
|
+
// Read-only Soroban contract call, simulated
|
|
79
|
+
const { data } = useSorobanContract<number>({ contractId: "C...", method: "get_count" })
|
|
80
|
+
|
|
81
|
+
// Poll a contract's events
|
|
82
|
+
const { events, latestLedger } = useContractEvents({ contractIds: ["C..."] })
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Requests are deduplicated and cached per app: two components asking for the same account share one request. `useBalance` accepts `maxRetries` (default 3) for rate limits.
|
|
86
|
+
|
|
87
|
+
### Writing
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
const { send, loading, error, result } = useSendPayment()
|
|
91
|
+
await send({ to: "G...", asset: "XLM", amount: "10" })
|
|
92
|
+
|
|
93
|
+
const { addTrustline } = useAddTrustline()
|
|
94
|
+
await addTrustline({ asset: { code: "USDC", issuer: "G..." } })
|
|
95
|
+
|
|
96
|
+
const { pathPayment } = usePathPayment()
|
|
97
|
+
await pathPayment({
|
|
98
|
+
mode: "strictSend",
|
|
99
|
+
destination: "G...",
|
|
100
|
+
sendAsset: "XLM",
|
|
101
|
+
sendAmount: "100",
|
|
102
|
+
destAsset: { code: "USDC", issuer: "G..." },
|
|
103
|
+
destMin: "24.75",
|
|
104
|
+
})
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Validation, fee policy, the network check, signing and cache invalidation are the core actions the React hooks use, so behaviour and error codes are identical.
|
|
108
|
+
|
|
109
|
+
### Connecting a wallet
|
|
110
|
+
|
|
111
|
+
This release has no `useWallet` composable yet. The write composables sign with the wallet set on the runtime, so connect through a core wallet adapter and hand the result to the runtime:
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
import { freighterAdapter, getWalletAdapter, registerWalletAdapter } from "use-stellar/core"
|
|
115
|
+
import { useStellarRuntime } from "@use-stellar/vue"
|
|
116
|
+
|
|
117
|
+
// Once, at startup. `use-stellar/core` registers no browser wallets itself.
|
|
118
|
+
registerWalletAdapter(freighterAdapter, { override: true })
|
|
119
|
+
|
|
120
|
+
const runtime = useStellarRuntime() // inside setup()
|
|
121
|
+
|
|
122
|
+
async function connect() {
|
|
123
|
+
const adapter = getWalletAdapter("freighter")
|
|
124
|
+
const connection = await adapter.connect(runtime.network)
|
|
125
|
+
runtime.setWallet({
|
|
126
|
+
connected: true,
|
|
127
|
+
connecting: false,
|
|
128
|
+
address: connection.address,
|
|
129
|
+
network: runtime.network,
|
|
130
|
+
wallet: connection.wallet,
|
|
131
|
+
walletName: adapter.metadata.name,
|
|
132
|
+
error: null,
|
|
133
|
+
walletNetwork: connection.network,
|
|
134
|
+
walletNetworkPassphrase: connection.networkPassphrase,
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`setWallet` also sets the address the read composables default to. Register each wallet you offer the same way: `freighterAdapter` and `rabetAdapter` are exported from `use-stellar/core`, `createWalletConnectAdapter` creates a WalletConnect adapter, and any object implementing the `WalletAdapter` contract works.
|
|
140
|
+
|
|
141
|
+
## Nuxt
|
|
142
|
+
|
|
143
|
+
A Nuxt module is included. It installs the runtime and, on the client, can restore the previous wallet session:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
// nuxt.config.ts
|
|
147
|
+
export default defineNuxtConfig({
|
|
148
|
+
modules: ["@use-stellar/vue/nuxt"],
|
|
149
|
+
runtimeConfig: {
|
|
150
|
+
public: {
|
|
151
|
+
stellar: {
|
|
152
|
+
network: "testnet",
|
|
153
|
+
autoConnect: true, // or { enabled: true, storage: "session" }
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
})
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
The settings are read from `runtimeConfig.public.stellar`, so they can also come from environment variables in the usual Nuxt way.
|
|
161
|
+
|
|
162
|
+
Session restore never prompts: it reconnects only when the wallet allows a silent reconnect, and otherwise just remembers which wallet was used. Nothing is fetched during server rendering.
|
|
163
|
+
|
|
164
|
+
## Testing
|
|
165
|
+
|
|
166
|
+
`@use-stellar/vue/test-utils` gives a composable a runtime without mounting a component:
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
import { createStellarTestHarness } from "@use-stellar/vue/test-utils"
|
|
170
|
+
import { useBalance } from "@use-stellar/vue"
|
|
171
|
+
|
|
172
|
+
const harness = createStellarTestHarness({ network: "testnet" })
|
|
173
|
+
const { balance } = harness.run(() => useBalance({ address: "G..." }))
|
|
174
|
+
|
|
175
|
+
// ...assert...
|
|
176
|
+
|
|
177
|
+
harness.dispose() // tears down every watcher created inside run()
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Mock `@stellar/stellar-sdk`'s `Horizon.Server` in tests rather than calling the network.
|
|
181
|
+
|
|
182
|
+
## Errors
|
|
183
|
+
|
|
184
|
+
Every `error` ref holds a typed `StellarError` with a stable `code` (`ACCOUNT_NOT_FOUND`, `RATE_LIMITED`, `WALLET_NOT_CONNECTED`, `TX_TIMEOUT`, …) and a readable `message` — the same codes as the React package. See the [error-code table](https://github.com/RaceeyXo/use-stellar/tree/main/packages/core#error-codes).
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
[Apache-2.0](https://github.com/RaceeyXo/use-stellar/blob/main/LICENSE)
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { shallowRef, inject } from 'vue';
|
|
2
|
+
import { QueryStore, readWalletSession, getWalletAdapter } from 'use-stellar/core';
|
|
3
|
+
|
|
4
|
+
// src/types.ts
|
|
5
|
+
var NETWORK_PASSPHRASES = {
|
|
6
|
+
testnet: "Test SDF Network ; September 2015",
|
|
7
|
+
mainnet: "Public Global Stellar Network ; September 2015",
|
|
8
|
+
futurenet: "Test SDF Future Network ; October 2022"
|
|
9
|
+
};
|
|
10
|
+
var NETWORK_CONFIGS = {
|
|
11
|
+
testnet: {
|
|
12
|
+
network: "testnet",
|
|
13
|
+
horizonUrl: "https://horizon-testnet.stellar.org",
|
|
14
|
+
sorobanUrl: "https://soroban-testnet.stellar.org",
|
|
15
|
+
networkPassphrase: NETWORK_PASSPHRASES.testnet
|
|
16
|
+
},
|
|
17
|
+
mainnet: {
|
|
18
|
+
network: "mainnet",
|
|
19
|
+
horizonUrl: "https://horizon.stellar.org",
|
|
20
|
+
sorobanUrl: "https://soroban.stellar.org",
|
|
21
|
+
networkPassphrase: NETWORK_PASSPHRASES.mainnet
|
|
22
|
+
},
|
|
23
|
+
futurenet: {
|
|
24
|
+
network: "futurenet",
|
|
25
|
+
horizonUrl: "https://horizon-futurenet.stellar.org",
|
|
26
|
+
sorobanUrl: "https://rpc-futurenet.stellar.org",
|
|
27
|
+
networkPassphrase: NETWORK_PASSPHRASES.futurenet
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var STELLAR_RUNTIME_KEY = /* @__PURE__ */ Symbol("use-stellar/vue:runtime");
|
|
31
|
+
var DISCONNECTED_WALLET = {
|
|
32
|
+
connected: false,
|
|
33
|
+
connecting: false,
|
|
34
|
+
address: null,
|
|
35
|
+
network: null,
|
|
36
|
+
wallet: null,
|
|
37
|
+
walletName: null,
|
|
38
|
+
error: null,
|
|
39
|
+
walletNetwork: null,
|
|
40
|
+
walletNetworkPassphrase: null
|
|
41
|
+
};
|
|
42
|
+
function resolveNetworkConfig(network, override) {
|
|
43
|
+
const builtIn = NETWORK_CONFIGS[network];
|
|
44
|
+
if (!override) return builtIn;
|
|
45
|
+
return {
|
|
46
|
+
network,
|
|
47
|
+
horizonUrl: override.horizonUrl ?? builtIn.horizonUrl,
|
|
48
|
+
sorobanUrl: override.sorobanUrl ?? builtIn.sorobanUrl,
|
|
49
|
+
networkPassphrase: override.networkPassphrase ?? builtIn.networkPassphrase
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function createStellarRuntime(options = {}) {
|
|
53
|
+
const network = options.network ?? "testnet";
|
|
54
|
+
const walletAddress = shallowRef(options.walletAddress ?? null);
|
|
55
|
+
const runtime = {
|
|
56
|
+
network,
|
|
57
|
+
networkConfig: resolveNetworkConfig(network, options.networkConfig),
|
|
58
|
+
queryStore: new QueryStore(options.queryConfig),
|
|
59
|
+
wallet: { ...DISCONNECTED_WALLET, address: options.walletAddress ?? null },
|
|
60
|
+
setWallet(wallet) {
|
|
61
|
+
runtime.wallet = wallet;
|
|
62
|
+
walletAddress.value = wallet.address;
|
|
63
|
+
},
|
|
64
|
+
walletAddress,
|
|
65
|
+
setWalletAddress(address) {
|
|
66
|
+
walletAddress.value = address;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
return runtime;
|
|
70
|
+
}
|
|
71
|
+
async function restoreWalletSession(runtime, storage = "local") {
|
|
72
|
+
const session = await readWalletSession(storage);
|
|
73
|
+
if (!session) return;
|
|
74
|
+
try {
|
|
75
|
+
const adapter = getWalletAdapter(session.wallet);
|
|
76
|
+
const available = await adapter.isAvailable();
|
|
77
|
+
if (!available) return;
|
|
78
|
+
const canReconnect = adapter.canAutoConnect ? await adapter.canAutoConnect(runtime.network) : false;
|
|
79
|
+
if (!canReconnect) {
|
|
80
|
+
runtime.setWallet({
|
|
81
|
+
...runtime.wallet,
|
|
82
|
+
wallet: session.wallet,
|
|
83
|
+
walletName: adapter.metadata.name,
|
|
84
|
+
address: session.address ?? null
|
|
85
|
+
});
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const connection = await adapter.connect(runtime.network);
|
|
89
|
+
const network = await adapter.getNetworkDetails(runtime.network);
|
|
90
|
+
runtime.setWallet({
|
|
91
|
+
connected: true,
|
|
92
|
+
connecting: false,
|
|
93
|
+
address: connection.address,
|
|
94
|
+
network: runtime.network,
|
|
95
|
+
wallet: connection.wallet,
|
|
96
|
+
walletName: adapter.metadata.name,
|
|
97
|
+
error: null,
|
|
98
|
+
walletNetwork: network.network,
|
|
99
|
+
walletNetworkPassphrase: network.networkPassphrase
|
|
100
|
+
});
|
|
101
|
+
} catch {
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function createStellarPlugin(options = {}) {
|
|
105
|
+
return {
|
|
106
|
+
install(app) {
|
|
107
|
+
app.provide(STELLAR_RUNTIME_KEY, createStellarRuntime(options));
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function useStellarRuntime() {
|
|
112
|
+
const runtime = inject(STELLAR_RUNTIME_KEY);
|
|
113
|
+
if (!runtime) {
|
|
114
|
+
throw new Error(
|
|
115
|
+
"@use-stellar/vue: no plugin installed. Call app.use(createStellarPlugin()) before using any composable."
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
return runtime;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { NETWORK_CONFIGS, STELLAR_RUNTIME_KEY, createStellarPlugin, createStellarRuntime, restoreWalletSession, useStellarRuntime };
|
|
122
|
+
//# sourceMappingURL=chunk-7ROX7ZPL.mjs.map
|
|
123
|
+
//# sourceMappingURL=chunk-7ROX7ZPL.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types.ts","../src/plugin.ts"],"names":[],"mappings":";;;;AA0BA,IAAM,mBAAA,GAAsD;AAAA,EAC1D,OAAA,EAAS,mCAAA;AAAA,EACT,OAAA,EAAS,gDAAA;AAAA,EACT,SAAA,EAAW;AACb,CAAA;AAEO,IAAM,eAAA,GAAyD;AAAA,EACpE,OAAA,EAAS;AAAA,IACP,OAAA,EAAS,SAAA;AAAA,IACT,UAAA,EAAY,qCAAA;AAAA,IACZ,UAAA,EAAY,qCAAA;AAAA,IACZ,mBAAmB,mBAAA,CAAoB;AAAA,GACzC;AAAA,EACA,OAAA,EAAS;AAAA,IACP,OAAA,EAAS,SAAA;AAAA,IACT,UAAA,EAAY,6BAAA;AAAA,IACZ,UAAA,EAAY,6BAAA;AAAA,IACZ,mBAAmB,mBAAA,CAAoB;AAAA,GACzC;AAAA,EACA,SAAA,EAAW;AAAA,IACT,OAAA,EAAS,WAAA;AAAA,IACT,UAAA,EAAY,uCAAA;AAAA,IACZ,UAAA,EAAY,mCAAA;AAAA,IACZ,mBAAmB,mBAAA,CAAoB;AAAA;AAE3C;ACdO,IAAM,mBAAA,0BAA2D,y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A,MACZ,SAAS,UAAA,CAAW,OAAA;AAAA,MACpB,SAAS,OAAA,CAAQ,OAAA;AAAA,MACjB,QAAQ,UAAA,CAAW,MAAA;AAAA,MACnB,UAAA,EAAY,QAAQ,QAAA,CAAS,IAAA;AAAA,MAC7B,KAAA,EAAO,IAAA;AAAA,MACP,eAAe,OAAA,CAAQ,OAAA;AAAA,MACvB,yBAAyB,OAAA,CAAQ;AAAA,KAClC,CAAA;AAAA,EACH,CAAA,CAAA,MAAQ;AAAA,EAER;AACF;AAaO,SAAS,mBAAA,CAAoB,OAAA,GAAgC,EAAC,EAAG;AACtE,EAAA,OAAO;AAAA,IACL,QAAQ,GAAA,EAAgB;AACtB,MAAA,GAAA,CAAI,OAAA,CAAQ,mBAAA,EAAqB,oBAAA,CAAqB,OAAO,CAAC,CAAA;AAAA,IAChE;AAAA,GACF;AACF;AAQO,SAAS,iBAAA,GAAoC;AAClD,EAAA,MAAM,OAAA,GAAU,OAAO,mBAAmB,CAAA;AAC1C,EAAA,IAAI,CAAC,OAAA,EAAS;AACZ,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KAEF;AAAA,EACF;AACA,EAAA,OAAO,OAAA;AACT","file":"chunk-7ROX7ZPL.mjs","sourcesContent":["// packages/vue/src/types.ts\n//\n// A small, self-contained type set for the Vue adapter. Deliberately not\n// imported from the `use-stellar` (React) package: that package declares\n// `react` as a required peer dependency, and pulling any part of it in here\n// would put a React peer-dependency warning in front of every Vue user who\n// installs this package — the exact installation friction `rn-03` fixed for\n// React Native. Custom networks aren't supported yet; only the three SDF\n// defaults are.\n\nexport type StellarNetwork = \"testnet\" | \"mainnet\" | \"futurenet\"\n\nexport interface NetworkConfig {\n network: StellarNetwork\n horizonUrl: string\n sorobanUrl: string\n networkPassphrase: string\n}\n\nexport type Asset = \"XLM\" | { code: string; issuer: string }\n\nexport type Balance =\n | { asset: \"XLM\"; balance: string }\n | { asset: { code: string; issuer: string }; balance: string; limit?: string }\n | { asset: \"liquidity_pool_shares\"; balance: string; liquidityPoolId: string }\n\nconst NETWORK_PASSPHRASES: Record<StellarNetwork, string> = {\n testnet: \"Test SDF Network ; September 2015\",\n mainnet: \"Public Global Stellar Network ; September 2015\",\n futurenet: \"Test SDF Future Network ; October 2022\",\n}\n\nexport const NETWORK_CONFIGS: Record<StellarNetwork, NetworkConfig> = {\n testnet: {\n network: \"testnet\",\n horizonUrl: \"https://horizon-testnet.stellar.org\",\n sorobanUrl: \"https://soroban-testnet.stellar.org\",\n networkPassphrase: NETWORK_PASSPHRASES.testnet,\n },\n mainnet: {\n network: \"mainnet\",\n horizonUrl: \"https://horizon.stellar.org\",\n sorobanUrl: \"https://soroban.stellar.org\",\n networkPassphrase: NETWORK_PASSPHRASES.mainnet,\n },\n futurenet: {\n network: \"futurenet\",\n horizonUrl: \"https://horizon-futurenet.stellar.org\",\n sorobanUrl: \"https://rpc-futurenet.stellar.org\",\n networkPassphrase: NETWORK_PASSPHRASES.futurenet,\n },\n}\n","// packages/vue/src/plugin.ts\n\nimport { inject, shallowRef, type App, type InjectionKey, type Ref } from \"vue\"\nimport {\n QueryStore,\n getWalletAdapter,\n readWalletSession,\n type QueryConfig,\n type WalletState,\n} from \"use-stellar/core\"\nimport { NETWORK_CONFIGS, type NetworkConfig, type StellarNetwork } from \"./types\"\n\nexport interface StellarPluginOptions {\n /** Defaults to `\"testnet\"`. */\n network?: StellarNetwork\n /** Override the built-in Horizon/Soroban endpoints for `network`. Any field left out keeps the default. */\n networkConfig?: Partial<Pick<NetworkConfig, \"horizonUrl\" | \"sorobanUrl\" | \"networkPassphrase\">>\n /** Cache configuration shared by every composable. */\n queryConfig?: QueryConfig\n /** Initial connected wallet address, primarily for restored client sessions. */\n walletAddress?: string | null\n}\n\nexport interface StellarRuntime {\n network: StellarNetwork\n networkConfig: NetworkConfig\n /** One query cache per app, shared by every composable. */\n queryStore: QueryStore\n /** The wallet connection the write composables sign with. */\n wallet: WalletState\n /** Replaces the wallet state, and keeps `walletAddress` in step with it. */\n setWallet: (wallet: WalletState) => void\n /** The address read composables default to when given none. */\n walletAddress: Ref<string | null>\n setWalletAddress: (address: string | null) => void\n}\n\nexport const STELLAR_RUNTIME_KEY: InjectionKey<StellarRuntime> = Symbol(\"use-stellar/vue:runtime\")\n\nconst DISCONNECTED_WALLET: WalletState = {\n connected: false,\n connecting: false,\n address: null,\n network: null,\n wallet: null,\n walletName: null,\n error: null,\n walletNetwork: null,\n walletNetworkPassphrase: null,\n}\n\nfunction resolveNetworkConfig(\n network: StellarNetwork,\n override: StellarPluginOptions[\"networkConfig\"]\n): NetworkConfig {\n const builtIn = NETWORK_CONFIGS[network]\n if (!override) return builtIn\n\n return {\n network,\n horizonUrl: override.horizonUrl ?? builtIn.horizonUrl,\n sorobanUrl: override.sorobanUrl ?? builtIn.sorobanUrl,\n networkPassphrase: override.networkPassphrase ?? builtIn.networkPassphrase,\n }\n}\n\n/**\n * Builds a Stellar runtime without installing it as a plugin. Used by\n * {@link createStellarPlugin} itself, and by the test harness in\n * `use-stellar/vue/test-utils` to provide a runtime without a real app.\n */\nexport function createStellarRuntime(options: StellarPluginOptions = {}): StellarRuntime {\n const network = options.network ?? \"testnet\"\n const walletAddress = shallowRef(options.walletAddress ?? null)\n\n const runtime: StellarRuntime = {\n network,\n networkConfig: resolveNetworkConfig(network, options.networkConfig),\n queryStore: new QueryStore(options.queryConfig),\n wallet: { ...DISCONNECTED_WALLET, address: options.walletAddress ?? null },\n setWallet(wallet): void {\n runtime.wallet = wallet\n walletAddress.value = wallet.address\n },\n walletAddress,\n setWalletAddress(address: string | null): void {\n walletAddress.value = address\n },\n }\n return runtime\n}\n\n/** Restores a persisted wallet without ever prompting during server rendering. */\nexport async function restoreWalletSession(\n runtime: StellarRuntime,\n storage: \"local\" | \"session\" = \"local\"\n): Promise<void> {\n const session = await readWalletSession(storage)\n if (!session) return\n\n try {\n const adapter = getWalletAdapter(session.wallet)\n const available = await adapter.isAvailable()\n if (!available) return\n\n const canReconnect = adapter.canAutoConnect\n ? await adapter.canAutoConnect(runtime.network)\n : false\n if (!canReconnect) {\n runtime.setWallet({\n ...runtime.wallet,\n wallet: session.wallet,\n walletName: adapter.metadata.name,\n address: session.address ?? null,\n })\n return\n }\n\n const connection = await adapter.connect(runtime.network)\n const network = await adapter.getNetworkDetails(runtime.network)\n runtime.setWallet({\n connected: true,\n connecting: false,\n address: connection.address,\n network: runtime.network,\n wallet: connection.wallet,\n walletName: adapter.metadata.name,\n error: null,\n walletNetwork: network.network,\n walletNetworkPassphrase: network.networkPassphrase,\n })\n } catch {\n // Session restoration is best-effort; a normal disconnected app remains usable.\n }\n}\n\n/**\n * Vue plugin that installs the Stellar runtime — resolved network config plus\n * one shared query cache — so every composable in this package reads and\n * writes through the same instance.\n *\n * @example\n * import { createApp } from \"vue\"\n * import { createStellarPlugin } from \"@use-stellar/vue\"\n *\n * createApp(App).use(createStellarPlugin({ network: \"testnet\" })).mount(\"#app\")\n */\nexport function createStellarPlugin(options: StellarPluginOptions = {}) {\n return {\n install(app: App): void {\n app.provide(STELLAR_RUNTIME_KEY, createStellarRuntime(options))\n },\n }\n}\n\n/**\n * Reads the installed Stellar runtime.\n *\n * @throws {Error} when called outside a component tree the plugin was\n * installed on (or outside `app.runWithContext()` in tests).\n */\nexport function useStellarRuntime(): StellarRuntime {\n const runtime = inject(STELLAR_RUNTIME_KEY)\n if (!runtime) {\n throw new Error(\n \"@use-stellar/vue: no plugin installed. Call \" +\n \"app.use(createStellarPlugin()) before using any composable.\"\n )\n }\n return runtime\n}\n"]}
|