viem 2.19.8 → 2.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/_cjs/account-abstraction/accounts/toSmartAccount.js +4 -5
  3. package/_cjs/account-abstraction/accounts/toSmartAccount.js.map +1 -1
  4. package/_cjs/account-abstraction/actions/bundler/estimateUserOperationGas.js +12 -9
  5. package/_cjs/account-abstraction/actions/bundler/estimateUserOperationGas.js.map +1 -1
  6. package/_cjs/account-abstraction/actions/bundler/sendUserOperation.js +14 -8
  7. package/_cjs/account-abstraction/actions/bundler/sendUserOperation.js.map +1 -1
  8. package/_cjs/accounts/privateKeyToAccount.js.map +1 -1
  9. package/_cjs/actions/test/setCode.js +10 -4
  10. package/_cjs/actions/test/setCode.js.map +1 -1
  11. package/_cjs/errors/version.js +1 -1
  12. package/_cjs/experimental/eip5792/actions/sendCalls.js +22 -7
  13. package/_cjs/experimental/eip5792/actions/sendCalls.js.map +1 -1
  14. package/_cjs/experimental/eip5792/actions/writeContracts.js.map +1 -1
  15. package/_cjs/op-stack/actions/getTimeToFinalize.js +7 -1
  16. package/_cjs/op-stack/actions/getTimeToFinalize.js.map +1 -1
  17. package/_cjs/op-stack/actions/getWithdrawalStatus.js +7 -1
  18. package/_cjs/op-stack/actions/getWithdrawalStatus.js.map +1 -1
  19. package/_cjs/zksync/accounts/toMultisigSmartAccount.js +16 -0
  20. package/_cjs/zksync/accounts/toMultisigSmartAccount.js.map +1 -0
  21. package/_cjs/zksync/accounts/toSinglesigSmartAccount.js +15 -0
  22. package/_cjs/zksync/accounts/toSinglesigSmartAccount.js.map +1 -0
  23. package/_cjs/zksync/accounts/toSmartAccount.js +42 -0
  24. package/_cjs/zksync/accounts/toSmartAccount.js.map +1 -0
  25. package/_cjs/zksync/index.js +7 -1
  26. package/_cjs/zksync/index.js.map +1 -1
  27. package/_cjs/zksync/types/account.js +3 -0
  28. package/_cjs/zksync/types/account.js.map +1 -0
  29. package/_esm/account-abstraction/accounts/toSmartAccount.js +4 -5
  30. package/_esm/account-abstraction/accounts/toSmartAccount.js.map +1 -1
  31. package/_esm/account-abstraction/actions/bundler/estimateUserOperationGas.js +12 -9
  32. package/_esm/account-abstraction/actions/bundler/estimateUserOperationGas.js.map +1 -1
  33. package/_esm/account-abstraction/actions/bundler/sendUserOperation.js +14 -8
  34. package/_esm/account-abstraction/actions/bundler/sendUserOperation.js.map +1 -1
  35. package/_esm/accounts/privateKeyToAccount.js.map +1 -1
  36. package/_esm/actions/test/setCode.js +10 -4
  37. package/_esm/actions/test/setCode.js.map +1 -1
  38. package/_esm/errors/version.js +1 -1
  39. package/_esm/experimental/eip5792/actions/sendCalls.js +22 -7
  40. package/_esm/experimental/eip5792/actions/sendCalls.js.map +1 -1
  41. package/_esm/experimental/eip5792/actions/writeContracts.js +2 -0
  42. package/_esm/experimental/eip5792/actions/writeContracts.js.map +1 -1
  43. package/_esm/op-stack/actions/getTimeToFinalize.js +7 -1
  44. package/_esm/op-stack/actions/getTimeToFinalize.js.map +1 -1
  45. package/_esm/op-stack/actions/getWithdrawalStatus.js +7 -1
  46. package/_esm/op-stack/actions/getWithdrawalStatus.js.map +1 -1
  47. package/_esm/zksync/accounts/toMultisigSmartAccount.js +17 -0
  48. package/_esm/zksync/accounts/toMultisigSmartAccount.js.map +1 -0
  49. package/_esm/zksync/accounts/toSinglesigSmartAccount.js +16 -0
  50. package/_esm/zksync/accounts/toSinglesigSmartAccount.js.map +1 -0
  51. package/_esm/zksync/accounts/toSmartAccount.js +43 -0
  52. package/_esm/zksync/accounts/toSmartAccount.js.map +1 -0
  53. package/_esm/zksync/index.js +3 -0
  54. package/_esm/zksync/index.js.map +1 -1
  55. package/_esm/zksync/types/account.js +2 -0
  56. package/_esm/zksync/types/account.js.map +1 -0
  57. package/_types/account-abstraction/accounts/toSmartAccount.d.ts.map +1 -1
  58. package/_types/account-abstraction/accounts/types.d.ts +3 -0
  59. package/_types/account-abstraction/accounts/types.d.ts.map +1 -1
  60. package/_types/account-abstraction/actions/bundler/estimateUserOperationGas.d.ts +6 -4
  61. package/_types/account-abstraction/actions/bundler/estimateUserOperationGas.d.ts.map +1 -1
  62. package/_types/account-abstraction/actions/bundler/sendUserOperation.d.ts +7 -5
  63. package/_types/account-abstraction/actions/bundler/sendUserOperation.d.ts.map +1 -1
  64. package/_types/actions/test/setCode.d.ts.map +1 -1
  65. package/_types/errors/version.d.ts +1 -1
  66. package/_types/experimental/eip5792/actions/sendCalls.d.ts +33 -12
  67. package/_types/experimental/eip5792/actions/sendCalls.d.ts.map +1 -1
  68. package/_types/experimental/eip5792/actions/writeContracts.d.ts +2 -0
  69. package/_types/experimental/eip5792/actions/writeContracts.d.ts.map +1 -1
  70. package/_types/op-stack/actions/getTimeToFinalize.d.ts.map +1 -1
  71. package/_types/op-stack/actions/getWithdrawalStatus.d.ts.map +1 -1
  72. package/_types/types/eip1193.d.ts +23 -9
  73. package/_types/types/eip1193.d.ts.map +1 -1
  74. package/_types/zksync/accounts/toMultisigSmartAccount.d.ts +15 -0
  75. package/_types/zksync/accounts/toMultisigSmartAccount.d.ts.map +1 -0
  76. package/_types/zksync/accounts/toSinglesigSmartAccount.d.ts +15 -0
  77. package/_types/zksync/accounts/toSinglesigSmartAccount.d.ts.map +1 -0
  78. package/_types/zksync/accounts/toSmartAccount.d.ts +19 -0
  79. package/_types/zksync/accounts/toSmartAccount.d.ts.map +1 -0
  80. package/_types/zksync/index.d.ts +4 -0
  81. package/_types/zksync/index.d.ts.map +1 -1
  82. package/_types/zksync/types/account.d.ts +5 -0
  83. package/_types/zksync/types/account.d.ts.map +1 -0
  84. package/account-abstraction/accounts/toSmartAccount.ts +12 -10
  85. package/account-abstraction/accounts/types.ts +3 -0
  86. package/account-abstraction/actions/bundler/estimateUserOperationGas.ts +53 -32
  87. package/account-abstraction/actions/bundler/sendUserOperation.ts +50 -33
  88. package/accounts/privateKeyToAccount.ts +1 -1
  89. package/actions/test/setCode.ts +10 -4
  90. package/errors/version.ts +1 -1
  91. package/experimental/eip5792/actions/sendCalls.ts +107 -21
  92. package/experimental/eip5792/actions/writeContracts.ts +2 -0
  93. package/op-stack/actions/getTimeToFinalize.ts +8 -1
  94. package/op-stack/actions/getWithdrawalStatus.ts +8 -1
  95. package/package.json +1 -1
  96. package/types/eip1193.ts +25 -14
  97. package/zksync/accounts/toMultisigSmartAccount.ts +37 -0
  98. package/zksync/accounts/toSinglesigSmartAccount.ts +30 -0
  99. package/zksync/accounts/toSmartAccount.ts +63 -0
  100. package/zksync/index.ts +15 -0
  101. package/zksync/types/account.ts +5 -0
