@vechain/vechain-kit 1.2.2 → 1.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/README.md +199 -113
- package/dist/{Constants-Q8PYUyoh.d.cts → Constants-Cu1LVhH4.d.cts} +1 -0
- package/dist/{Constants-Q8PYUyoh.d.ts → Constants-Cu1LVhH4.d.ts} +1 -0
- package/dist/index.cjs +241 -76
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +115 -59
- package/dist/index.d.ts +115 -59
- package/dist/index.js +230 -77
- package/dist/index.js.map +1 -1
- package/dist/metafile-cjs.json +1 -1
- package/dist/metafile-esm.json +1 -1
- package/dist/utils/index.d.cts +2 -2
- package/dist/utils/index.d.ts +2 -2
- package/package.json +105 -105
package/README.md
CHANGED
|
@@ -6,21 +6,26 @@
|
|
|
6
6
|
<img src="https://i.ibb.co/k539SN7/kit-banner.png" alt="VeChain Kit Banner">
|
|
7
7
|
</div>
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
VeChain Kit is a comprehensive library designed to make building VeChain applications fast and straightforward.
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
11
|
+
It offers:
|
|
12
|
+
|
|
13
|
+
- <b>Seamless Wallet Integration:</b> Support for VeWorld, Sync2, WalletConnect, VeChain Embedded Wallet, and social logins (powered by Privy).
|
|
14
|
+
- <b>Unified Ecosystem Accounts:</b> Leverage Privy’s Ecosystem feature to give users a single wallet across multiple dApps, providing a consistent identity within the VeChain network.
|
|
15
|
+
- <b>Developer-Friendly Hooks:</b> Easy-to-use React Hooks that let you read and write data on the VeChainThor blockchain.
|
|
16
|
+
- <b>Pre-Built UI Components:</b> Ready-to-use components (e.g., TransactionModal) to simplify wallet operations and enhance your users’ experience.
|
|
17
|
+
- <b>Multi-Language Support:</b> Built-in i18n for a global audience.
|
|
18
|
+
- <b>Token Operations:</b> Send tokens, check balances, manage VET domains, and more—all in one place.
|
|
16
19
|
|
|
17
20
|
> **Note**: Currently supports React and Next.js only
|
|
18
21
|
|
|
19
|
-
📚 For detailed documentation, visit our [VeChain Kit Docs](https://vechain-
|
|
22
|
+
📚 For detailed documentation, visit our [VeChain Kit Docs](https://docs.vechain-kit.vechain.org/)
|
|
20
23
|
|
|
21
|
-
|
|
24
|
+
## Demo & Examples
|
|
22
25
|
|
|
23
|
-
|
|
26
|
+
- [Live Demo](https://vechain-kit.vechain.org/)
|
|
27
|
+
- [Sample Next.js App](https://github.com/vechain/vechain-kit/tree/main/examples/next-template)
|
|
28
|
+
- [Smart Account Factory](https://vechain.github.io/smart-accounts-factory/)
|
|
24
29
|
|
|
25
30
|
# Installation
|
|
26
31
|
|
|
@@ -30,40 +35,26 @@ yarn add @tanstack/react-query@"^5.64.2" @chakra-ui/react@"^2.8.2" @vechain/dapp
|
|
|
30
35
|
|
|
31
36
|
# Quick Start
|
|
32
37
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
Import the `VechainKitProvider` provider and wrap your app in it.
|
|
38
|
+
### Define Provider
|
|
36
39
|
|
|
37
40
|
```typescript
|
|
38
|
-
|
|
41
|
+
'use client';
|
|
39
42
|
|
|
40
|
-
|
|
43
|
+
import DAppKitPrivyProvider from '@vechain/dapp-kit-react-privy'
|
|
44
|
+
|
|
45
|
+
export function DAppKitPrivyProvider({ children }: Props) {
|
|
41
46
|
return (
|
|
42
|
-
|
|
47
|
+
<VechainKitProvider
|
|
43
48
|
// Mandatory
|
|
44
49
|
feeDelegation={{
|
|
45
|
-
delegatorUrl: process.env.NEXT_PUBLIC_DELEGATOR_URL
|
|
50
|
+
delegatorUrl: process.env.NEXT_PUBLIC_DELEGATOR_URL!
|
|
46
51
|
}}
|
|
47
52
|
dappKit={{
|
|
48
|
-
allowedWallets: ['veworld', '
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
metadata: {
|
|
54
|
-
name: 'Your App',
|
|
55
|
-
description: 'Your app description',
|
|
56
|
-
url:
|
|
57
|
-
typeof window !== 'undefined'
|
|
58
|
-
? window.location.origin
|
|
59
|
-
: '',
|
|
60
|
-
icons: [
|
|
61
|
-
typeof window !== 'undefined'
|
|
62
|
-
? `${window.location.origin}/images/logo/my-dapp.png`
|
|
63
|
-
: '',
|
|
64
|
-
],
|
|
65
|
-
},
|
|
66
|
-
},
|
|
53
|
+
allowedWallets: ['veworld', 'sync2'],
|
|
54
|
+
}}
|
|
55
|
+
privyEcosystemApps=[] // remove for using default ones
|
|
56
|
+
loginModalUI={{
|
|
57
|
+
variant: 'vechain-wallet-ecosystem',
|
|
67
58
|
}}
|
|
68
59
|
darkMode={true}
|
|
69
60
|
language="en"
|
|
@@ -77,9 +68,44 @@ export default function App({ Component, pageProps }: AppProps) {
|
|
|
77
68
|
}
|
|
78
69
|
```
|
|
79
70
|
|
|
80
|
-
|
|
71
|
+
On Next.js you will need to dynamically load the import
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import dynamic from 'next/dynamic';
|
|
75
|
+
const VeChainKitProviderWrapper = dynamic(
|
|
76
|
+
async () => (await import('@vechain/vechain-kit')).VeChainKitProvider,
|
|
77
|
+
{
|
|
78
|
+
ssr: false,
|
|
79
|
+
},
|
|
80
|
+
);
|
|
81
|
+
interface Props {
|
|
82
|
+
children: React.ReactNode;
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Setup Fee Delegation (mandatory)
|
|
87
|
+
|
|
88
|
+
Fee delegation is mandatory in order to use this kit. Learn how to setup fee delegation in the following guide:
|
|
89
|
+
|
|
90
|
+
[Fee Delegation](https://app.gitbook.com/o/PqN0Gs1QEzg8tbeJCHXC/s/S8udqSGhGctlwwL1kst7/~/changes/42/vechain-kit/fee-delegation)
|
|
91
|
+
|
|
92
|
+
### Setup Privy (optional)
|
|
93
|
+
|
|
94
|
+
If you already use Privy you can pass an additional prop with you settings and you will be able to access Privy SDK, customizing the login modal based on your needs.
|
|
95
|
+
|
|
96
|
+
Pros of self hosting Privy:
|
|
81
97
|
|
|
82
|
-
|
|
98
|
+
- No UI confirmations on users transactions
|
|
99
|
+
- Allow your users to backup their keys and update security settings directly in your app
|
|
100
|
+
- Targetted social login methods
|
|
101
|
+
|
|
102
|
+
Cons:
|
|
103
|
+
|
|
104
|
+
- Price
|
|
105
|
+
- Responsibilities to correctly secure your Privy account, since it contains access to user's wallet settings
|
|
106
|
+
- Your users will need to login into other apps through ecosystem mode
|
|
107
|
+
|
|
108
|
+
To setup Privy you need to add the following parameters:
|
|
83
109
|
|
|
84
110
|
```typescript
|
|
85
111
|
import { VechainKitProvider } from '@vechain/vechain-kit';
|
|
@@ -103,6 +129,7 @@ export default function App({ Component, pageProps }: AppProps) {
|
|
|
103
129
|
allowPasskeyLinking: true,
|
|
104
130
|
}}
|
|
105
131
|
...
|
|
132
|
+
//other props
|
|
106
133
|
>
|
|
107
134
|
{children}
|
|
108
135
|
</VechainKitProvider>
|
|
@@ -110,83 +137,146 @@ export default function App({ Component, pageProps }: AppProps) {
|
|
|
110
137
|
}
|
|
111
138
|
```
|
|
112
139
|
|
|
113
|
-
|
|
140
|
+
Go to privy.io and create an app. You will find the APP ID and the CLIENT ID in the App Settings tab.
|
|
141
|
+
For further information on how to implement Privy SDK please refer to their [docs](https://docs.privy.io/)
|
|
114
142
|
|
|
115
|
-
|
|
143
|
+
This project uses:
|
|
116
144
|
|
|
117
|
-
|
|
118
|
-
|
|
145
|
+
- `@privy-io/react-auth` for Privy connection type
|
|
146
|
+
- `@privy-io/cross-app-connect` for ecosystem cross app connection
|
|
119
147
|
|
|
120
|
-
|
|
121
|
-
// 1. The VechainKit component uses browser-specific APIs that aren't available during server-side rendering
|
|
122
|
-
// 2. Code splitting - this component will only be loaded when needed, reducing initial bundle size
|
|
123
|
-
// 3. The 'ssr: false' option ensures this component is only rendered on the client side
|
|
124
|
-
const VechainKitProvider = dynamic(
|
|
125
|
-
async () => (await import('@vechain/vechain-kit')).VechainKitProvider,
|
|
126
|
-
{
|
|
127
|
-
ssr: false,
|
|
128
|
-
},
|
|
129
|
-
);
|
|
148
|
+
You can import privy from the kit as
|
|
130
149
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
</VechainKitProvider>
|
|
136
|
-
);
|
|
137
|
-
}
|
|
150
|
+
```typescript
|
|
151
|
+
import { usePrivy } from '@vechain/vechain-kit';
|
|
152
|
+
|
|
153
|
+
const { user } = usePrivy();
|
|
138
154
|
```
|
|
139
155
|
|
|
140
|
-
|
|
156
|
+
### Use the kit
|
|
157
|
+
|
|
158
|
+
Once you setup the kit provider and created your fee delegation service you are good to go and you can allow your users to login.
|
|
141
159
|
|
|
142
|
-
|
|
160
|
+
#### Wallet Button
|
|
143
161
|
|
|
144
|
-
|
|
162
|
+
You can use this component by importing it from the kit, it will handle for you the connection state and show a login button if the user is disconnected or the profile button when the user is connected.
|
|
145
163
|
|
|
146
164
|
```typescript
|
|
165
|
+
'use client';
|
|
166
|
+
|
|
147
167
|
import { WalletButton } from '@vechain/vechain-kit';
|
|
148
168
|
|
|
149
|
-
|
|
169
|
+
export function Page() {
|
|
170
|
+
return <WalletButton />;
|
|
171
|
+
}
|
|
150
172
|
```
|
|
151
173
|
|
|
152
|
-
|
|
174
|
+
#### Custom button
|
|
175
|
+
|
|
176
|
+
Alternatively, you can create your own custom button and invoke the connect modal or account modal based on your needs.
|
|
153
177
|
|
|
154
178
|
```typescript
|
|
155
|
-
|
|
179
|
+
'use client';
|
|
156
180
|
|
|
157
|
-
|
|
181
|
+
import {
|
|
182
|
+
useConnectModal,
|
|
183
|
+
useAccountModal,
|
|
184
|
+
useWallet,
|
|
185
|
+
} from '@vechain/vechain-kit';
|
|
158
186
|
|
|
159
|
-
|
|
160
|
-
|
|
187
|
+
export function Page() {
|
|
188
|
+
const { connection } = useWallet();
|
|
161
189
|
|
|
162
|
-
|
|
190
|
+
const {
|
|
191
|
+
open: openConnectModal,
|
|
192
|
+
close: closeConnectModal,
|
|
193
|
+
isOpen: isConnectModalOpen,
|
|
194
|
+
} = useConnectModal();
|
|
163
195
|
|
|
164
|
-
|
|
165
|
-
|
|
196
|
+
const {
|
|
197
|
+
open: openAccountModal,
|
|
198
|
+
close: openAccountModal,
|
|
199
|
+
isOpen: isAccountModalOpen,
|
|
200
|
+
} = useAccountModal();
|
|
201
|
+
|
|
202
|
+
if (!connection.isConnected) {
|
|
203
|
+
return <button onClick={openConnectModal}> Connect </button>;
|
|
204
|
+
}
|
|
166
205
|
|
|
167
|
-
|
|
206
|
+
return <button onClick={openAccountModal}> View Account </button>;
|
|
207
|
+
}
|
|
168
208
|
```
|
|
169
209
|
|
|
170
|
-
|
|
210
|
+
# Usage Guide
|
|
171
211
|
|
|
172
|
-
|
|
212
|
+
## Wallet Information
|
|
213
|
+
|
|
214
|
+
The `useWallet` hook provides a unified interface for managing wallet connections in a VeChain application, supporting multiple connection methods including social logins (via Privy), direct wallet connections (via DappKit), and cross-application connections.
|
|
215
|
+
This will be the hook you will use more frequently and it provides quite a few useful informations.
|
|
173
216
|
|
|
174
217
|
```typescript
|
|
175
|
-
import {
|
|
218
|
+
import { useWallet } from "@vechain/vechain-kit";
|
|
176
219
|
|
|
177
|
-
|
|
220
|
+
function MyComponent = () => {
|
|
221
|
+
const {
|
|
222
|
+
account,
|
|
223
|
+
connectedWallet,
|
|
224
|
+
smartAccount,
|
|
225
|
+
dappKitWallet,
|
|
226
|
+
embeddedWallet,
|
|
227
|
+
crossAppWallet,
|
|
228
|
+
privyUser,
|
|
229
|
+
connection,
|
|
230
|
+
disconnect
|
|
231
|
+
} = useWallet();
|
|
232
|
+
|
|
233
|
+
return <></>
|
|
234
|
+
}
|
|
178
235
|
```
|
|
179
236
|
|
|
180
|
-
|
|
237
|
+
## Transaction Management
|
|
238
|
+
|
|
239
|
+
You need to use the `useSendTransaction` hook to send transactions to the blockchain, all you will need is to build the clause and pass it to the hook.
|
|
240
|
+
|
|
241
|
+
This hook will take care of checking your connection type and handle the transaction submission between privy, cross-app and wallet connections.
|
|
242
|
+
When implementing VeChain Kit it is mandatory to use this hook to send transaction.
|
|
181
243
|
|
|
182
244
|
```typescript
|
|
245
|
+
'use client';
|
|
246
|
+
|
|
183
247
|
import {
|
|
184
|
-
|
|
248
|
+
useWallet,
|
|
185
249
|
useSendTransaction,
|
|
186
250
|
useTransactionModal,
|
|
251
|
+
TransactionModal,
|
|
252
|
+
getConfig
|
|
187
253
|
} from '@vechain/vechain-kit';
|
|
254
|
+
import { IB3TR__factory } from '@vechain/vechain-kit/contracts';
|
|
255
|
+
import { humanAddress } from '@vechain/vechain-kit/utils';
|
|
256
|
+
import { useMemo, useCallback } from 'react';
|
|
257
|
+
|
|
258
|
+
export function TransactionExamples() {
|
|
259
|
+
const { account } = useWallet();
|
|
260
|
+
const b3trMainnetAddress = getConfig("main").b3trContractAddress;
|
|
261
|
+
|
|
262
|
+
const clauses = useMemo(() => {
|
|
263
|
+
const B3TRInterface = IB3TR__factory.createInterface();
|
|
264
|
+
|
|
265
|
+
const clausesArray: any[] = [];
|
|
266
|
+
clausesArray.push({
|
|
267
|
+
to: b3trMainnetAddress,
|
|
268
|
+
value: '0x0',
|
|
269
|
+
data: B3TRInterface.encodeFunctionData('transfer', [
|
|
270
|
+
"0x0, // receiver address
|
|
271
|
+
'0', // 0 B3TR (in wei)
|
|
272
|
+
]),
|
|
273
|
+
comment: `This is a dummy transaction to test the transaction modal. Confirm to transfer ${0} B3TR to ${humanAddress("Ox0")}`,
|
|
274
|
+
abi: B3TRInterface.getFunction('transfer'),
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
return clausesArray;
|
|
278
|
+
}, [connectedWallet?.address]);
|
|
188
279
|
|
|
189
|
-
const MyComponent = () => {
|
|
190
280
|
const {
|
|
191
281
|
sendTransaction,
|
|
192
282
|
status,
|
|
@@ -196,7 +286,7 @@ const MyComponent = () => {
|
|
|
196
286
|
error,
|
|
197
287
|
progress,
|
|
198
288
|
} = useSendTransaction({
|
|
199
|
-
signerAccountAddress: account?.address,
|
|
289
|
+
signerAccountAddress: account?.address ?? '',
|
|
200
290
|
});
|
|
201
291
|
|
|
202
292
|
const {
|
|
@@ -205,55 +295,51 @@ const MyComponent = () => {
|
|
|
205
295
|
isOpen: isTransactionModalOpen,
|
|
206
296
|
} = useTransactionModal();
|
|
207
297
|
|
|
208
|
-
//
|
|
209
|
-
const
|
|
210
|
-
if (!connectedWallet.address) return [];
|
|
211
|
-
|
|
212
|
-
const clausesArray: any[] = [];
|
|
213
|
-
const abi = new Interface(b3trAbi);
|
|
214
|
-
clausesArray.push({
|
|
215
|
-
to: b3trMainnetAddress,
|
|
216
|
-
value: '0x0',
|
|
217
|
-
data: abi.encodeFunctionData('transfer', [
|
|
218
|
-
connectedWallet.address,
|
|
219
|
-
'0', // 1 B3TR (in wei)
|
|
220
|
-
]),
|
|
221
|
-
comment: `This is a dummy transaction to test the transaction modal. Confirm to transfer ${0} B3TR to ${humanAddress(
|
|
222
|
-
connectedWallet.address,
|
|
223
|
-
)}`,
|
|
224
|
-
abi: abi.getFunction('transfer'),
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
return clausesArray;
|
|
228
|
-
}, [connectedWallet.address]);
|
|
229
|
-
|
|
230
|
-
const handleTransactionWithModal = useCallback(async () => {
|
|
298
|
+
// This is the function triggering the transaction and opening the modal
|
|
299
|
+
const handleTransaction = useCallback(async () => {
|
|
231
300
|
openTransactionModal();
|
|
232
301
|
await sendTransaction(clauses);
|
|
233
|
-
}, [sendTransaction, clauses]);
|
|
302
|
+
}, [sendTransaction, clauses, openTransactionModal]);
|
|
234
303
|
|
|
235
304
|
return (
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
305
|
+
<>
|
|
306
|
+
<button
|
|
307
|
+
onClick={handleTransactionWithModal}
|
|
308
|
+
isLoading={isTransactionPending}
|
|
309
|
+
isDisabled={isTransactionPending}
|
|
310
|
+
>
|
|
311
|
+
Send B3TR
|
|
312
|
+
</button>
|
|
313
|
+
|
|
314
|
+
<TransactionModal
|
|
315
|
+
isOpen={isTransactionModalOpen}
|
|
316
|
+
onClose={closeTransactionModal}
|
|
317
|
+
status={status}
|
|
318
|
+
progress={progress}
|
|
319
|
+
txId={txReceipt?.meta.txID}
|
|
320
|
+
errorDescription={error?.reason ?? 'Unknown error'}
|
|
321
|
+
showSocialButtons={true}
|
|
322
|
+
showExplorerButton={true}
|
|
323
|
+
onTryAgain={handleTransactionWithModal}
|
|
324
|
+
showTryAgainButton={true}
|
|
325
|
+
/>
|
|
326
|
+
</>
|
|
243
327
|
);
|
|
244
|
-
}
|
|
328
|
+
}
|
|
245
329
|
```
|
|
246
330
|
|
|
247
331
|
You can also use `useSignMessage` and `useSignTypedData` hooks to sign messages and typed data.
|
|
248
332
|
|
|
249
333
|
## Blockchain Data Reading
|
|
250
334
|
|
|
251
|
-
The kit provides
|
|
335
|
+
The kit provides hooks for developers to interact with smart contracts like VeBetterDAO, VePassport, veDelegate, and price oracles. These hooks work with react-query, improving query capabilities by caching responses and offering real-time states like isLoading and isError. This helps developers manage and update user interfaces effectively, ensuring a responsive experience.
|
|
252
336
|
|
|
253
337
|
For example you can use `useGetB3trBalance` to get the balance of the user's wallet.
|
|
254
338
|
|
|
255
339
|
```typescript
|
|
256
340
|
import { useGetB3trBalance } from '@vechain/vechain-kit';
|
|
257
341
|
|
|
258
|
-
const { data: balance, isLoading, isError } = useGetB3trBalance();
|
|
342
|
+
const { data: balance, isLoading, isError } = useGetB3trBalance('0x.....');
|
|
343
|
+
|
|
344
|
+
console.log(balance.formatted, balance.original, balance.scaled);
|
|
259
345
|
```
|