Skip to content

Commit

Permalink
refactor: update addresses type
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Aug 4, 2023
1 parent b2ff103 commit c600a39
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 30 deletions.
2 changes: 1 addition & 1 deletion docs/core/actions/connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ import { type ConnectReturnType } from '@wagmi/core'

### accounts

`readonly Address[]`
`readonly [Address, ...Address[]]`

Connected accounts from connector.

Expand Down
2 changes: 1 addition & 1 deletion docs/core/actions/switchAccount.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import { type SwitchAccountReturnType } from '@wagmi/core'

### accounts

`readonly Address[]`
`readonly [Address, ...Address[]]`

Connected accounts from connector.

Expand Down
2 changes: 1 addition & 1 deletion docs/react/hooks/useConnect.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const packageName = 'wagmi'
const actionName = 'connect'
const typeName = 'Connect'
const mutate = 'connect'
const TData = '{ accounts: readonly Address[]; chainId: number; }'
const TData = '{ accounts: readonly [Address, ...Address[]]; chainId: number; }'
const TError = 'ConnectError'
const TVariables = '{ chainId?: number | undefined; connector?: CreateConnectorFn | Connector | undefined; }'
</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/react/hooks/useReconnect.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const packageName = 'wagmi'
const actionName = 'reconnect'
const typeName = 'Reconnect'
const mutate = 'reconnect'
const TData = '{ accounts: readonly Address[]; chainId: number; connector: Connector }'
const TData = '{ accounts: readonly [Address, ...Address[]]; chainId: number; connector: Connector }'
const TError = 'ReconnectError'
const TVariables = '{ connectors?: (CreateConnectorFn | Connector)[] | undefined; }'
</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/shared/createConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ import { type Connection } from '{{packageName}}'

### accounts

`readonly Address[]`
`readonly [Address, ...Address[]]`

Array of addresses associated with the connection.

Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/actions/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type ConnectParameters<config extends Config = Config> = Evaluate<
>

export type ConnectReturnType<config extends Config = Config> = {
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId:
| config['chains'][number]['id']
| (number extends config['chains'][number]['id'] ? number : number & {})
Expand Down Expand Up @@ -51,6 +51,7 @@ export async function connect<config extends Config>(
connector.emitter.emit('message', { type: 'connecting' })

const data = await connector.connect({ chainId: parameters.chainId })
const accounts = data.accounts as readonly [Address, ...Address[]]

connector.emitter.off('connect', config._internal.connect)
connector.emitter.on('change', config._internal.change)
Expand All @@ -60,7 +61,7 @@ export async function connect<config extends Config>(
config.setState((x) => ({
...x,
connections: new Map(x.connections).set(connector.uid, {
accounts: data.accounts,
accounts,
chainId: data.chainId,
connector: connector,
}),
Expand All @@ -69,7 +70,7 @@ export async function connect<config extends Config>(
}))

return {
accounts: data.accounts,
accounts,
chainId: data.chainId,
}
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/actions/getAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { type Config, type Connector } from '../config.js'
export type GetAccountReturnType =
| {
address: Address
addresses: readonly Address[]
addresses: readonly [Address, ...Address[]]
chainId: number
connector: Connector
isConnected: true
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/actions/getConnectorClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
type Account,
type Address,
type Client,
type Transport,
createClient,
Expand Down Expand Up @@ -50,7 +51,11 @@ export async function getConnectorClient<
connector.getAccounts(),
connector.getChainId(),
])
connection = { accounts, chainId, connector }
connection = {
accounts: accounts as readonly [Address, ...Address[]],
chainId,
connector,
}
} else connection = config.state.connections.get(config.state.current!)
if (!connection) throw new ConnectorNotFoundError()

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/actions/reconnect.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Address } from 'viem'

import type { Config, Connection, Connector } from '../config.js'
import { type CreateConnectorFn } from '../connector.js'
import type { Evaluate } from '../types/utils.js'
Expand Down Expand Up @@ -78,7 +80,7 @@ export async function reconnect(
}
})
connections.push({
accounts: data.accounts,
accounts: data.accounts as readonly [Address, ...Address[]],
chainId: data.chainId,
connector,
})
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/actions/switchAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type SwitchAccountParameters = {
}

