/
index.ts
103 lines (88 loc) · 2.47 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import type { State, StoreApi } from 'zustand/vanilla'
import type { EventEmitter } from 'node:events'
export interface Web3ReactState extends State {
chainId: number | undefined
accounts: string[] | undefined
activating: boolean
error: Error | undefined
}
export type Web3ReactStore = StoreApi<Web3ReactState>
export type Web3ReactStateUpdate =
| {
chainId: number
accounts: string[]
}
| {
chainId: number
accounts?: never
}
| {
chainId?: never
accounts: string[]
}
export interface Actions {
startActivation: () => () => void
update: (stateUpdate: Web3ReactStateUpdate) => void
reportError: (error: Error | undefined) => void
}
// per EIP-1193
export interface RequestArguments {
readonly method: string
readonly params?: readonly unknown[] | object
}
// per EIP-1193
export interface Provider extends EventEmitter {
request(args: RequestArguments): Promise<unknown>
}
// per EIP-1193
export interface ProviderConnectInfo {
readonly chainId: string
}
// per EIP-1193
export interface ProviderRpcError extends Error {
message: string
code: number
data?: unknown
}
// per EIP-3085
export interface AddEthereumChainParameter {
chainId: number
chainName: string
nativeCurrency: {
name: string
symbol: string // 2-6 characters long
decimals: 18
}
rpcUrls: string[]
blockExplorerUrls?: string[]
iconUrls?: string[] // Currently ignored.
}
export abstract class Connector {
/**
* An
* EIP-1193 ({@link https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1193.md}) and
* EIP-1102 ({@link https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1102.md}) compliant provider.
* May also comply with EIP-3085 ({@link https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3085.md}).
* This property must be defined while the connector is active.
*/
public provider: Provider | undefined
protected readonly actions: Actions
/**
* @param actions - Methods bound to a zustand store that tracks the state of the connector.
* Actions are used by the connector to report changes in connection status.
*/
constructor(actions: Actions) {
this.actions = actions
}
/**
* Initiate a connection.
*/
public abstract activate(...args: unknown[]): Promise<void> | void
/**
* Initiate a disconnect.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public deactivate(...args: unknown[]): Promise<void> | void {
this.actions.reportError(undefined)
}
}