viem 2.33.2 → 2.33.4-canary-20250807091217

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/_cjs/chains/definitions/autheoTestnet.js +28 -0
  3. package/_cjs/chains/definitions/autheoTestnet.js.map +1 -0
  4. package/_cjs/chains/definitions/humanityTestnet.js +27 -0
  5. package/_cjs/chains/definitions/humanityTestnet.js.map +1 -0
  6. package/_cjs/chains/definitions/pulsechain.js +1 -1
  7. package/_cjs/chains/definitions/pulsechain.js.map +1 -1
  8. package/_cjs/chains/index.js +17 -13
  9. package/_cjs/chains/index.js.map +1 -1
  10. package/_cjs/errors/version.js +1 -1
  11. package/_cjs/errors/version.js.map +1 -1
  12. package/_cjs/experimental/erc7811/actions/getAssets.js +89 -0
  13. package/_cjs/experimental/erc7811/actions/getAssets.js.map +1 -0
  14. package/_cjs/experimental/erc7811/decorators/erc7811.js +12 -0
  15. package/_cjs/experimental/erc7811/decorators/erc7811.js.map +1 -0
  16. package/_cjs/experimental/erc7811/index.js +8 -0
  17. package/_cjs/experimental/erc7811/index.js.map +1 -0
  18. package/_cjs/experimental/index.js +3 -1
  19. package/_cjs/experimental/index.js.map +1 -1
  20. package/_cjs/index.js.map +1 -1
  21. package/_cjs/utils/transaction/parseTransaction.js +2 -2
  22. package/_cjs/utils/transaction/parseTransaction.js.map +1 -1
  23. package/_esm/chains/definitions/autheoTestnet.js +25 -0
  24. package/_esm/chains/definitions/autheoTestnet.js.map +1 -0
  25. package/_esm/chains/definitions/humanityTestnet.js +24 -0
  26. package/_esm/chains/definitions/humanityTestnet.js.map +1 -0
  27. package/_esm/chains/definitions/pulsechain.js +1 -1
  28. package/_esm/chains/definitions/pulsechain.js.map +1 -1
  29. package/_esm/chains/index.js +4 -0
  30. package/_esm/chains/index.js.map +1 -1
  31. package/_esm/errors/version.js +1 -1
  32. package/_esm/errors/version.js.map +1 -1
  33. package/_esm/experimental/erc7811/actions/getAssets.js +109 -0
  34. package/_esm/experimental/erc7811/actions/getAssets.js.map +1 -0
  35. package/_esm/experimental/erc7811/decorators/erc7811.js +27 -0
  36. package/_esm/experimental/erc7811/decorators/erc7811.js.map +1 -0
  37. package/_esm/experimental/erc7811/index.js +4 -0
  38. package/_esm/experimental/erc7811/index.js.map +1 -0
  39. package/_esm/experimental/index.js +1 -0
  40. package/_esm/experimental/index.js.map +1 -1
  41. package/_esm/index.js.map +1 -1
  42. package/_esm/utils/transaction/parseTransaction.js +2 -2
  43. package/_esm/utils/transaction/parseTransaction.js.map +1 -1
  44. package/_types/chains/definitions/autheoTestnet.d.ts +39 -0
  45. package/_types/chains/definitions/autheoTestnet.d.ts.map +1 -0
  46. package/_types/chains/definitions/humanityTestnet.d.ts +40 -0
  47. package/_types/chains/definitions/humanityTestnet.d.ts.map +1 -0
  48. package/_types/chains/definitions/pulsechain.d.ts +1 -1
  49. package/_types/chains/index.d.ts +4 -0
  50. package/_types/chains/index.d.ts.map +1 -1
  51. package/_types/errors/version.d.ts +1 -1
  52. package/_types/errors/version.d.ts.map +1 -1
  53. package/_types/experimental/erc7811/actions/getAssets.d.ts +106 -0
  54. package/_types/experimental/erc7811/actions/getAssets.d.ts.map +1 -0
  55. package/_types/experimental/erc7811/decorators/erc7811.d.ts +50 -0
  56. package/_types/experimental/erc7811/decorators/erc7811.d.ts.map +1 -0
  57. package/_types/experimental/erc7811/index.d.ts +3 -0
  58. package/_types/experimental/erc7811/index.d.ts.map +1 -0
  59. package/_types/experimental/index.d.ts +1 -0
  60. package/_types/experimental/index.d.ts.map +1 -1
  61. package/_types/index.d.ts +1 -1
  62. package/_types/index.d.ts.map +1 -1
  63. package/_types/types/eip1193.d.ts +31 -0
  64. package/_types/types/eip1193.d.ts.map +1 -1
  65. package/chains/definitions/autheoTestnet.ts +25 -0
  66. package/chains/definitions/humanityTestnet.ts +24 -0
  67. package/chains/definitions/pulsechain.ts +1 -1
  68. package/chains/index.ts +4 -0
  69. package/errors/version.ts +1 -1
  70. package/experimental/erc7811/actions/getAssets.ts +249 -0
  71. package/experimental/erc7811/decorators/erc7811.ts +80 -0
  72. package/experimental/erc7811/index.ts +9 -0
  73. package/experimental/erc7811/package.json +6 -0
  74. package/experimental/index.ts +5 -0
  75. package/index.ts +2 -0
  76. package/package.json +6 -1
  77. package/types/eip1193.ts +37 -0
  78. package/utils/transaction/parseTransaction.ts +2 -2
