@mysten/dapp-kit-react 2.0.2 → 2.0.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/dist/index.d.mts +9 -9
- package/docs/actions/connect-wallet.md +69 -0
- package/docs/actions/disconnect-wallet.md +39 -0
- package/docs/actions/sign-and-execute-transaction.md +96 -0
- package/docs/actions/sign-personal-message.md +58 -0
- package/docs/actions/sign-transaction.md +65 -0
- package/docs/actions/switch-account.md +37 -0
- package/docs/actions/switch-network.md +37 -0
- package/docs/dapp-kit-instance.md +163 -0
- package/docs/getting-started/create-dapp.md +151 -0
- package/docs/getting-started/next-js.md +163 -0
- package/docs/getting-started/react.md +172 -0
- package/docs/getting-started/vue.md +193 -0
- package/docs/index.md +70 -0
- package/docs/llms-index.md +33 -0
- package/docs/react/components/connect-button.md +42 -0
- package/docs/react/components/connect-modal.md +51 -0
- package/docs/react/components/index.md +13 -0
- package/docs/react/dapp-kit-provider.md +86 -0
- package/docs/react/hooks/index.md +26 -0
- package/docs/react/hooks/use-current-account.md +33 -0
- package/docs/react/hooks/use-current-client.md +36 -0
- package/docs/react/hooks/use-current-network.md +28 -0
- package/docs/react/hooks/use-current-wallet.md +36 -0
- package/docs/react/hooks/use-dapp-kit.md +100 -0
- package/docs/react/hooks/use-wallet-connection.md +48 -0
- package/docs/react/hooks/use-wallets.md +33 -0
- package/docs/state.md +169 -0
- package/docs/theming.md +196 -0
- package/docs/web-components/connect-button.md +89 -0
- package/docs/web-components/connect-modal.md +177 -0
- package/package.json +2 -2
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# @mysten/create-dapp
|
|
2
|
+
|
|
3
|
+
> Create a Sui app with one command using the create-dapp CLI scaffolding tool.
|
|
4
|
+
|
|
5
|
+
`@mysten/create-dapp` is a CLI tool that scaffolds a complete Sui app project with best practices
|
|
6
|
+
built in.
|
|
7
|
+
|
|
8
|
+
## Quick start
|
|
9
|
+
|
|
10
|
+
```sh npm2yarn
|
|
11
|
+
npm create @mysten/dapp
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
The CLI prompts you to choose a template and project name, then creates a ready-to-run project.
|
|
15
|
+
|
|
16
|
+
## Templates
|
|
17
|
+
|
|
18
|
+
### react-client-dapp
|
|
19
|
+
|
|
20
|
+
A basic React app that demonstrates wallet connection and fetching owned objects:
|
|
21
|
+
|
|
22
|
+
- Wallet connection with `ConnectButton`
|
|
23
|
+
- Display connected wallet address
|
|
24
|
+
- List objects owned by the connected wallet
|
|
25
|
+
- Clean, modern UI with Tailwind CSS
|
|
26
|
+
|
|
27
|
+
### react-e2e-counter
|
|
28
|
+
|
|
29
|
+
A complete end-to-end example with Move smart contract integration:
|
|
30
|
+
|
|
31
|
+
- Move counter contract in `move/counter/`
|
|
32
|
+
- TypeScript bindings generated with `@mysten/codegen`
|
|
33
|
+
- Create, increment, and reset counters
|
|
34
|
+
- MVR (Move Registry) name resolution for package IDs
|
|
35
|
+
- Full transaction signing and execution
|
|
36
|
+
|
|
37
|
+
## Tech stack
|
|
38
|
+
|
|
39
|
+
Both templates include:
|
|
40
|
+
|
|
41
|
+
- [React](https://react.dev/) - UI framework
|
|
42
|
+
- [TypeScript](https://www.typescriptlang.org/) - Type safety
|
|
43
|
+
- [Vite](https://vitejs.dev/) - Fast build tooling
|
|
44
|
+
- [Tailwind CSS v4](https://tailwindcss.com/) - Utility-first styling
|
|
45
|
+
- [Lucide React](https://lucide.dev/) - Modern icons
|
|
46
|
+
- [`@mysten/dapp-kit-react`](/dapp-kit) - Wallet connection and blockchain interaction
|
|
47
|
+
- [pnpm](https://pnpm.io/) - Package management
|
|
48
|
+
|
|
49
|
+
The e2e-counter template also includes:
|
|
50
|
+
|
|
51
|
+
- [`@mysten/codegen`](/codegen) - Generate TypeScript from Move code
|
|
52
|
+
|
|
53
|
+
## Project structure
|
|
54
|
+
|
|
55
|
+
### react-client-dapp
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
src/
|
|
59
|
+
├── components/ui/ # Reusable UI components (Card)
|
|
60
|
+
├── lib/utils.ts # Utility functions
|
|
61
|
+
├── App.tsx # Main application
|
|
62
|
+
├── WalletStatus.tsx # Wallet connection display
|
|
63
|
+
├── OwnedObjects.tsx # Object list component
|
|
64
|
+
├── dApp-kit.ts # dApp Kit configuration
|
|
65
|
+
└── index.css # Tailwind CSS theme
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### react-e2e-counter
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
src/
|
|
72
|
+
├── components/ui/ # UI components (Button, Card)
|
|
73
|
+
├── contracts/ # Generated TypeScript bindings
|
|
74
|
+
├── lib/utils.ts # Utility functions
|
|
75
|
+
├── App.tsx # Main application
|
|
76
|
+
├── Counter.tsx # Counter display and controls
|
|
77
|
+
├── CreateCounter.tsx # Counter creation
|
|
78
|
+
├── dApp-kit.ts # dApp Kit configuration
|
|
79
|
+
├── constants.ts # Package IDs per network
|
|
80
|
+
└── index.css # Tailwind CSS theme
|
|
81
|
+
move/
|
|
82
|
+
└── counter/ # Move smart contract
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Using the e2e-counter template
|
|
86
|
+
|
|
87
|
+
### Step 1: Deploy the Move contract
|
|
88
|
+
|
|
89
|
+
First, install the [Sui CLI](https://docs.sui.io/build/install) and set up testnet:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
sui client new-env --alias testnet --rpc https://fullnode.testnet.sui.io:443
|
|
93
|
+
sui client switch --env testnet
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Get testnet SUI from the [faucet](https://faucet.sui.io), then publish:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
cd move
|
|
100
|
+
sui client publish --gas-budget 100000000 counter
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Step 2: Configure the package ID
|
|
104
|
+
|
|
105
|
+
Copy the `packageId` from the publish output to `src/constants.ts`:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Step 3: Generate TypeScript bindings
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
pnpm codegen
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
This generates type-safe functions in `src/contracts/` for interacting with your Move modules.
|
|
118
|
+
|
|
119
|
+
### Step 4: Start development
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
pnpm install
|
|
123
|
+
pnpm dev
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Customizing the UI
|
|
127
|
+
|
|
128
|
+
The templates use [Tailwind CSS v4](https://tailwindcss.com/docs) with
|
|
129
|
+
[shadcn/ui](https://ui.shadcn.com/)-style components. The UI components in `src/components/ui/` can
|
|
130
|
+
be customized or extended.
|
|
131
|
+
|
|
132
|
+
To add more components, copy them from the
|
|
133
|
+
[shadcn/ui components](https://ui.shadcn.com/docs/components) documentation and adapt them to your
|
|
134
|
+
project.
|
|
135
|
+
|
|
136
|
+
Theme variables are defined in `src/index.css` using Tailwind's `@theme` directive.
|
|
137
|
+
|
|
138
|
+
## CLI options
|
|
139
|
+
|
|
140
|
+
You can skip prompts with command-line flags:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
# Create with specific template
|
|
144
|
+
npm create @mysten/dapp -- -t react-e2e-counter
|
|
145
|
+
|
|
146
|
+
# Create with specific name
|
|
147
|
+
npm create @mysten/dapp -- -n my-app
|
|
148
|
+
|
|
149
|
+
# Create with both
|
|
150
|
+
npm create @mysten/dapp -- -t react-client-dapp -n my-app
|
|
151
|
+
```
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# Next.js
|
|
2
|
+
|
|
3
|
+
> Set up dApp Kit in a Next.js application.
|
|
4
|
+
|
|
5
|
+
This guide covers integrating dApp Kit into Next.js applications, including handling server-side
|
|
6
|
+
rendering (SSR).
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```sh npm2yarn
|
|
11
|
+
npm i @mysten/dapp-kit-react @mysten/sui
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Setup
|
|
15
|
+
|
|
16
|
+
### Step 1: Create a dApp Kit instance
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
// app/dapp-kit.ts
|
|
20
|
+
|
|
21
|
+
const GRPC_URLS = {
|
|
22
|
+
testnet: 'https://fullnode.testnet.sui.io:443',
|
|
23
|
+
} as const;
|
|
24
|
+
|
|
25
|
+
networks: ['testnet'],
|
|
26
|
+
createClient: (network) => new SuiGrpcClient({ network, baseUrl: GRPC_URLS[network] }),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Register types for hook type inference
|
|
30
|
+
declare module '@mysten/dapp-kit-react' {
|
|
31
|
+
interface Register {
|
|
32
|
+
dAppKit: typeof dAppKit;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Step 2: Create a client-only wrapper
|
|
38
|
+
|
|
39
|
+
Wallet detection only works in the browser, so dApp Kit components must be client-side rendered:
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
// app/DAppKitClientProvider.tsx
|
|
43
|
+
'use client';
|
|
44
|
+
|
|
45
|
+
return <DAppKitProvider dAppKit={dAppKit}>{children}</DAppKitProvider>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Step 3: Use dynamic import in pages
|
|
51
|
+
|
|
52
|
+
Use Next.js dynamic imports with `ssr: false` to prevent server-side rendering:
|
|
53
|
+
|
|
54
|
+
```tsx
|
|
55
|
+
// app/page.tsx
|
|
56
|
+
|
|
57
|
+
const DAppKitClientProvider = dynamic(
|
|
58
|
+
() => import('./DAppKitClientProvider').then((mod) => mod.DAppKitClientProvider),
|
|
59
|
+
{ ssr: false },
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
const ConnectButton = dynamic(
|
|
63
|
+
() => import('./DAppKitClientProvider').then((mod) => mod.ConnectButton),
|
|
64
|
+
{ ssr: false, loading: () => <button disabled>Loading...</button> },
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<DAppKitClientProvider>
|
|
69
|
+
<main>
|
|
70
|
+
<h1>My Sui dApp</h1>
|
|
71
|
+
<ConnectButton />
|
|
72
|
+
</main>
|
|
73
|
+
</DAppKitClientProvider>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Alternative: single client component
|
|
79
|
+
|
|
80
|
+
For simpler apps, create a single client component:
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
// app/WalletApp.tsx
|
|
84
|
+
'use client';
|
|
85
|
+
|
|
86
|
+
function WalletStatus() {
|
|
87
|
+
const account = useCurrentAccount();
|
|
88
|
+
const dAppKit = useDAppKit();
|
|
89
|
+
|
|
90
|
+
if (!account) {
|
|
91
|
+
return <p>Connect your wallet to get started</p>;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function handleTransfer() {
|
|
95
|
+
const tx = new Transaction();
|
|
96
|
+
tx.transferObjects([coinWithBalance({ balance: 1_000_000 })], account.address);
|
|
97
|
+
const result = await dAppKit.signAndExecuteTransaction({ transaction: tx });
|
|
98
|
+
|
|
99
|
+
if (result.FailedTransaction) {
|
|
100
|
+
throw new Error(`Transaction failed: ${result.FailedTransaction.status.error?.message}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
alert(`Transaction: ${result.Transaction.digest}`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return (
|
|
107
|
+
<div>
|
|
108
|
+
<p>Connected: {account.address}</p>
|
|
109
|
+
<button onClick={handleTransfer}>Send Transaction</button>
|
|
110
|
+
</div>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return (
|
|
115
|
+
<DAppKitProvider dAppKit={dAppKit}>
|
|
116
|
+
<ConnectButton />
|
|
117
|
+
<WalletStatus />
|
|
118
|
+
</DAppKitProvider>
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
```tsx
|
|
124
|
+
// app/page.tsx
|
|
125
|
+
|
|
126
|
+
const WalletApp = dynamic(() => import('./WalletApp'), {
|
|
127
|
+
ssr: false,
|
|
128
|
+
loading: () => <p>Loading wallet...</p>,
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
return (
|
|
132
|
+
<main>
|
|
133
|
+
<h1>My Sui dApp</h1>
|
|
134
|
+
<WalletApp />
|
|
135
|
+
</main>
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Key considerations
|
|
141
|
+
|
|
142
|
+
### Why SSR: false?
|
|
143
|
+
|
|
144
|
+
Wallets are detected through the browser's `window` object and the Wallet Standard API. This
|
|
145
|
+
detection cannot happen on the server, so components that interact with wallets must be client-side
|
|
146
|
+
only.
|
|
147
|
+
|
|
148
|
+
### Hydration errors
|
|
149
|
+
|
|
150
|
+
If you see hydration errors, ensure all dApp Kit components are wrapped with `ssr: false` dynamic
|
|
151
|
+
imports or are inside a `'use client'` component that's dynamically imported.
|
|
152
|
+
|
|
153
|
+
## Example application
|
|
154
|
+
|
|
155
|
+
See the complete
|
|
156
|
+
[Next.js example](https://github.com/MystenLabs/ts-sdks/tree/main/packages/dapp-kit/examples/next-js/simple)
|
|
157
|
+
on GitHub.
|
|
158
|
+
|
|
159
|
+
## Next steps
|
|
160
|
+
|
|
161
|
+
- [React Hooks](/dapp-kit/react/hooks) - All available hooks
|
|
162
|
+
- [React Components](/dapp-kit/react/components) - UI components
|
|
163
|
+
- [Theming](/dapp-kit/theming) - Customize appearance
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# React
|
|
2
|
+
|
|
3
|
+
> Set up dApp Kit in a React application.
|
|
4
|
+
|
|
5
|
+
This guide walks you through integrating dApp Kit into a React application.
|
|
6
|
+
|
|
7
|
+
## Quick start with create-dapp
|
|
8
|
+
|
|
9
|
+
The fastest way to get started is using our CLI tool:
|
|
10
|
+
|
|
11
|
+
```sh npm2yarn
|
|
12
|
+
npm create @mysten/dapp
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This creates a new project with:
|
|
16
|
+
|
|
17
|
+
- React + TypeScript + Vite
|
|
18
|
+
- Tailwind CSS v4 for styling
|
|
19
|
+
- dApp Kit pre-configured
|
|
20
|
+
- Example wallet connection UI
|
|
21
|
+
|
|
22
|
+
Choose from two templates:
|
|
23
|
+
|
|
24
|
+
- **react-client-dapp**: Basic wallet connection and object display
|
|
25
|
+
- **react-e2e-counter**: Full example with Move smart contract and codegen
|
|
26
|
+
|
|
27
|
+
See the [create-dapp guide](/dapp-kit/getting-started/create-dapp) for more details.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Manual installation
|
|
32
|
+
|
|
33
|
+
```sh npm2yarn
|
|
34
|
+
npm i @mysten/dapp-kit-react @mysten/sui
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Setup
|
|
38
|
+
|
|
39
|
+
### Step 1: Create a dApp Kit instance
|
|
40
|
+
|
|
41
|
+
Create a file to configure your dApp Kit instance:
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
// dapp-kit.ts
|
|
45
|
+
|
|
46
|
+
const GRPC_URLS = {
|
|
47
|
+
testnet: 'https://fullnode.testnet.sui.io:443',
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
networks: ['testnet'],
|
|
51
|
+
createClient: (network) => new SuiGrpcClient({ network, baseUrl: GRPC_URLS[network] }),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Register types for hook type inference
|
|
55
|
+
declare module '@mysten/dapp-kit-react' {
|
|
56
|
+
interface Register {
|
|
57
|
+
dAppKit: typeof dAppKit;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Step 2: Add the provider
|
|
63
|
+
|
|
64
|
+
Wrap your application with `DAppKitProvider`:
|
|
65
|
+
|
|
66
|
+
```tsx
|
|
67
|
+
// App.tsx
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
<DAppKitProvider dAppKit={dAppKit}>
|
|
71
|
+
<div>
|
|
72
|
+
<h1>My Sui dApp</h1>
|
|
73
|
+
<ConnectButton />
|
|
74
|
+
<WalletStatus />
|
|
75
|
+
</div>
|
|
76
|
+
</DAppKitProvider>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Step 3: Display connection status
|
|
82
|
+
|
|
83
|
+
Use hooks to access wallet state:
|
|
84
|
+
|
|
85
|
+
```tsx
|
|
86
|
+
|
|
87
|
+
function WalletStatus() {
|
|
88
|
+
const account = useCurrentAccount();
|
|
89
|
+
const wallet = useCurrentWallet();
|
|
90
|
+
const network = useCurrentNetwork();
|
|
91
|
+
|
|
92
|
+
if (!account) {
|
|
93
|
+
return <p>Connect your wallet to get started</p>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return (
|
|
97
|
+
<div>
|
|
98
|
+
<p>Wallet: {wallet?.name}</p>
|
|
99
|
+
<p>Address: {account.address}</p>
|
|
100
|
+
<p>Network: {network}</p>
|
|
101
|
+
</div>
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Step 4: Execute transactions
|
|
107
|
+
|
|
108
|
+
Use the `useDAppKit` hook to sign and execute transactions:
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
|
|
112
|
+
function TransferButton() {
|
|
113
|
+
const dAppKit = useDAppKit();
|
|
114
|
+
const account = useCurrentAccount();
|
|
115
|
+
|
|
116
|
+
async function handleTransfer() {
|
|
117
|
+
const tx = new Transaction();
|
|
118
|
+
tx.transferObjects([coinWithBalance({ balance: 1_000_000 })], 'RECIPIENT_ADDRESS');
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
const result = await dAppKit.signAndExecuteTransaction({ transaction: tx });
|
|
122
|
+
|
|
123
|
+
if (result.FailedTransaction) {
|
|
124
|
+
throw new Error(`Transaction failed: ${result.FailedTransaction.status.error?.message}`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
console.log('Transaction digest:', result.Transaction.digest);
|
|
128
|
+
} catch (error) {
|
|
129
|
+
console.error('Transaction failed:', error);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<button onClick={handleTransfer} disabled={!account}>
|
|
135
|
+
Send Transaction
|
|
136
|
+
</button>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Available hooks
|
|
142
|
+
|
|
143
|
+
| Hook | Description |
|
|
144
|
+
| -------------------------------------------------------------------- | ---------------------------------------- |
|
|
145
|
+
| [`useDAppKit`](/dapp-kit/react/hooks/use-dapp-kit) | Access the dApp Kit instance and actions |
|
|
146
|
+
| [`useWalletConnection`](/dapp-kit/react/hooks/use-wallet-connection) | Full connection state with status flags |
|
|
147
|
+
| [`useCurrentAccount`](/dapp-kit/react/hooks/use-current-account) | Currently selected account |
|
|
148
|
+
| [`useCurrentWallet`](/dapp-kit/react/hooks/use-current-wallet) | Currently connected wallet |
|
|
149
|
+
| [`useCurrentNetwork`](/dapp-kit/react/hooks/use-current-network) | Current network |
|
|
150
|
+
| [`useCurrentClient`](/dapp-kit/react/hooks/use-current-client) | SuiClient for current network |
|
|
151
|
+
| [`useWallets`](/dapp-kit/react/hooks/use-wallets) | List of available wallets |
|
|
152
|
+
|
|
153
|
+
## Components
|
|
154
|
+
|
|
155
|
+
| Component | Description |
|
|
156
|
+
| ------------------------------------------------------------ | --------------------------------- |
|
|
157
|
+
| [`ConnectButton`](/dapp-kit/react/components/connect-button) | Complete wallet connection UI |
|
|
158
|
+
| [`ConnectModal`](/dapp-kit/react/components/connect-modal) | Standalone wallet selection modal |
|
|
159
|
+
| [`DAppKitProvider`](/dapp-kit/react/dapp-kit-provider) | Context provider for hooks |
|
|
160
|
+
|
|
161
|
+
## Example application
|
|
162
|
+
|
|
163
|
+
See the complete
|
|
164
|
+
[React example](https://github.com/MystenLabs/ts-sdks/tree/main/packages/dapp-kit/examples/react/simple)
|
|
165
|
+
on GitHub.
|
|
166
|
+
|
|
167
|
+
## Next steps
|
|
168
|
+
|
|
169
|
+
- [`DAppKitProvider`](/dapp-kit/react/dapp-kit-provider) - Provider configuration
|
|
170
|
+
- [Hooks Reference](/dapp-kit/react/hooks) - All available hooks
|
|
171
|
+
- [Components](/dapp-kit/react/components) - UI components
|
|
172
|
+
- [Theming](/dapp-kit/theming) - Customize appearance
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# Vue
|
|
2
|
+
|
|
3
|
+
> Set up dApp Kit in a Vue application.
|
|
4
|
+
|
|
5
|
+
This guide walks you through integrating dApp Kit into a Vue application using Web Components and
|
|
6
|
+
direct store access.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```sh npm2yarn
|
|
11
|
+
npm i @mysten/dapp-kit-core @mysten/sui @nanostores/vue
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Setup
|
|
15
|
+
|
|
16
|
+
### Step 1: Create a dApp Kit instance
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
// src/dapp-kit.ts
|
|
20
|
+
|
|
21
|
+
const GRPC_URLS = {
|
|
22
|
+
testnet: 'https://fullnode.testnet.sui.io:443',
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
networks: ['testnet'],
|
|
26
|
+
createClient: (network) => new SuiGrpcClient({ network, baseUrl: GRPC_URLS[network] }),
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Step 2: Register web components
|
|
31
|
+
|
|
32
|
+
Import the web components in your app entry point:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
// src/main.ts
|
|
36
|
+
|
|
37
|
+
createApp(App).mount('#app');
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Step 3: Use the connect button
|
|
41
|
+
|
|
42
|
+
```vue
|
|
43
|
+
<script setup lang="ts">
|
|
44
|
+
|
|
45
|
+
</script>
|
|
46
|
+
|
|
47
|
+
<template>
|
|
48
|
+
<div>
|
|
49
|
+
<h1>My Sui dApp</h1>
|
|
50
|
+
<mysten-dapp-kit-connect-button :instance="dAppKit" />
|
|
51
|
+
</div>
|
|
52
|
+
</template>
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Step 4: Access connection state
|
|
56
|
+
|
|
57
|
+
Use `@nanostores/vue` for reactive state:
|
|
58
|
+
|
|
59
|
+
```vue
|
|
60
|
+
<script setup lang="ts">
|
|
61
|
+
|
|
62
|
+
const connection = useStore(dAppKit.stores.$connection);
|
|
63
|
+
const network = useStore(dAppKit.stores.$currentNetwork);
|
|
64
|
+
</script>
|
|
65
|
+
|
|
66
|
+
<template>
|
|
67
|
+
<div>
|
|
68
|
+
<mysten-dapp-kit-connect-button :instance="dAppKit" />
|
|
69
|
+
|
|
70
|
+
<div v-if="connection.account">
|
|
71
|
+
<p>Wallet: {{ connection.wallet?.name }}</p>
|
|
72
|
+
<p>Address: {{ connection.account.address }}</p>
|
|
73
|
+
<p>Network: {{ network }}</p>
|
|
74
|
+
</div>
|
|
75
|
+
<p v-else>Connect your wallet to get started</p>
|
|
76
|
+
</div>
|
|
77
|
+
</template>
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Step 5: Execute transactions
|
|
81
|
+
|
|
82
|
+
```vue
|
|
83
|
+
<script setup lang="ts">
|
|
84
|
+
|
|
85
|
+
const connection = useStore(dAppKit.stores.$connection);
|
|
86
|
+
|
|
87
|
+
async function handleTransfer() {
|
|
88
|
+
const tx = new Transaction();
|
|
89
|
+
tx.transferObjects([coinWithBalance({ balance: 1_000_000 })], connection.value.account!.address);
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
const result = await dAppKit.signAndExecuteTransaction({ transaction: tx });
|
|
93
|
+
|
|
94
|
+
if (result.FailedTransaction) {
|
|
95
|
+
throw new Error(`Transaction failed: ${result.FailedTransaction.status.error?.message}`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
alert(`Transaction: ${result.Transaction.digest}`);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
console.error('Transaction failed:', error);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
</script>
|
|
104
|
+
|
|
105
|
+
<template>
|
|
106
|
+
<div>
|
|
107
|
+
<mysten-dapp-kit-connect-button :instance="dAppKit" />
|
|
108
|
+
|
|
109
|
+
<button v-if="connection.account" @click="handleTransfer">Send Transaction</button>
|
|
110
|
+
</div>
|
|
111
|
+
</template>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Complete example
|
|
115
|
+
|
|
116
|
+
```vue
|
|
117
|
+
<script setup lang="ts">
|
|
118
|
+
|
|
119
|
+
const connection = useStore(dAppKit.stores.$connection);
|
|
120
|
+
const network = useStore(dAppKit.stores.$currentNetwork);
|
|
121
|
+
|
|
122
|
+
async function handleTransfer() {
|
|
123
|
+
if (!connection.value.account) return;
|
|
124
|
+
|
|
125
|
+
const tx = new Transaction();
|
|
126
|
+
tx.transferObjects([coinWithBalance({ balance: 1_000_000 })], connection.value.account.address);
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
const result = await dAppKit.signAndExecuteTransaction({ transaction: tx });
|
|
130
|
+
|
|
131
|
+
if (result.FailedTransaction) {
|
|
132
|
+
throw new Error(`Transaction failed: ${result.FailedTransaction.status.error?.message}`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
alert(`Transaction successful: ${result.Transaction.digest}`);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
console.error('Transaction failed:', error);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
</script>
|
|
141
|
+
|
|
142
|
+
<template>
|
|
143
|
+
<div class="app">
|
|
144
|
+
<h1>My Sui dApp</h1>
|
|
145
|
+
<mysten-dapp-kit-connect-button :instance="dAppKit" />
|
|
146
|
+
|
|
147
|
+
<div v-if="connection.account" class="wallet-info">
|
|
148
|
+
<p>Wallet: {{ connection.wallet?.name }}</p>
|
|
149
|
+
<p>Address: {{ connection.account.address }}</p>
|
|
150
|
+
<p>Network: {{ network }}</p>
|
|
151
|
+
<button @click="handleTransfer">Send Test Transaction</button>
|
|
152
|
+
</div>
|
|
153
|
+
<p v-else>Connect your wallet to get started</p>
|
|
154
|
+
</div>
|
|
155
|
+
</template>
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Web components
|
|
159
|
+
|
|
160
|
+
Vue works with dApp Kit's Web Components through property binding:
|
|
161
|
+
|
|
162
|
+
| Component | Usage |
|
|
163
|
+
| -------------- | ---------------------------------------------------------------------- |
|
|
164
|
+
| Connect Button | `<mysten-dapp-kit-connect-button :instance="dAppKit" />` |
|
|
165
|
+
| Connect Modal | `<mysten-dapp-kit-connect-modal :instance="dAppKit" :open="isOpen" />` |
|
|
166
|
+
|
|
167
|
+
See [Web Components](/dapp-kit/web-components/connect-button) for full documentation.
|
|
168
|
+
|
|
169
|
+
## Available stores
|
|
170
|
+
|
|
171
|
+
Access these stores through `dAppKit.stores`:
|
|
172
|
+
|
|
173
|
+
| Store | Description |
|
|
174
|
+
| ----------------- | ------------------------------------------------- |
|
|
175
|
+
| `$connection` | Connection state with wallet, account, and status |
|
|
176
|
+
| `$currentNetwork` | Current network string |
|
|
177
|
+
| `$currentClient` | SuiClient for current network |
|
|
178
|
+
| `$wallets` | List of available wallets |
|
|
179
|
+
|
|
180
|
+
See [State Management](/dapp-kit/state) for full documentation.
|
|
181
|
+
|
|
182
|
+
## Example application
|
|
183
|
+
|
|
184
|
+
See the complete
|
|
185
|
+
[Vue example](https://github.com/MystenLabs/ts-sdks/tree/main/packages/dapp-kit/examples/vue/simple)
|
|
186
|
+
on GitHub.
|
|
187
|
+
|
|
188
|
+
## Next steps
|
|
189
|
+
|
|
190
|
+
- [Web Components](/dapp-kit/web-components/connect-button) - Component documentation
|
|
191
|
+
- [State Management](/dapp-kit/state) - Store details
|
|
192
|
+
- [Actions](/dapp-kit/actions/connect-wallet) - All available actions
|
|
193
|
+
- [Theming](/dapp-kit/theming) - Customize appearance
|