@wagmi/core 2.0.0-alpha.2 → 2.0.0-alpha.3

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.
Files changed (125) hide show
  1. package/dist/esm/actions/connect.js +7 -12
  2. package/dist/esm/actions/connect.js.map +1 -1
  3. package/dist/esm/actions/disconnect.js +2 -0
  4. package/dist/esm/actions/disconnect.js.map +1 -1
  5. package/dist/esm/actions/getBalance.js +11 -4
  6. package/dist/esm/actions/getBalance.js.map +1 -1
  7. package/dist/esm/actions/getConnectorClient.js.map +1 -1
  8. package/dist/esm/actions/getConnectors.js +11 -0
  9. package/dist/esm/actions/getConnectors.js.map +1 -0
  10. package/dist/esm/actions/reconnect.js +1 -1
  11. package/dist/esm/actions/reconnect.js.map +1 -1
  12. package/dist/esm/actions/signTypedData.js.map +1 -1
  13. package/dist/esm/actions/switchAccount.js.map +1 -1
  14. package/dist/esm/actions/waitForTransactionReceipt.js.map +1 -1
  15. package/dist/esm/actions/watchAccount.js.map +1 -1
  16. package/dist/esm/actions/watchBlockNumber.js.map +1 -1
  17. package/dist/esm/actions/watchBlocks.js.map +1 -1
  18. package/dist/esm/actions/watchConnectors.js +9 -0
  19. package/dist/esm/actions/watchConnectors.js.map +1 -0
  20. package/dist/esm/createConfig.js +22 -12
  21. package/dist/esm/createConfig.js.map +1 -1
  22. package/dist/esm/createConnector.js.map +1 -1
  23. package/dist/esm/createStorage.js +2 -2
  24. package/dist/esm/createStorage.js.map +1 -1
  25. package/dist/esm/exports/actions.js +2 -0
  26. package/dist/esm/exports/actions.js.map +1 -1
  27. package/dist/esm/exports/index.js +2 -0
  28. package/dist/esm/exports/index.js.map +1 -1
  29. package/dist/esm/query/estimateGas.js.map +1 -1
  30. package/dist/esm/query/getBalance.js +5 -4
  31. package/dist/esm/query/getBalance.js.map +1 -1
  32. package/dist/esm/query/getTransaction.js +3 -2
  33. package/dist/esm/query/getTransaction.js.map +1 -1
  34. package/dist/esm/query/infiniteReadContracts.js +1 -1
  35. package/dist/esm/query/infiniteReadContracts.js.map +1 -1
  36. package/dist/esm/query/readContract.js +2 -2
  37. package/dist/esm/query/readContract.js.map +1 -1
  38. package/dist/esm/query/readContracts.js +2 -2
  39. package/dist/esm/query/readContracts.js.map +1 -1
  40. package/dist/esm/query/simulateContract.js +5 -1
  41. package/dist/esm/query/simulateContract.js.map +1 -1
  42. package/dist/esm/tsconfig.build.tsbuildinfo +1 -1
  43. package/dist/esm/version.js +1 -1
  44. package/dist/types/actions/connect.d.ts +2 -2
  45. package/dist/types/actions/connect.d.ts.map +1 -1
  46. package/dist/types/actions/disconnect.d.ts +3 -4
  47. package/dist/types/actions/disconnect.d.ts.map +1 -1
  48. package/dist/types/actions/getBalance.d.ts.map +1 -1
  49. package/dist/types/actions/getConnectors.d.ts +5 -0
  50. package/dist/types/actions/getConnectors.d.ts.map +1 -0
  51. package/dist/types/actions/getToken.d.ts +3 -3
  52. package/dist/types/actions/getToken.d.ts.map +1 -1
  53. package/dist/types/actions/multicall.d.ts +1 -1
  54. package/dist/types/actions/multicall.d.ts.map +1 -1
  55. package/dist/types/actions/signTypedData.d.ts +2 -1
  56. package/dist/types/actions/signTypedData.d.ts.map +1 -1
  57. package/dist/types/actions/switchAccount.d.ts.map +1 -1
  58. package/dist/types/actions/switchChain.d.ts +2 -2
  59. package/dist/types/actions/switchChain.d.ts.map +1 -1
  60. package/dist/types/actions/waitForTransactionReceipt.d.ts.map +1 -1
  61. package/dist/types/actions/watchAccount.d.ts.map +1 -1
  62. package/dist/types/actions/watchBlocks.d.ts.map +1 -1
  63. package/dist/types/actions/watchConnectors.d.ts +9 -0
  64. package/dist/types/actions/watchConnectors.d.ts.map +1 -0
  65. package/dist/types/createConfig.d.ts +12 -8
  66. package/dist/types/createConfig.d.ts.map +1 -1
  67. package/dist/types/createConnector.d.ts.map +1 -1
  68. package/dist/types/createStorage.d.ts +1 -1
  69. package/dist/types/createStorage.d.ts.map +1 -1
  70. package/dist/types/errors/base.d.ts +7 -7
  71. package/dist/types/errors/base.d.ts.map +1 -1
  72. package/dist/types/exports/actions.d.ts +2 -0
  73. package/dist/types/exports/actions.d.ts.map +1 -1
  74. package/dist/types/exports/index.d.ts +2 -0
  75. package/dist/types/exports/index.d.ts.map +1 -1
  76. package/dist/types/query/connect.d.ts +1 -1
  77. package/dist/types/query/getBalance.d.ts.map +1 -1
  78. package/dist/types/query/getToken.d.ts +3 -3
  79. package/dist/types/query/getTransaction.d.ts.map +1 -1
  80. package/dist/types/query/infiniteReadContracts.d.ts +4 -16
  81. package/dist/types/query/infiniteReadContracts.d.ts.map +1 -1
  82. package/dist/types/query/readContract.d.ts +3 -3
  83. package/dist/types/query/readContract.d.ts.map +1 -1
  84. package/dist/types/query/readContracts.d.ts +4 -4
  85. package/dist/types/query/readContracts.d.ts.map +1 -1
  86. package/dist/types/query/signTypedData.d.ts +219 -206
  87. package/dist/types/query/signTypedData.d.ts.map +1 -1
  88. package/dist/types/query/simulateContract.d.ts +3 -3
  89. package/dist/types/query/simulateContract.d.ts.map +1 -1
  90. package/dist/types/query/switchChain.d.ts +1 -1
  91. package/dist/types/query/waitForTransactionReceipt.d.ts +1 -1
  92. package/dist/types/version.d.ts +1 -1
  93. package/package.json +1 -1
  94. package/src/actions/connect.ts +9 -15
  95. package/src/actions/disconnect.ts +4 -3
  96. package/src/actions/getBalance.ts +12 -4
  97. package/src/actions/getBlock.ts +1 -1
  98. package/src/actions/getConnectorClient.ts +1 -1
  99. package/src/actions/getConnectors.ts +14 -0
  100. package/src/actions/getToken.ts +3 -3
  101. package/src/actions/multicall.ts +1 -1
  102. package/src/actions/reconnect.ts +1 -1
  103. package/src/actions/signTypedData.ts +5 -7
  104. package/src/actions/switchAccount.ts +1 -0
  105. package/src/actions/switchChain.ts +2 -2
  106. package/src/actions/waitForTransactionReceipt.ts +3 -1
  107. package/src/actions/watchAccount.ts +1 -0
  108. package/src/actions/watchBlockNumber.ts +2 -2
  109. package/src/actions/watchBlocks.ts +2 -5
  110. package/src/actions/watchConnectors.ts +22 -0
  111. package/src/createConfig.ts +75 -54
  112. package/src/createConnector.ts +2 -10
  113. package/src/createStorage.ts +3 -4
  114. package/src/errors/base.ts +5 -5
  115. package/src/exports/actions.ts +11 -0
  116. package/src/exports/index.ts +11 -0
  117. package/src/query/estimateGas.ts +1 -1
  118. package/src/query/getBalance.ts +5 -13
  119. package/src/query/getTransaction.ts +3 -9
  120. package/src/query/infiniteReadContracts.ts +9 -9
  121. package/src/query/readContract.ts +7 -7
  122. package/src/query/readContracts.ts +8 -8
  123. package/src/query/simulateContract.ts +8 -4
  124. package/src/query/waitForTransactionReceipt.ts +1 -1
  125. package/src/version.ts +1 -1
