Skip to content

Commit f017a05

Browse files
WIP connect params
1 parent 1d59753 commit f017a05

File tree

3 files changed

+79
-47
lines changed

3 files changed

+79
-47
lines changed

packages/wagmi-adapter/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
},
2626
"files": ["dist/*", "src/*"],
2727
"devDependencies": {
28-
"@wagmi/core": "0.0.0-canary-20241211210803",
28+
"@wagmi/core": "2.16.0",
2929
"rimraf": "6.0.1",
3030
"thirdweb": "workspace:*"
3131
},
3232
"peerDependencies": {
33-
"@wagmi/core": "^2",
33+
"@wagmi/core": "^2.16.0",
3434
"thirdweb": "^5",
3535
"typescript": ">=5.0.4"
3636
},

packages/wagmi-adapter/src/connector.ts

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { EventEmitter } from "stream";
21
import { type CreateConnectorFn, createConnector } from "@wagmi/core";
32
import type { Prettify } from "@wagmi/core/chains";
4-
import { createThirdwebClient, defineChain, getAddress } from "thirdweb";
3+
import { type ThirdwebClient, defineChain, getAddress } from "thirdweb";
54
import {
65
EIP1193,
76
type InAppWalletConnectionOptions,
@@ -11,20 +10,22 @@ import {
1110
import type { InAppWalletCreationOptions } from "thirdweb/wallets/in-app";
1211

1312
export type InAppWalletParameters = Prettify<
14-
Omit<InAppWalletConnectionOptions, "client"> &
15-
InAppWalletCreationOptions & {
16-
clientId: string;
17-
ecosystemId?: `ecosystem.${string}`;
18-
}
13+
InAppWalletCreationOptions & {
14+
client: ThirdwebClient;
15+
ecosystemId?: `ecosystem.${string}`;
16+
}
1917
>;
2018

2119
type Provider = EIP1193.EIP1193Provider | undefined;
2220
type Properties = {
23-
connect(parameters?: {
24-
chainId?: number | undefined;
25-
isReconnecting?: boolean | undefined;
26-
foo?: string;
27-
}): Promise<{
21+
connect(
22+
parameters?: Prettify<
23+
Omit<InAppWalletConnectionOptions, "client"> & {
24+
chainId?: number | undefined;
25+
isReconnecting?: boolean | undefined;
26+
}
27+
>,
28+
): Promise<{
2829
accounts: readonly `0x${string}`[];
2930
chainId: number;
3031
}>;
@@ -39,53 +40,86 @@ type StorageItem = { "tw.lastChainId": number };
3940
* ```ts
4041
* import { http, createConfig } from "wagmi";
4142
* import { inAppWalletConnector } from "@thirdweb-dev/wagmi-adapter";
43+
* import { createThirdwebClient, defineChain as thirdwebChain } from "thirdweb";
44+
*
45+
* const client = createThirdwebClient({
46+
* clientId: "...",
47+
* });
4248
*
4349
* export const config = createConfig({
4450
* chains: [sepolia],
4551
* connectors: [
4652
* inAppWalletConnector({
47-
* clientId: "...",
48-
* strategy: "google",
53+
* client,
54+
* // optional: turn on smart accounts
55+
* smartAccounts: {
56+
* sponsorGas: true,
57+
* chain: thirdwebChain(sepolia)
58+
* }
4959
* }),
5060
* ],
5161
* transports: {
5262
* [sepolia.id]: http(),
5363
* },
5464
* });
5565
* ```
66+
*
67+
* Then in your app, you can use the connector to connect with any supported strategy:
68+
*
69+
* ```ts
70+
* const { connect, connectors } = useConnect();
71+
*
72+
* const onClick = () => {
73+
* const inAppWallet = connectors.find((x) => x.id === "in-app-wallet");
74+
* connect({
75+
* connector: inAppWallet, strategy: "google"
76+
* });
77+
* };
78+
* ```
5679
*/
5780
export function inAppWalletConnector(
5881
args: InAppWalletParameters,
5982
): CreateConnectorFn<Provider, Properties, StorageItem> {
60-
const client = createThirdwebClient({ clientId: args.clientId });
6183
const wallet = args.ecosystemId
6284
? ecosystemWallet(args.ecosystemId, { partnerId: args.partnerId })
6385
: thirdwebInAppWallet(args);
86+
const client = args.client;
6487
return createConnector<Provider, Properties, StorageItem>((config) => ({
6588
id: "in-app-wallet",
6689
name: "In-App wallet",
6790
type: "in-app",
6891
connect: async (params) => {
69-
const inAppOptions = params && "client" in params ? params : undefined;
70-
const wagmiConnectOptions =
71-
params && "chainId" in params ? params : undefined;
72-
console.log("inAppOPtions", inAppOptions);
73-
console.log("wagmiConnectOptions", wagmiConnectOptions);
7492
const lastChainId = await config.storage?.getItem("tw.lastChainId");
75-
const chain = defineChain(
76-
wagmiConnectOptions?.chainId || lastChainId || 1,
77-
);
78-
const options = {
93+
if (params?.isReconnecting) {
94+
const account = await wallet.autoConnect({
95+
client,
96+
chain: defineChain(lastChainId || 1),
97+
});
98+
return {
99+
accounts: [getAddress(account.address)],
100+
chainId: lastChainId || 1,
101+
};
102+
}
103+
const inAppOptions = params && "strategy" in params ? params : undefined;
104+
if (!inAppOptions) {
105+
throw new Error(
106+
"Missing strategy prop, pass it to connect() when connecting to this connector",
107+
);
108+
}
109+
const chain =
110+
inAppOptions.chain ||
111+
defineChain(inAppOptions?.chainId || lastChainId || 1);
112+
const decoratedOptions = {
113+
...inAppOptions,
79114
client,
80115
chain,
81-
...args,
82-
} as unknown as InAppWalletConnectionOptions;
83-
const account = wagmiConnectOptions?.isReconnecting
116+
} as InAppWalletConnectionOptions;
117+
const account = inAppOptions?.isReconnecting
84118
? await wallet.autoConnect({
85119
client,
86120
chain,
87121
})
88-
: await wallet.connect(options);
122+
: await wallet.connect(decoratedOptions);
89123
await config.storage?.setItem("tw.lastChainId", chain.id);
90124
return { accounts: [getAddress(account.address)], chainId: chain.id };
91125
},
@@ -103,10 +137,18 @@ export function inAppWalletConnector(
103137
return wallet.getChain()?.id || 1;
104138
},
105139
getProvider: async (params) => {
140+
const lastChainId = await config.storage?.getItem("tw.lastChainId");
141+
const chain = defineChain(params?.chainId || lastChainId || 1);
142+
if (!wallet.getAccount()) {
143+
await wallet.autoConnect({
144+
client,
145+
chain,
146+
});
147+
}
106148
return EIP1193.toProvider({
107149
wallet,
108150
client,
109-
chain: wallet.getChain() || defineChain(params?.chainId || 1),
151+
chain: wallet.getChain() || chain,
110152
});
111153
},
112154
isAuthorized: async () => true,
@@ -129,13 +171,3 @@ export function inAppWalletConnector(
129171
},
130172
}));
131173
}
132-
133-
const c = inAppWalletConnector({
134-
clientId: "...",
135-
strategy: "google",
136-
})({
137-
chains: [sepolia],
138-
emitter: new EventEmitter() as any,
139-
});
140-
141-
c.connect({});

pnpm-lock.yaml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)