Skip to content

Commit

Permalink
feat: account property
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Sep 6, 2023
1 parent 352d950 commit 978eef8
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 36 deletions.
18 changes: 18 additions & 0 deletions docs/core/actions/getConnectorClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ const client = getConnectorClient(config)
import { type GetConnectorClientParameters } from '@wagmi/core'
```

### account

`Address | Account | undefined`

Account to use with client. Throws if account is not found on [`connector`](#connector).

::: code-group
```ts [index.ts]
import { getConnectorClient } from '@wagmi/core'
import { config } from './config'

const client = getConnectorClient(config, {
account: '0xd2135CfB216b74109775236E36d4b433F1DF507B', // [!code focus]
})
```
<<< @/snippets/core/config.ts[config.ts]
:::

### chainId

`config['chains'][number]['id'] | undefined`
Expand Down
21 changes: 21 additions & 0 deletions docs/core/actions/sendTransaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ const result = await sendTransaction(config, {
<<< @/snippets/core/config.ts[config.ts]
:::

### account

`Address | Account | undefined`

Account to use when sending transaction. Throws if account is not found on [`connector`](#connector).

::: code-group
```ts [index.ts]
import { sendTransaction } from '@wagmi/core'
import { parseEther } from 'viem'
import { config } from './config'

const result = await sendTransaction(config, {
account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', // [!code focus]
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
value: parseEther('0.01'),
})
```
<<< @/snippets/core/config.ts[config.ts]
:::

### chainId

`config['chains'][number]['id'] | undefined`
Expand Down
19 changes: 19 additions & 0 deletions docs/core/actions/signMessage.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ await signMessage(config, { message: 'hello world' })
import { type SignMessageParameters } from '@wagmi/core'
```

### account

`Address | Account | undefined`