@@ -9,9 +9,9 @@ import type { ChainIdParameter } from '../types/properties.js'
9
9
  import type { Evaluate } from '../types/utils.js'
10
10
 
11
11
  export type ConnectParameters<config extends Config = Config> = Evaluate<
12
- {
12
+ ChainIdParameter<config> & {
13
13
  connector: Connector | CreateConnectorFn
14
- } & ChainIdParameter<config>
14
+ }
15
15
  >
16
16
 
17
17
  export type ConnectReturnType<config extends Config = Config> = {
@@ -22,7 +22,6 @@ export type ConnectReturnType<config extends Config = Config> = {
22
22
  }
23
23
 
24
24
  export type ConnectError =
25
- // connect()
26
25
  | ConnectorAlreadyConnectedError
27
26
  // connector.connect()
28
27
  | UserRejectedRequestError
@@ -39,7 +38,7 @@ export async function connect<config extends Config>(
39
38
  // "Register" connector if not already created
40
39
  let connector: Connector
41
40
  if (typeof parameters.connector === 'function') {
42
- connector = config._internal.setup(parameters.connector)
41
+ connector = config._internal.connectors.setup(parameters.connector)
43
42
  } else connector = parameters.connector
44
43
 
45
44
  // Check if connector is already connected
@@ -69,18 +68,13 @@ export async function connect<config extends Config>(
69
68
  status: 'connected',
70
69
  }))
