viem 2.0.0-alpha.10 → 2.0.0-alpha.11

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 (106) hide show
  1. package/_cjs/actions/getContract.js +16 -0
  2. package/_cjs/actions/getContract.js.map +1 -1
  3. package/_cjs/actions/public/getContractEvents.js +26 -0
  4. package/_cjs/actions/public/getContractEvents.js.map +1 -0
  5. package/_cjs/actions/public/watchContractEvent.js +4 -7
  6. package/_cjs/actions/public/watchContractEvent.js.map +1 -1
  7. package/_cjs/chains/definitions/moonbeamDev.js +25 -0
  8. package/_cjs/chains/definitions/moonbeamDev.js.map +1 -0
  9. package/_cjs/chains/definitions/optimismSepolia.js +39 -0
  10. package/_cjs/chains/definitions/optimismSepolia.js.map +1 -0
  11. package/_cjs/chains/definitions/sepolia.js +1 -1
  12. package/_cjs/chains/definitions/sepolia.js.map +1 -1
  13. package/_cjs/chains/index.js +6 -2
  14. package/_cjs/chains/index.js.map +1 -1
  15. package/_cjs/clients/decorators/public.js +2 -0
  16. package/_cjs/clients/decorators/public.js.map +1 -1
  17. package/_cjs/errors/version.js +1 -1
  18. package/_cjs/index.js +5 -1
  19. package/_cjs/index.js.map +1 -1
  20. package/_cjs/utils/hash/getEventSelector.js +5 -6
  21. package/_cjs/utils/hash/getEventSelector.js.map +1 -1
  22. package/_cjs/utils/hash/getEventSignature.js +9 -0
  23. package/_cjs/utils/hash/getEventSignature.js.map +1 -0
  24. package/_cjs/utils/hash/getFunctionSelector.js +5 -6
  25. package/_cjs/utils/hash/getFunctionSelector.js.map +1 -1
  26. package/_cjs/utils/hash/getFunctionSignature.js +15 -0
  27. package/_cjs/utils/hash/getFunctionSignature.js.map +1 -0
  28. package/_cjs/utils/hash/hashFunction.js +2 -4
  29. package/_cjs/utils/hash/hashFunction.js.map +1 -1
  30. package/_esm/actions/getContract.js +16 -0
  31. package/_esm/actions/getContract.js.map +1 -1
  32. package/_esm/actions/public/getContractEvents.js +48 -0
  33. package/_esm/actions/public/getContractEvents.js.map +1 -0
  34. package/_esm/actions/public/watchContractEvent.js +4 -7
  35. package/_esm/actions/public/watchContractEvent.js.map +1 -1
  36. package/_esm/chains/definitions/moonbeamDev.js +22 -0
  37. package/_esm/chains/definitions/moonbeamDev.js.map +1 -0
  38. package/_esm/chains/definitions/optimismSepolia.js +36 -0
  39. package/_esm/chains/definitions/optimismSepolia.js.map +1 -0
  40. package/_esm/chains/definitions/sepolia.js +1 -1
  41. package/_esm/chains/definitions/sepolia.js.map +1 -1
  42. package/_esm/chains/index.js +2 -0
  43. package/_esm/chains/index.js.map +1 -1
  44. package/_esm/clients/decorators/public.js +2 -0
  45. package/_esm/clients/decorators/public.js.map +1 -1
  46. package/_esm/errors/version.js +1 -1
  47. package/_esm/index.js +2 -0
  48. package/_esm/index.js.map +1 -1
  49. package/_esm/utils/hash/getEventSelector.js +5 -6
  50. package/_esm/utils/hash/getEventSelector.js.map +1 -1
  51. package/_esm/utils/hash/getEventSignature.js +5 -0
  52. package/_esm/utils/hash/getEventSignature.js.map +1 -0
  53. package/_esm/utils/hash/getFunctionSelector.js +5 -6
  54. package/_esm/utils/hash/getFunctionSelector.js.map +1 -1
  55. package/_esm/utils/hash/getFunctionSignature.js +11 -0
  56. package/_esm/utils/hash/getFunctionSignature.js.map +1 -0
  57. package/_esm/utils/hash/hashFunction.js +2 -4
  58. package/_esm/utils/hash/hashFunction.js.map +1 -1
  59. package/_types/actions/getContract.d.ts +29 -0
  60. package/_types/actions/getContract.d.ts.map +1 -1
  61. package/_types/actions/public/createContractEventFilter.d.ts +1 -1
  62. package/_types/actions/public/createContractEventFilter.d.ts.map +1 -1
  63. package/_types/actions/public/getContractEvents.d.ts +62 -0
  64. package/_types/actions/public/getContractEvents.d.ts.map +1 -0
  65. package/_types/actions/public/watchContractEvent.d.ts +1 -1
  66. package/_types/actions/public/watchContractEvent.d.ts.map +1 -1
  67. package/_types/chains/definitions/moonbeamDev.d.ts +24 -0
  68. package/_types/chains/definitions/moonbeamDev.d.ts.map +1 -0
  69. package/_types/chains/definitions/optimismSepolia.d.ts +1524 -0
  70. package/_types/chains/definitions/optimismSepolia.d.ts.map +1 -0
  71. package/_types/chains/definitions/sepolia.d.ts +1 -1
  72. package/_types/chains/index.d.ts +2 -0
  73. package/_types/chains/index.d.ts.map +1 -1
  74. package/_types/clients/decorators/public.d.ts +27 -0
  75. package/_types/clients/decorators/public.d.ts.map +1 -1
  76. package/_types/errors/version.d.ts +1 -1
  77. package/_types/index.d.ts +2 -0
  78. package/_types/index.d.ts.map +1 -1
  79. package/_types/utils/abi/decodeEventLog.d.ts +2 -2
  80. package/_types/utils/abi/decodeEventLog.d.ts.map +1 -1
  81. package/_types/utils/hash/getEventSelector.d.ts +1 -2
  82. package/_types/utils/hash/getEventSelector.d.ts.map +1 -1
  83. package/_types/utils/hash/getEventSignature.d.ts +3 -0
  84. package/_types/utils/hash/getEventSignature.d.ts.map +1 -0
  85. package/_types/utils/hash/getFunctionSelector.d.ts.map +1 -1
  86. package/_types/utils/hash/getFunctionSignature.d.ts +3 -0
  87. package/_types/utils/hash/getFunctionSignature.d.ts.map +1 -0
  88. package/_types/utils/hash/hashFunction.d.ts.map +1 -1
  89. package/actions/getContract.ts +95 -0
  90. package/actions/public/createContractEventFilter.ts +1 -1
  91. package/actions/public/getContractEvents.ts +155 -0
  92. package/actions/public/watchContractEvent.ts +10 -13
  93. package/chains/definitions/moonbeamDev.ts +22 -0
  94. package/chains/definitions/optimismSepolia.ts +39 -0
  95. package/chains/definitions/sepolia.ts +1 -1
  96. package/chains/index.ts +2 -0
  97. package/clients/decorators/public.ts +48 -0
  98. package/errors/version.ts +1 -1
  99. package/index.ts +2 -0
  100. package/package.json +1 -1
  101. package/utils/abi/decodeEventLog.ts +2 -2
  102. package/utils/hash/getEventSelector.ts +7 -6
  103. package/utils/hash/getEventSignature.ts +7 -0
  104. package/utils/hash/getFunctionSelector.ts +7 -5
  105. package/utils/hash/getFunctionSignature.ts +17 -0
  106. package/utils/hash/hashFunction.ts +2 -7
