Skip to content

Commit 6495b94

Browse files
[SDK] Simplify wallet connection code and add Abstract wallet support (#8061)
1 parent 210f631 commit 6495b94

File tree

3 files changed

+218
-73
lines changed

3 files changed

+218
-73
lines changed

apps/portal/src/app/typescript/v5/connecting-wallets/page.mdx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ import { createWallet, injectedProvider } from "thirdweb/wallets";
4141
const client = createThirdwebClient({ clientId });
4242

4343
const metamask = createWallet("io.metamask"); // pass the wallet id
44-
45-
// if user has metamask installed, connect to it
46-
if (injectedProvider("io.metamask")) {
47-
await metamask.connect({ client });
48-
}
49-
50-
// open wallet connect modal so user can scan the QR code and connect
51-
else {
52-
await metamask.connect({ client, walletConnect: { showQrModal: true } });
53-
}
44+
// if user has metamask installed, connect to it, otherwise opens a WalletConnect modal
45+
await metamask.connect({ client });
5446
```

apps/portal/src/app/wallets/external-wallets/[walletId]/page.tsx

Lines changed: 12 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,7 @@ import { createWallet, injectedProvider } from "thirdweb/wallets";
188188
const client = createThirdwebClient({ clientId });
189189
190190
const wallet = createWallet("${id}"); // pass the wallet id
191-
192-
// if the wallet extension is installed, connect to it
193-
if (injectedProvider("${id}")) {
194-
await wallet.connect({ client });
195-
}
196-
197-
// show error message to user that wallet is not installed
198-
else {
199-
alert('wallet is not installed');
200-
}
191+
await wallet.connect({ client });
201192
`;
202193
}
203194

@@ -209,12 +200,7 @@ import { createWallet } from "thirdweb/wallets";
209200
const client = createThirdwebClient({ clientId });
210201
211202
const wallet = createWallet("${id}"); // pass the wallet id
212-
213-
// open WalletConnect modal so user can scan the QR code and connect
214-
await wallet.connect({
215-
client,
216-
walletConnect: { showQrModal: true },
217-
});
203+
await wallet.connect({ client });
218204
`;
219205
}
220206

@@ -226,19 +212,8 @@ import { createWallet, injectedProvider } from "thirdweb/wallets";
226212
const client = createThirdwebClient({ clientId });
227213
228214
const wallet = createWallet("${id}"); // pass the wallet id
229-
230-
// if user has wallet installed, connect to it
231-
if (injectedProvider("${id}")) {
232-
await wallet.connect({ client });
233-
}
234-
235-
// open WalletConnect modal so user can scan the QR code and connect
236-
else {
237-
await wallet.connect({
238-
client,
239-
walletConnect: { showQrModal: true },
240-
});
241-
}
215+
// if user has wallet installed, connects to it, otherwise opens a WalletConnect modal
216+
await wallet.connect({ client });
242217
`;
243218
}
244219

