@vechain/vechain-kit 1.2.1 → 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 +200 -117
- 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 +1 -1
package/README.md
CHANGED
|
@@ -3,23 +3,29 @@
|
|
|
3
3
|
<p>
|
|
4
4
|
<strong>An all-in-one library for building VeChain applications</strong>
|
|
5
5
|
</p>
|
|
6
|
+
<img src="https://i.ibb.co/k539SN7/kit-banner.png" alt="VeChain Kit Banner">
|
|
6
7
|
</div>
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
VeChain Kit is a comprehensive library designed to make building VeChain applications fast and straightforward.
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
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.
|
|
15
19
|
|
|
16
20
|
> **Note**: Currently supports React and Next.js only
|
|
17
21
|
|
|
18
|
-
📚 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/)
|
|
19
23
|
|
|
20
|
-
|
|
24
|
+
## Demo & Examples
|
|
21
25
|
|
|
22
|
-
|
|
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/)
|
|
23
29
|
|
|
24
30
|
# Installation
|
|
25
31
|
|
|
@@ -29,46 +35,28 @@ yarn add @tanstack/react-query@"^5.64.2" @chakra-ui/react@"^2.8.2" @vechain/dapp
|
|
|
29
35
|
|
|
30
36
|
# Quick Start
|
|
31
37
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
Import the `VechainKitProvider` provider and wrap your app in it.
|
|
38
|
+
### Define Provider
|
|
35
39
|
|
|
36
40
|
```typescript
|
|
37
|
-
|
|
41
|
+
'use client';
|
|
38
42
|
|
|
39
|
-
|
|
43
|
+
import DAppKitPrivyProvider from '@vechain/dapp-kit-react-privy'
|
|
44
|
+
|
|
45
|
+
export function DAppKitPrivyProvider({ children }: Props) {
|
|
40
46
|
return (
|
|
41
|
-
|
|
47
|
+
<VechainKitProvider
|
|
48
|
+
// Mandatory
|
|
42
49
|
feeDelegation={{
|
|
43
|
-
delegatorUrl: process.env.NEXT_PUBLIC_DELEGATOR_URL
|
|
44
|
-
delegateAllTransactions: true,
|
|
50
|
+
delegatorUrl: process.env.NEXT_PUBLIC_DELEGATOR_URL!
|
|
45
51
|
}}
|
|
46
52
|
dappKit={{
|
|
47
|
-
allowedWallets: ['veworld', '
|
|
48
|
-
walletConnectOptions: {
|
|
49
|
-
projectId:
|
|
50
|
-
process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID!,
|
|
51
|
-
metadata: {
|
|
52
|
-
name: 'Your App',
|
|
53
|
-
description: 'Your app description',
|
|
54
|
-
url:
|
|
55
|
-
typeof window !== 'undefined'
|
|
56
|
-
? window.location.origin
|
|
57
|
-
: '',
|
|
58
|
-
icons: [
|
|
59
|
-
typeof window !== 'undefined'
|
|
60
|
-
? `${window.location.origin}/images/logo/my-dapp.png`
|
|
61
|
-
: '',
|
|
62
|
-
],
|
|
63
|
-
},
|
|
64
|
-
},
|
|
53
|
+
allowedWallets: ['veworld', 'sync2'],
|
|
65
54
|
}}
|
|
55
|
+
privyEcosystemApps=[] // remove for using default ones
|
|
66
56
|
loginModalUI={{
|
|
67
|
-
|
|
68
|
-
description:
|
|
69
|
-
'Choose between social login through VeChain or by connecting your wallet.',
|
|
57
|
+
variant: 'vechain-wallet-ecosystem',
|
|
70
58
|
}}
|
|
71
|
-
darkMode={
|
|
59
|
+
darkMode={true}
|
|
72
60
|
language="en"
|
|
73
61
|
network={{
|
|
74
62
|
type: 'main',
|
|
@@ -80,9 +68,44 @@ export default function App({ Component, pageProps }: AppProps) {
|
|
|
80
68
|
}
|
|
81
69
|
```
|
|
82
70
|
|
|
83
|
-
|
|
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:
|
|
97
|
+
|
|
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
|
|
84
101
|
|
|
85
|
-
|
|
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:
|
|
86
109
|
|
|
87
110
|
```typescript
|
|
88
111
|
import { VechainKitProvider } from '@vechain/vechain-kit';
|
|
@@ -106,6 +129,7 @@ export default function App({ Component, pageProps }: AppProps) {
|
|
|
106
129
|
allowPasskeyLinking: true,
|
|
107
130
|
}}
|
|
108
131
|
...
|
|
132
|
+
//other props
|
|
109
133
|
>
|
|
110
134
|
{children}
|
|
111
135
|
</VechainKitProvider>
|
|
@@ -113,83 +137,146 @@ export default function App({ Component, pageProps }: AppProps) {
|
|
|
113
137
|
}
|
|
114
138
|
```
|
|
115
139
|
|
|
116
|
-
|
|
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/)
|
|
117
142
|
|
|
118
|
-
|
|
143
|
+
This project uses:
|
|
119
144
|
|
|
120
|
-
|
|
121
|
-
|
|
145
|
+
- `@privy-io/react-auth` for Privy connection type
|
|
146
|
+
- `@privy-io/cross-app-connect` for ecosystem cross app connection
|
|
122
147
|
|
|
123
|
-
|
|
124
|
-
// 1. The VechainKit component uses browser-specific APIs that aren't available during server-side rendering
|
|
125
|
-
// 2. Code splitting - this component will only be loaded when needed, reducing initial bundle size
|
|
126
|
-
// 3. The 'ssr: false' option ensures this component is only rendered on the client side
|
|
127
|
-
const VechainKitProvider = dynamic(
|
|
128
|
-
async () => (await import('@vechain/vechain-kit')).VechainKitProvider,
|
|
129
|
-
{
|
|
130
|
-
ssr: false,
|
|
131
|
-
},
|
|
132
|
-
);
|
|
148
|
+
You can import privy from the kit as
|
|
133
149
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
</VechainKitProvider>
|
|
139
|
-
);
|
|
140
|
-
}
|
|
150
|
+
```typescript
|
|
151
|
+
import { usePrivy } from '@vechain/vechain-kit';
|
|
152
|
+
|
|
153
|
+
const { user } = usePrivy();
|
|
141
154
|
```
|
|
142
155
|
|
|
143
|
-
|
|
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.
|
|
144
159
|
|
|
145
|
-
|
|
160
|
+
#### Wallet Button
|
|
146
161
|
|
|
147
|
-
|
|
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.
|
|
148
163
|
|
|
149
164
|
```typescript
|
|
165
|
+
'use client';
|
|
166
|
+
|
|
150
167
|
import { WalletButton } from '@vechain/vechain-kit';
|
|
151
168
|
|
|
152
|
-
|
|
169
|
+
export function Page() {
|
|
170
|
+
return <WalletButton />;
|
|
171
|
+
}
|
|
153
172
|
```
|
|
154
173
|
|
|
155
|
-
|
|
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.
|
|
156
177
|
|
|
157
178
|
```typescript
|
|
158
|
-
|
|
179
|
+
'use client';
|
|
180
|
+
|
|
181
|
+
import {
|
|
182
|
+
useConnectModal,
|
|
183
|
+
useAccountModal,
|
|
184
|
+
useWallet,
|
|
185
|
+
} from '@vechain/vechain-kit';
|
|
159
186
|
|
|
160
|
-
|
|
187
|
+
export function Page() {
|
|
188
|
+
const { connection } = useWallet();
|
|
161
189
|
|
|
162
|
-
|
|
163
|
-
|
|
190
|
+
const {
|
|
191
|
+
open: openConnectModal,
|
|
192
|
+
close: closeConnectModal,
|
|
193
|
+
isOpen: isConnectModalOpen,
|
|
194
|
+
} = useConnectModal();
|
|
164
195
|
|
|
165
|
-
|
|
196
|
+
const {
|
|
197
|
+
open: openAccountModal,
|
|
198
|
+
close: openAccountModal,
|
|
199
|
+
isOpen: isAccountModalOpen,
|
|
200
|
+
} = useAccountModal();
|
|
166
201
|
|
|
167
|
-
|
|
168
|
-
|
|
202
|
+
if (!connection.isConnected) {
|
|
203
|
+
return <button onClick={openConnectModal}> Connect </button>;
|
|
204
|
+
}
|
|
169
205
|
|
|
170
|
-
|
|
206
|
+
return <button onClick={openAccountModal}> View Account </button>;
|
|
207
|
+
}
|
|
171
208
|
```
|
|
172
209
|
|
|
173
|
-
|
|
210
|
+
# Usage Guide
|
|
174
211
|
|
|
175
|
-
|
|
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.
|
|
176
216
|
|
|
177
217
|
```typescript
|
|
178
|
-
import {
|
|
218
|
+
import { useWallet } from "@vechain/vechain-kit";
|
|
179
219
|
|
|
180
|
-
|
|
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
|
+
}
|
|
181
235
|
```
|
|
182
236
|
|
|
183
|
-
|
|
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.
|
|
184
243
|
|
|
185
244
|
```typescript
|
|
245
|
+
'use client';
|
|
246
|
+
|
|
186
247
|
import {
|
|
187
|
-
|
|
248
|
+
useWallet,
|
|
188
249
|
useSendTransaction,
|
|
189
250
|
useTransactionModal,
|
|
251
|
+
TransactionModal,
|
|
252
|
+
getConfig
|
|
190
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]);
|
|
191
279
|
|
|
192
|
-
const MyComponent = () => {
|
|
193
280
|
const {
|
|
194
281
|
sendTransaction,
|
|
195
282
|
status,
|
|
@@ -199,7 +286,7 @@ const MyComponent = () => {
|
|
|
199
286
|
error,
|
|
200
287
|
progress,
|
|
201
288
|
} = useSendTransaction({
|
|
202
|
-
signerAccountAddress: account?.address,
|
|
289
|
+
signerAccountAddress: account?.address ?? '',
|
|
203
290
|
});
|
|
204
291
|
|
|
205
292
|
const {
|
|
@@ -208,55 +295,51 @@ const MyComponent = () => {
|
|
|
208
295
|
isOpen: isTransactionModalOpen,
|
|
209
296
|
} = useTransactionModal();
|
|
210
297
|
|
|
211
|
-
//
|
|
212
|
-
const
|
|
213
|
-
if (!connectedWallet.address) return [];
|
|
214
|
-
|
|
215
|
-
const clausesArray: any[] = [];
|
|
216
|
-
const abi = new Interface(b3trAbi);
|
|
217
|
-
clausesArray.push({
|
|
218
|
-
to: b3trMainnetAddress,
|
|
219
|
-
value: '0x0',
|
|
220
|
-
data: abi.encodeFunctionData('transfer', [
|
|
221
|
-
connectedWallet.address,
|
|
222
|
-
'0', // 1 B3TR (in wei)
|
|
223
|
-
]),
|
|
224
|
-
comment: `This is a dummy transaction to test the transaction modal. Confirm to transfer ${0} B3TR to ${humanAddress(
|
|
225
|
-
connectedWallet.address,
|
|
226
|
-
)}`,
|
|
227
|
-
abi: abi.getFunction('transfer'),
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
return clausesArray;
|
|
231
|
-
}, [connectedWallet.address]);
|
|
232
|
-
|
|
233
|
-
const handleTransactionWithModal = useCallback(async () => {
|
|
298
|
+
// This is the function triggering the transaction and opening the modal
|
|
299
|
+
const handleTransaction = useCallback(async () => {
|
|
234
300
|
openTransactionModal();
|
|
235
301
|
await sendTransaction(clauses);
|
|
236
|
-
}, [sendTransaction, clauses]);
|
|
302
|
+
}, [sendTransaction, clauses, openTransactionModal]);
|
|
237
303
|
|
|
238
304
|
return (
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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
|
+
</>
|
|
246
327
|
);
|
|
247
|
-
}
|
|
328
|
+
}
|
|
248
329
|
```
|
|
249
330
|
|
|
250
331
|
You can also use `useSignMessage` and `useSignTypedData` hooks to sign messages and typed data.
|
|
251
332
|
|
|
252
333
|
## Blockchain Data Reading
|
|
253
334
|
|
|
254
|
-
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.
|
|
255
336
|
|
|
256
337
|
For example you can use `useGetB3trBalance` to get the balance of the user's wallet.
|
|
257
338
|
|
|
258
339
|
```typescript
|
|
259
340
|
import { useGetB3trBalance } from '@vechain/vechain-kit';
|
|
260
341
|
|
|
261
|
-
const { data: balance, isLoading, isError } = useGetB3trBalance();
|
|
342
|
+
const { data: balance, isLoading, isError } = useGetB3trBalance('0x.....');
|
|
343
|
+
|
|
344
|
+
console.log(balance.formatted, balance.original, balance.scaled);
|
|
262
345
|
```
|