@@ -7,7 +7,7 @@ import type { BaseError } from '../../../errors/base.js'
7
7
  import type { ErrorType } from '../../../errors/utils.js'
8
8
  import type { Chain } from '../../../types/chain.js'
9
9
  import type { Hex } from '../../../types/misc.js'
10
- import type { Assign, OneOf } from '../../../types/utils.js'
10
+ import type { Assign, MaybeRequired, OneOf } from '../../../types/utils.js'
11
11
  import type { RequestErrorType } from '../../../utils/buildRequest.js'
12
12
  import { getAction } from '../../../utils/getAction.js'
13
13
  import type { SmartAccount } from '../../accounts/types.js'
@@ -47,27 +47,39 @@ export type SendUserOperationParameters<
47
47
  >,
48
48
  _derivedVersion extends
49
49
  EntryPointVersion = DeriveEntryPointVersion<_derivedAccount>,
50
- > = Assign<
51
- UserOperationRequest<_derivedVersion>,
52
- OneOf<{ calls: UserOperationCalls<Narrow<calls>> } | { callData: Hex }> & {
53
- paymaster?:
54
- | Address
55
- | true
56
- | {
57
- /** Retrieves paymaster-related User Operation properties to be used for sending the User Operation. */
58
- getPaymasterData?: PaymasterActions['getPaymasterData'] | undefined
59
- /** Retrieves paymaster-related User Operation properties to be used for gas estimation. */
60
- getPaymasterStubData?:
61
- | PaymasterActions['getPaymasterStubData']
50
+ > = GetSmartAccountParameter<account, accountOverride, false> &
51
+ (
52
+ | UserOperation // Accept a full-formed User Operation.
53
+ | Assign<
54
+ // Accept a partially-formed User Operation (UserOperationRequest) to be filled.
55
+ UserOperationRequest<_derivedVersion>,
56
+ OneOf<
57
+ { calls: UserOperationCalls<Narrow<calls>> } | { callData: Hex }
58
+ > & {
59
+ paymaster?:
60
+ | Address
61
+ | true
62
+ | {
63
+ /** Retrieves paymaster-related User Operation properties to be used for sending the User Operation. */
64
+ getPaymasterData?:
65
+ | PaymasterActions['getPaymasterData']
66
+ | undefined
67
+ /** Retrieves paymaster-related User Operation properties to be used for gas estimation. */
68
+ getPaymasterStubData?:
69
+ | PaymasterActions['getPaymasterStubData']
70
+ | undefined
71
+ }
62
72
  | undefined
73
+ /** Paymaster context to pass to `getPaymasterData` and `getPaymasterStubData` calls. */
74
+ paymasterContext?: unknown | undefined
63
75
  }
64
- | undefined
65
- /** Paymaster context to pass to `getPaymasterData` and `getPaymasterStubData` calls. */
66
- paymasterContext?: unknown
67
- }
68
- > &
69
- GetSmartAccountParameter<account, accountOverride>
70
-
76
+ >
77
+ ) &
78
+ // Allow the EntryPoint address to be overridden, if no Account is provided, it will need to be required.
79
+ MaybeRequired<
80
+ { entryPointAddress?: Address },
81
+ _derivedAccount extends undefined ? true : false
82
+ >
71
83
  export type SendUserOperationReturnType = Hex
72
84
 
73
85
  export type SendUserOperationErrorType =
@@ -111,20 +123,21 @@ export async function sendUserOperation<
111
123
  client: Client<Transport, Chain | undefined, account>,
112
124
  parameters: SendUserOperationParameters<account, accountOverride, calls>,
113
125
  ) {
114
- const { account: account_ = client.account } = parameters
126
+ const { account: account_ = client.account, entryPointAddress } = parameters
115
127
 
116
- if (!account_) throw new AccountNotFoundError()
117
- const account = parseAccount(account_)
128
+ if (!account_ && !parameters.sender) throw new AccountNotFoundError()
129
+ const account = account_ ? parseAccount(account_) : undefined
118
130
 
119
- const request = await getAction(
120
- client,
121
- prepareUserOperation,
122
- 'prepareUserOperation',
123
- )(parameters as unknown as PrepareUserOperationParameters)
131
+ const request = account
132
+ ? await getAction(
133
+ client,
134
+ prepareUserOperation,
135
+ 'prepareUserOperation',
136
+ )(parameters as unknown as PrepareUserOperationParameters)
137
+ : parameters
124
138
 
125
- const signature =
126
- parameters.signature ||
127
- (await account.signUserOperation(request as UserOperation))
139
+ const signature = (parameters.signature ||
140
+ (await account?.signUserOperation(request as UserOperation)))!
128
141
 
129
142
  const rpcParameters = formatUserOperationRequest({
130
143
  ...request,
@@ -135,14 +148,18 @@ export async function sendUserOperation<
135
148
  return await client.request(
136
149
  {
137
150
  method: 'eth_sendUserOperation',
138
- params: [rpcParameters, account.entryPoint.address],
151
+ params: [
152
+ rpcParameters,
153
+ (entryPointAddress ?? account?.entryPoint.address)!,
154
+ ],
139
155
  },
140
156
  { retryCount: 0 },
141
157
  )
142
158
  } catch (error) {
159
+ const calls = (parameters as any).calls
143
160
  throw getUserOperationError(error as BaseError, {
144
161
  ...(request as UserOperation),
145
- calls: parameters.calls,
162
+ ...(calls ? { calls } : {}),
146
163
  signature,
147
164
  })
148
165
  }
@@ -66,7 +66,7 @@ export function privateKeyToAccount(
66
66
  return signTransaction({ privateKey, transaction, serializer })
67
67
  },
68
68
  async signTypedData(typedData) {
69
- return signTypedData({ ...typedData, privateKey })
69
+ return signTypedData({ ...typedData, privateKey } as any)
70
70
  },
71
71
  })