@@ -257,20 +232,7 @@ function Example() {
257232
onClick={() =>
258233
connect(async () => {
259234
const wallet = createWallet("${id}"); // pass the wallet id
260-
261-
// if user has wallet installed, connect to it
262-
if (injectedProvider("${id}")) {
263-
await wallet.connect({ client });
264-
}
265-
266-
// open WalletConnect modal so user can scan the QR code and connect
267-
else {
268-
await wallet.connect({
269-
client,
270-
walletConnect: { showQrModal: true },
271-
});
272-
}
273-
235+
await wallet.connect({ client });
274236
// return the wallet to set it as active wallet
275237
return wallet;
276238
})
@@ -298,13 +260,7 @@ function Example() {
298260
onClick={() =>
299261
connect(async () => {
300262
const wallet = createWallet("${id}"); // pass the wallet id
301-
302-
// open WalletConnect modal so user can scan the QR code and connect
303-
await wallet.connect({
304-
client,
305-
walletConnect: { showQrModal: true },
306-
});
307-
263+
await wallet.connect({ client });
308264
// return the wallet to set it as active wallet
309265
return wallet;
310266
})
@@ -330,19 +286,12 @@ function Example() {
330286
return (
331287
<button
332288
onClick={() => {
333-
// if the wallet extension is installed, connect to it
334-
if (injectedProvider("${id}")) {
335-
connect(async () => {
336-
const wallet = createWallet("${id}");
337-
await wallet.connect({ client });
338-
return wallet;
339-
});
340-
}
341-
342-
// show error message to user that wallet is not installed
343-
else {
344-
alert("wallet is not installed");
345-
}
289+
// if the wallet extension is installed, connect to it, otherwise opens a WalletConnect modal
290+
connect(async () => {
291+
const wallet = createWallet("${id}");
292+
await wallet.connect({ client });
293+
return wallet;
294+
});
346295
}}
347296
>
348297
{isConnecting ? "Connecting..." : "Connect"}
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/* eslint-disable @next/next/no-img-element */
2+
3+
import { GlobeIcon } from "lucide-react";
4+
import Image from "next/image";
5+
import { getWalletInfo } from "thirdweb/wallets";
6+
import {
7+
Breadcrumb,
8+
CodeBlock,
9+
createMetadata,
10+
DocLink,
11+
Heading,
12+
Paragraph,
13+
} from "@/components/Document";
14+
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
15+
16+
const walletId = "xyz.abs";
17+
18+
export async function generateMetadata() {
19+
const walletMetadata = await getWalletInfo(walletId);
20+
21+
return createMetadata({
22+
description: `Connect ${walletMetadata.name} with thirdweb TypeScript SDK`,
23+
image: {
24+
icon: "wallets",
25+
title: walletMetadata.name,
26+
},
27+
title: walletMetadata.name,
28+
});
29+
}
30+
31+
export default async function Page() {
32+
const [walletMetadata, walletImage] = await Promise.all([
33+
getWalletInfo(walletId),
34+
getWalletInfo(walletId, true),
35+
]);
36+
37+
return (
38+
<div>
39+
<Breadcrumb
40+
crumbs={[
41+
{
42+
href: "/wallets/external-wallets",
43+
name: "External Wallets",
44+
},
45+
{
46+
href: `/wallets/external-wallets/${walletId}`,
47+
name: walletMetadata.name,
48+
},
49+
]}
50+
/>
51+
52+
<div className="mb-10 flex items-center gap-3 [&_h1]:m-0">
53+
<Image
54+
alt=""
55+
className="rounded-lg"
56+
height={36}
57+
src={walletImage}
58+
width={36}
59+
/>
60+
<Heading anchorId="title" level={1}>
61+
{walletMetadata.name}
62+
</Heading>
63+
</div>
64+
65+
<DocLink
66+
className="flex items-center gap-2"
67+
href={walletMetadata.homepage}
68+
>
69+
<GlobeIcon className="size-5" />
70+
{walletMetadata.homepage}
71+
</DocLink>
72+
73+
<Heading anchorId="wallet-id" level={2}>
74+
Wallet ID
75+
</Heading>
76+
77+
<CodeBlock code={`"${walletId}"`} lang="ts" />
78+
79+
<Heading anchorId="connect-wallet" level={2}>
80+
Installation
81+
</Heading>
82+
83+
<CodeBlock
84+
code={`npm i thirdweb @abstract-foundation/agw-react`}
85+
lang="bash"
86+
/>
87+
88+
<Heading anchorId="connect-wallet" level={2}>
89+
Connect Wallet
90+
</Heading>
91+
92+
<Tabs defaultValue="tab-1">
93+
<TabsList>
94+
<TabsTrigger value="tab-1"> TypeScript </TabsTrigger>
95+
<TabsTrigger value="tab-2"> React (Custom UI) </TabsTrigger>
96+
<TabsTrigger value="tab-3"> React (Component) </TabsTrigger>
97+
</TabsList>
98+
<TabsContent value="tab-1">
99+
<CodeBlock code={injectedSupportedTS()} lang="ts" />
100+
</TabsContent>
101+
<TabsContent value="tab-2">
102+
<CodeBlock code={injectedSupportedCodeReact()} lang="ts" />
103+
</TabsContent>
104+
<TabsContent value="tab-3">
105+
<Paragraph>
106+
You can add this wallet in the{" "}
107+
<DocLink href="/react/v5/ConnectButton" target="_blank">
108+
ConnectButton
109+
</DocLink>{" "}
110+
or{" "}
111+
<DocLink href="/react/v5/ConnectEmbed" target="_blank">
112+
ConnectEmbed
113+
</DocLink>{" "}
114+
component to get a pre-built UI for connecting the wallet.{" "}
115+
<Heading anchorId="connect-component" level={3}>
116+
Example
117+
</Heading>{" "}
118+
<CodeBlock code={componentCode()} lang="tsx" />
119+
</Paragraph>
120+
</TabsContent>
121+
</Tabs>
122+
123+
<Heading anchorId="reference" level={2}>
124+
Reference
125+
</Heading>
126+
127+
<DocLink href="/references/typescript/v5/AbsWalletCreationOptions">
128+
View All Creation Options
129+
</DocLink>
130+
</div>
131+
);
132+
}
133+
134+
function injectedSupportedTS() {
135+
return `\
136+
import { createThirdwebClient } from "thirdweb";
137+
import { createWallet } from "thirdweb/wallets";
138+
import { abstractWallet } from "@abstract-foundation/agw-react/thirdweb";
139+
140+
const client = createThirdwebClient({ clientId });
141+
142+
const wallet = ${createWallet()};
143+
144+
await wallet.connect({ client });
145+
`;
146+
}
147+
148+
function injectedSupportedCodeReact() {
149+
return `\
150+
import { createThirdwebClient } from "thirdweb";
151+
import { useConnect } from "thirdweb/react";
152+
import { createWallet } from "thirdweb/wallets";
153+
import { abstractWallet } from "@abstract-foundation/agw-react/thirdweb";
154+
155+
const client = createThirdwebClient({ clientId });
156+
const wallet = ${createWallet()};
157+
158+
function Example() {
159+
const { connect, isConnecting, error } = useConnect();
160+
return (
161+
<button
162+
onClick={() => {
163+
// if the wallet extension is installed, connect to it
164+
connect(async () => {
165+
await wallet.connect({ client });
166+
return wallet;
167+
});
168+
}}
169+
>
170+
{isConnecting ? "Connecting..." : "Connect"}
171+
</button>
172+
);
173+
}
174+
`;
175+
}
176+
177+
function componentCode() {
178+
return `\
179+
import { ConnectButton } from "thirdweb/react";
180+
import { createWallet } from "thirdweb/wallets";
181+
import { abstractWallet } from "@abstract-foundation/agw-react/thirdweb";
182+
183+
const wallets = [
184+
// Add your wallet in wallet list
185+
${createWallet()},
186+
// add other wallets...
187+
];
188+
189+
function Example() {
190+
return (
191+
<div>
192+
{/* Use ConnectButton */}
193+
<ConnectButton client={client} wallets={wallets} />
194+
195+
{/* Or Use ConnectEmbed */}
196+
<ConnectEmbed client={client} wallets={wallets} />
197+
</div>
198+
);
199+
}`;
200+
}
201+
202+
function createWallet() {
203+
return `abstractWallet()`;
204+
}

0 commit comments

Comments
 (0)