@@ -0,0 +1,249 @@
1
+ import type { Address } from 'abitype'
2
+ import {
3
+ type ParseAccountErrorType,
4
+ parseAccount,
5
+ } from '../../../accounts/utils/parseAccount.js'
6
+ import type { Client } from '../../../clients/createClient.js'
7
+ import type { Transport } from '../../../clients/transports/createTransport.js'
8
+ import { ethAddress } from '../../../constants/address.js'
9
+ import { AccountNotFoundError } from '../../../errors/account.js'
10
+ import type { ErrorType } from '../../../errors/utils.js'
11
+ import type { Account, GetAccountParameter } from '../../../types/account.js'
12
+ import type { Chain } from '../../../types/chain.js'
13
+ import type {
14
+ WalletGetAssetsParameters,
15
+ WalletGetAssetsReturnType,
16
+ } from '../../../types/eip1193.js'
17
+ import type { Hex } from '../../../types/misc.js'
18
+ import type { OneOf, Prettify } from '../../../types/utils.js'
19
+ import {
20
+ type HexToBigIntErrorType,
21
+ hexToBigInt,
22
+ } from '../../../utils/encoding/fromHex.js'
23
+ import {
24
+ type NumberToHexErrorType,
25
+ numberToHex,
26
+ } from '../../../utils/encoding/toHex.js'
27
+
28
+ export type Asset<chainIds extends boolean = false> = OneOf<
29
+ CustomAsset | Erc20Asset | Erc721Asset | NativeAsset
30
+ > &
31
+ (chainIds extends true ? { chainIds: readonly number[] } : {})
32
+
33
+ export type GetAssetsParameters<
34
+ aggregate extends
35
+ | boolean
36
+ | ((asset: Asset) => string)
37
+ | undefined = undefined,
38
+ account extends Account | undefined = Account | undefined,
39
+ > = GetAccountParameter<account> & {
40
+ /**
41
+ * Whether or not to aggregate assets across multiple chains,
42
+ * and assign them to a '0' key.
43
+ * @default true
44
+ */
45
+ aggregate?: aggregate | boolean | ((asset: Asset) => string) | undefined
46
+ /** Filter by assets. */
47
+ assets?:
48
+ | {
49
+ [chainId: number]: readonly (
50
+ | {
51
+ address: 'native'
52
+ type: 'native'
53
+ }
54
+ | {
55
+ address: Address
56
+ type: AssetType
57
+ }
58
+ )[]
59
+ }
60
+ | undefined
61
+ /** Filter by asset types. */
62
+ assetTypes?: readonly AssetType[] | undefined
63
+ /** Filter by chain IDs. */
64
+ chainIds?: readonly number[] | undefined
65
+ }
66
+
67
+ export type GetAssetsReturnType<
68
+ aggregate extends
69
+ | boolean
70
+ | ((asset: Asset) => string)
71
+ | undefined = undefined,
72
+ > = {
73
+ [chainId: number]: readonly Asset<false>[]
74
+ } & (aggregate extends false ? {} : { 0: readonly Asset<true>[] })
75
+
76
+ export type GetAssetsErrorType =
77
+ | HexToBigIntErrorType
78
+ | NumberToHexErrorType
79
+ | ParseAccountErrorType
80
+ | ErrorType
81
+
82
+ /**
83
+ * Retrieves assets for a given account from the target Wallet.
84
+ *
85
+ * @example
86
+ * import { createWalletClient, custom } from 'viem'
87
+ * import { mainnet } from 'viem/chains'
88
+ * import { getAssets } from 'viem/experimental'
89
+ *
90
+ * const client = createWalletClient({
91
+ * chain: mainnet,
92
+ * transport: custom(window.ethereum),
93
+ * })
94
+ *
95
+ * const result = await getAssets(client, {
96
+ * account: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef',
97
+ * })
98
+ *
99
+ * @param client - Client to use to make the request.
100
+ * @param parameters - Parameters.
101
+ * @returns Assets for the given account.
102
+ */
103
+ export async function getAssets<
104
+ chain extends Chain | undefined,
105
+ account extends Account | undefined = Account | undefined,
106
+ aggregate extends
107
+ | boolean
108
+ | ((asset: Asset) => string)
109
+ | undefined = undefined,
110
+ >(
111
+ client: Client<Transport, chain, account>,
112
+ ...[parameters]: account extends Account
113
+ ? [GetAssetsParameters<aggregate, account>] | []
114
+ : [GetAssetsParameters<aggregate, account>]
115
+ ): Promise<Prettify<GetAssetsReturnType<aggregate>>> {
116
+ const { aggregate = true } = parameters ?? {}
117
+
118
+ const result = await client.request({
119
+ method: 'wallet_getAssets',
120
+ params: [formatRequest(parameters)],
121
+ })
122
+ const response = formatResponse(result)
123
+
124
+ const aggregated = (() => {
125
+ if (!aggregate) return undefined
126
+ const aggregated = {} as Record<string, Asset<boolean>>
127
+ for (const [chainId, assets] of Object.entries(response)) {
128
+ if (chainId === '0') continue
129
+ for (const asset of assets) {
130
+ const key =
131
+ typeof aggregate === 'function'
132
+ ? aggregate(asset)
133
+ : (asset.address ?? ethAddress)
134
+ const item = (aggregated[key] ?? {}) as Asset<true>
135
+ aggregated[key] = {
136
+ ...asset,
137
+ balance: asset.balance + (item?.balance ?? 0n),
138
+ chainIds: [...(item?.chainIds ?? []), Number(chainId)],
139
+ }
140
+ }
141
+ }
142
+ return Object.values(aggregated)
143
+ })()
144
+
145
+ if (aggregated) return { 0: aggregated, ...response } as never
146
+ return response as never
147
+ }
148
+
149
+ /** @internal */
150
+ type AssetType = 'native' | 'erc20' | 'erc721' | (string & {})
151
+
152
+ /** @internal */
153
+ type CustomAsset = {
154
+ address: Address
155
+ balance: bigint
156
+ metadata: {
157
+ [key: string]: unknown
158
+ }
159
+ type: { custom: string }
160
+ }
161
+
162
+ /** @internal */
163
+ type Erc20Asset = {
164
+ address: Address
165
+ balance: bigint
166
+ metadata: {
167
+ name: string
168
+ symbol: string
169
+ decimals: number
170
+ }
171
+ type: 'erc20'
172
+ }
173
+
174
+ /** @internal */
175
+ type Erc721Asset = {
176
+ address: Address
177
+ balance: bigint
178
+ metadata: {
179
+ name: string
180
+ symbol: string
181
+ tokenId: bigint
182
+ tokenUri?: string | undefined
183
+ }
184
+ type: 'erc721'
185
+ }
186
+
187
+ /** @internal */
188
+ type NativeAsset = {
189
+ balance: bigint
190
+ type: 'native'
191
+ }
192
+
193
+ /** @internal */
194
+ function formatRequest(
195
+ parameters: GetAssetsParameters<undefined, Account> | undefined = {},
196
+ ): WalletGetAssetsParameters {
197
+ const { account: account_, assets, assetTypes, chainIds } = parameters
198
+
199
+ if (typeof account_ === 'undefined')
200
+ throw new AccountNotFoundError({
201
+ docsPath: '/experimental/erc7811/getAssets',
202
+ })
203
+ const account = parseAccount(account_)
204
+
205
+ return {
206
+ account: account.address,
207
+ assetFilter: assets,
208
+ assetTypeFilter: assetTypes,
209
+ chainFilter: chainIds?.map((chainId) => numberToHex(chainId)),
210
+ }
211
+ }
212
+
213
+ /** @internal */
214
+ function formatResponse(
215
+ response: WalletGetAssetsReturnType,
216
+ ): GetAssetsReturnType<false> {
217
+ return Object.fromEntries(
218
+ Object.entries(response).map(([chainId, assets]) => [
219
+ Number(chainId),
220
+ assets.map((asset) => {
221
+ const balance = hexToBigInt(asset.balance)
222
+ const metadata = asset.metadata as Asset['metadata']
223
+ const type = (() => {
224
+ if (asset.type === 'native') return 'native'
225
+ if (asset.type === 'erc20') return 'erc20'
226
+ if (asset.type === 'erc721') return 'erc721'
227
+ return { custom: asset.type }
228
+ })()
229
+ const address = type === 'native' ? undefined : asset.address
230
+
231
+ return {
232
+ balance,
233
+ type,
234
+ ...(address ? { address } : {}),
235
+ ...(metadata
236
+ ? {
237
+ metadata: {
238
+ ...metadata,
239
+ ...('tokenId' in metadata
240
+ ? { tokenId: hexToBigInt(metadata.tokenId as Hex) }
241
+ : {}),
242
+ },
243
+ }
244
+ : {}),
245
+ }
246
+ }),
247
+ ]),
248
+ ) as GetAssetsReturnType<false>
249
+ }
@@ -0,0 +1,80 @@
1
+ import type { Client } from '../../../clients/createClient.js'
2
+ import type { Transport } from '../../../clients/transports/createTransport.js'
3
+ import type { Account } from '../../../types/account.js'
4
+ import type { Chain } from '../../../types/chain.js'
5
+ import {
6
+ type Asset,
7
+ type GetAssetsParameters,
8
+ type GetAssetsReturnType,
9
+ getAssets,
10
+ } from '../actions/getAssets.js'
11
+
12
+ export type Erc7811Actions<
13
+ account extends Account | undefined = Account | undefined,
14
+ > = {
15
+ /**
16
+ * Requests to get assets for an account from a wallet.
17
+ *
18
+ * - Docs: https://viem.sh/experimental/erc7811/getAssets
19
+ *
20
+ * @param client - Client to use
21
+ * @param parameters - {@link GetAssetsParameters}
22
+ * @returns List of assets for the given account {@link GetAssetsReturnType}
23
+ *
24
+ * @example
25
+ * import { createWalletClient, custom } from 'viem'
26
+ * import { mainnet } from 'viem/chains'
27
+ * import { erc7811Actions } from 'viem/experimental/erc7811'
28
+ *
29
+ * const client = createWalletClient({
30
+ * chain: mainnet,
31
+ * transport: custom(window.ethereum),
32
+ * }).extend(erc7811Actions())
33
+ *
34
+ * const response = await client.getAssets({
35
+ * account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
36
+ * })
37
+ */
38
+ getAssets: <
39
+ aggregate extends
40
+ | boolean
41
+ | ((asset: Asset) => string)
42
+ | undefined = undefined,
43
+ >(
44
+ ...[parameters]: account extends Account
45
+ ? [GetAssetsParameters<aggregate, account>] | []
46
+ : [GetAssetsParameters<aggregate, account>]
47
+ ) => Promise<GetAssetsReturnType<aggregate>>
48
+ }
49
+
50
+ /**
51
+ * A suite of ERC-7811 Wallet Actions.
52
+ *
53
+ * @example
54
+ * import { createPublicClient, createWalletClient, http } from 'viem'
55
+ * import { mainnet } from 'viem/chains'
56
+ * import { erc7811Actions } from 'viem/experimental/erc7811'
57
+ *
58
+ * const client = createWalletClient({
59
+ * chain: mainnet,
60
+ * transport: http(),
61
+ * }).extend(erc7811Actions())
62
+ *
63
+ * const response = await client.getAssets({
64
+ * account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
65
+ * })
66
+ */
67
+ export function erc7811Actions() {
68
+ return <
69
+ transport extends Transport,
70
+ chain extends Chain | undefined = Chain | undefined,
71
+ account extends Account | undefined = Account | undefined,
72
+ >(
73
+ client: Client<transport, chain, account>,
74
+ ): Erc7811Actions<account> => {
75
+ return {
76
+ // @ts-expect-error
77
+ getAssets: (...[parameters]) => getAssets(client, parameters),
78
+ }
79
+ }
80
+ }
@@ -0,0 +1,9 @@
1
+ // biome-ignore lint/performance/noBarrelFile: entrypoint
2
+ export {
3
+ type GetAssetsErrorType,
4
+ type GetAssetsParameters,
5
+ type GetAssetsReturnType,
6
+ getAssets,
7
+ } from './actions/getAssets.js'
8
+
9
+ export { type Erc7811Actions, erc7811Actions } from './decorators/erc7811.js'
@@ -0,0 +1,6 @@
1
+ {
2
+ "type": "module",
3
+ "types": "../../_types/experimental/erc7811/index.d.ts",
4
+ "module": "../../_esm/experimental/erc7811/index.js",
5
+ "main": "../../_cjs/experimental/erc7811/index.js"
6
+ }
@@ -181,6 +181,11 @@ export {
181
181
  erc7846Actions,
182
182
  } from './erc7846/decorators/erc7846.js'