72
72
 
@@ -50,8 +50,14 @@ export async function setCode<
50
50
  client: TestClient<TestClientMode, Transport, chain, account, false>,
51
51
  { address, bytecode }: SetCodeParameters,
52
52
  ) {
53
- await client.request({
54
- method: `${client.mode}_setCode`,
55
- params: [address, bytecode],
56
- })
53
+ if (client.mode === 'ganache')
54
+ await client.request({
55
+ method: 'evm_setAccountCode',
56
+ params: [address, bytecode],
57
+ })
58
+ else
59
+ await client.request({
60
+ method: `${client.mode}_setCode`,
61
+ params: [address, bytecode],
62
+ })
57
63
  }
package/errors/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '2.19.8'
1
+ export const version = '2.20.0'
@@ -1,3 +1,4 @@
1
+ import type { AbiStateMutability, Address, Narrow } from 'abitype'
1
2
  import { parseAccount } from '../../../accounts/utils/parseAccount.js'
2
3
  import type { Client } from '../../../clients/createClient.js'
3
4
  import type { Transport } from '../../../clients/transports/createTransport.js'
@@ -6,13 +7,16 @@ import type { BaseError } from '../../../errors/base.js'
6
7
  import { ChainNotFoundError } from '../../../errors/chain.js'
7
8
  import type { ErrorType } from '../../../errors/utils.js'