Account to use when signing message. Throws if account is not found on [`connector`](#connector).

::: code-group
```ts [index.ts]
import { signMessage } from '@wagmi/core'
import { config } from './config'

const result = await signMessage(config, {
account: '0xd2135CfB216b74109775236E36d4b433F1DF507B', // [!code focus]
message: 'hello world',
})
```
<<< @/snippets/core/config.ts[config.ts]
:::

### connector

`Connector | undefined`
Expand Down
33 changes: 33 additions & 0 deletions docs/core/actions/signTypedData.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,39 @@ const result = await signTypedData(config, {
import { type SignTypedDataParameters } from '@wagmi/core'
```

### account

`Address | Account | undefined`

Account to use when signing data. Throws if account is not found on [`connector`](#connector).

::: code-group
```ts [index.ts]
import { signTypedData } from '@wagmi/core'
import { config } from './config'
import { types } from './typedData'

const result = await signTypedData(config, {
account: '0xd2135CfB216b74109775236E36d4b433F1DF507B', // [!code focus]
types,
primaryType: 'Mail',
message: {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
})
```
<<< @/snippets/typedData.ts[typedData.ts]
<<< @/snippets/core/config.ts[config.ts]
:::

### connector

`Connector | undefined`
Expand Down
8 changes: 2 additions & 6 deletions packages/core/src/actions/estimateGas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { type Config } from '../createConfig.js'
import type { SelectChains } from '../types/chain.js'
import type {
AccountParameter,
ChainIdParameter,
ConnectorParameter,
} from '../types/properties.js'
Expand All @@ -24,12 +23,8 @@ export type EstimateGasParameters<
chains extends readonly Chain[] = SelectChains<config, chainId>,
> = {
[key in keyof chains]: UnionEvaluate<
UnionLooseOmit<
viem_EstimateGasParameters<chains[key]>,
'account' | 'chain'
> &
UnionLooseOmit<viem_EstimateGasParameters<chains[key], Account>, 'chain'> &
ChainIdParameter<config, chainId> &
AccountParameter &
ConnectorParameter
>
}[number]
Expand All @@ -52,6 +47,7 @@ export async function estimateGas<
if (parameters.account) account = parameters.account
else {
const connectorClient = await getConnectorClient(config, {
account: parameters.account,
chainId,
connector,
})
Expand Down
14 changes: 13 additions & 1 deletion packages/core/src/actions/getConnectorClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { config } from '@wagmi/test'
import { address, config } from '@wagmi/test'
import { expect, test } from 'vitest'

import { connect } from './connect.js'
Expand Down Expand Up @@ -33,3 +33,15 @@ test('behavior: not connected', async () => {
Version: @wagmi/core@x.y.z"
`)
})

test('behavior: account does not exist on connector', async () => {
await connect(config, { connector })
await expect(
getConnectorClient(config, { account: address.usdcHolder }),
).rejects.toThrowErrorMatchingInlineSnapshot(`
"Account \\"0x5414d89a8bf7e99d732bc52f3e6a3ef461c0c078\\" not found for connector \\"Test Connector\\".
Version: @wagmi/core@x.y.z"
`)
await disconnect(config, { connector })
})
28 changes: 20 additions & 8 deletions packages/core/src/actions/getConnectorClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import {
custom,
} from 'viem'

import { parseAccount } from 'viem/utils'
import type { Config, Connection } from '../createConfig.js'
import { ConnectorNotFoundError } from '../errors/config.js'
import {
ConnectorAccountNotFound,
ConnectorNotFoundError,
} from '../errors/config.js'
import type {
AccountParameter,
ChainIdParameter,
ConnectorParameter,
} from '../types/properties.js'
Expand All @@ -18,7 +23,9 @@ import type { Evaluate } from '../types/utils.js'
export type GetConnectorClientParameters<
config extends Config = Config,
chainId extends config['chains'][number]['id'] = config['chains'][number]['id'],
> = Evaluate<ChainIdParameter<config, chainId> & ConnectorParameter>
> = Evaluate<
ChainIdParameter<config, chainId> & ConnectorParameter & AccountParameter
>

export type GetConnectorClientReturnType<
config extends Config = Config,
Expand All @@ -33,7 +40,10 @@ export type GetConnectorClientReturnType<
>
>

export type GetConnectorClientError = ConnectorNotFoundError | Error
export type GetConnectorClientError =
| ConnectorNotFoundError
| ConnectorAccountNotFound
| Error

/** https://alpha.wagmi.sh/core/actions/getConnectorClient */
export async function getConnectorClient<
Expand All @@ -43,8 +53,6 @@ export async function getConnectorClient<
config: config,
parameters: GetConnectorClientParameters<config, chainId> = {},
): Promise<GetConnectorClientReturnType<config, chainId>> {
const { chainId: chainId_ } = parameters

// Get connection
let connection: Connection | undefined
if (parameters.connector) {
Expand All @@ -61,21 +69,25 @@ export async function getConnectorClient<
} else connection = config.state.connections.get(config.state.current!)
if (!connection) throw new ConnectorNotFoundError()

const chainId = chainId_ ?? connection.chainId
type Return = GetConnectorClientReturnType<config, chainId>
const chainId = parameters.chainId ?? connection.chainId

// If connector has custom `getClient` implementation
type Return = GetConnectorClientReturnType<config, chainId>
const connector = connection.connector
if (connector.getClient)
return connector.getClient({ chainId: chainId }) as unknown as Return

// Default using `custom` transport
const account = connection.accounts[0]!
const account = parseAccount(parameters.account ?? connection.accounts[0]!)
const chain = config.chains.find((chain) => chain.id === chainId)
const provider = (await connection.connector.getProvider({ chainId })) as {
request(...args: any): Promise<any>
}

// if account was provided, check that it exists on the connector
if (parameters.account && !connection.accounts.includes(account.address))
throw new ConnectorAccountNotFound({ address: account.address, connector })

return createClient({
account,
chain,
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/actions/sendTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@ test('default', async () => {
).resolves.toMatch(transactionHashRegex)
await disconnect(config, { connector })
})

test('behavior: account does not exist on connector', async () => {
await connect(config, { connector })
await expect(
sendTransaction(config, {
account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
value: parseEther('0.01'),
}),
).rejects.toThrowErrorMatchingInlineSnapshot(`
"Account \\"0xA0Cf798816D4b9b9866b5330EEa46a18382f251e\\" not found for connector \\"Test Connector\\".
Version: @wagmi/core@x.y.z"
`)
await disconnect(config, { connector })
})
10 changes: 7 additions & 3 deletions packages/core/src/actions/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type SendTransactionParameters<
[key in keyof chains]: Evaluate<
Omit<
viem_SendTransactionParameters<chains[key], Account, chains[key]>,
'account' | 'chain'
'chain'
> &
ChainIdParameter<config, chainId> &
ConnectorParameter & {
Expand All @@ -47,9 +47,13 @@ export async function sendTransaction<
config: config,
parameters: SendTransactionParameters<config, chainId>,
): Promise<SendTransactionReturnType> {
const { chainId, connector, ...rest } = parameters
const { account, chainId, connector, ...rest } = parameters

const client = await getConnectorClient(config, { chainId, connector })
const client = await getConnectorClient(config, {
account,
chainId,
connector,
})
if (chainId)
assertActiveChain(config, { activeChainId: client.chain.id, chainId })

Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/actions/signMessage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Account } from 'viem'
import {
type SignMessageParameters as viem_SignMessageParameters,
type SignMessageReturnType as viem_SignMessageReturnType,
Expand All @@ -6,11 +7,11 @@ import {

import { type Config } from '../createConfig.js'
import type { ConnectorParameter } from '../types/properties.js'
import type { Evaluate, Omit } from '../types/utils.js'
import type { Evaluate } from '../types/utils.js'
import { getConnectorClient } from './getConnectorClient.js'

export type SignMessageParameters = Evaluate<
Omit<viem_SignMessageParameters, 'account'> & ConnectorParameter
viem_SignMessageParameters<Account> & ConnectorParameter
>

export type SignMessageReturnType = viem_SignMessageReturnType
Expand All @@ -22,7 +23,7 @@ export async function signMessage(
config: Config,
parameters: SignMessageParameters,
): Promise<SignMessageReturnType> {
const { connector, ...rest } = parameters
const client = await getConnectorClient(config, { connector })
const { account, connector, ...rest } = parameters
const client = await getConnectorClient(config, { account, connector })
return viem_signMessage(client, rest)
}
8 changes: 4 additions & 4 deletions packages/core/src/actions/signTypedData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TypedData } from 'viem'
import type { Account, TypedData } from 'viem'
import {
type SignTypedDataParameters as viem_SignTypedDataParameters,
type SignTypedDataReturnType as viem_SignTypedDataReturnType,
Expand All @@ -12,7 +12,7 @@ import { getConnectorClient } from './getConnectorClient.js'
export type SignTypedDataParameters<
typedData extends TypedData | Record<string, unknown> = TypedData,
primaryType extends keyof typedData | 'EIP712Domain' = keyof typedData,
> = viem_SignTypedDataParameters<typedData, primaryType, never> &
> = viem_SignTypedDataParameters<typedData, primaryType, Account> &
ConnectorParameter

export type SignTypedDataReturnType = viem_SignTypedDataReturnType
Expand All @@ -27,8 +27,8 @@ export async function signTypedData<
config: Config,
parameters: SignTypedDataParameters<typedData, primaryType>,
): Promise<SignTypedDataReturnType> {
const { connector, ...rest } = parameters
const client = await getConnectorClient(config, { connector })
const { account, connector, ...rest } = parameters
const client = await getConnectorClient(config, { account, connector })
return viem_signTypedData(
client,
rest as unknown as viem_SignTypedDataParameters,
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/actions/simulateContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
import { type Config } from '../createConfig.js'
import type { SelectChains } from '../types/chain.js'
import type {
AccountParameter,
ChainIdParameter,
ConnectorParameter,
} from '../types/properties.js'
Expand Down Expand Up @@ -50,11 +49,10 @@ export type SimulateContractParameters<
chains[key],
chains[key]
>,
'account' | 'chain'
'chain'
>
> &
ChainIdParameter<config, chainId> &
AccountParameter &
ConnectorParameter
}[number]

Expand Down
Loading

1 comment on commit 978eef8

@vercel
Copy link

@vercel vercel bot commented on 978eef8 Sep 6, 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.vercel.app
wagmi-v2-git-alpha-wagmi-dev.vercel.app
alpha.wagmi.sh
wagmi-v2-wagmi-dev.vercel.app

Please sign in to comment.