@farcaster/miniapp-wagmi-connector 0.0.0-canary-20250630212339
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/LICENSE +21 -0
- package/README.md +33 -0
- package/dist/connector.d.ts +445 -0
- package/dist/connector.js +132 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +6 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +37 -0
- package/src/connector.ts +148 -0
- package/src/index.ts +9 -0
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@farcaster/miniapp-wagmi-connector",
|
|
3
|
+
"version": "0.0.0-canary-20250630212339",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/farcasterxyz/frames.git",
|
|
8
|
+
"directory": "packages/miniapp-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.8.3",
|
|
19
|
+
"viem": "^2.21.57",
|
|
20
|
+
"@farcaster/miniapp-sdk": "0.0.0-canary-20250630212339",
|
|
21
|
+
"@farcaster/tsconfig": "0.0.0-canary-20250630212339"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@farcaster/miniapp-sdk": "0.0.0-canary-20250630212339",
|
|
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
|
+
}
|
package/src/connector.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import MiniAppSDK from '@farcaster/miniapp-sdk'
|
|
2
|
+
import {
|
|
3
|
+
ChainNotConfiguredError,
|
|
4
|
+
type Connector,
|
|
5
|
+
createConnector,
|
|
6
|
+
} from '@wagmi/core'
|
|
7
|
+
import { SwitchChainError, fromHex, getAddress, numberToHex } from 'viem'
|
|
8
|
+
|
|
9
|
+
farcasterMiniApp.type = 'farcasterMiniApp'
|
|
10
|
+
|
|
11
|
+
let accountsChanged: Connector['onAccountsChanged'] | undefined
|
|
12
|
+
let chainChanged: Connector['onChainChanged'] | undefined
|
|
13
|
+
let disconnect: Connector['onDisconnect'] | undefined
|
|
14
|
+
|
|
15
|
+
export function farcasterMiniApp() {
|
|
16
|
+
return createConnector<typeof MiniAppSDK.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: farcasterMiniApp.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
|
+
let targetChainId = chainId
|
|
30
|
+
if (!targetChainId) {
|
|
31
|
+
const state = (await config.storage?.getItem('state')) ?? {}
|
|
32
|
+
const isChainSupported = config.chains.some(
|
|
33
|
+
(x) => x.id === state.chainId,
|
|
34
|
+
)
|
|
35
|
+
if (isChainSupported) targetChainId = state.chainId
|
|
36
|
+
else targetChainId = config.chains[0]?.id
|
|
37
|
+
}
|
|
38
|
+
if (!targetChainId) throw new Error('No chains found on connector.')
|
|
39
|
+
|
|
40
|
+
if (!accountsChanged) {
|
|
41
|
+
accountsChanged = this.onAccountsChanged.bind(this)
|
|
42
|
+
// @ts-expect-error - provider type is stricter
|
|
43
|
+
provider.on('accountsChanged', accountsChanged)
|
|
44
|
+
}
|
|
45
|
+
if (!chainChanged) {
|
|
46
|
+
chainChanged = this.onChainChanged.bind(this)
|
|
47
|
+
provider.on('chainChanged', chainChanged)
|
|
48
|
+
}
|
|
49
|
+
if (!disconnect) {
|
|
50
|
+
disconnect = this.onDisconnect.bind(this)
|
|
51
|
+
provider.on('disconnect', disconnect)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let currentChainId = await this.getChainId()
|
|
55
|
+
if (targetChainId && currentChainId !== targetChainId) {
|
|
56
|
+
const chain = await this.switchChain!({ chainId: targetChainId })
|
|
57
|
+
currentChainId = chain.id
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
accounts: accounts.map((x) => getAddress(x)),
|
|
62
|
+
chainId: currentChainId,
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
async disconnect() {
|
|
66
|
+
const provider = await this.getProvider()
|
|
67
|
+
|
|
68
|
+
if (accountsChanged) {
|
|
69
|
+
// @ts-expect-error - provider type is stricter
|
|
70
|
+
provider.removeListener('accountsChanged', accountsChanged)
|
|
71
|
+
accountsChanged = undefined
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (chainChanged) {
|
|
75
|
+
provider.removeListener('chainChanged', chainChanged)
|
|
76
|
+
chainChanged = undefined
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (disconnect) {
|
|
80
|
+
provider.removeListener('disconnect', disconnect)
|
|
81
|
+
disconnect = undefined
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
async getAccounts() {
|
|
85
|
+
const provider = await this.getProvider()
|
|
86
|
+
const accounts = await provider.request({
|
|
87
|
+
method: 'eth_accounts',
|
|
88
|
+
})
|
|
89
|
+
return accounts.map((x) => getAddress(x))
|
|
90
|
+
},
|
|
91
|
+
async getChainId() {
|
|
92
|
+
const provider = await this.getProvider()
|
|
93
|
+
const hexChainId = await provider.request({ method: 'eth_chainId' })
|
|
94
|
+
return fromHex(hexChainId, 'number')
|
|
95
|
+
},
|
|
96
|
+
async isAuthorized() {
|
|
97
|
+
try {
|
|
98
|
+
const accounts = await this.getAccounts()
|
|
99
|
+
return !!accounts.length
|
|
100
|
+
} catch {
|
|
101
|
+
return false
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
async switchChain({ chainId }) {
|
|
105
|
+
const provider = await this.getProvider()
|
|
106
|
+
const chain = config.chains.find((x) => x.id === chainId)
|
|
107
|
+
if (!chain) {
|
|
108
|
+
throw new SwitchChainError(new ChainNotConfiguredError())
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
await provider.request({
|
|
112
|
+
method: 'wallet_switchEthereumChain',
|
|
113
|
+
params: [{ chainId: numberToHex(chainId) }],
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
// providers should start emitting these events - remove when hosts have upgraded
|
|
117
|
+
//
|
|
118
|
+
// explicitly emit this event as a workaround for ethereum provider not
|
|
119
|
+
// emitting events, can remove once events are flowing
|
|
120
|
+
config.emitter.emit('change', { chainId })
|
|
121
|
+
|
|
122
|
+
return chain
|
|
123
|
+
},
|
|
124
|
+
onAccountsChanged(accounts) {
|
|
125
|
+
if (accounts.length === 0) {
|
|
126
|
+
this.onDisconnect()
|
|
127
|
+
} else {
|
|
128
|
+
config.emitter.emit('change', {
|
|
129
|
+
accounts: accounts.map((x) => getAddress(x)),
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
onChainChanged(chain) {
|
|
134
|
+
const chainId = Number(chain)
|
|
135
|
+
config.emitter.emit('change', { chainId })
|
|
136
|
+
},
|
|
137
|
+
async onDisconnect() {
|
|
138
|
+
config.emitter.emit('disconnect')
|
|
139
|
+
},
|
|
140
|
+
async getProvider() {
|
|
141
|
+
return MiniAppSDK.wallet.ethProvider
|
|
142
|
+
},
|
|
143
|
+
}))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Backward compatibility
|
|
147
|
+
export const farcasterFrame = farcasterMiniApp
|
|
148
|
+
farcasterFrame.type = 'farcasterFrame' as const
|
package/src/index.ts
ADDED