Skip to content

Commit

Permalink
feat: 5792 updates (#2086)
Browse files Browse the repository at this point in the history
* wip: 5792 updates

* docs

* decorator

* Create fresh-cows-deliver.md
  • Loading branch information
jxom authored Apr 7, 2024
1 parent e6d6e15 commit 2dfa617
Show file tree
Hide file tree
Showing 21 changed files with 400 additions and 98 deletions.
7 changes: 7 additions & 0 deletions .changeset/fresh-cows-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"viem": patch
---

**Experimental:** Added `showCallsStatus`
**Experimental:** Renamed `getCallsReceipt` to `getCallsStatus`
**Experimental:** `getCapabilities` now requires an account.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
description: Returns the status and receipts of a call batch.
---

# getCallsReceipt
# getCallsStatus

Returns the status and receipts of a call batch that was sent via `sendCalls`.

[Read more.](https://github.com/ethereum/EIPs/blob/815028dc634463e1716fc5ce44c019a6040f0bef/EIPS/eip-5792.md#wallet_getcallsreceipt)
[Read more.](https://github.com/ethereum/EIPs/blob/1663ea2e7a683285f977eda51c32cec86553f585/EIPS/eip-5792.md#wallet_getcallsstatus)

:::warning[Warning]
This is an experimental action that is not supported in most wallets. It is recommended to have a fallback mechanism if using this in production.
Expand All @@ -19,7 +19,7 @@ This is an experimental action that is not supported in most wallets. It is reco
```ts twoslash [example.ts]
import { walletClient } from './config'

const { status, receipts } = await walletClient.getCallsReceipt({ // [!code focus:99]
const { status, receipts } = await walletClient.getCallsStatus({ // [!code focus:99]
id: '0x1234567890abcdef',
})
```
Expand All @@ -43,7 +43,7 @@ export const [account] = await walletClient.getAddresses()

## Returns

`WalletGetCallsReceiptReturnType`
`WalletGetCallsStatusReturnType`

Status and receipts of the calls.

Expand Down
52 changes: 50 additions & 2 deletions site/pages/experimental/actions/getCapabilities.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ This is an experimental action that is not supported in most wallets. It is reco
:::code-group

```ts twoslash [example.ts]
import { parseEther } from 'viem'
import { account, walletClient } from './config'

const capabilities = await walletClient.getCapabilities() // [!code focus:99]
const capabilities = await walletClient.getCapabilities({
account,
})
// @log: {
// @log: 8453: {
// @log: paymasterService: {
Expand Down Expand Up @@ -55,9 +56,56 @@ export const [account] = await walletClient.getAddresses()

:::

### Account Hoisting

If you do not wish to pass an `account` to every `getCapabilities`, you can also hoist the Account on the Wallet Client (see `config.ts`).

[Learn more](/docs/clients/wallet#account).

:::code-group

```ts twoslash [example.ts]
import { walletClient } from './config'

const capabilities = await walletClient.getCapabilities()
```

```ts [config.ts] filename="config.ts"
import 'viem/window'
import { createWalletClient, custom } from 'viem'
import { walletActionsEip5792 } from 'viem/experimental'

// Retrieve Account from an EIP-1193 Provider.
const [account] = await window.ethereum!.request({
method: 'eth_requestAccounts'
})

export const walletClient = createWalletClient({
account,
transport: custom(window.ethereum!)
}).extend(walletActionsEip5792())
```

:::

## Returns

`WalletCapabilities`

Capabilities of the wallet.

## Parameters

### account

- **Type:** `Address`

The account to get capabilities for.

```ts twoslash [example.ts]
import { walletClient } from './config'
// ---cut---
const capabilities = await walletClient.getCapabilities({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', // [!code focus]
})
```
2 changes: 1 addition & 1 deletion site/pages/experimental/actions/sendCalls.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const walletClient = createWalletClient({

`string`

The identifier can be any arbitrary string. The only requirement is that for a given session, consumers should be able to call `getCallsReceipt` with this identifier to retreive a batch call status and call receipts.
The identifier can be any arbitrary string. The only requirement is that for a given session, consumers should be able to call `getCallsStatus` with this identifier to retreive a batch call status and call receipts.

## Parameters

Expand Down
55 changes: 55 additions & 0 deletions site/pages/experimental/actions/showCallsStatus.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
description: Requests for the wallet to show information about a call batch.
---

# showCallsStatus

Requests for the wallet to show information about a call batch that was sent via `showCalls`.

[Read more.](https://github.com/ethereum/EIPs/blob/1663ea2e7a683285f977eda51c32cec86553f585/EIPS/eip-5792.md#wallet_showcallsstatus)

:::warning[Warning]
This is an experimental action that is not supported in most wallets. It is recommended to have a fallback mechanism if using this in production.
:::

## Usage

:::code-group

```ts twoslash [example.ts]
import { walletClient } from './config'

await walletClient.showCallsStatus({ // [!code focus:99]
id: '0x1234567890abcdef',
})
```

```ts twoslash [config.ts] filename="config.ts"
import 'viem/window'
// ---cut---
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
import { walletActionsEip5792 } from 'viem/experimental'

export const walletClient = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum!),
}).extend(walletActionsEip5792())