8
9
  import type { Account, GetAccountParameter } from '../../../types/account.js'
9
- import type { Chain, GetChainParameter } from '../../../types/chain.js'
10
+ import type { Chain, DeriveChain } from '../../../types/chain.js'
11
+ import type { ContractFunctionParameters } from '../../../types/contract.js'
10
12
  import type {
11
13
  WalletCapabilities,
12
14
  WalletSendCallsParameters,
13
15
  } from '../../../types/eip1193.js'
14
16
  import type { Hex } from '../../../types/misc.js'
15
- import type { OneOf } from '../../../types/utils.js'
17
+ import type { GetMulticallContractParameters } from '../../../types/multicall.js'
18
+ import type { MaybeRequired, OneOf, Prettify } from '../../../types/utils.js'
19
+ import { encodeFunctionData } from '../../../utils/abi/encodeFunctionData.js'
16
20
  import type { RequestErrorType } from '../../../utils/buildRequest.js'
17
21
  import { numberToHex } from '../../../utils/encoding/toHex.js'
18
22
  import { getTransactionError } from '../../../utils/errors/getTransactionError.js'
@@ -21,23 +25,17 @@ export type SendCallsParameters<
21
25
  chain extends Chain | undefined = Chain | undefined,
22
26
  account extends Account | undefined = Account | undefined,
23
27
  chainOverride extends Chain | undefined = Chain | undefined,
28
+ calls extends readonly unknown[] = readonly unknown[],
29
+ //
30
+ _chain extends Chain | undefined = DeriveChain<chain, chainOverride>,
24
31
  > = {
25
- calls: OneOf<
26
- | {
27
- to: Hex
28
- data?: Hex | undefined
29
- value?: bigint | undefined
30
- }
31
- | {
32
- data: Hex
33
- }
34
- >[]
32
+ chain?: chainOverride | Chain | undefined
33
+ calls: Calls<Narrow<calls>, _chain>
35
34
  capabilities?:
36
35
  | WalletSendCallsParameters<WalletCapabilities>[number]['capabilities']
37
36
  | undefined
38
37
  version?: WalletSendCallsParameters[number]['version'] | undefined
39
- } & GetAccountParameter<account> &
40
- GetChainParameter<chain, chainOverride>
38
+ } & GetAccountParameter<account>
41
39
 
