@farcaster/frame-wagmi-connector 0.0.0-canary-20250430150627

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/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@farcaster/frame-wagmi-connector",
3
+ "version": "0.0.0-canary-20250430150627",
4
+ "license": "MIT",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/farcasterxyz/frames.git",
8
+ "directory": "packages/frame-wagmi-connector"
9
+ },
10
+ "type": "module",
11
+ "main": "dist/index.js",
12
+ "files": [
13
+ "dist",
14
+ "src"
15
+ ],
16
+ "devDependencies": {
17
+ "@wagmi/core": "^2.16.3",
18
+ "typescript": "^5.7.2",
19
+ "viem": "^2.21.57",
20
+ "@farcaster/frame-sdk": "0.0.0-canary-20250430150627",
21
+ "@farcaster/tsconfig": "0.0.2"
22
+ },
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "peerDependencies": {
27
+ "@farcaster/frame-sdk": "0.0.0-canary-20250430150627",
28
+ "@wagmi/core": "^2.14.1",
29
+ "viem": "^2.21.55"
30
+ },
31
+ "scripts": {
32
+ "clean": "rm -rf dist",
33
+ "prebuild": "npm run clean",
34
+ "build": "tsc",
35
+ "typecheck": "tsc --noEmit"
36
+ }
37
+ }
@@ -0,0 +1,133 @@
1
+ import FrameSDK from '@farcaster/frame-sdk'
2
+ import {
3
+ ChainNotConfiguredError,
4
+ type Connector,
5
+ createConnector,
6
+ } from '@wagmi/core'
7
+ import { SwitchChainError, fromHex, getAddress, numberToHex } from 'viem'
8
+
9
+ farcasterFrame.type = 'farcasterFrame' as const
10
+
11
+ let accountsChanged: Connector['onAccountsChanged'] | undefined
12
+ let chainChanged: Connector['onChainChanged'] | undefined
13
+ let disconnect: Connector['onDisconnect'] | undefined
14
+
15
+ export function farcasterFrame() {
16
+ return createConnector<typeof FrameSDK.wallet.ethProvider>((config) => ({
17
+ id: 'farcaster',
18
+ name: 'Farcaster',
19
+ rdns: 'xyz.farcaster.MiniAppWallet',
20
+ icon: 'https://imagedelivery.net/BXluQx4ige9GuW0Ia56BHw/055c25d6-7fe7-4a49-abf9-49772021cf00/original',
21
+ type: farcasterFrame.type,
22
+
23
+ async connect({ chainId } = {}) {
24
+ const provider = await this.getProvider()
25
+ const accounts = await provider.request({
26
+ method: 'eth_requestAccounts',
27
+ })
28
+
29
+ if (!accountsChanged) {
30
+ accountsChanged = this.onAccountsChanged.bind(this)
31
+ // @ts-expect-error - provider type is stricter
32
+ provider.on('accountsChanged', accountsChanged)
33
+ }
34
+ if (!chainChanged) {
35
+ chainChanged = this.onChainChanged.bind(this)
36
+ provider.on('chainChanged', chainChanged)
37
+ }
38
+ if (!disconnect) {
39
+ disconnect = this.onDisconnect.bind(this)
40
+ provider.on('disconnect', disconnect)
41
+ }
42
+
43
+ let currentChainId = await this.getChainId()
44
+ if (chainId && currentChainId !== chainId) {
45
+ const chain = await this.switchChain!({ chainId })
46
+ currentChainId = chain.id
47
+ }
48
+
49
+ return {
50
+ accounts: accounts.map((x) => getAddress(x)),
51
+ chainId: currentChainId,
52
+ }
53
+ },
54
+ async disconnect() {
55
+ const provider = await this.getProvider()
56
+
57
+ if (accountsChanged) {
58
+ // @ts-expect-error - provider type is stricter
59
+ provider.removeListener('accountsChanged', accountsChanged)
60
+ accountsChanged = undefined
61
+ }
62
+
63
+ if (chainChanged) {
64
+ provider.removeListener('chainChanged', chainChanged)
65
+ chainChanged = undefined
66
+ }
67
+
68
+ if (disconnect) {
69
+ provider.removeListener('disconnect', disconnect)
70
+ disconnect = undefined
71
+ }
72
+ },
73
+ async getAccounts() {
74
+ const provider = await this.getProvider()
75
+ const accounts = await provider.request({
76
+ method: 'eth_accounts',
77
+ })
78
+ return accounts.map((x) => getAddress(x))
79
+ },
80
+ async getChainId() {
81
+ const provider = await this.getProvider()
82
+ const hexChainId = await provider.request({ method: 'eth_chainId' })
83
+ return fromHex(hexChainId, 'number')
84
+ },
85
+ async isAuthorized() {
86
+ try {
87
+ const accounts = await this.getAccounts()
88
+ return !!accounts.length
89
+ } catch {
90
+ return false
91
+ }
92
+ },
93
+ async switchChain({ chainId }) {
94
+ const provider = await this.getProvider()
95
+ const chain = config.chains.find((x) => x.id === chainId)
96
+ if (!chain) {
97
+ throw new SwitchChainError(new ChainNotConfiguredError())
98
+ }
99
+
100
+ await provider.request({
101
+ method: 'wallet_switchEthereumChain',
102
+ params: [{ chainId: numberToHex(chainId) }],
103
+ })
104
+
105
+ // providers should start emitting these events - remove when hosts have upgraded
106
+ //
107
+ // explicitly emit this event as a workaround for ethereum provider not
108
+ // emitting events, can remove once events are flowing
109
+ config.emitter.emit('change', { chainId })
110
+
111
+ return chain
112
+ },
113
+ onAccountsChanged(accounts) {
114
+ if (accounts.length === 0) {
115
+ this.onDisconnect()
116
+ } else {
117
+ config.emitter.emit('change', {
118
+ accounts: accounts.map((x) => getAddress(x)),
119
+ })
120
+ }
121
+ },
122
+ onChainChanged(chain) {
123
+ const chainId = Number(chain)
124
+ config.emitter.emit('change', { chainId })
125
+ },
126
+ async onDisconnect() {
127
+ config.emitter.emit('disconnect')
128
+ },
129
+ async getProvider() {
130
+ return FrameSDK.wallet.ethProvider
131
+ },
132
+ }))
133
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { farcasterFrame } from './connector'
2
+
3
+ export * from './connector'
4
+
5
+ export default farcasterFrame