Skip to content

Commit

Permalink
feat(erc5792): allow nullish account
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Feb 20, 2025
1 parent 9b1229c commit efcdc57
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-suns-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

**Experimental(ERC-5792):** Added support for nullish `account` in `sendCalls`.
4 changes: 2 additions & 2 deletions site/pages/experimental/eip5792/sendCalls.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ The identifier can be any arbitrary string. The only requirement is that for a g

### account

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

The Account to sign & broadcast the call from.
The Account to sign & broadcast the call from. If set to `null`, it is assumed that the wallet will handle filling the sender of the calls.

Accepts a [JSON-RPC Account](/docs/clients/wallet#json-rpc-accounts).

Expand Down
93 changes: 91 additions & 2 deletions src/experimental/eip5792/actions/sendCalls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ const getClient = <chain extends Chain | undefined = undefined>({
const callResult = await rpcClient.request({
body: {
method: 'eth_call',
params: [{ ...call, from: params[0].from }],
params: [
{ ...call, from: params[0].from ?? accounts[0].address },
],
id: 0,
},
})
Expand All @@ -82,7 +84,9 @@ const getClient = <chain extends Chain | undefined = undefined>({
const { result, error } = await rpcClient.request({
body: {
method: 'eth_sendTransaction',
params: [{ ...call, from: params[0].from }],
params: [
{ ...call, from: params[0].from ?? accounts[0].address },
],
id: 0,
},
})
Expand Down Expand Up @@ -274,6 +278,91 @@ test('behavior: chain on client', async () => {
`)
})

test('behavior: inferred account', async () => {
const requests: unknown[] = []

const client = getClient({
onRequest({ params }) {
requests.push(params)
},
})

await reset(testClient, {
blockNumber: 16280770n,
jsonRpcUrl: anvilMainnet.forkUrl,
})

const id_ = await sendCalls(client, {
account: null,
chain: mainnet,
calls: [
{
to: accounts[1].address,
value: parseEther('1'),
},
{
to: accounts[2].address,
},
{
data: '0xcafebabe',
to: accounts[3].address,
value: parseEther('100'),
},
{
abi: wagmiContractConfig.abi,
functionName: 'mint',
to: wagmiContractConfig.address,
},
{
abi: wagmiContractConfig.abi,
functionName: 'mint',
to: wagmiContractConfig.address,
},
],
})

expect(id_).toBeDefined()
expect(requests).toMatchInlineSnapshot(`
[
[
{
"calls": [
{
"data": undefined,
"to": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
"value": "0xde0b6b3a7640000",
},
{
"data": undefined,
"to": "0x3c44cdddb6a900fa2b585dd299e03d12fa4293bc",
"value": undefined,
},
{
"data": "0xcafebabe",
"to": "0x90f79bf6eb2c4f870365e785982e1f101e93b906",
"value": "0x56bc75e2d63100000",
},
{
"data": "0x1249c58b",
"to": "0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2",
"value": undefined,
},
{
"data": "0x1249c58b",
"to": "0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2",
"value": undefined,
},
],
"capabilities": undefined,
"chainId": "0x1",
"from": undefined,
"version": "1.0",
},
],
]
`)
})

test('error: no account', async () => {
const requests: unknown[] = []

Expand Down
10 changes: 5 additions & 5 deletions src/experimental/eip5792/actions/sendCalls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Narrow } from 'abitype'
import type { Address, Narrow } from 'abitype'
import { parseAccount } from '../../../accounts/utils/parseAccount.js'
import type { Client } from '../../../clients/createClient.js'
import type { Transport } from '../../../clients/transports/createTransport.js'
Expand Down Expand Up @@ -31,7 +31,7 @@ export type SendCallsParameters<
| WalletSendCallsParameters<WalletCapabilities>[number]['capabilities']
| undefined
version?: WalletSendCallsParameters[number]['version'] | undefined
} & GetAccountParameter<account>
} & GetAccountParameter<account, Account | Address, true, true>

export type SendCallsReturnType = string

Expand Down Expand Up @@ -85,11 +85,11 @@ export async function sendCalls<
version = '1.0',
} = parameters

if (!account_)
if (typeof account_ === 'undefined')
throw new AccountNotFoundError({
docsPath: '/experimental/eip5792/sendCalls',
})
const account = parseAccount(account_)
const account = account_ ? parseAccount(account_) : null

const calls = parameters.calls.map((call_: unknown) => {
const call = call_ as Call
Expand Down Expand Up @@ -118,7 +118,7 @@ export async function sendCalls<
calls,
capabilities,
chainId: numberToHex(chain!.id),
from: account.address,
from: account?.address,
version,
},
],
Expand Down
4 changes: 1 addition & 3 deletions src/types/eip1193.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,13 @@ export type WalletSendCallsParameters<
> = [
{
calls: readonly {
chainId?: chainId | undefined
to?: Address | undefined
data?: Hex | undefined
value?: quantity | undefined
}[]
capabilities?: capabilities | undefined
/** @deprecated Use `chainId` on `calls` instead. */
chainId?: chainId | undefined
from: Address
from?: Address | undefined
version: string
},
]
Expand Down

0 comments on commit efcdc57

Please sign in to comment.