42
40
  export type SendCallsReturnType = string
43
41
 
@@ -76,16 +74,16 @@ export type SendCallsErrorType = RequestErrorType | ErrorType
76
74
  * })
77
75
  */
78
76
  export async function sendCalls<
77
+ const calls extends readonly unknown[],
79
78
  chain extends Chain | undefined,
80
79
  account extends Account | undefined = undefined,
81
80
  chainOverride extends Chain | undefined = undefined,
82
81
  >(
83
82
  client: Client<Transport, chain, account>,
84
- parameters: SendCallsParameters<chain, account, chainOverride>,
83
+ parameters: SendCallsParameters<chain, account, chainOverride, calls>,
85
84
  ): Promise<SendCallsReturnType> {
86
85
  const {
87
86
  account: account_ = client.account,
88
- calls,
89
87
  capabilities,
90
88
  chain = client.chain,
91
89
  version = '1.0',
@@ -97,7 +95,27 @@ export async function sendCalls<
97
95
  })
98
96
  const account = parseAccount(account_)
99
97
 
100
- if (!chain) throw new ChainNotFoundError()
98
+ const calls = parameters.calls.map((call_: unknown) => {
99
+ const call = call_ as Call
100
+
101
+ const chainId = call.chain?.id ?? call.chainId ?? chain?.id
102
+ if (!chainId) throw new ChainNotFoundError()
103
+
104
+ const data = call.abi
105
+ ? encodeFunctionData({
106
+ abi: call.abi,
107
+ functionName: call.functionName,
108
+ args: call.args,
109
+ })
110
+ : call.data
111
+
112
+ return {
113
+ chainId: numberToHex(chainId),
114
+ data,
115
+ to: call.to,
116
+ value: call.value ? numberToHex(call.value) : undefined,
117
+ }
118
+ })
101
119
 
102
120
  try {
103
121
  return await client.request(
@@ -105,10 +123,7 @@ export async function sendCalls<
105
123
  method: 'wallet_sendCalls',
106
124
  params: [
107
125
  {
108
- calls: calls.map((call) => ({
109
- ...call,
110
- value: call.value ? numberToHex(call.value) : undefined,
111
- })) as any,
126
+ calls,
112
127
  capabilities,
113
128
  chainId: numberToHex(chain!.id),
114
129
  from: account.address,
@@ -126,3 +141,74 @@ export async function sendCalls<
126
141
  })
127
142
  }
128
143
  }
144
+
145
+ ///////////////////////////////////////////////////////////////////////////////////////////////////
146
+
147
+ type RawCall = { data?: Hex; to?: Address; value?: bigint }
148
+
149
+ type Call<
150
+ chain extends Chain | undefined = Chain | undefined,
151
+ contractFunctionParameters = Omit<ContractFunctionParameters, 'address'>,
152
+ > = OneOf<
153
+ | (contractFunctionParameters & {
154
+ to: Address
155
+ value?: bigint | undefined
156
+ })
157
+ | RawCall
158
+ > &
159
+ OneOf<
160
+ | MaybeRequired<
161
+ { chain?: Chain | undefined },
162
+ chain extends Chain ? false : true
163
+ >
164
+ | MaybeRequired<{ chainId?: number }, chain extends Chain ? false : true>
165
+ >
166
+
167
+ type Calls<
168
+ calls extends readonly unknown[],
169
+ chain extends Chain | undefined,
170
+ ///
171
+ result extends readonly any[] = [],
172
+ > = calls extends readonly [] // no calls, return empty
173
+ ? readonly []
174
+ : calls extends readonly [infer call] // one call left before returning `result`
175
+ ? readonly [
176
+ ...result,
177
+ Prettify<
178
+ Call<
179
+ chain,
180
+ Omit<
181
+ GetMulticallContractParameters<call, AbiStateMutability>,
182
+ 'address'
183
+ >
184
+ >
185
+ >,
186
+ ]
187
+ : calls extends readonly [infer call, ...infer rest] // grab first call and recurse through `rest`
188
+ ? Calls<
189
+ [...rest],
190
+ chain,
191
+ [
192
+ ...result,
193
+ Prettify<
194
+ Call<
195
+ chain,
196
+ Omit<
197
+ GetMulticallContractParameters<call, AbiStateMutability>,
198
+ 'address'
199
+ >
200
+ >
201
+ >,
202
+ ]
203
+ >
204
+ : readonly unknown[] extends calls
205
+ ? calls
206
+ : // If `calls` is *some* array but we couldn't assign `unknown[]` to it, then it must hold some known/homogenous type!
207
+ // use this to infer the param types in the case of Array.map() argument
208
+ calls extends readonly (infer call extends Call<
209
+ chain,
210
+ Omit<ContractFunctionParameters, 'address'>
211
+ >)[]
212
+ ? readonly Prettify<call>[]
213
+ : // Fallback
214
+ readonly Call<chain, Omit<ContractFunctionParameters, 'address'>>[]
@@ -50,6 +50,8 @@ export type WriteContractsErrorType =
50
50
  | ErrorType
