Skip to content

Commit

Permalink
feat: add ability to set a nullish account on simulateContract
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Nov 21, 2024
1 parent 373eafa commit c4ba3cf
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-knives-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Added ability to set a nullish `account` on `simulateContract`.
4 changes: 2 additions & 2 deletions site/pages/docs/contract/simulateContract.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,11 @@ const { result } = await publicClient.simulateContract({

### account

- **Type:** `Account | Address`
- **Type:** `Account | Address | null`

The Account to simulate the contract method from.

Accepts a [JSON-RPC Account](/docs/clients/wallet#json-rpc-accounts) or [Local Account (Private Key, etc)](/docs/clients/wallet#local-accounts-private-key-mnemonic-etc).
Accepts a [JSON-RPC Account](/docs/clients/wallet#json-rpc-accounts) or [Local Account (Private Key, etc)](/docs/clients/wallet#local-accounts-private-key-mnemonic-etc). If set to `null`, it is assumed that the transport will handle the filling the sender of the transaction.

```ts
const { result } = await publicClient.simulateContract({
Expand Down
22 changes: 22 additions & 0 deletions src/actions/public/simulateContract.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ test('args: account - no client account, no account arg', async () => {
}>()
})

test('args: account - no client account, nullish account arg', async () => {
const result = await simulateContract(client, {
...args,
account: null,
})

expectTypeOf<Pick<(typeof result)['request'], 'account'>>().toEqualTypeOf<{
account: null
}>()
})

test('args: account - with client account, no account arg', async () => {
const result = await simulateContract(clientWithAccount, {
...args,
Expand All @@ -39,6 +50,17 @@ test('args: account - with client account, no account arg', async () => {
}>()
})

test('args: account - with client account, nullish account arg', async () => {
const result = await simulateContract(clientWithAccount, {
...args,
account: null,
})

expectTypeOf<Pick<(typeof result)['request'], 'account'>>().toEqualTypeOf<{
account: null
}>()
})

test('args: account - no client account, with account arg', async () => {
const result = await simulateContract(client, {
...args,
Expand Down
16 changes: 9 additions & 7 deletions src/actions/public/simulateContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ export type SimulateContractParameters<
> = ContractFunctionArgs<abi, 'nonpayable' | 'payable', functionName>,
chain extends Chain | undefined = Chain | undefined,
chainOverride extends Chain | undefined = Chain | undefined,
accountOverride extends Account | Address | undefined = undefined,
accountOverride extends Account | Address | null | undefined = undefined,
///
derivedChain extends Chain | undefined = DeriveChain<chain, chainOverride>,
> = {
account?: accountOverride | undefined
account?: accountOverride | null | undefined
chain?: chainOverride | undefined
/** Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f). */
dataSuffix?: Hex | undefined
Expand Down Expand Up @@ -131,9 +131,10 @@ export type SimulateContractReturnType<
out chain extends Chain | undefined = Chain | undefined,
out account extends Account | undefined = Account | undefined,
out chainOverride extends Chain | undefined = Chain | undefined,
out accountOverride extends Account | Address | undefined =
out accountOverride extends Account | Address | null | undefined =
| Account
| Address
| null
| undefined,
///
in out minimizedAbi extends Abi = readonly [
Expand All @@ -144,9 +145,10 @@ export type SimulateContractReturnType<
args
>,
],
out resolvedAccount extends Account | undefined = accountOverride extends
out resolvedAccount extends
| Account
| Address
| null
| undefined = accountOverride extends Account | Address | null
? ParseAccount<accountOverride>
: account,
> = {
Expand Down Expand Up @@ -177,7 +179,7 @@ export type SimulateContractReturnType<
args
> & {
chain: DeriveChain<chain, chainOverride>
} & (resolvedAccount extends Account
} & (resolvedAccount extends Account | null
? { account: resolvedAccount }
: { account?: undefined })
>
Expand Down Expand Up @@ -231,7 +233,7 @@ export async function simulateContract<
functionName
>,
chainOverride extends Chain | undefined = undefined,
accountOverride extends Account | Address | undefined = undefined,
accountOverride extends Account | Address | null | undefined = undefined,
>(
client: Client<Transport, chain, account>,
parameters: SimulateContractParameters<
Expand Down
3 changes: 2 additions & 1 deletion src/types/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ export type GetAccountParameter<
>

export type ParseAccount<
accountOrAddress extends Account | Address | undefined =
accountOrAddress extends Account | Address | null | undefined =
| Account
| Address
| null
| undefined,
> = accountOrAddress extends Address
? Prettify<JsonRpcAccount<accountOrAddress>>
Expand Down

0 comments on commit c4ba3cf

Please sign in to comment.