183
183
 
184
+ export {
185
+ type Erc7811Actions,
186
+ erc7811Actions,
187
+ } from './erc7811/decorators/erc7811.js'
188
+
184
189
  export {
185
190
  type Erc7895Actions,
186
191
  erc7895Actions,
package/index.ts CHANGED
@@ -1156,6 +1156,8 @@ export type {
1156
1156
  RpcSchema,
1157
1157
  RpcSchemaOverride,
1158
1158
  TestRpcSchema,
1159
+ WalletGetAssetsParameters,
1160
+ WalletGetAssetsReturnType,
1159
1161
  WalletCallReceipt,
1160
1162
  WalletGetCallsStatusReturnType,
1161
1163
  WalletGrantPermissionsParameters,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "viem",
3
3
  "description": "TypeScript Interface for Ethereum",
4
- "version": "2.33.2",
4
+ "version": "2.33.4-canary-20250807091217",
5
5
  "main": "./_cjs/index.js",
6
6
  "module": "./_esm/index.js",
7
7
  "types": "./_types/index.d.ts",
@@ -74,6 +74,11 @@
74
74
  "import": "./_esm/experimental/erc7821/index.js",
75
75
  "default": "./_cjs/experimental/erc7821/index.js"
76
76
  },
77
+ "./experimental/erc7811": {
78
+ "types": "./_types/experimental/erc7811/index.d.ts",
79
+ "import": "./_esm/experimental/erc7811/index.js",
80
+ "default": "./_cjs/experimental/erc7811/index.js"
81
+ },
77
82
  "./experimental/erc7846": {
78
83
  "types": "./_types/experimental/erc7846/index.d.ts",
79
84
  "import": "./_esm/experimental/erc7846/index.js",
package/types/eip1193.ts CHANGED
@@ -176,6 +176,31 @@ export type WalletGrantPermissionsReturnType = {
176
176
  | undefined
177
177
  }
178
178
 
179
+ export type WalletGetAssetsParameters = {
180
+ account: Address
181
+ assetFilter?:
182
+ | {
183
+ [chainId: Hex]: readonly {
184
+ address: Address
185
+ type: 'native' | 'erc20' | 'erc721' | (string & {})
186
+ }[]
187
+ }
188
+ | undefined
189
+ assetTypeFilter?:
190
+ | readonly ('native' | 'erc20' | 'erc721' | (string & {}))[]
191
+ | undefined
192
+ chainFilter?: readonly Hex[] | undefined
193
+ }
194
+
195
+ export type WalletGetAssetsReturnType = {
196
+ [chainId: Hex]: readonly {
197
+ address: Address | 'native'
198
+ balance: Hex
199
+ metadata?: unknown | undefined
200
+ type: 'native' | 'erc20' | 'erc721' | (string & {})
201
+ }[]
202
+ }
203
+
179
204
  export type WalletGetCallsStatusReturnType<
180
205
  capabilities extends Capabilities = Capabilities,
181
206
  numberType = Hex,
@@ -1843,6 +1868,18 @@ export type WalletRpcSchema = [
1843
1868
  Parameters?: undefined
1844
1869
  ReturnType: void
1845
1870
  },
1871
+ /**
1872
+ * @description Returns the assets owned by the wallet.
1873
+ * @link https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7811.md
1874
+ * @example
1875
+ * provider.request({ method: 'wallet_getAssets', params: [...] })
1876
+ * // => { ... }
1877
+ */
1878
+ {
1879
+ Method: 'wallet_getAssets'
1880
+ Parameters?: [WalletGetAssetsParameters]
1881
+ ReturnType: WalletGetAssetsReturnType
1882
+ },
1846
1883
  /**
1847
1884
  * @description Returns the status of a call batch that was sent via `wallet_sendCalls`.
1848
1885
  * @link https://eips.ethereum.org/EIPS/eip-5792
@@ -581,8 +581,8 @@ function parseAuthorizationList(
581
581
 
582
582
  authorizationList.push({
583
583
  address,
584
- chainId: hexToNumber(chainId),
585
- nonce: hexToNumber(nonce),
584
+ chainId: chainId === '0x' ? 0 : hexToNumber(chainId),
585
+ nonce: nonce === '0x' ? 0 : hexToNumber(nonce),
586
586
  ...parseEIP155Signature([yParity, r, s]),
587
587
  })
588
588
  }