@@ -1,4 +1,4 @@
1
- import type { Abi, AbiEvent, Address, ExtractAbiEvent } from 'abitype'
1
+ import type { Abi, Address, ExtractAbiEvent } from 'abitype'
2
2
 
3
3
  import type { Client } from '../../clients/createClient.js'
4
4
  import type { Transport } from '../../clients/transports/createTransport.js'
@@ -21,18 +21,17 @@ import {
21
21
  type EncodeEventTopicsParameters,
22
22
  encodeEventTopics,
23
23
  } from '../../utils/abi/encodeEventTopics.js'
24
- import {
25
- type GetAbiItemParameters,
26
- getAbiItem,
27
- } from '../../utils/abi/getAbiItem.js'
28
24
  import { formatLog } from '../../utils/formatters/log.js'
29
25
  import { observe } from '../../utils/observe.js'
30
26
  import { poll } from '../../utils/poll.js'
31
27
  import { stringify } from '../../utils/stringify.js'
32
28
  import { createContractEventFilter } from './createContractEventFilter.js'
33
29
  import { getBlockNumber } from './getBlockNumber.js'
30
+ import {
31
+ type GetContractEventsParameters,
32
+ getContractEvents,
33
+ } from './getContractEvents.js'
34
34
  import { getFilterChanges } from './getFilterChanges.js'
35
- import { getLogs } from './getLogs.js'
36
35
  import { uninstallFilter } from './uninstallFilter.js'
37
36
 
38
37
  export type WatchContractEventOnLogsParameter<