51
51
 
52
52
  /**
53
+ * @deprecated Use {@link sendCalls} instead. See https://viem.sh/experimental/eip5792/sendCalls#contract-calls.
54
+ *
53
55
  * Requests for the wallet to sign and broadcast a batch of write contract calls (transactions) to the network.
54
56
  *
55
57
  * - Docs: https://viem.sh/experimental/eip5792/writeContracts
@@ -128,11 +128,18 @@ export async function getTimeToFinalize<
128
128
  return { period: Number(period), seconds, timestamp }
129
129
  }
130
130
 
131
+ const numProofSubmitters = await readContract(client, {
132
+ abi: portal2Abi,
133
+ address: portalAddress,
134
+ functionName: 'numProofSubmitters',
135
+ args: [withdrawalHash],
136
+ }).catch(() => 1n)
137
+
131
138
  const proofSubmitter = await readContract(client, {
132
139
  abi: portal2Abi,
133
140
  address: portalAddress,
134
141
  functionName: 'proofSubmitters',
135
- args: [withdrawalHash, 0n],
142
+ args: [withdrawalHash, numProofSubmitters - 1n],
136
143
  }).catch(() => undefined)
137
144
 
138
145
  const [[_disputeGameProxy, proveTimestamp], proofMaturityDelaySeconds] =
@@ -197,11 +197,18 @@ export async function getWithdrawalStatus<
197
197
  return seconds > 0 ? 'waiting-to-finalize' : 'ready-to-finalize'
198
198
  }
199
199
 
200
+ const numProofSubmitters = await readContract(client, {
201
+ abi: portal2Abi,
202
+ address: portalAddress,
203
+ functionName: 'numProofSubmitters',
204
+ args: [withdrawal.withdrawalHash],
205
+ }).catch(() => 1n)
206
+
200
207
  const proofSubmitter = await readContract(client, {
201
208
  abi: portal2Abi,
202
209
  address: portalAddress,
203
210
  functionName: 'proofSubmitters',
204
- args: [withdrawal.withdrawalHash, 0n],
211
+ args: [withdrawal.withdrawalHash, numProofSubmitters - 1n],
205
212
  }).catch(() => withdrawal.sender)
206
213
 
207
214
  const [disputeGameResult, checkWithdrawalResult, finalizedResult] =
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "viem",
3
3
  "description": "TypeScript Interface for Ethereum",
4
- "version": "2.19.8",
4
+ "version": "2.20.0",
5
5
  "main": "./_cjs/index.js",
6
6
  "module": "./_esm/index.js",
7
7
  "types": "./_types/index.d.ts",
package/types/eip1193.ts CHANGED
@@ -207,18 +207,15 @@ export type WalletSendCallsParameters<
207
207
  quantity extends Quantity | bigint = Quantity,
208
208
  > = [
209
209
  {
210
- calls: OneOf<
211
- | {
212
- to: Address
213
- data?: Hex | undefined
214
- value?: quantity | undefined
215
- }
216
- | {
217
- data: Hex
218
- }
219
- >[]
210
+ calls: readonly {
211
+ chainId?: chainId | undefined
212
+ to?: Address | undefined
213
+ data?: Hex | undefined
214
+ value?: quantity | undefined
215
+ }[]
220
216
  capabilities?: capabilities | undefined
221
- chainId: chainId
217
+ /** @deprecated Use `chainId` on `calls` instead. */
218
+ chainId?: chainId | undefined
222
219
  from: Address
223
220
  version: string
224
221
  },