export type SwitchAccountReturnType<config extends Config = Config> = {
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId:
| config['chains'][number]['id']
| (number extends config['chains'][number]['id'] ? number : number & {})
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export type PartializedState = Evaluate<
Partial<Pick<State, 'chainId' | 'connections' | 'current' | 'status'>>
>
export type Connection = {
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
connector: Connector
}
Expand Down Expand Up @@ -238,7 +238,9 @@ export function createConfig<
return {
...x,
connections: new Map(x.connections).set(data.uid, {
accounts: data.accounts ?? connection.accounts,
accounts:
(data.accounts as readonly [Address, ...Address[]]) ??
connection.accounts,
chainId: data.chainId ?? connection.chainId,
connector: connection.connector,
}),
Expand All @@ -255,7 +257,7 @@ export function createConfig<
return {
...x,
connections: new Map(x.connections).set(data.uid, {
accounts: data.accounts,
accounts: data.accounts as readonly [Address, ...Address[]],
chainId: data.chainId,
connector: connector,
}),
Expand Down
10 changes: 5 additions & 5 deletions packages/react/src/hooks/useConnect.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ test('context', () => {
connector: Connector | CreateConnectorFn
}>()
expectTypeOf(data).toEqualTypeOf<{
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
}>()
expectTypeOf(context).toEqualTypeOf<typeof contextValue | undefined>()
},
onSettled(data, error, variables, context) {
expectTypeOf(data).toEqualTypeOf<
| {
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
}
| undefined
Expand All @@ -59,7 +59,7 @@ test('context', () => {

expectTypeOf(data).toEqualTypeOf<
| {
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
}
| undefined
Expand Down Expand Up @@ -91,15 +91,15 @@ test('context', () => {
connector: Connector | CreateConnectorFn
}>()
expectTypeOf(data).toEqualTypeOf<{
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
}>()
expectTypeOf(context).toEqualTypeOf<typeof contextValue>()
},
onSettled(data, error, variables, context) {
expectTypeOf(data).toEqualTypeOf<
| {
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
}
| undefined
Expand Down
10 changes: 5 additions & 5 deletions packages/react/src/hooks/useReconnect.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test('context', () => {
>()
expectTypeOf(data).toEqualTypeOf<
{
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
connector: Connector
}[]
Expand All @@ -52,7 +52,7 @@ test('context', () => {
onSettled(data, error, variables, context) {
expectTypeOf(data).toEqualTypeOf<
| {
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
connector: Connector
}[]
Expand All @@ -71,7 +71,7 @@ test('context', () => {

expectTypeOf(data).toEqualTypeOf<
| {
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
connector: Connector
}[]
Expand Down Expand Up @@ -112,7 +112,7 @@ test('context', () => {
>()
expectTypeOf(data).toEqualTypeOf<
| {
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
connector: Connector
}[]
Expand All @@ -122,7 +122,7 @@ test('context', () => {
onSettled(data, error, variables, context) {
expectTypeOf(data).toEqualTypeOf<
| {
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
connector: Connector
}[]
Expand Down
10 changes: 5 additions & 5 deletions packages/react/src/hooks/useSwitchAccount.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ test('context', () => {
onSuccess(data, variables, context) {
expectTypeOf(variables).toEqualTypeOf<{ connector: Connector }>()
expectTypeOf(data).toEqualTypeOf<{
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
}>()
expectTypeOf(context).toEqualTypeOf<typeof contextValue | undefined>()
},
onSettled(data, error, variables, context) {
expectTypeOf(data).toEqualTypeOf<
| {
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
}
| undefined
Expand All @@ -43,7 +43,7 @@ test('context', () => {

expectTypeOf(data).toEqualTypeOf<
| {
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
}
| undefined
Expand All @@ -63,15 +63,15 @@ test('context', () => {
onSuccess(data, variables, context) {
expectTypeOf(variables).toEqualTypeOf<{ connector: Connector }>()
expectTypeOf(data).toEqualTypeOf<{
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
}>()
expectTypeOf(context).toEqualTypeOf<typeof contextValue>()
},
onSettled(data, error, variables, context) {
expectTypeOf(data).toEqualTypeOf<
| {
accounts: readonly Address[]
accounts: readonly [Address, ...Address[]]
chainId: number
}
| undefined
Expand Down

1 comment on commit c600a39

@vercel
Copy link

@vercel vercel bot commented on c600a39 Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

wagmi-v2 – ./docs

wagmi-v2-git-alpha-wagmi-dev.vercel.app
wagmi-v2-wagmi-dev.vercel.app
wagmi-v2.vercel.app
alpha.wagmi.sh

Please sign in to comment.