export const [account] = await walletClient.getAddresses()
```

:::

## Returns

`void`

## Parameters

### id

- **Type:** `string`

Identifier of the call batch.

2 changes: 1 addition & 1 deletion site/pages/experimental/actions/writeContracts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const walletClient = createWalletClient({

`string`

The identifier can be any arbitrary string. The only requirement is that for a given session, consumers should be able to call `getCallsReceipt` with this identifier to retreive a batch call status and call receipts.
The identifier can be any arbitrary string. The only requirement is that for a given session, consumers should be able to call `getCallsStatus` with this identifier to retreive a batch call status and call receipts.

## Parameters

Expand Down
8 changes: 6 additions & 2 deletions site/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,8 @@ export const sidebar = {
text: 'EIP-5792',
items: [
{
text: 'getCallsReceipt',
link: '/experimental/actions/getCallsReceipt',
text: 'getCallsStatus',
link: '/experimental/actions/getCallsStatus',
},
{
text: 'getCapabilities',
Expand All @@ -1024,6 +1024,10 @@ export const sidebar = {
text: 'sendCalls',
link: '/experimental/actions/sendCalls',
},
{
text: 'showCallsStatus',
link: '/experimental/actions/showCallsStatus',
},
{
text: 'writeContracts',
link: '/experimental/actions/writeContracts',
Expand Down
2 changes: 1 addition & 1 deletion src/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

- [`8a8fab2ace50a9d31df5d990ae6603c210a67860`](https://github.com/wevm/viem/commit/8a8fab2ace50a9d31df5d990ae6603c210a67860) Thanks [@jxom](https://github.com/jxom)! - Fixed parsing of `stateOverrides`.

- [`03095724427a0662694801392d38d6a7b5dd43c7`](https://github.com/wevm/viem/commit/03095724427a0662694801392d38d6a7b5dd43c7) Thanks [@jxom](https://github.com/jxom)! - (Experimental) Renamed `getCallsStatus` to `getCallsReceipt`
- [`03095724427a0662694801392d38d6a7b5dd43c7`](https://github.com/wevm/viem/commit/03095724427a0662694801392d38d6a7b5dd43c7) Thanks [@jxom](https://github.com/jxom)! - (Experimental) Renamed `getCallsStatus` to `getCallsStatus`

- [#2056](https://github.com/wevm/viem/pull/2056) [`368d8e6526fa32b4d153e78440b428ebcd577c15`](https://github.com/wevm/viem/commit/368d8e6526fa32b4d153e78440b428ebcd577c15) Thanks [@VGau](https://github.com/VGau)! - Added Linea Sepolia chain.
Updated Linea Goerli export name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { WalletCallReceipt } from '../../types/eip1193.js'
import type { Hex } from '../../types/misc.js'
import { getHttpRpcClient, parseEther } from '../../utils/index.js'
import { uid } from '../../utils/uid.js'
import { getCallsReceipt } from './getCallsReceipt.js'
import { getCallsStatus } from './getCallsStatus.js'
import { sendCalls } from './sendCalls.js'

type Uid = string
Expand All @@ -27,7 +27,7 @@ const getClient = ({

const rpcClient = getHttpRpcClient(localHttpUrl)

if (method === 'wallet_getCallsReceipt') {
if (method === 'wallet_getCallsStatus') {
const hashes = calls.get(params)
if (!hashes) return null
const receipts = await Promise.all(
Expand Down Expand Up @@ -118,7 +118,7 @@ test('default', async () => {

await mine(testClient, { blocks: 1 })

const { status, receipts } = await getCallsReceipt(client, { id })
const { status, receipts } = await getCallsStatus(client, { id })
expect(status).toMatchInlineSnapshot(`"CONFIRMED"`)
expect(receipts![0].blockHash).toBeDefined()
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,51 @@ import type { Transport } from '../../clients/transports/createTransport.js'
import type { ErrorType } from '../../errors/utils.js'
import type { Account } from '../../types/account.js'
import type { Chain } from '../../types/chain.js'
import type { WalletGetCallsReceiptReturnType } from '../../types/eip1193.js'
import type { WalletGetCallsStatusReturnType } from '../../types/eip1193.js'
import type { Prettify } from '../../types/utils.js'
import type { RequestErrorType } from '../../utils/buildRequest.js'
import { hexToBigInt } from '../../utils/encoding/fromHex.js'
import { receiptStatuses } from '../../utils/formatters/transactionReceipt.js'

export type GetCallsReceiptParameters = { id: string }
export type GetCallsStatusParameters = { id: string }

export type GetCallsReceiptReturnType = Prettify<
WalletGetCallsReceiptReturnType<bigint, 'success' | 'reverted'>
export type GetCallsStatusReturnType = Prettify<
WalletGetCallsStatusReturnType<bigint, 'success' | 'reverted'>
>

export type GetCallsReceiptErrorType = RequestErrorType | ErrorType
export type GetCallsStatusErrorType = RequestErrorType | ErrorType

/**
* Returns the status of a call batch that was sent via `sendCalls`.
*
* - Docs: https://viem.sh/experimental/actions/getCallsReceipt
* - JSON-RPC Methods: [`wallet_getCallsReceipt`](https://eips.ethereum.org/EIPS/eip-5792)
* - Docs: https://viem.sh/experimental/actions/getCallsStatus
* - JSON-RPC Methods: [`wallet_getCallsStatus`](https://eips.ethereum.org/EIPS/eip-5792)
*
* @param client - Client to use
* @returns Status of the calls. {@link GetCallsReceiptReturnType}
* @returns Status of the calls. {@link GetCallsStatusReturnType}
*
* @example
* import { createWalletClient, custom } from 'viem'
* import { mainnet } from 'viem/chains'
* import { getCallsReceipt } from 'viem/wallet'
* import { getCallsStatus } from 'viem/wallet'
*
* const client = createWalletClient({
* chain: mainnet,
* transport: custom(window.ethereum),
* })
* const { receipts, status } = await getCallsReceipt(client, { id: '0xdeadbeef' })
* const { receipts, status } = await getCallsStatus(client, { id: '0xdeadbeef' })
*/
export async function getCallsReceipt<
export async function getCallsStatus<
chain extends Chain | undefined,
account extends Account | undefined = undefined,
>(
client: Client<Transport, chain, account>,
parameters: GetCallsReceiptParameters,
): Promise<GetCallsReceiptReturnType> {
parameters: GetCallsStatusParameters,
): Promise<GetCallsStatusReturnType> {
const { id } = parameters
const { receipts, status } = await client.request({
method: 'wallet_getCallsReceipt',
params: id,
method: 'wallet_getCallsStatus',
params: [id],
})
return {
status,
Expand Down
43 changes: 38 additions & 5 deletions src/experimental/actions/getCapabilities.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
import { expect, test } from 'vitest'
import { createClient } from '../../clients/createClient.js'
import { accounts } from '../../../test/src/constants.js'
import { type Client, createClient } from '../../clients/createClient.js'
import type { Transport } from '../../clients/transports/createTransport.js'
import { custom } from '../../clients/transports/custom.js'
import type { JsonRpcAccount } from '../../types/account.js'
import type { Chain } from '../../types/chain.js'
import { getCapabilities } from './getCapabilities.js'

const client = createClient({
transport: custom({
async request({ method }) {
async request({ method, params }) {
if (method === 'wallet_getCapabilities')
return {
'0x2105': {
paymasterService: {
supported: true,
supported: params[0] === accounts[0].address,
},
sessionKeys: {
supported: true,
},
},
'0x14A34': {
paymasterService: {
supported: true,
supported: params[0] === accounts[0].address,
},
},
}

return null
},
}),
})

test('default', async () => {
const capabilities = await getCapabilities(client)
const capabilities = await getCapabilities(client, {
account: accounts[0].address,
})
expect(capabilities).toMatchInlineSnapshot(`
{
"8453": {
Expand All @@ -47,3 +54,29 @@ test('default', async () => {
}
`)
})

test('account on client', async () => {
const client_2 = {
...client,
account: accounts[1].address,
} as unknown as Client<Transport, Chain, JsonRpcAccount>

const capabilities = await getCapabilities(client_2)
expect(capabilities).toMatchInlineSnapshot(`
{
"8453": {
"paymasterService": {
"supported": false,
},
"sessionKeys": {
"supported": true,
},
},
"84532": {
"paymasterService": {
"supported": false,
},
},
}
`)
})
Loading

0 comments on commit 2dfa617

Please sign in to comment.