@@ -1335,7 +1332,7 @@ export type TestRpcSchema<mode extends string> = [
1335
1332
  * @link https://ganache.dev/#evm_setAccountBalance
1336
1333
  */
1337
1334
  {
1338
- Method: `evm_setAccountBalance`
1335
+ Method: 'evm_setAccountBalance'
1339
1336
  Parameters: [
1340
1337
  /** The address of the target account. */
1341
1338
  address: Address,
@@ -1344,12 +1341,26 @@ export type TestRpcSchema<mode extends string> = [
1344
1341
  ]
1345
1342
  ReturnType: void
1346
1343
  },
1344
+ /**
1345
+ * @description Modifies the bytecode stored at an account's address.
1346
+ * @link https://ganache.dev/#evm_setAccountCode
1347
+ */
1348
+ {
1349
+ Method: 'evm_setAccountCode'
1350
+ Parameters: [
1351
+ /** The address of the contract. */
1352
+ address: Address,
1353
+ /** Data bytecode. */
1354
+ data: string,
1355
+ ]
1356
+ ReturnType: void
1357
+ },
1347
1358
  /**
1348
1359
  * @description Enables or disables, based on the single boolean argument, the automatic mining of new blocks with each new transaction submitted to the network.
1349
1360
  * @link https://hardhat.org/hardhat-network/docs/reference#evm_setautomine
1350
1361
  */
1351
1362
  {
1352
- Method: `evm_setAutomine`
1363
+ Method: 'evm_setAutomine'
1353
1364
  Parameters: [boolean]
1354
1365
  ReturnType: void
1355
1366
  },
@@ -1367,7 +1378,7 @@ export type TestRpcSchema<mode extends string> = [
1367
1378
  * @link https://github.com/trufflesuite/ganache/blob/ef1858d5d6f27e4baeb75cccd57fb3dc77a45ae8/src/chains/ethereum/ethereum/RPC-METHODS.md#evm_increasetime
1368
1379
  */
1369
1380
  {
1370
- Method: `evm_increaseTime`
1381
+ Method: 'evm_increaseTime'
1371
1382
  Parameters: [seconds: Quantity]
1372
1383
  ReturnType: Quantity
1373
1384
  },
@@ -0,0 +1,37 @@
1
+ import type { Address } from 'abitype'
2
+
3
+ import { sign } from '../../accounts/utils/sign.js'
4
+ import type { Hex } from '../../types/misc.js'
5
+ import { concatHex } from '../../utils/index.js'
6
+ import type { ZksyncSmartAccount } from '../types/account.js'
7
+ import { toSmartAccount } from './toSmartAccount.js'
8
+
9
+ export type ToMultisigSmartAccountParameters = {
10
+ /** Address of the deployed Account's Contract implementation. */
11
+ address: Address
12
+ /** Array of Private Keys belonging to the owners. */
13
+ privateKeys: readonly Hex[]
14
+ }
15
+
16
+ /**
17
+ * Creates a [ZKsync Smart Account](https://docs.zksync.io/build/developer-reference/account-abstraction/building-smart-accounts)
18
+ * from a Contract Address and an array of Private Keys belonging to the owners.
19
+ */
20
+ export function toMultisigSmartAccount(
21
+ parameters: ToMultisigSmartAccountParameters,
22
+ ): ZksyncSmartAccount {
23
+ const { address, privateKeys } = parameters
24
+
25
+ return toSmartAccount({
26
+ address,
27
+ async sign({ hash }) {
28
+ return concatHex(
29
+ await Promise.all(
30
+ privateKeys.map((privateKey) =>
31
+ sign({ hash, privateKey, to: 'hex' }),
32
+ ),
33
+ ),
34
+ )
35
+ },
36
+ })
37
+ }
@@ -0,0 +1,30 @@
1
+ import type { Address } from 'abitype'
2
+
3
+ import { sign } from '../../accounts/utils/sign.js'
4
+ import type { Hex } from '../../types/misc.js'
5
+ import type { ZksyncSmartAccount } from '../types/account.js'
6
+ import { toSmartAccount } from './toSmartAccount.js'
7
+
8
+ export type ToSinglesigSmartAccountParameters = {
9
+ /** Address of the deployed Account's Contract implementation. */
10
+ address: Address
11
+ /** Private Key of the owner. */
12
+ privateKey: Hex
13
+ }
14
+
15
+ /**
16
+ * Creates a [ZKsync Smart Account](https://docs.zksync.io/build/developer-reference/account-abstraction/building-smart-accounts)
17
+ * from a Contract Address and a Private Key belonging to the owner.
18
+ */
19
+ export function toSinglesigSmartAccount(
20
+ parameters: ToSinglesigSmartAccountParameters,
21
+ ): ZksyncSmartAccount {
22
+ const { address, privateKey } = parameters
23
+
24
+ return toSmartAccount({
25
+ address,
26
+ async sign({ hash }) {
27
+ return sign({ hash, privateKey, to: 'hex' })
28
+ },
29
+ })
30
+ }
@@ -0,0 +1,63 @@
1
+ import type { Address } from 'abitype'
2
+
3
+ import { toAccount } from '../../accounts/toAccount.js'
4
+ import type { ErrorType } from '../../errors/utils.js'
5
+ import type { Hash, Hex } from '../../types/misc.js'
6
+ import { keccak256 } from '../../utils/index.js'
7
+ import { hashMessage } from '../../utils/signature/hashMessage.js'
8
+ import { hashTypedData } from '../../utils/signature/hashTypedData.js'
9
+ import { serializeTransaction } from '../serializers.js'
10
+ import type { ZksyncSmartAccount } from '../types/account.js'
11
+ import type { ZksyncTransactionSerializableEIP712 } from '../types/transaction.js'
12
+
13
+ export type ToSmartAccountParameters = {
14
+ /** Address of the deployed Account's Contract implementation. */
15
+ address: Address
16
+ /** Function to sign a hash. */
17
+ sign: (parameters: { hash: Hash }) => Promise<Hex>
18
+ }
19
+
20
+ export type ToSmartAccountErrorType = ErrorType
21
+
22
+ /**
23
+ * Creates a [ZKsync Smart Account](https://docs.zksync.io/build/developer-reference/account-abstraction/building-smart-accounts)
24
+ * from a Contract Address and a custom sign function.
25
+ */
26
+ export function toSmartAccount(
27
+ parameters: ToSmartAccountParameters,
28
+ ): ZksyncSmartAccount {
29
+ const { address, sign } = parameters
30
+
31
+ const account = toAccount({
32
+ address,
33
+ sign,
34
+ async signMessage({ message }) {
35
+ return sign({
36
+ hash: hashMessage(message),
37
+ })
38
+ },
39
+ async signTransaction(transaction) {
40
+ const signableTransaction = {
41
+ ...transaction,
42
+ from: this.address!,
43
+ } as ZksyncTransactionSerializableEIP712
44
+
45
+ return serializeTransaction({
46
+ ...signableTransaction,
47
+ customSignature: await sign({
48
+ hash: keccak256(serializeTransaction(signableTransaction)),
49
+ }),
50
+ })
51
+ },
52
+ async signTypedData(typedData) {
53
+ return sign({
54
+ hash: hashTypedData(typedData),
55
+ })
56
+ },
57
+ })
58
+
59
+ return {
60
+ ...account,
61
+ source: 'smartAccountZksync',
62
+ } as ZksyncSmartAccount
63
+ }
package/zksync/index.ts CHANGED
@@ -1,4 +1,18 @@
1
1
  // biome-ignore lint/performance/noBarrelFile: entrypoint module
2
+ export {
3
+ type ToSmartAccountErrorType,
4
+ type ToSmartAccountParameters,
5
+ toSmartAccount,
6
+ } from './accounts/toSmartAccount.js'
7
+ export {
8
+ type ToMultisigSmartAccountParameters,
9
+ toMultisigSmartAccount,
10
+ } from './accounts/toMultisigSmartAccount.js'
11
+ export {
12
+ type ToSinglesigSmartAccountParameters,
13
+ toSinglesigSmartAccount,
14
+ } from './accounts/toSinglesigSmartAccount.js'
15
+
2
16
  export {
3
17
  type DeployContractErrorType,
4
18
  type DeployContractParameters,
@@ -131,6 +145,7 @@ export {
131
145
 
132
146
  export { serializeTransaction } from './serializers.js'
133
147
 
148
+ export type { ZksyncSmartAccount } from './types/account.js'
134
149
  export type {
135
150
  /** @deprecated Use `ZksyncBlock` instead */
136
151
  ZksyncBlock as ZkSyncBlock,
@@ -0,0 +1,5 @@
1
+ import type { CustomSource, LocalAccount } from '../../accounts/types.js'
2
+
3
+ export type ZksyncSmartAccount = LocalAccount<'smartAccountZksync'> & {
4
+ sign: NonNullable<CustomSource['sign']>
5
+ }