71
70
 
72
- return {
73
- accounts,
74
- chainId: data.chainId,
75
- }
71
+ return { accounts, chainId: data.chainId }
76
72
  } catch (error) {
77
- config.setState((x) => {
78
- return {
79
- ...x,
80
- // Keep existing connector connected in case of error
81
- status: x.current ? 'connected' : 'disconnected',
82
- }
83
- })
73
+ config.setState((x) => ({
74
+ ...x,
75
+ // Keep existing connector connected in case of error
76
+ status: x.current ? 'connected' : 'disconnected',
77
+ }))
84
78
  throw error
85
79
  }
86
80
  }
@@ -7,10 +7,9 @@ import {
7
7
  ConnectorNotConnectedError,
8
8
  ConnectorNotFoundError,
9
9
  } from '../errors/config.js'
10
+ import type { ConnectorParameter } from '../types/properties.js'
10
11
 
11
- export type DisconnectParameters = {
12
- connector?: Connector | undefined
13
- }
12
+ export type DisconnectParameters = ConnectorParameter
14
13
 
15
14
  export type DisconnectReturnType = void
16
15
 
@@ -41,6 +40,7 @@ export async function disconnect(
41
40
  connections.delete(connector.uid)
42
41
 
43
42
  config.setState((x) => {
43
+ // if no connections exist, move to disconnected state
44
44
  if (connections.size === 0)
45
45
  return {
46
46
  ...x,
@@ -49,6 +49,7 @@ export async function disconnect(
49
49
  status: 'disconnected',
50
50
  }
51
51
 
52
+ // switch over to another connection
52
53
  const nextConnection = connections.values().next().value as Connection
53
54
  return {
54
55
  ...x,
@@ -54,6 +54,7 @@ export async function getBalance<config extends Config>(
54
54
  try {
55
55
  return getTokenBalance(config, {
56
56
  balanceAddress: address,
57
+ chainId,
57
58
  symbolType: 'string',
58
59
  tokenAddress,
59
60
  })
@@ -64,6 +65,7 @@ export async function getBalance<config extends Config>(
64
65
  if (error instanceof ContractFunctionExecutionError) {
65
66
  const balance = await getTokenBalance(config, {
66
67
  balanceAddress: address,
68
+ chainId,
67
69
  symbolType: 'bytes32',
68
70
  tokenAddress,
69
71
  })
@@ -92,6 +94,7 @@ export async function getBalance<config extends Config>(
92
94
 
93
95
  type GetTokenBalanceParameters = {
94
96
  balanceAddress: Address
97
+ chainId?: number | undefined
95
98
  symbolType: 'bytes32' | 'string'
96
99
  tokenAddress: Address
97
100
  unit?: Unit | undefined
@@ -101,7 +104,7 @@ async function getTokenBalance(
101
104
  config: Config,
102
105
  parameters: GetTokenBalanceParameters,
103
106
  ) {
104
- const { balanceAddress, symbolType, tokenAddress, unit } = parameters
107
+ const { balanceAddress, chainId, symbolType, tokenAddress, unit } = parameters
105
108
  const contract = {
106
109
  abi: [
107
110
  {
@@ -131,9 +134,14 @@ async function getTokenBalance(
131
134
  const [value, decimals, symbol] = await readContracts(config, {
132
135
  allowFailure: false,
133
136
  contracts: [
134
- { ...contract, functionName: 'balanceOf', args: [balanceAddress] },
135
- { ...contract, functionName: 'decimals' },
136
- { ...contract, functionName: 'symbol' },
137
+ {
138
+ ...contract,
139
+ functionName: 'balanceOf',
140
+ args: [balanceAddress],
141
+ chainId,
142
+ },
143
+ { ...contract, functionName: 'decimals', chainId },
144
+ { ...contract, functionName: 'symbol', chainId },
137
145
  ] as const,
138
146
  })
139
147
  const formatted = formatUnits(value ?? '0', getUnit(unit ?? decimals))
@@ -57,7 +57,7 @@ export function getBlock<
57
57
  ): Promise<GetBlockReturnType<includeTransactions, blockTag, config, chainId>> {
58
58
  const { chainId } = parameters
59
59
  const client = config.getClient({ chainId })
60
- return viem_getBlock(client, parameters) as {} as Promise<
60
+ return viem_getBlock(client, parameters) as Promise<
61
61
  GetBlockReturnType<includeTransactions, blockTag, config, chainId>
62
62
  >
63
63
  }
@@ -92,5 +92,5 @@ export async function getConnectorClient<
92
92
  chain,
93
93
  name: 'Connector Client',
94
94
  transport: (opts) => custom(provider)({ ...opts, retryCount: 0 }),
95
- }) as unknown as Return
95
+ }) as Return
96
96
  }
@@ -0,0 +1,14 @@
1
+ import type { Config, Connector } from '../createConfig.js'
2
+ import { deepEqual } from '../utils/deepEqual.js'
3
+
4
+ export type GetConnectorsReturnType = readonly Connector[]
5
+
6
+ let previousConnectors: readonly Connector[] = []
7
+
8
+ /** https://alpha.wagmi.sh/core/api/actions/getConnectors */
9
+ export function getConnectors(config: Config): GetConnectorsReturnType {
10
+ const connectors = config.connectors
11
+ if (deepEqual(previousConnectors, connectors)) return previousConnectors
12
+ previousConnectors = connectors
13
+ return connectors
14
+ }
@@ -14,10 +14,10 @@ import { getUnit } from '../utils/getUnit.js'
14
14
  import { readContracts } from './readContracts.js'
15
15
 
16
16
  export type GetTokenParameters<config extends Config = Config> = Evaluate<
17
- {
17
+ ChainIdParameter<config> & {
18
18
  address: Address
19
- formatUnits?: Unit
20
- } & ChainIdParameter<config>
19
+ formatUnits?: Unit | undefined
20
+ }
21
21
  >
22
22
 
23
23
  export type GetTokenReturnType = {
@@ -21,7 +21,7 @@ export type MulticallReturnType<
21
21
 
22
22
  export async function multicall<
23
23
  config extends Config,
24
- const contracts extends readonly unknown[],
24
+ const contracts extends readonly ContractFunctionParameters[],
25
25
  allowFailure extends boolean = true,
26
26
  >(
27
27
  config: config,
@@ -35,7 +35,7 @@ export async function reconnect(
35
35
  let connector: Connector
36
36
  // "Register" connector if not already created
37
37
  if (typeof connector_ === 'function')
38
- connector = config._internal.setup(connector_)
38
+ connector = config._internal.connectors.setup(connector_)
39
39
  else connector = connector_
40
40
  connectors.push(connector)
41
41
  }
@@ -7,6 +7,7 @@ import {
7
7
 
8
8
  import { type Config } from '../createConfig.js'
9
9
  import type { ConnectorParameter } from '../types/properties.js'
10
+ import type { UnionEvaluate } from '../types/utils.js'
10
11
  import { getConnectorClient } from './getConnectorClient.js'
11
12
 
12
13
  export type SignTypedDataParameters<
@@ -14,13 +15,10 @@ export type SignTypedDataParameters<
14
15
  primaryType extends keyof typedData | 'EIP712Domain' = keyof typedData,
15
16
  ///
16
17
  primaryTypes = typedData extends TypedData ? keyof typedData : string,
17
- > = viem_SignTypedDataParameters<
18
- typedData,
19
- primaryType,
20
- Account,
21
- primaryTypes
22
- > &
23
- ConnectorParameter
18
+ > = UnionEvaluate<
19
+ viem_SignTypedDataParameters<typedData, primaryType, Account, primaryTypes> &
20
+ ConnectorParameter
21
+ >
24
22
 
25
23
  export type SignTypedDataReturnType = viem_SignTypedDataReturnType
26
24
 
@@ -22,6 +22,7 @@ export async function switchAccount<config extends Config>(
22
22
  parameters: SwitchAccountParameters,
23
23
  ): Promise<SwitchAccountReturnType<config>> {
24
24
  const { connector } = parameters
25
+
25
26
  const connection = config.state.connections.get(connector.uid)
26
27
  if (!connection) throw new ConnectorNotConnectedError()
27
28
 
@@ -15,9 +15,9 @@ export type SwitchChainParameters<
15
15
  config extends Config = Config,
16
16
  chainId extends config['chains'][number]['id'] = config['chains'][number]['id'],
17
17
  > = Evaluate<
18
- {
18
+ ConnectorParameter & {
19
19
  chainId: chainId | config['chains'][number]['id']
20
- } & ConnectorParameter
20
+ }
21
21
  >
22
22
 
23
23
  export type SwitchChainReturnType<
@@ -44,12 +44,13 @@ export async function waitForTransactionReceipt<
44
44
  parameters: WaitForTransactionReceiptParameters<config, chainId>,
45
45
  ): Promise<WaitForTransactionReceiptReturnType<config, chainId>> {
46
46
  const { chainId, timeout = 0, ...rest } = parameters
47
- const client = config.getClient({ chainId })
48
47
 
48
+ const client = config.getClient({ chainId })
49
49
  const receipt = await viem_waitForTransactionReceipt(client, {
50
50
  ...rest,
51
51
  timeout,
52
52
  })
53
+
53
54
  if (receipt.status === 'reverted') {
54
55
  const txn = await getTransaction(client, { hash: receipt.transactionHash })
55
56
  const code = (await call(client, {
@@ -62,6 +63,7 @@ export async function waitForTransactionReceipt<
62
63
  const reason = hexToString(`0x${code.substring(138)}`)
63
64
  throw new Error(reason)
64
65
  }
66
+
65
67
  return receipt as unknown as WaitForTransactionReceiptReturnType<
66
68
  config,
67
69
  chainId
@@ -17,6 +17,7 @@ export function watchAccount(
17
17
  parameters: WatchAccountParameters,
18
18
  ): WatchAccountReturnType {
19
19
  const { onChange } = parameters
20
+
20
21
  return config.subscribe(() => getAccount(config), onChange, {
21
22
  equalityFn(a, b) {
22
23
  const { connector: aConnector, ...aRest } = a
@@ -44,7 +44,7 @@ export function watchBlockNumber<
44
44
  parameters: WatchBlockNumberParameters<config, chainId>,
45
45
  ): WatchBlockNumberReturnType {
46
46
  const { syncConnectedChain = config._internal.syncConnectedChain, ...rest } =
47
- parameters
47
+ parameters as WatchBlockNumberParameters
48
48
 
49
49
  let unwatch: WatchBlockNumberReturnType | undefined
50
50
  const listener = (chainId: number | undefined) => {
@@ -53,7 +53,7 @@ export function watchBlockNumber<
53
53
  const client = config.getClient({ chainId })
54
54
  unwatch = viem_watchBlockNumber(
55
55
  client,
56
- rest as unknown as viem_WatchBlockNumberParameters,
56
+ rest as viem_WatchBlockNumberParameters,
57
57
  )
58
58
  return unwatch
59
59
  }
@@ -56,17 +56,14 @@ export function watchBlocks<
56
56
  >,
57
57
  ): WatchBlocksReturnType {
58
58
  const { syncConnectedChain = config._internal.syncConnectedChain, ...rest } =
59
- parameters
59
+ parameters as WatchBlocksParameters
60
60
 
61
61
  let unwatch: WatchBlocksReturnType | undefined
62
62
  const listener = (chainId: number | undefined) => {
63
63
  if (unwatch) unwatch()
64
64
 
65
65
  const client = config.getClient({ chainId })
66
- unwatch = viem_watchBlocks(
67
- client,
68
- rest as unknown as viem_WatchBlocksParameters,
69
- )
66
+ unwatch = viem_watchBlocks(client, rest as viem_WatchBlocksParameters)
70
67
  return unwatch
71
68
  }
72
69
 
@@ -0,0 +1,22 @@
1
+ import type { Config } from '../createConfig.js'
2
+ import { type GetConnectorsReturnType } from './getConnectors.js'
3
+
4
+ export type WatchConnectorsParameters = {
5
+ onChange(
6
+ connections: GetConnectorsReturnType,
7
+ prevConnectors: GetConnectorsReturnType,
8
+ ): void
9
+ }
10
+
11
+ export type WatchConnectorsReturnType = () => void
12
+
13
+ /** https://alpha.wagmi.sh/core/api/actions/watchConnectors */
14
+ export function watchConnectors(
15
+ config: Config,
16
+ parameters: WatchConnectorsParameters,
17
+ ): WatchConnectorsReturnType {
18
+ const { onChange } = parameters
19
+ return config._internal.connectors.subscribe((connectors, prevConnectors) => {
20
+ onChange(Object.values(connectors), prevConnectors)
21
+ })
22
+ }
@@ -55,47 +55,6 @@ export type CreateConfigParameters<
55
55
  >
56
56
  >
57
57
 
58
- export type Config<
59
- chains extends readonly [Chain, ...Chain[]] = readonly [Chain, ...Chain[]],
60
- transports extends Record<chains[number]['id'], Transport> = Record<
61
- chains[number]['id'],
62
- Transport
63
- >,
64
- > = {
65
- readonly chains: chains
66
- readonly connectors: readonly Connector[]
67
- readonly state: State<chains>
68
- readonly storage: Storage | null
69
-
70
- getClient<chainId extends chains[number]['id']>(parameters?: {
71
- chainId?: chainId | chains[number]['id'] | undefined
72
- }): Client<transports[chainId], Extract<chains[number], { id: chainId }>>
73
- setState<tchains extends readonly [Chain, ...Chain[]] = chains>(
74
- value: State<tchains> | ((state: State<tchains>) => State<tchains>),
75
- ): void
76
- subscribe<state>(
77
- selector: (state: State<chains>) => state,
78
- listener: (selectedState: state, previousSelectedState: state) => void,
79
- options?:
80
- | {
81
- equalityFn?: ((a: state, b: state) => boolean) | undefined
82
- fireImmediately?: boolean | undefined
83
- }
84
- | undefined,
85
- ): () => void
86
-
87
- _internal: {
88
- readonly reconnectOnMount: boolean
89
- readonly syncConnectedChain: boolean
90
- readonly transports: transports
91
-
92
- change(data: EventData<ConnectorEventMap, 'change'>): void
93
- connect(data: EventData<ConnectorEventMap, 'connect'>): void
94
- disconnect(data: EventData<ConnectorEventMap, 'disconnect'>): void
95
- setup(connectorFn: CreateConnectorFn): Connector
96
- }
97
- }
98
-
99
58
  export function createConfig<
100
59
  const chains extends readonly [Chain, ...Chain[]],
101
60
  transports extends Record<chains[number]['id'], Transport>,
@@ -120,7 +79,7 @@ export function createConfig<
120
79
  // Set up connectors, clients, etc.
121
80
  /////////////////////////////////////////////////////////////////////////////////////////////////
122
81
 
123
- const connectors = (rest.connectors ?? []).map(setup)
82
+ const connectors = createStore(() => (rest.connectors ?? []).map(setup))
124
83
  function setup(connectorFn: CreateConnectorFn) {
125
84
  // Set up emitter with uid and add to connector so they are "linked" together.
126
85
  const emitter = createEmitter<ConnectorEventMap>(uid())
@@ -205,12 +164,7 @@ export function createConfig<
205
164
  ? persist(() => initialState, {
206
165
  name: 'store',
207
166
  partialize(state) {
208
- return {
209
- chainId: state.chainId,
210
- connections: state.connections,
211
- current: state.current,
212
- status: state.status,
213
- } satisfies PartializedState
167
+ return state satisfies PartializedState
214
168
  },
215
169
  skipHydration: !reconnectOnMount,
216
170
  storage: storage as Storage<Record<string, unknown>>,
@@ -265,7 +219,7 @@ export function createConfig<
265
219
  if (store.getState().status === 'reconnecting') return
266
220
 
267
221
  store.setState((x) => {
268
- const connector = connectors.find((x) => x.uid === data.uid)
222
+ const connector = connectors.getState().find((x) => x.uid === data.uid)
269
223
  if (!connector) return x
270
224
  return {
271
225
  ...x,
@@ -309,13 +263,14 @@ export function createConfig<
309
263
 
310
264
  return {
311
265
  chains: chains as chains,
312
- connectors,
313
- get state() {
314
- return store.getState() as unknown as State<chains>
266
+ get connectors() {
267
+ return connectors.getState()
315
268
  },
316
269
  storage,
317
270
 
318
- getClient,
271
+ get state() {
272
+ return store.getState() as unknown as State<chains>
273
+ },
319
274
  setState(value) {
320
275
  let newState: State
321
276
  if (typeof value === 'function') newState = value(store.getState() as any)
@@ -336,6 +291,8 @@ export function createConfig<
336
291
  )
337
292
  },
338
293
 
294
+ getClient,
295
+
339
296
  _internal: {
340
297
  reconnectOnMount,
341
298
  syncConnectedChain,
@@ -343,7 +300,19 @@ export function createConfig<
343
300
  change,
344
301
  connect,
345
302
  disconnect,
346
- setup,
303
+ connectors: {
304
+ setup,
305
+ setState(value) {
306
+ let newState: Connector[]
307
+ if (typeof value === 'function')
308
+ newState = value(connectors.getState())
309
+ else newState = value
310
+ connectors.setState(newState, true)
311
+ },
312
+ subscribe(listener) {
313
+ return connectors.subscribe(listener)
314
+ },
315
+ },
347
316
  },
348
317
  }
349
318
  }
@@ -352,6 +321,58 @@ export function createConfig<
352
321
  // Types
353
322
  /////////////////////////////////////////////////////////////////////////////////////////////////
354
323
 
324
+ export type Config<
325
+ chains extends readonly [Chain, ...Chain[]] = readonly [Chain, ...Chain[]],
326
+ transports extends Record<chains[number]['id'], Transport> = Record<
327
+ chains[number]['id'],
328
+ Transport
329
+ >,
330
+ > = {
331
+ readonly chains: chains
332
+ readonly connectors: readonly Connector[]
333
+ readonly storage: Storage | null
334
+
335
+ readonly state: State<chains>
336
+ setState<tchains extends readonly [Chain, ...Chain[]] = chains>(
337
+ value: State<tchains> | ((state: State<tchains>) => State<tchains>),
338
+ ): void
339
+ subscribe<state>(
340
+ selector: (state: State<chains>) => state,
341
+ listener: (state: state, previousState: state) => void,
342
+ options?:
343
+ | {
344
+ equalityFn?: ((a: state, b: state) => boolean) | undefined
345
+ fireImmediately?: boolean | undefined
346
+ }
347
+ | undefined,
348
+ ): () => void
349
+
350
+ getClient<chainId extends chains[number]['id']>(parameters?: {
351
+ chainId?: chainId | chains[number]['id'] | undefined
352
+ }): Client<transports[chainId], Extract<chains[number], { id: chainId }>>
353
+
354
+ _internal: {
355
+ readonly reconnectOnMount: boolean
356
+ readonly syncConnectedChain: boolean
357
+ readonly transports: transports
358
+
359
+ change(data: EventData<ConnectorEventMap, 'change'>): void
360
+ connect(data: EventData<ConnectorEventMap, 'connect'>): void
361
+ disconnect(data: EventData<ConnectorEventMap, 'disconnect'>): void
362
+
363
+ connectors: {
364
+ setup(connectorFn: CreateConnectorFn): Connector
365
+ setState(value: Connector[] | ((state: Connector[]) => Connector[])): void
366
+ subscribe(
367
+ listener: (
368
+ state: readonly Connector[],
369
+ prevState: readonly Connector[],
370
+ ) => void,
371
+ ): () => void
372
+ }
373
+ }
374
+ }
375
+
355
376
  export type State<
356
377
  chains extends readonly [Chain, ...Chain[]] = readonly [Chain, ...Chain[]],
357
378
  > = {
@@ -45,18 +45,10 @@ export type CreateConnectorFn<
45
45
  getAccounts(): Promise<readonly Address[]>
46
46
  getChainId(): Promise<number>
47
47
  getProvider(
48
- parameters?:
49
- | {
50
- chainId?: number | undefined
51
- }
52
- | undefined,
48
+ parameters?: { chainId?: number | undefined } | undefined,
53
49
  ): Promise<provider>
54
50
  getClient?(
55
- parameters?:
56
- | {
57
- chainId?: number | undefined
58
- }
59
- | undefined,
51
+ parameters?: { chainId?: number | undefined } | undefined,
60
52
  ): Promise<Client>
61
53
  isAuthorized(): Promise<boolean>
62
54
  switchChain?(parameters: { chainId: number }): Promise<Chain>
@@ -43,7 +43,7 @@ export type CreateStorageParameters = {
43
43
  deserialize?: (<T>(value: string) => T) | undefined
44
44
  key?: string | undefined
45
45
  serialize?: (<T>(value: T) => string) | undefined
46
- storage: Evaluate<BaseStorage>
46
+ storage?: Evaluate<BaseStorage> | undefined
47
47
  }
48
48
 
49
49
  export function createStorage<
@@ -54,12 +54,11 @@ export function createStorage<
54
54
  deserialize = deserialize_,
55
55
  key: prefix = 'wagmi',
56
56
  serialize = serialize_,
57
- storage,
57
+ storage = noopStorage,
58
58
  } = parameters
59
59
 
60
60
  function unwrap<type>(value: type): type | Promise<type> {
61
- if (value instanceof Promise)
62
- return value.then((x) => x).catch(() => undefined)
61
+ if (value instanceof Promise) return value.then((x) => x).catch(() => null)
63
62
  return value
64
63
  }
65
64
 
@@ -2,11 +2,11 @@ import type { Evaluate, OneOf } from '../types/utils.js'
2
2
  import { getVersion } from '../utils/getVersion.js'
3
3
 
4
4
  type BaseErrorOptions = Evaluate<
5
- {
6
- docsPath?: string
7
- docsSlug?: string
8
- metaMessages?: string[]
9
- } & OneOf<{ details?: string } | { cause: BaseError | Error }>
5
+ OneOf<{ details?: string | undefined } | { cause: BaseError | Error }> & {
6
+ docsPath?: string | undefined
7
+ docsSlug?: string | undefined
8
+ metaMessages?: string[] | undefined
9
+ }
10
10
  >
11
11
 
12
12
  export class BaseError extends Error {
@@ -64,6 +64,11 @@ export {
64
64
  getConnections,
65
65
  } from '../actions/getConnections.js'
66
66
 
67
+ export {
68
+ type GetConnectorsReturnType,
69
+ getConnectors,
70
+ } from '../actions/getConnectors.js'
71
+
67
72
  export {
68
73
  type GetConnectorClientError,
69
74
  type GetConnectorClientParameters,
@@ -225,6 +230,12 @@ export {
225
230
  watchConnections,
226
231
  } from '../actions/watchConnections.js'
227
232
 
233
+ export {
234
+ type WatchConnectorsParameters,
235
+ type WatchConnectorsReturnType,
236
+ watchConnectors,
237
+ } from '../actions/watchConnectors.js'
238
+
228
239
  export {
229
240
  type WatchContractEventParameters,
230
241
  type WatchContractEventReturnType,
@@ -82,6 +82,11 @@ export {
82
82
  getConnections,
83
83
  } from '../actions/getConnections.js'
84
84
 
85
+ export {
86
+ type GetConnectorsReturnType,
87
+ getConnectors,
88
+ } from '../actions/getConnectors.js'
89
+
85
90
  export {
86
91
  type GetConnectorClientError,
87
92
  type GetConnectorClientParameters,
@@ -249,6 +254,12 @@ export {
249
254
  watchConnections,
250
255
  } from '../actions/watchConnections.js'
251
256
 
257
+ export {
258
+ type WatchConnectorsParameters,
259
+ type WatchConnectorsReturnType,
260
+ watchConnectors,
261
+ } from '../actions/watchConnectors.js'
262
+
252
263
  export {
253
264
  type WatchContractEventParameters,
254
265
  type WatchContractEventReturnType,
@@ -26,7 +26,7 @@ export function estimateGasQueryOptions<
26
26
  const { account, scopeKey: _, ...parameters } = queryKey[1]
27
27
  if (!account && !connector)
28
28
  throw new Error('account or connector is required')
29
- return estimateGas(config, { account, connector, ...parameters } as any)
29
+ return estimateGas(config, { account, connector, ...(parameters as any) })
30
30
  },
31
31
  queryKey: estimateGasQueryKey(options),
32
32
  } as const satisfies QueryOptions<