viem 2.13.9 → 2.14.0
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.
- package/CHANGELOG.md +14 -0
- package/_cjs/actions/index.js +4 -2
- package/_cjs/actions/index.js.map +1 -1
- package/_cjs/actions/public/call.js +24 -2
- package/_cjs/actions/public/call.js.map +1 -1
- package/_cjs/actions/public/getEip712Domain.js +56 -0
- package/_cjs/actions/public/getEip712Domain.js.map +1 -0
- package/_cjs/actions/public/readContract.js.map +1 -1
- package/_cjs/clients/decorators/public.js +2 -0
- package/_cjs/clients/decorators/public.js.map +1 -1
- package/_cjs/constants/contracts.js +2 -1
- package/_cjs/constants/contracts.js.map +1 -1
- package/_cjs/errors/contract.js +19 -1
- package/_cjs/errors/contract.js.map +1 -1
- package/_cjs/errors/eip712.js +24 -0
- package/_cjs/errors/eip712.js.map +1 -0
- package/_cjs/errors/version.js +1 -1
- package/_cjs/index.js +6 -5
- package/_cjs/index.js.map +1 -1
- package/_esm/actions/index.js +1 -0
- package/_esm/actions/index.js.map +1 -1
- package/_esm/actions/public/call.js +31 -3
- package/_esm/actions/public/call.js.map +1 -1
- package/_esm/actions/public/getEip712Domain.js +84 -0
- package/_esm/actions/public/getEip712Domain.js.map +1 -0
- package/_esm/actions/public/readContract.js.map +1 -1
- package/_esm/clients/decorators/public.js +2 -0
- package/_esm/clients/decorators/public.js.map +1 -1
- package/_esm/constants/contracts.js +1 -0
- package/_esm/constants/contracts.js.map +1 -1
- package/_esm/errors/contract.js +17 -0
- package/_esm/errors/contract.js.map +1 -1
- package/_esm/errors/eip712.js +20 -0
- package/_esm/errors/eip712.js.map +1 -0
- package/_esm/errors/version.js +1 -1
- package/_esm/index.js +2 -1
- package/_esm/index.js.map +1 -1
- package/_types/actions/index.d.ts +1 -0
- package/_types/actions/index.d.ts.map +1 -1
- package/_types/actions/public/call.d.ts +8 -2
- package/_types/actions/public/call.d.ts.map +1 -1
- package/_types/actions/public/getEip712Domain.d.ts +51 -0
- package/_types/actions/public/getEip712Domain.d.ts.map +1 -0
- package/_types/actions/public/readContract.d.ts +1 -1
- package/_types/actions/public/readContract.d.ts.map +1 -1
- package/_types/actions/public/simulateContract.d.ts +1 -1
- package/_types/actions/public/simulateContract.d.ts.map +1 -1
- package/_types/clients/decorators/public.d.ts +34 -0
- package/_types/clients/decorators/public.d.ts.map +1 -1
- package/_types/constants/contracts.d.ts +1 -0
- package/_types/constants/contracts.d.ts.map +1 -1
- package/_types/errors/contract.d.ts +9 -0
- package/_types/errors/contract.d.ts.map +1 -1
- package/_types/errors/eip712.d.ts +12 -0
- package/_types/errors/eip712.d.ts.map +1 -0
- package/_types/errors/version.d.ts +1 -1
- package/_types/index.d.ts +2 -1
- package/_types/index.d.ts.map +1 -1
- package/actions/index.ts +6 -0
- package/actions/public/call.ts +53 -3
- package/actions/public/getEip712Domain.ts +134 -0
- package/actions/public/readContract.ts +9 -1
- package/actions/public/simulateContract.ts +1 -1
- package/clients/decorators/public.ts +41 -0
- package/constants/contracts.ts +3 -0
- package/errors/contract.ts +20 -0
- package/errors/eip712.ts +19 -0
- package/errors/version.ts +1 -1
- package/index.ts +7 -0
- package/package.json +1 -1
@@ -0,0 +1,134 @@
|
|
1
|
+
import type { Address, TypedDataDomain } from 'abitype'
|
2
|
+
import type { Client } from '../../clients/createClient.js'
|
3
|
+
import type { Transport } from '../../clients/transports/createTransport.js'
|
4
|
+
import {
|
5
|
+
Eip712DomainNotFoundError,
|
6
|
+
type Eip712DomainNotFoundErrorType,
|
7
|
+
} from '../../errors/eip712.js'
|
8
|
+
import type { ErrorType } from '../../errors/utils.js'
|
9
|
+
import type { Hex } from '../../types/misc.js'
|
10
|
+
import type { RequiredBy } from '../../types/utils.js'
|
11
|
+
import { getAction } from '../../utils/getAction.js'
|
12
|
+
import {
|
13
|
+
type ReadContractErrorType,
|
14
|
+
type ReadContractParameters,
|
15
|
+
readContract,
|
16
|
+
} from './readContract.js'
|
17
|
+
|
18
|
+
export type GetEip712DomainParameters = {
|
19
|
+
address: Address
|
20
|
+
} & Pick<ReadContractParameters, 'factory' | 'factoryData'>
|
21
|
+
|
22
|
+
export type GetEip712DomainReturnType = {
|
23
|
+
domain: RequiredBy<
|
24
|
+
TypedDataDomain,
|
25
|
+
'chainId' | 'name' | 'verifyingContract' | 'version'
|
26
|
+
>
|
27
|
+
fields: Hex
|
28
|
+
extensions: readonly bigint[]
|
29
|
+
}
|
30
|
+
|
31
|
+
export type GetEip712DomainErrorType =
|
32
|
+
| Eip712DomainNotFoundErrorType
|
33
|
+
| ReadContractErrorType
|
34
|
+
| ErrorType
|
35
|
+
|
36
|
+
/**
|
37
|
+
* Reads the EIP-712 domain from a contract, based on the ERC-5267 specification.
|
38
|
+
*
|
39
|
+
* @param client - A {@link Client} instance.
|
40
|
+
* @param parameters - The parameters of the action. {@link GetEip712DomainParameters}
|
41
|
+
* @returns The EIP-712 domain, fields, and extensions. {@link GetEip712DomainReturnType}
|
42
|
+
*
|
43
|
+
* @example
|
44
|
+
* ```ts
|
45
|
+
* import { createPublicClient, http, getEip712Domain } from 'viem'
|
46
|
+
* import { mainnet } from 'viem/chains'
|
47
|
+
*
|
48
|
+
* const client = createPublicClient({
|
49
|
+
* chain: mainnet,
|
50
|
+
* transport: http(),
|
51
|
+
* })
|
52
|
+
*
|
53
|
+
* const domain = await getEip712Domain(client, {
|
54
|
+
* address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
|
55
|
+
* })
|
56
|
+
* // {
|
57
|
+
* // domain: {
|
58
|
+
* // name: 'ExampleContract',
|
59
|
+
* // version: '1',
|
60
|
+
* // chainId: 1,
|
61
|
+
* // verifyingContract: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
|
62
|
+
* // },
|
63
|
+
* // fields: '0x0f',
|
64
|
+
* // extensions: [],
|
65
|
+
* // }
|
66
|
+
* ```
|
67
|
+
*/
|
68
|
+
export async function getEip712Domain(
|
69
|
+
client: Client<Transport>,
|
70
|
+
parameters: GetEip712DomainParameters,
|
71
|
+
): Promise<GetEip712DomainReturnType> {
|
72
|
+
const { address, factory, factoryData } = parameters
|
73
|
+
|
74
|
+
try {
|
75
|
+
const [
|
76
|
+
fields,
|
77
|
+
name,
|
78
|
+
version,
|
79
|
+
chainId,
|
80
|
+
verifyingContract,
|
81
|
+
salt,
|
82
|
+
extensions,
|
83
|
+
] = await getAction(
|
84
|
+
client,
|
85
|
+
readContract,
|
86
|
+
'readContract',
|
87
|
+
)({
|
88
|
+
abi,
|
89
|
+
address,
|
90
|
+
functionName: 'eip712Domain',
|
91
|
+
factory,
|
92
|
+
factoryData,
|
93
|
+
})
|
94
|
+
|
95
|
+
return {
|
96
|
+
domain: {
|
97
|
+
name,
|
98
|
+
version,
|
99
|
+
chainId: Number(chainId),
|
100
|
+
verifyingContract,
|
101
|
+
salt,
|
102
|
+
},
|
103
|
+
extensions,
|
104
|
+
fields,
|
105
|
+
}
|
106
|
+
} catch (e) {
|
107
|
+
const error = e as ReadContractErrorType
|
108
|
+
if (
|
109
|
+
error.name === 'ContractFunctionExecutionError' &&
|
110
|
+
error.cause.name === 'ContractFunctionZeroDataError'
|
111
|
+
) {
|
112
|
+
throw new Eip712DomainNotFoundError({ address })
|
113
|
+
}
|
114
|
+
throw error
|
115
|
+
}
|
116
|
+
}
|
117
|
+
|
118
|
+
const abi = [
|
119
|
+
{
|
120
|
+
inputs: [],
|
121
|
+
name: 'eip712Domain',
|
122
|
+
outputs: [
|
123
|
+
{ name: 'fields', type: 'bytes1' },
|
124
|
+
{ name: 'name', type: 'string' },
|
125
|
+
{ name: 'version', type: 'string' },
|
126
|
+
{ name: 'chainId', type: 'uint256' },
|
127
|
+
{ name: 'verifyingContract', type: 'address' },
|
128
|
+
{ name: 'salt', type: 'bytes32' },
|
129
|
+
{ name: 'extensions', type: 'uint256[]' },
|
130
|
+
],
|
131
|
+
stateMutability: 'view',
|
132
|
+
type: 'function',
|
133
|
+
},
|
134
|
+
] as const
|
@@ -40,7 +40,15 @@ export type ReadContractParameters<
|
|
40
40
|
functionName
|
41
41
|
> = ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
|
42
42
|
> = UnionEvaluate<
|
43
|
-
Pick<
|
43
|
+
Pick<
|
44
|
+
CallParameters,
|
45
|
+
| 'account'
|
46
|
+
| 'blockNumber'
|
47
|
+
| 'blockTag'
|
48
|
+
| 'factory'
|
49
|
+
| 'factoryData'
|
50
|
+
| 'stateOverride'
|
51
|
+
>
|
44
52
|
> &
|
45
53
|
ContractFunctionParameters<abi, 'pure' | 'view', functionName, args>
|
46
54
|
|
@@ -106,6 +106,11 @@ import {
|
|
106
106
|
type GetContractEventsReturnType,
|
107
107
|
getContractEvents,
|
108
108
|
} from '../../actions/public/getContractEvents.js'
|
109
|
+
import {
|
110
|
+
type GetEip712DomainParameters,
|
111
|
+
type GetEip712DomainReturnType,
|
112
|
+
getEip712Domain,
|
113
|
+
} from '../../actions/public/getEip712Domain.js'
|
109
114
|
import {
|
110
115
|
type GetFeeHistoryParameters,
|
111
116
|
type GetFeeHistoryReturnType,
|
@@ -703,6 +708,41 @@ export type PublicActions<
|
|
703
708
|
) => Promise<
|
704
709
|
GetContractEventsReturnType<abi, eventName, strict, fromBlock, toBlock>
|
705
710
|
>
|
711
|
+
/**
|
712
|
+
* Reads the EIP-712 domain from a contract, based on the ERC-5267 specification.
|
713
|
+
*
|
714
|
+
* @param client - A {@link Client} instance.
|
715
|
+
* @param parameters - The parameters of the action. {@link GetEip712DomainParameters}
|
716
|
+
* @returns The EIP-712 domain, fields, and extensions. {@link GetEip712DomainReturnType}
|
717
|
+
*
|
718
|
+
* @example
|
719
|
+
* ```ts
|
720
|
+
* import { createPublicClient, http } from 'viem'
|
721
|
+
* import { mainnet } from 'viem/chains'
|
722
|
+
*
|
723
|
+
* const client = createPublicClient({
|
724
|
+
* chain: mainnet,
|
725
|
+
* transport: http(),
|
726
|
+
* })
|
727
|
+
*
|
728
|
+
* const domain = await client.getEip712Domain({
|
729
|
+
* address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
|
730
|
+
* })
|
731
|
+
* // {
|
732
|
+
* // domain: {
|
733
|
+
* // name: 'ExampleContract',
|
734
|
+
* // version: '1',
|
735
|
+
* // chainId: 1,
|
736
|
+
* // verifyingContract: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
|
737
|
+
* // },
|
738
|
+
* // fields: '0x0f',
|
739
|
+
* // extensions: [],
|
740
|
+
* // }
|
741
|
+
* ```
|
742
|
+
*/
|
743
|
+
getEip712Domain: (
|
744
|
+
args: GetEip712DomainParameters,
|
745
|
+
) => Promise<GetEip712DomainReturnType>
|
706
746
|
/**
|
707
747
|
* Gets address for ENS name.
|
708
748
|
*
|
@@ -1816,6 +1856,7 @@ export function publicActions<
|
|
1816
1856
|
getBytecode: (args) => getBytecode(client, args),
|
1817
1857
|
getChainId: () => getChainId(client),
|
1818
1858
|
getContractEvents: (args) => getContractEvents(client, args),
|
1859
|
+
getEip712Domain: (args) => getEip712Domain(client, args),
|
1819
1860
|
getEnsAddress: (args) => getEnsAddress(client, args),
|
1820
1861
|
getEnsAvatar: (args) => getEnsAvatar(client, args),
|
1821
1862
|
getEnsName: (args) => getEnsName(client, args),
|
package/constants/contracts.ts
CHANGED
@@ -1,2 +1,5 @@
|
|
1
|
+
export const counterfactualContractCallByteCode =
|
2
|
+
'0x608060405234801561001057600080fd5b506040516102c03803806102c083398101604081905261002f916101e6565b836001600160a01b03163b6000036100e457600080836001600160a01b03168360405161005c9190610270565b6000604051808303816000865af19150503d8060008114610099576040519150601f19603f3d011682016040523d82523d6000602084013e61009e565b606091505b50915091508115806100b857506001600160a01b0386163b155b156100e1578060405163101bb98d60e01b81526004016100d8919061028c565b60405180910390fd5b50505b6000808451602086016000885af16040513d6000823e81610103573d81fd5b3d81f35b80516001600160a01b038116811461011e57600080fd5b919050565b634e487b7160e01b600052604160045260246000fd5b60005b8381101561015457818101518382015260200161013c565b50506000910152565b600082601f83011261016e57600080fd5b81516001600160401b0381111561018757610187610123565b604051601f8201601f19908116603f011681016001600160401b03811182821017156101b5576101b5610123565b6040528181528382016020018510156101cd57600080fd5b6101de826020830160208701610139565b949350505050565b600080600080608085870312156101fc57600080fd5b61020585610107565b60208601519094506001600160401b0381111561022157600080fd5b61022d8782880161015d565b93505061023c60408601610107565b60608601519092506001600160401b0381111561025857600080fd5b6102648782880161015d565b91505092959194509250565b60008251610282818460208701610139565b9190910192915050565b60208152600082518060208401526102ab816040850160208701610139565b601f01601f1916919091016040019291505056fe'
|
3
|
+
|
1
4
|
export const universalSignatureValidatorByteCode =
|
2
5
|
'0x60806040523480156200001157600080fd5b50604051620007003803806200070083398101604081905262000034916200056f565b6000620000438484846200004f565b9050806000526001601ff35b600080846001600160a01b0316803b806020016040519081016040528181526000908060200190933c90507f6492649264926492649264926492649264926492649264926492649264926492620000a68462000451565b036200021f57600060608085806020019051810190620000c79190620005ce565b8651929550909350915060000362000192576000836001600160a01b031683604051620000f5919062000643565b6000604051808303816000865af19150503d806000811462000134576040519150601f19603f3d011682016040523d82523d6000602084013e62000139565b606091505b5050905080620001905760405162461bcd60e51b815260206004820152601e60248201527f5369676e617475726556616c696461746f723a206465706c6f796d656e74000060448201526064015b60405180910390fd5b505b604051630b135d3f60e11b808252906001600160a01b038a1690631626ba7e90620001c4908b90869060040162000661565b602060405180830381865afa158015620001e2573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906200020891906200069d565b6001600160e01b031916149450505050506200044a565b805115620002b157604051630b135d3f60e11b808252906001600160a01b03871690631626ba7e9062000259908890889060040162000661565b602060405180830381865afa15801562000277573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906200029d91906200069d565b6001600160e01b031916149150506200044a565b8251604114620003195760405162461bcd60e51b815260206004820152603a6024820152600080516020620006e083398151915260448201527f3a20696e76616c6964207369676e6174757265206c656e677468000000000000606482015260840162000187565b620003236200046b565b506020830151604080850151855186939260009185919081106200034b576200034b620006c9565b016020015160f81c9050601b81148015906200036b57508060ff16601c14155b15620003cf5760405162461bcd60e51b815260206004820152603b6024820152600080516020620006e083398151915260448201527f3a20696e76616c6964207369676e617475726520762076616c75650000000000606482015260840162000187565b6040805160008152602081018083528a905260ff83169181019190915260608101849052608081018390526001600160a01b038a169060019060a0016020604051602081039080840390855afa1580156200042e573d6000803e3d6000fd5b505050602060405103516001600160a01b031614955050505050505b9392505050565b60006020825110156200046357600080fd5b508051015190565b60405180606001604052806003906020820280368337509192915050565b6001600160a01b03811681146200049f57600080fd5b50565b634e487b7160e01b600052604160045260246000fd5b60005b83811015620004d5578181015183820152602001620004bb565b50506000910152565b600082601f830112620004f057600080fd5b81516001600160401b03808211156200050d576200050d620004a2565b604051601f8301601f19908116603f01168101908282118183101715620005385762000538620004a2565b816040528381528660208588010111156200055257600080fd5b62000565846020830160208901620004b8565b9695505050505050565b6000806000606084860312156200058557600080fd5b8351620005928162000489565b6020850151604086015191945092506001600160401b03811115620005b657600080fd5b620005c486828701620004de565b9150509250925092565b600080600060608486031215620005e457600080fd5b8351620005f18162000489565b60208501519093506001600160401b03808211156200060f57600080fd5b6200061d87838801620004de565b935060408601519150808211156200063457600080fd5b50620005c486828701620004de565b6000825162000657818460208701620004b8565b9190910192915050565b828152604060208201526000825180604084015262000688816060850160208701620004b8565b601f01601f1916919091016060019392505050565b600060208284031215620006b057600080fd5b81516001600160e01b0319811681146200044a57600080fd5b634e487b7160e01b600052603260045260246000fdfe5369676e617475726556616c696461746f72237265636f7665725369676e6572'
|
package/errors/contract.ts
CHANGED
@@ -278,6 +278,26 @@ export class ContractFunctionZeroDataError extends BaseError {
|
|
278
278
|
}
|
279
279
|
}
|
280
280
|
|
281
|
+
export type CounterfactualDeploymentFailedErrorType =
|
282
|
+
CounterfactualDeploymentFailedError & {
|
283
|
+
name: 'CounterfactualDeploymentFailedError'
|
284
|
+
}
|
285
|
+
export class CounterfactualDeploymentFailedError extends BaseError {
|
286
|
+
override name = 'CounterfactualDeploymentFailedError'
|
287
|
+
constructor({ factory }: { factory: Address }) {
|
288
|
+
super(
|
289
|
+
`Deployment for counterfactual contract call failed for factory "${factory}".`,
|
290
|
+
{
|
291
|
+
metaMessages: [
|
292
|
+
'Please ensure:',
|
293
|
+
'- The `factory` is a valid contract deployment factory (ie. Create2 Factory, ERC-4337 Factory, etc).',
|
294
|
+
'- The `factoryData` is a valid encoded function call for contract deployment function on the factory.',
|
295
|
+
],
|
296
|
+
},
|
297
|
+
)
|
298
|
+
}
|
299
|
+
}
|
300
|
+
|
281
301
|
export type RawContractErrorType = RawContractError & {
|
282
302
|
name: 'RawContractError'
|
283
303
|
}
|
package/errors/eip712.ts
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
import type { Address } from 'abitype'
|
2
|
+
import { BaseError } from './base.js'
|
3
|
+
|
4
|
+
export type Eip712DomainNotFoundErrorType = Eip712DomainNotFoundError & {
|
5
|
+
name: 'Eip712DomainNotFoundError'
|
6
|
+
}
|
7
|
+
export class Eip712DomainNotFoundError extends BaseError {
|
8
|
+
override name = 'Eip712DomainNotFoundError'
|
9
|
+
constructor({ address }: { address: Address }) {
|
10
|
+
super(`No EIP-712 domain found on contract "${address}".`, {
|
11
|
+
metaMessages: [
|
12
|
+
'Ensure that:',
|
13
|
+
`- The contract is deployed at the address "${address}".`,
|
14
|
+
'- `eip712Domain()` function exists on the contract.',
|
15
|
+
'- `eip712Domain()` function matches signature to ERC-5267 specification.',
|
16
|
+
],
|
17
|
+
})
|
18
|
+
}
|
19
|
+
}
|
package/errors/version.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export const version = '2.
|
1
|
+
export const version = '2.14.0'
|
package/index.ts
CHANGED
@@ -48,6 +48,11 @@ export {
|
|
48
48
|
type GetContractEventsParameters,
|
49
49
|
type GetContractEventsReturnType,
|
50
50
|
} from './actions/public/getContractEvents.js'
|
51
|
+
export {
|
52
|
+
type GetEip712DomainErrorType,
|
53
|
+
type GetEip712DomainParameters,
|
54
|
+
type GetEip712DomainReturnType,
|
55
|
+
} from './actions/public/getEip712Domain.js'
|
51
56
|
export {
|
52
57
|
type AddChainErrorType,
|
53
58
|
type AddChainParameters,
|
@@ -715,6 +720,8 @@ export {
|
|
715
720
|
type ContractFunctionZeroDataErrorType,
|
716
721
|
RawContractError,
|
717
722
|
type RawContractErrorType,
|
723
|
+
CounterfactualDeploymentFailedError,
|
724
|
+
type CounterfactualDeploymentFailedErrorType,
|
718
725
|
} from './errors/contract.js'
|
719
726
|
export {
|
720
727
|
BaseFeeScalarError,
|