@@ -85,7 +84,7 @@ export type WatchContractEventParameters<
85
84
  * Whether or not the logs must match the indexed/non-indexed arguments on `event`.
86
85
  * @default false
87
86
  */
88
- strict?: strict | undefined
87
+ strict?: strict | boolean | undefined
89
88
  } & GetPollOptions<transport>
90
89
 
91
90
  export type WatchContractEventReturnType = () => void
@@ -194,16 +193,14 @@ export function watchContractEvent<
194
193
  // If the block number doesn't exist, we are yet to reach the first poll interval,
195
194
  // so do not emit any logs.
196
195
  if (previousBlockNumber && previousBlockNumber !== blockNumber) {
197
- logs = await getLogs(client, {
196
+ logs = await getContractEvents(client, {
197
+ abi,
198
198
  address,
199
199
  args: args as any,
200
200
  fromBlock: previousBlockNumber + 1n,
201
201
  toBlock: blockNumber,
202
- event: getAbiItem({
203
- abi,
204
- name: eventName,
205
- } as unknown as GetAbiItemParameters) as AbiEvent,
206
- })
202
+ strict,
203
+ } as {} as GetContractEventsParameters)
207
204
  } else {
208
205
  logs = []
209
206
  }
@@ -0,0 +1,22 @@
1
+ import { defineChain } from '../../utils/chain.js'
2
+
3
+ export const moonbeamDev = /*#__PURE__*/ defineChain({
4
+ id: 1281,
5
+ name: 'Moonbeam Development Node',
6
+ network: 'development',
7
+ nativeCurrency: {
8
+ decimals: 18,
9
+ name: 'DEV',
10
+ symbol: 'DEV',
11
+ },
12
+ rpcUrls: {
13
+ default: {
14
+ http: ['http://127.0.0.1:9944'],
15
+ webSocket: ['wss://127.0.0.1:9944'],
16
+ },
17
+ public: {
18
+ http: ['http://127.0.0.1:9944'],
19
+ webSocket: ['wss://127.0.0.1:9944'],
20
+ },
21
+ },
22
+ })
@@ -0,0 +1,39 @@
1
+ import { defineChain } from '../../utils/chain.js'
2
+ import { formattersOptimism } from '../optimism/formatters.js'
3
+
4
+ export const optimismSepolia = /*#__PURE__*/ defineChain(
5
+ {
6
+ id: 11155420,
7
+ name: 'Optimism Sepolia',
8
+ network: 'optimism-sepolia',
9
+ nativeCurrency: { name: 'Sepolia Ether', symbol: 'ETH', decimals: 18 },
10
+ rpcUrls: {
11
+ default: {
12
+ http: ['https://sepolia.optimism.io'],
13
+ },
14
+ public: {
15
+ http: ['https://sepolia.optimism.io'],
16
+ },
17
+ },
18
+ blockExplorers: {
19
+ blockscout: {
20
+ name: 'Blockscout',
21
+ url: 'https://optimism-sepolia.blockscout.com',
22
+ },
23
+ default: {
24
+ name: 'Blockscout',
25
+ url: 'https://optimism-sepolia.blockscout.com',
26
+ },
27
+ },
28
+ contracts: {
29
+ multicall3: {
30
+ address: '0xca11bde05977b3631167028862be2a173976ca11',
31
+ blockCreated: 1620204,
32
+ },
33
+ },
34
+ testnet: true,
35
+ },
36
+ {
37
+ formatters: formattersOptimism,
38
+ },
39
+ )
@@ -34,7 +34,7 @@ export const sepolia = /*#__PURE__*/ defineChain({
34
34
  contracts: {
35
35
  multicall3: {
36
36
  address: '0xca11bde05977b3631167028862be2a173976ca11',
37
- blockCreated: 6507670,
37
+ blockCreated: 751532,
38
38
  },
39
39
  ensRegistry: { address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e' },
40
40
  ensUniversalResolver: {
package/chains/index.ts CHANGED
@@ -80,6 +80,7 @@ export { mevTestnet } from './definitions/mevTestnet.js'
80
80
  export { modeTestnet } from './definitions/modeTestnet.js'
81
81
  export { moonbaseAlpha } from './definitions/moonbaseAlpha.js'
82
82
  export { moonbeam } from './definitions/moonbeam.js'
83
+ export { moonbeamDev } from './definitions/moonbeamDev.js'
83
84
  export { moonriver } from './definitions/moonriver.js'
84
85
  export { neonDevnet } from './definitions/neonDevnet.js'
85
86
  export { nexi } from './definitions/nexi.js'
@@ -88,6 +89,7 @@ export { oasys } from './definitions/oasys.js'
88
89
  export { okc } from './definitions/okc.js'
89
90
  export { optimism } from './definitions/optimism.js'
90
91
  export { optimismGoerli } from './definitions/optimismGoerli.js'
92
+ export { optimismSepolia } from './definitions/optimismSepolia.js'
91
93
  export { opBNB } from './definitions/opBNB.js'
92
94
  export { opBNBTestnet } from './definitions/opBNBTestnet.js'
93
95
  export { plinga } from './definitions/plinga.js'
@@ -97,6 +97,11 @@ import {
97
97
  type GetChainIdReturnType,
98
98
  getChainId,
99
99
  } from '../../actions/public/getChainId.js'
100
+ import {
101
+ type GetContractEventsParameters,
102
+ type GetContractEventsReturnType,
103
+ getContractEvents,
104
+ } from '../../actions/public/getContractEvents.js'
100
105
  import {
101
106
  type GetFeeHistoryParameters,
102
107
  type GetFeeHistoryReturnType,
@@ -624,6 +629,48 @@ export type PublicActions<
624
629
  * // 1
625
630
  */
626
631
  getChainId: () => Promise<GetChainIdReturnType>
632
+ /**
633
+ * Returns a list of event logs emitted by a contract.
634
+ *
635
+ * - Docs: https://viem.sh/docs/actions/public/getContractEvents.html
636
+ * - JSON-RPC Methods: [`eth_getLogs`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getlogs)
637
+ *
638
+ * @param client - Client to use
639
+ * @param parameters - {@link GetContractEventsParameters}
640
+ * @returns A list of event logs. {@link GetContractEventsReturnType}
641
+ *
642
+ * @example
643
+ * import { createPublicClient, http } from 'viem'
644
+ * import { mainnet } from 'viem/chains'
645
+ * import { wagmiAbi } from './abi'
646
+ *
647
+ * const client = createPublicClient({
648
+ * chain: mainnet,
649
+ * transport: http(),
650
+ * })
651
+ * const logs = await client.getContractEvents(client, {
652
+ * address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
653
+ * abi: wagmiAbi,
654
+ * eventName: 'Transfer'
655
+ * })
656
+ */
657
+ getContractEvents: <
658
+ const abi extends Abi | readonly unknown[],
659
+ eventName extends ContractEventName<abi> | undefined = undefined,
660
+ strict extends boolean | undefined = undefined,
661
+ fromBlock extends BlockNumber | BlockTag | undefined = undefined,
662
+ toBlock extends BlockNumber | BlockTag | undefined = undefined,
663
+ >(
664
+ args: GetContractEventsParameters<
665
+ abi,
666
+ eventName,
667
+ strict,
668
+ fromBlock,
669
+ toBlock
670
+ >,
671
+ ) => Promise<
672
+ GetContractEventsReturnType<abi, eventName, strict, fromBlock, toBlock>
673
+ >
627
674
  /**
628
675
  * Gets address for ENS name.
629
676
  *
@@ -1645,6 +1692,7 @@ export function publicActions<
1645
1692
  getBlockTransactionCount: (args) => getBlockTransactionCount(client, args),
1646
1693
  getBytecode: (args) => getBytecode(client, args),
1647
1694
  getChainId: () => getChainId(client),
1695
+ getContractEvents: (args) => getContractEvents(client, args),
1648
1696
  getEnsAddress: (args) => getEnsAddress(client, args),
1649
1697
  getEnsAvatar: (args) => getEnsAvatar(client, args),
1650
1698
  getEnsName: (args) => getEnsName(client, args),
package/errors/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '2.0.0-alpha.10'
1
+ export const version = '2.0.0-alpha.11'
package/index.ts CHANGED
@@ -868,7 +868,9 @@ export { fromRlp } from './utils/encoding/fromRlp.js'
868
868
  export { getAddress } from './utils/address/getAddress.js'
869
869
  export { getContractError } from './utils/errors/getContractError.js'
870
870
  export { getEventSelector } from './utils/hash/getEventSelector.js'
871
+ export { getEventSignature } from './utils/hash/getEventSignature.js'
871
872
  export { getFunctionSelector } from './utils/hash/getFunctionSelector.js'
873
+ export { getFunctionSignature } from './utils/hash/getFunctionSignature.js'
872
874
  export { hashMessage } from './utils/signature/hashMessage.js'
873
875
  export { isAddress } from './utils/address/isAddress.js'
874
876
  export { isAddressEqual } from './utils/address/isAddressEqual.js'
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "viem",
3
3
  "description": "TypeScript Interface for Ethereum",
4
- "version": "2.0.0-alpha.10",
4
+ "version": "2.0.0-alpha.11",
5
5
  "type": "module",
6
6
  "main": "./_cjs/index.js",
7
7
  "module": "./_esm/index.js",
@@ -31,9 +31,9 @@ export type DecodeEventLogParameters<
31
31
  strict extends boolean = true,
32
32
  > = {
33
33
  abi: abi
34
- data?: data
34
+ data?: data | undefined
35
35
  eventName?: eventName | ContractEventName<abi>
36
- strict?: strict
36
+ strict?: strict | boolean | undefined
37
37
  topics: [signature: Hex, ...args: topics] | []
38
38
  }
39
39
 
@@ -1,9 +1,10 @@
1
1
  import type { AbiEvent } from 'abitype'
2
- import type { EventDefinition } from '../../types/contract.js'
3
2
 
4
- import { hashAbiItem, hashFunction } from './hashFunction.js'
3
+ import { toBytes } from '../encoding/toBytes.js'
4
+ import { getEventSignature } from './getEventSignature.js'
5
+ import { keccak256 } from './keccak256.js'
5
6
 
6
- export const getEventSelector = (event: EventDefinition | AbiEvent) => {
7
- if (typeof event === 'string') return hashFunction(event)
8
- return hashAbiItem(event)
9
- }
7
+ const hash = (value: string) => keccak256(toBytes(value))
8
+
9
+ export const getEventSelector = (fn: string | AbiEvent) =>
10
+ hash(getEventSignature(fn))
@@ -0,0 +1,7 @@
1
+ import type { AbiEvent, AbiFunction } from 'abitype'
2
+
3
+ import { getFunctionSignature } from './getFunctionSignature.js'
4
+
5
+ export const getEventSignature = (fn: string | AbiEvent) => {
6
+ return getFunctionSignature(fn as {} as AbiFunction)
7
+ }
@@ -1,9 +1,11 @@
1
1
  import type { AbiFunction } from 'abitype'
2
2
  import { slice } from '../data/slice.js'
3
3
 
4
- import { hashAbiItem, hashFunction } from './hashFunction.js'
4
+ import { toBytes } from '../encoding/toBytes.js'
5
+ import { getFunctionSignature } from './getFunctionSignature.js'
6
+ import { keccak256 } from './keccak256.js'
5
7
 
6
- export const getFunctionSelector = (fn: string | AbiFunction) => {
7
- if (typeof fn === 'string') return slice(hashFunction(fn), 0, 4)
8
- return slice(hashAbiItem(fn), 0, 4)
9
- }
8
+ const hash = (value: string) => keccak256(toBytes(value))
9
+
10
+ export const getFunctionSelector = (fn: string | AbiFunction) =>
11
+ slice(hash(getFunctionSignature(fn)), 0, 4)
@@ -0,0 +1,17 @@
1
+ import type { AbiFunction } from 'abitype'
2
+
3
+ import { formatAbiItem } from '../abi/formatAbiItem.js'
4
+ import {
5
+ extractFunctionName,
6
+ extractFunctionParams,
7
+ } from '../contract/extractFunctionParts.js'
8
+
9
+ export const getFunctionSignature = (fn: string | AbiFunction) => {
10
+ if (typeof fn === 'string') {
11
+ const name = extractFunctionName(fn)
12
+ const params = extractFunctionParams(fn) || []
13
+ return `${name}(${params.map(({ type }) => type).join(',')})`
14
+ }
15
+
16
+ return formatAbiItem(fn)
17
+ }
@@ -1,19 +1,14 @@
1
1
  import type { AbiEvent, AbiFunction } from 'abitype'
2
2
  import { formatAbiItem } from '../abi/formatAbiItem.js'
3
- import {
4
- extractFunctionName,
5
- extractFunctionParams,
6
- } from '../contract/extractFunctionParts.js'
7
3
  import { toBytes } from '../encoding/toBytes.js'
8
4
 
5
+ import { getFunctionSignature } from './getFunctionSignature.js'
9
6
  import { keccak256 } from './keccak256.js'
10
7
 
11
8
  const hash = (value: string) => keccak256(toBytes(value))
12
9
 
13
10
  export function hashFunction(def: string) {
14
- const name = extractFunctionName(def)
15
- const params = extractFunctionParams(def) || []
16
- return hash(`${name}(${params.map(({ type }) => type).join(',')})`)
11
+ return hash(getFunctionSignature(def))
17
12
  }
18
13
 
19
14
  export function hashAbiItem(abiItem: AbiFunction | AbiEvent) {