@farcaster/frame-wagmi-connector 0.0.53 → 1.0.0

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/src/connector.ts DELETED
@@ -1,144 +0,0 @@
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
- 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 FrameSDK.wallet.ethProvider
142
- },
143
- }))
144
- }