@berachain/wagmi 0.4.2-beta.3 → 0.4.2-beta.5
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/{BeraWagmi-D_9r9lFE.d.cts → BeraWagmi-DPriTsqn.d.cts} +1 -1
- package/dist/{BeraWagmi-BYoBR8sq.d.ts → BeraWagmi-PCCV3r-u.d.ts} +1 -1
- package/dist/chunk-FMEKPQER.cjs +1 -0
- package/dist/chunk-FPJ6HXP4.mjs +1 -0
- package/dist/chunk-HU7OL27P.mjs +1 -0
- package/dist/chunk-OLYL3V2D.cjs +1 -0
- package/dist/context/index.cjs +1 -1
- package/dist/context/index.d.cts +1 -1
- package/dist/context/index.d.ts +1 -1
- package/dist/context/index.mjs +1 -1
- package/dist/context/lightweight/index.cjs +1 -0
- package/dist/context/lightweight/index.d.cts +27 -0
- package/dist/context/lightweight/index.d.ts +27 -0
- package/dist/context/lightweight/index.mjs +1 -0
- package/dist/hooks/index.cjs +1 -1
- package/dist/hooks/index.d.cts +2 -2
- package/dist/hooks/index.d.ts +2 -2
- package/dist/hooks/index.mjs +1 -1
- package/package.json +13 -1
- package/src/context/BeraWagmi.tsx +18 -140
- package/src/context/BeraWagmiLightWeight.tsx +36 -167
- package/src/context/WagmiProviderLightWeight.tsx +39 -14
- package/src/context/useThirdweb.tsx +21 -158
- package/src/context/useThirdwebLightWeight.tsx +26 -23
- package/dist/chunk-KNUNU6MD.cjs +0 -1
- package/dist/chunk-KXR247FP.mjs +0 -1
- package/dist/chunk-LEALKQIH.cjs +0 -1
- package/dist/chunk-XFXKXV5F.mjs +0 -1
- package/dist/context/thirdweb/index.cjs +0 -1
- package/dist/context/thirdweb/index.d.cts +0 -47
- package/dist/context/thirdweb/index.d.ts +0 -47
- package/dist/context/thirdweb/index.mjs +0 -1
- /package/src/context/{thirdweb → lightweight}/index.ts +0 -0
|
@@ -1,15 +1,8 @@
|
|
|
1
|
-
import { type PropsWithChildren,
|
|
1
|
+
import { type PropsWithChildren, useMemo } from "react";
|
|
2
2
|
import useSWRImmutable from "swr/immutable";
|
|
3
|
-
import type {
|
|
4
|
-
MultiStepAuthArgsType,
|
|
5
|
-
Profile,
|
|
6
|
-
SingleStepAuthArgsType,
|
|
7
|
-
Wallet,
|
|
8
|
-
} from "thirdweb/wallets";
|
|
9
3
|
import type { Address, Chain } from "viem";
|
|
10
4
|
import { normalize } from "viem/ens";
|
|
11
5
|
import {
|
|
12
|
-
type UseWalletClientReturnType,
|
|
13
6
|
useAccount,
|
|
14
7
|
useDisconnect,
|
|
15
8
|
useEnsAvatar,
|
|
@@ -19,151 +12,40 @@ import {
|
|
|
19
12
|
} from "wagmi";
|
|
20
13
|
|
|
21
14
|
import type { getConfig, ThirdwebConfig } from "../config";
|
|
15
|
+
import type { BeraWagmiApi } from "./BeraWagmi";
|
|
22
16
|
import { BeraWagmiContext } from "./beraWagmiContext";
|
|
23
17
|
import { LightweightAccountProvider } from "./LightweightAccountProvider";
|
|
24
|
-
import {
|
|
18
|
+
import type { UseThirdwebBaseResult } from "./useThirdwebLightWeight";
|
|
25
19
|
|
|
26
|
-
export
|
|
27
|
-
const context = useContext(BeraWagmiContext);
|
|
28
|
-
if (!context) {
|
|
29
|
-
throw new Error("BeraWagmiContext not found");
|
|
30
|
-
}
|
|
31
|
-
return context;
|
|
32
|
-
}
|
|
33
|
-
export interface BeraWagmiApi {
|
|
34
|
-
/**
|
|
35
|
-
* @deprecated used for prototyping only.
|
|
36
|
-
*/
|
|
37
|
-
DebugConnectButton(): React.ReactNode;
|
|
38
|
-
/**
|
|
39
|
-
* Account connected to berachain dapp. It might include some additional data once embedded wallets are enabled.
|
|
40
|
-
*/
|
|
41
|
-
account:
|
|
42
|
-
| {
|
|
43
|
-
/**
|
|
44
|
-
* Chain ID to which the wallet provider is currently connected to.
|
|
45
|
-
*/
|
|
46
|
-
chainId: number;
|
|
47
|
-
/**
|
|
48
|
-
* Whether the wallet is an embedded wallet.
|
|
49
|
-
*/
|
|
50
|
-
isEmbeddedWallet: boolean;
|
|
51
|
-
connectorName: string | null;
|
|
52
|
-
connectionId: string;
|
|
53
|
-
profiles: Profile[] | undefined;
|
|
54
|
-
/**
|
|
55
|
-
* Wallet to display.
|
|
56
|
-
*
|
|
57
|
-
* This might be manually overridden by the accountOverride prop, thus not being the same as the connected wallet.
|
|
58
|
-
*/
|
|
59
|
-
wallet:
|
|
60
|
-
| {
|
|
61
|
-
address: Address;
|
|
62
|
-
/**
|
|
63
|
-
* ENS name of the wallet.
|
|
64
|
-
*/
|
|
65
|
-
ensName: string | null;
|
|
66
|
-
/**
|
|
67
|
-
* ENS avatar of the wallet.
|
|
68
|
-
*/
|
|
69
|
-
ensAvatar: string | null;
|
|
70
|
-
/**
|
|
71
|
-
* Whether the wallet has 7702 authorization.
|
|
72
|
-
*/
|
|
73
|
-
has7702Authorization: boolean;
|
|
74
|
-
/**
|
|
75
|
-
* Whether the wallet is a contract.
|
|
76
|
-
*/
|
|
77
|
-
isContract: boolean;
|
|
78
|
-
}
|
|
79
|
-
| undefined;
|
|
80
|
-
}
|
|
81
|
-
| undefined;
|
|
82
|
-
isEmbeddedWalletsEnabled: boolean;
|
|
83
|
-
isConnected: boolean;
|
|
84
|
-
isConnecting: boolean;
|
|
85
|
-
flags: {
|
|
86
|
-
disableEip5792: boolean;
|
|
87
|
-
disable5792Upgrade: boolean;
|
|
88
|
-
};
|
|
89
|
-
walletClient: UseWalletClientReturnType["data"];
|
|
90
|
-
openConnectModal(): Promise<void> | void;
|
|
91
|
-
/**
|
|
92
|
-
* Connects a wallet using a specific strategy.
|
|
93
|
-
* For email/phone use MultiStepAuthArgsType (requires preAuthenticate first).
|
|
94
|
-
*/
|
|
95
|
-
connect(
|
|
96
|
-
args: SingleStepAuthArgsType | MultiStepAuthArgsType,
|
|
97
|
-
): Promise<Wallet | null> | undefined;
|
|
98
|
-
/**
|
|
99
|
-
* Sends an OTP to the user's email or phone number.
|
|
100
|
-
* Call this before connect() when using email or phone strategy.
|
|
101
|
-
*/
|
|
102
|
-
preAuthenticate(
|
|
103
|
-
args:
|
|
104
|
-
| { strategy: "email"; email: string }
|
|
105
|
-
| { strategy: "phone"; phoneNumber: string },
|
|
106
|
-
): Promise<void>;
|
|
107
|
-
disconnect(): void;
|
|
108
|
-
/**
|
|
109
|
-
* A callback that is called when a smart account upgrade is rejected.
|
|
110
|
-
*
|
|
111
|
-
* This is used to disable EIP-5792 temporarily.
|
|
112
|
-
*/
|
|
113
|
-
onSmartAccountRejection(): void;
|
|
114
|
-
|
|
115
|
-
defaultChainId: ChainId;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export enum ChainId {
|
|
119
|
-
MAINNET = 80094,
|
|
120
|
-
BEPOLIA = 80069,
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export type ConnectEvent =
|
|
124
|
-
| { type: "connect"; data: SingleStepAuthArgsType | MultiStepAuthArgsType }
|
|
125
|
-
| { type: "connect_modal" };
|
|
126
|
-
|
|
127
|
-
export interface IBeraWagmiProviderProps extends PropsWithChildren {
|
|
20
|
+
export interface IBeraWagmiProviderBaseProps extends PropsWithChildren {
|
|
128
21
|
config: ReturnType<typeof getConfig>;
|
|
129
|
-
theme?: "dark" | "light" | "auto";
|
|
130
22
|
flags?: {
|
|
131
23
|
disableEip5792?: boolean;
|
|
132
|
-
/**
|
|
133
|
-
* Whether to disable the 5792 upgrade for users that haven't upgraded yet.
|
|
134
|
-
*
|
|
135
|
-
* `atomic.status === "ready"`
|
|
136
|
-
*/
|
|
137
24
|
disable5792Upgrade?: boolean;
|
|
138
|
-
/**
|
|
139
|
-
* A value that mocks the connected wallet address, while transactions are still sent from the connected wallet.
|
|
140
|
-
* It can be used for advanced use cases like sending permissionless transactions (like incentive claims).
|
|
141
|
-
*/
|
|
142
25
|
accountOverride?: Address;
|
|
143
26
|
};
|
|
144
27
|
onSmartAccountRejection?: () => void;
|
|
145
|
-
onConnect?: (event: ConnectEvent) => void;
|
|
146
|
-
onConnectError?: (error: Error) => void;
|
|
147
|
-
chains?: Chain[];
|
|
148
28
|
defaultChain: Chain;
|
|
149
29
|
thirdweb: ThirdwebConfig;
|
|
150
|
-
|
|
30
|
+
/** The thirdweb hook result — lightweight or full. */
|
|
31
|
+
thirdwebResult: UseThirdwebBaseResult;
|
|
32
|
+
/** Provided by the full variant; lightweight stubs these. */
|
|
33
|
+
openConnectModal?: BeraWagmiApi["openConnectModal"];
|
|
34
|
+
DebugConnectButton?: BeraWagmiApi["DebugConnectButton"];
|
|
151
35
|
}
|
|
152
36
|
|
|
153
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Base provider that wires up the BeraWagmiContext.
|
|
39
|
+
* Both the lightweight and full WagmiProviders render this.
|
|
40
|
+
*/
|
|
41
|
+
export function BeraWagmiProviderBase({
|
|
154
42
|
children,
|
|
155
43
|
config,
|
|
156
44
|
flags: { disableEip5792, disable5792Upgrade, accountOverride } = {},
|
|
157
|
-
theme,
|
|
158
45
|
onSmartAccountRejection,
|
|
159
|
-
chains = [],
|
|
160
46
|
defaultChain,
|
|
161
47
|
thirdweb,
|
|
162
|
-
|
|
163
|
-
onConnect,
|
|
164
|
-
onConnectError,
|
|
165
|
-
}: IBeraWagmiProviderProps) {
|
|
166
|
-
const {
|
|
48
|
+
thirdwebResult: {
|
|
167
49
|
wallet,
|
|
168
50
|
connect,
|
|
169
51
|
connectionId,
|
|
@@ -173,25 +55,17 @@ export function BeraWagmiProvider({
|
|
|
173
55
|
profiles,
|
|
174
56
|
preAuthenticate,
|
|
175
57
|
isConnecting,
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
publicJsonRpcUrl,
|
|
182
|
-
onConnect,
|
|
183
|
-
onConnectError,
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
const { address: connectedAddress, chainId } = useAccount(); // wagmi
|
|
58
|
+
},
|
|
59
|
+
openConnectModal,
|
|
60
|
+
DebugConnectButton,
|
|
61
|
+
}: IBeraWagmiProviderBaseProps) {
|
|
62
|
+
const { address: connectedAddress, chainId } = useAccount();
|
|
187
63
|
|
|
188
64
|
const { disconnect: disconnectWagmi } = useDisconnect();
|
|
189
65
|
const publicClient = usePublicClient();
|
|
190
66
|
|
|
191
67
|
const address = useMemo<Address | undefined>(() => {
|
|
192
|
-
// no address anyway if no connected address
|
|
193
68
|
if (!connectedAddress || !wallet) return undefined;
|
|
194
|
-
|
|
195
69
|
return accountOverride ?? connectedAddress;
|
|
196
70
|
}, [accountOverride, connectedAddress, wallet]);
|
|
197
71
|
|
|
@@ -213,18 +87,14 @@ export function BeraWagmiProvider({
|
|
|
213
87
|
},
|
|
214
88
|
);
|
|
215
89
|
const [isContract, has7702Authorization] = useMemo(() => {
|
|
216
|
-
if (!code)
|
|
217
|
-
// if no code, it's not a contract or has 7702 authorization
|
|
218
|
-
return [false, false];
|
|
90
|
+
if (!code) return [false, false];
|
|
219
91
|
|
|
220
92
|
const has7702Authorization = code.startsWith("0xef01");
|
|
221
93
|
|
|
222
94
|
if (has7702Authorization) {
|
|
223
|
-
// if has 7702 authorization, it's not a contract
|
|
224
95
|
return [false, true];
|
|
225
96
|
}
|
|
226
97
|
|
|
227
|
-
// if doesn't have 7702 authorization but has code, it's a contract
|
|
228
98
|
return [true, false];
|
|
229
99
|
}, [code]);
|
|
230
100
|
|
|
@@ -232,11 +102,10 @@ export function BeraWagmiProvider({
|
|
|
232
102
|
|
|
233
103
|
const normalizedEns = useMemo(() => {
|
|
234
104
|
try {
|
|
235
|
-
// some characters might throw an error when normalizing
|
|
236
|
-
// for instance `_` throws an error (e.g. `bera_boy`)
|
|
237
105
|
return ens ? normalize(ens) : undefined;
|
|
238
106
|
} catch {
|
|
239
|
-
//
|
|
107
|
+
// some characters might throw an error when normalizing
|
|
108
|
+
// for instance `_` throws an error (e.g. `bera_boy`)
|
|
240
109
|
// @see https://berachain-dapps.sentry.io/issues/6949741717/?alert_rule_id=16379160&alert_type=issue¬ification_uuid=b01366a6-78b0-4348-a3c1-5f76ced1d539&project=4509842561630209
|
|
241
110
|
}
|
|
242
111
|
}, [ens]);
|
|
@@ -259,13 +128,12 @@ export function BeraWagmiProvider({
|
|
|
259
128
|
},
|
|
260
129
|
isEmbeddedWalletsEnabled: thirdweb.embeddedWallets ?? true,
|
|
261
130
|
isConnecting,
|
|
262
|
-
isConnected:
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
},
|
|
131
|
+
isConnected: !!address && !!walletClient,
|
|
132
|
+
DebugConnectButton:
|
|
133
|
+
DebugConnectButton ??
|
|
134
|
+
(() => {
|
|
135
|
+
return null;
|
|
136
|
+
}),
|
|
269
137
|
account:
|
|
270
138
|
wallet && chainId && address
|
|
271
139
|
? {
|
|
@@ -284,16 +152,17 @@ export function BeraWagmiProvider({
|
|
|
284
152
|
}
|
|
285
153
|
: undefined,
|
|
286
154
|
connect,
|
|
287
|
-
openConnectModal
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
155
|
+
openConnectModal:
|
|
156
|
+
openConnectModal ??
|
|
157
|
+
(() => {
|
|
158
|
+
throw new Error(
|
|
159
|
+
"openConnectModal is not available in lightweight mode. Use @berachain/wagmi/context instead.",
|
|
160
|
+
);
|
|
161
|
+
}),
|
|
292
162
|
disconnect: () => {
|
|
293
163
|
disconnectWagmi();
|
|
294
164
|
disconnectProvider();
|
|
295
165
|
},
|
|
296
|
-
// chains,
|
|
297
166
|
defaultChainId: defaultChain.id,
|
|
298
167
|
}}
|
|
299
168
|
>
|
|
@@ -1,21 +1,33 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
+
import type { PropsWithChildren } from "react";
|
|
3
4
|
import { useMemo } from "react";
|
|
4
5
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
5
6
|
import { ThirdwebProvider } from "thirdweb/react";
|
|
6
|
-
import type { Chain, Transport } from "viem";
|
|
7
|
+
import type { Address, Chain, Transport } from "viem";
|
|
7
8
|
import { WagmiProvider as WagmiProviderBase } from "wagmi";
|
|
8
9
|
|
|
10
|
+
import type { ThirdwebConfig } from "../config";
|
|
9
11
|
import { getConfig } from "../config/defaultWagmiConfig";
|
|
10
|
-
import {
|
|
12
|
+
import type { ConnectEvent } from "./BeraWagmi";
|
|
13
|
+
import { BeraWagmiProviderBase } from "./BeraWagmiLightWeight";
|
|
14
|
+
import { useThirdwebLightWeight } from "./useThirdwebLightWeight";
|
|
11
15
|
|
|
12
|
-
type
|
|
13
|
-
IBeraWagmiProviderProps,
|
|
14
|
-
"config" | "defaultChain"
|
|
15
|
-
> & {
|
|
16
|
+
type WagmiProviderLightWeightProps = PropsWithChildren<{
|
|
16
17
|
transports?: Record<number, Transport>;
|
|
17
18
|
defaultChain: Chain;
|
|
18
|
-
|
|
19
|
+
chains?: Chain[];
|
|
20
|
+
thirdweb: ThirdwebConfig;
|
|
21
|
+
flags?: {
|
|
22
|
+
disableEip5792?: boolean;
|
|
23
|
+
disable5792Upgrade?: boolean;
|
|
24
|
+
accountOverride?: Address;
|
|
25
|
+
};
|
|
26
|
+
onSmartAccountRejection?: () => void;
|
|
27
|
+
onConnect?: (event: ConnectEvent) => void;
|
|
28
|
+
onConnectError?: (error: Error) => void;
|
|
29
|
+
publicJsonRpcUrl?: string;
|
|
30
|
+
}>;
|
|
19
31
|
|
|
20
32
|
const queryClient = new QueryClient();
|
|
21
33
|
|
|
@@ -24,9 +36,12 @@ export function WagmiProvider({
|
|
|
24
36
|
defaultChain,
|
|
25
37
|
chains,
|
|
26
38
|
thirdweb,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
39
|
+
children,
|
|
40
|
+
onConnect,
|
|
41
|
+
onConnectError,
|
|
42
|
+
publicJsonRpcUrl,
|
|
43
|
+
...baseProps
|
|
44
|
+
}: WagmiProviderLightWeightProps) {
|
|
30
45
|
const config = useMemo(() => {
|
|
31
46
|
return getConfig({
|
|
32
47
|
thirdweb,
|
|
@@ -36,6 +51,14 @@ export function WagmiProvider({
|
|
|
36
51
|
});
|
|
37
52
|
}, [defaultChain, thirdweb, transports, chains]);
|
|
38
53
|
|
|
54
|
+
const thirdwebResult = useThirdwebLightWeight({
|
|
55
|
+
config,
|
|
56
|
+
chain: defaultChain,
|
|
57
|
+
publicJsonRpcUrl,
|
|
58
|
+
onConnect,
|
|
59
|
+
onConnectError,
|
|
60
|
+
});
|
|
61
|
+
|
|
39
62
|
return (
|
|
40
63
|
<ThirdwebProvider>
|
|
41
64
|
<WagmiProviderBase
|
|
@@ -44,13 +67,15 @@ export function WagmiProvider({
|
|
|
44
67
|
reconnectOnMount={false}
|
|
45
68
|
>
|
|
46
69
|
<QueryClientProvider client={queryClient}>
|
|
47
|
-
<
|
|
48
|
-
{...
|
|
70
|
+
<BeraWagmiProviderBase
|
|
71
|
+
{...baseProps}
|
|
49
72
|
thirdweb={thirdweb}
|
|
50
|
-
chains={chains}
|
|
51
73
|
defaultChain={defaultChain}
|
|
52
74
|
config={config}
|
|
53
|
-
|
|
75
|
+
thirdwebResult={thirdwebResult}
|
|
76
|
+
>
|
|
77
|
+
{children}
|
|
78
|
+
</BeraWagmiProviderBase>
|
|
54
79
|
</QueryClientProvider>
|
|
55
80
|
</WagmiProviderBase>
|
|
56
81
|
</ThirdwebProvider>
|
|
@@ -1,29 +1,16 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
2
|
-
import type { ConnectionOptions } from "@thirdweb-dev/wagmi-adapter";
|
|
3
1
|
import { defineChain } from "thirdweb";
|
|
4
2
|
import {
|
|
5
3
|
ConnectButton,
|
|
6
|
-
useActiveAccount,
|
|
7
|
-
useActiveWallet,
|
|
8
|
-
useAutoConnect,
|
|
9
|
-
useProfiles,
|
|
10
|
-
useConnect as useTwConnect,
|
|
11
4
|
useConnectModal as useTwConnectModal,
|
|
12
|
-
useDisconnect as useTwDisconnect,
|
|
13
|
-
useWalletInfo,
|
|
14
5
|
} from "thirdweb/react";
|
|
15
|
-
import type {
|
|
16
|
-
Account,
|
|
17
|
-
AutoConnectProps,
|
|
18
|
-
Profile,
|
|
19
|
-
Wallet,
|
|
20
|
-
} from "thirdweb/wallets";
|
|
21
|
-
import { preAuthenticate } from "thirdweb/wallets/in-app";
|
|
22
6
|
import type { Chain } from "viem";
|
|
23
|
-
import { useConnect } from "wagmi";
|
|
24
7
|
|
|
25
|
-
import type { getConfig
|
|
8
|
+
import type { getConfig } from "../config";
|
|
26
9
|
import type { BeraWagmiApi, ConnectEvent } from "./BeraWagmi";
|
|
10
|
+
import {
|
|
11
|
+
type UseThirdwebBaseResult,
|
|
12
|
+
useThirdwebLightWeight,
|
|
13
|
+
} from "./useThirdwebLightWeight";
|
|
27
14
|
|
|
28
15
|
export function useThirdweb({
|
|
29
16
|
config,
|
|
@@ -32,7 +19,7 @@ export function useThirdweb({
|
|
|
32
19
|
chain,
|
|
33
20
|
onConnect,
|
|
34
21
|
onConnectError,
|
|
35
|
-
publicJsonRpcUrl
|
|
22
|
+
publicJsonRpcUrl,
|
|
36
23
|
}: {
|
|
37
24
|
onConnect?: (event: ConnectEvent) => void;
|
|
38
25
|
onConnectError?: (error: Error) => void;
|
|
@@ -41,142 +28,32 @@ export function useThirdweb({
|
|
|
41
28
|
chains?: Chain[];
|
|
42
29
|
publicJsonRpcUrl?: string;
|
|
43
30
|
chain: Chain;
|
|
44
|
-
}):
|
|
45
|
-
BeraWagmiApi,
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
> & {
|
|
53
|
-
connectionId: string;
|
|
54
|
-
connectorName: string | null;
|
|
55
|
-
isEmbeddedWallet: boolean;
|
|
56
|
-
wallet: Account | undefined;
|
|
57
|
-
profiles: Profile[] | undefined;
|
|
58
|
-
} {
|
|
59
|
-
const { connectors, connect: wagmiConnect } = useConnect();
|
|
60
|
-
|
|
61
|
-
const activeWallet = useActiveWallet();
|
|
62
|
-
const activeAccount = useActiveAccount(); // thirdweb
|
|
63
|
-
const { disconnect: disconnectThirdweb } = useTwDisconnect();
|
|
64
|
-
const { data: walletInfo } = useWalletInfo(activeWallet?.id);
|
|
65
|
-
const { connect: twConnect } = useTwConnect();
|
|
66
|
-
|
|
67
|
-
const { data: profiles } = useProfiles({ client: config.client });
|
|
68
|
-
|
|
69
|
-
const thirdwebChain = useMemo(
|
|
70
|
-
() =>
|
|
71
|
-
defineChain({
|
|
72
|
-
id: chain.id,
|
|
73
|
-
blockExplorers: chain.blockExplorers,
|
|
74
|
-
blockTime: chain.blockTime,
|
|
75
|
-
ensTlds: chain.ensTlds,
|
|
76
|
-
testnet: chain.testnet,
|
|
77
|
-
contracts: chain.contracts,
|
|
78
|
-
nativeCurrency: chain.nativeCurrency,
|
|
79
|
-
name: chain.name,
|
|
80
|
-
rpcUrls: {
|
|
81
|
-
default: {
|
|
82
|
-
http: [publicJsonRpcUrl],
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
}),
|
|
86
|
-
[publicJsonRpcUrl, chain],
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
const connectOptions = {
|
|
90
|
-
client: config.client,
|
|
91
|
-
chain: thirdwebChain,
|
|
92
|
-
wallets: config.wallets,
|
|
93
|
-
} as const satisfies AutoConnectProps;
|
|
94
|
-
|
|
95
|
-
async function syncConnectedWallet(wallet: Wallet) {
|
|
96
|
-
// console.debug("syncConnectedWallet", wallet);
|
|
97
|
-
// now "connect" the wagmi connector
|
|
98
|
-
const twConnector = connectors.find((c) => c.id === "in-app-wallet");
|
|
99
|
-
|
|
100
|
-
if (twConnector) {
|
|
101
|
-
const options: ConnectionOptions = {
|
|
102
|
-
withCapabilities: true,
|
|
103
|
-
wallet,
|
|
104
|
-
} as const;
|
|
105
|
-
|
|
106
|
-
wagmiConnect({
|
|
107
|
-
connector: twConnector,
|
|
108
|
-
chainId: chain.id,
|
|
109
|
-
...options,
|
|
110
|
-
});
|
|
111
|
-
} else {
|
|
112
|
-
// captureException is not available here since we don't have shared-ui
|
|
113
|
-
// however this happens only if wagmi is misconfigured
|
|
114
|
-
console.error("no tw connector found");
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const { isLoading: isAutoConnecting } = useAutoConnect({
|
|
119
|
-
...connectOptions,
|
|
120
|
-
onConnect: syncConnectedWallet,
|
|
31
|
+
}): UseThirdwebBaseResult &
|
|
32
|
+
Pick<BeraWagmiApi, "openConnectModal" | "DebugConnectButton"> {
|
|
33
|
+
const base = useThirdwebLightWeight({
|
|
34
|
+
config,
|
|
35
|
+
chain,
|
|
36
|
+
onConnect,
|
|
37
|
+
onConnectError,
|
|
38
|
+
publicJsonRpcUrl,
|
|
121
39
|
});
|
|
122
40
|
|
|
123
|
-
const connectionId = useMemo(() => {
|
|
124
|
-
// this might not be available in some environments
|
|
125
|
-
if ("randomUUID" in crypto) {
|
|
126
|
-
return crypto.randomUUID();
|
|
127
|
-
}
|
|
128
|
-
return Math.random().toString(36).substring(2, 15);
|
|
129
|
-
}, [activeWallet?.id, activeAccount?.address]);
|
|
130
|
-
|
|
131
41
|
const { connect: twConnectModal } = useTwConnectModal();
|
|
132
42
|
|
|
133
43
|
return {
|
|
134
|
-
|
|
135
|
-
if (!config.hasEmbeddedWallets) {
|
|
136
|
-
console.error(
|
|
137
|
-
"useBeraWagmi.connect: this function is not available if embedded wallets are disabled",
|
|
138
|
-
);
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
return twConnect(async () => {
|
|
142
|
-
// this must be called inside twConnect or it won't work for social login wallets
|
|
143
|
-
const wallet = config.wallets.find(
|
|
144
|
-
(w) => w.id === "inApp",
|
|
145
|
-
) as InAppWallet;
|
|
146
|
-
|
|
147
|
-
if (!wallet) {
|
|
148
|
-
console.error("useBeraWagmi.connect: no inApp wallet found");
|
|
149
|
-
throw new Error("no inApp wallet found");
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return wallet
|
|
153
|
-
.connect({
|
|
154
|
-
...connectOptions,
|
|
155
|
-
...args,
|
|
156
|
-
})
|
|
157
|
-
.then(async () => {
|
|
158
|
-
onConnect?.({ type: "connect", data: args });
|
|
159
|
-
await syncConnectedWallet(wallet);
|
|
160
|
-
return wallet;
|
|
161
|
-
})
|
|
162
|
-
.catch((e) => {
|
|
163
|
-
onConnectError?.(e);
|
|
164
|
-
throw e;
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
},
|
|
44
|
+
...base,
|
|
168
45
|
async openConnectModal() {
|
|
169
46
|
try {
|
|
170
47
|
const wallet = await twConnectModal({
|
|
171
|
-
...connectOptions,
|
|
48
|
+
...base.connectOptions,
|
|
172
49
|
wallets: config.wallets,
|
|
173
50
|
theme: theme === "auto" ? undefined : theme,
|
|
174
51
|
size: "compact",
|
|
175
52
|
chains: [
|
|
176
|
-
thirdwebChain,
|
|
53
|
+
base.thirdwebChain,
|
|
177
54
|
...chains
|
|
178
55
|
// avoid duplicates
|
|
179
|
-
.filter((c) => c.id !== thirdwebChain.id)
|
|
56
|
+
.filter((c) => c.id !== base.thirdwebChain.id)
|
|
180
57
|
.map((c) =>
|
|
181
58
|
defineChain({
|
|
182
59
|
id: c.id,
|
|
@@ -186,34 +63,20 @@ export function useThirdweb({
|
|
|
186
63
|
}); // opens the connect modal
|
|
187
64
|
|
|
188
65
|
onConnect?.({ type: "connect_modal" });
|
|
189
|
-
await syncConnectedWallet(wallet);
|
|
66
|
+
await base.syncConnectedWallet(wallet);
|
|
190
67
|
} catch (e) {
|
|
191
68
|
onConnectError?.(e as Error);
|
|
192
69
|
throw e;
|
|
193
70
|
}
|
|
194
71
|
},
|
|
195
|
-
disconnect() {
|
|
196
|
-
if (activeWallet) {
|
|
197
|
-
disconnectThirdweb(activeWallet);
|
|
198
|
-
}
|
|
199
|
-
},
|
|
200
|
-
isConnecting: isAutoConnecting,
|
|
201
|
-
connectionId,
|
|
202
|
-
connectorName: walletInfo?.name ?? null,
|
|
203
|
-
isEmbeddedWallet: walletInfo?.id === "inApp",
|
|
204
|
-
wallet: activeAccount,
|
|
205
|
-
profiles,
|
|
206
|
-
preAuthenticate(args) {
|
|
207
|
-
return preAuthenticate({ ...args, client: config.client });
|
|
208
|
-
},
|
|
209
72
|
DebugConnectButton() {
|
|
210
73
|
return (
|
|
211
74
|
<ConnectButton
|
|
212
|
-
{...connectOptions}
|
|
75
|
+
{...base.connectOptions}
|
|
213
76
|
connectModal={{
|
|
214
77
|
size: "compact",
|
|
215
78
|
}}
|
|
216
|
-
onConnect={(wallet) => syncConnectedWallet(wallet)}
|
|
79
|
+
onConnect={(wallet) => base.syncConnectedWallet(wallet)}
|
|
217
80
|
/>
|
|
218
81
|
);
|
|
219
82
|
},
|
|
@@ -21,37 +21,39 @@ import type { Chain } from "viem";
|
|
|
21
21
|
import { useConnect } from "wagmi";
|
|
22
22
|
|
|
23
23
|
import type { getConfig, InAppWallet } from "../config";
|
|
24
|
-
import type { BeraWagmiApi, ConnectEvent } from "./
|
|
24
|
+
import type { BeraWagmiApi, ConnectEvent } from "./BeraWagmi";
|
|
25
25
|
|
|
26
|
-
export
|
|
27
|
-
config,
|
|
28
|
-
chain,
|
|
29
|
-
onConnect,
|
|
30
|
-
onConnectError,
|
|
31
|
-
publicJsonRpcUrl = chain.rpcUrls.default.http[0],
|
|
32
|
-
}: {
|
|
26
|
+
export interface UseThirdwebBaseArgs {
|
|
33
27
|
onConnect?: (event: ConnectEvent) => void;
|
|
34
28
|
onConnectError?: (error: Error) => void;
|
|
35
29
|
config: ReturnType<typeof getConfig>;
|
|
36
|
-
theme: "light" | "dark" | "auto" | undefined;
|
|
37
|
-
chains?: Chain[];
|
|
38
30
|
publicJsonRpcUrl?: string;
|
|
39
31
|
chain: Chain;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
| "preAuthenticate"
|
|
48
|
-
> & {
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface UseThirdwebBaseResult {
|
|
35
|
+
connect: BeraWagmiApi["connect"];
|
|
36
|
+
disconnect: BeraWagmiApi["disconnect"];
|
|
37
|
+
isConnecting: BeraWagmiApi["isConnecting"];
|
|
38
|
+
preAuthenticate: BeraWagmiApi["preAuthenticate"];
|
|
49
39
|
connectionId: string;
|
|
50
40
|
connectorName: string | null;
|
|
51
41
|
isEmbeddedWallet: boolean;
|
|
52
42
|
wallet: Account | undefined;
|
|
53
43
|
profiles: Profile[] | undefined;
|
|
54
|
-
|
|
44
|
+
/** Exposed so the full variant can call it after opening the modal. */
|
|
45
|
+
syncConnectedWallet: (wallet: Wallet) => Promise<void>;
|
|
46
|
+
connectOptions: AutoConnectProps;
|
|
47
|
+
thirdwebChain: ReturnType<typeof defineChain>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function useThirdwebLightWeight({
|
|
51
|
+
config,
|
|
52
|
+
chain,
|
|
53
|
+
onConnect,
|
|
54
|
+
onConnectError,
|
|
55
|
+
publicJsonRpcUrl = chain.rpcUrls.default.http[0],
|
|
56
|
+
}: UseThirdwebBaseArgs): UseThirdwebBaseResult {
|
|
55
57
|
const { connectors, connect: wagmiConnect } = useConnect();
|
|
56
58
|
|
|
57
59
|
const activeWallet = useActiveWallet();
|
|
@@ -89,8 +91,6 @@ export function useThirdwebLightWeight({
|
|
|
89
91
|
} as const satisfies AutoConnectProps;
|
|
90
92
|
|
|
91
93
|
async function syncConnectedWallet(wallet: Wallet) {
|
|
92
|
-
// console.debug("syncConnectedWallet", wallet);
|
|
93
|
-
// now "connect" the wagmi connector
|
|
94
94
|
const twConnector = connectors.find((c) => c.id === "in-app-wallet");
|
|
95
95
|
|
|
96
96
|
if (twConnector) {
|
|
@@ -172,6 +172,9 @@ export function useThirdwebLightWeight({
|
|
|
172
172
|
profiles,
|
|
173
173
|
preAuthenticate(args) {
|
|
174
174
|
return preAuthenticate({ ...args, client: config.client });
|
|
175
|
-
}
|
|
175
|
+
},
|
|
176
|
+
syncConnectedWallet,
|
|
177
|
+
connectOptions,
|
|
178
|
+
thirdwebChain,
|
|
176
179
|
};
|
|
177
180
|
}
|