viem 0.0.1-alpha.19 → 0.0.1-alpha.20
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/dist/{rpc-858670f1.d.ts → chain-32f56cfa.d.ts} +68 -18
- package/dist/chain-f12cdc7f.d.ts +5 -0
- package/dist/chains.d.ts +757 -170
- package/dist/chains.js +80 -86
- package/dist/chains.js.map +1 -1
- package/dist/chains.mjs +120 -126
- package/dist/chains.mjs.map +1 -1
- package/dist/{chunk-PHSAQQGZ.mjs → chunk-5Q6FSUU2.mjs} +75 -37
- package/dist/chunk-5Q6FSUU2.mjs.map +1 -0
- package/dist/{chunk-24EKKMUO.js → chunk-FHXXG7I6.js} +12 -12
- package/dist/chunk-FHXXG7I6.js.map +1 -0
- package/dist/{chunk-HRE6LYF3.js → chunk-IAQPMSGJ.js} +13 -13
- package/dist/chunk-IAQPMSGJ.js.map +1 -0
- package/dist/{chunk-IVWG22RQ.mjs → chunk-MYQNZSWD.mjs} +69 -12
- package/dist/chunk-MYQNZSWD.mjs.map +1 -0
- package/dist/{chunk-NYMS65YI.js → chunk-NHAKUPTF.js} +124 -67
- package/dist/chunk-NHAKUPTF.js.map +1 -0
- package/dist/{chunk-BZFF747V.js → chunk-SF4I2NKC.js} +81 -43
- package/dist/chunk-SF4I2NKC.js.map +1 -0
- package/dist/{chunk-VAT5CEF7.mjs → chunk-T7CBAKLI.mjs} +2 -2
- package/dist/chunk-T7CBAKLI.mjs.map +1 -0
- package/dist/{chunk-CTU4PQLY.js → chunk-YFKR74XG.js} +14 -14
- package/dist/{chunk-CTU4PQLY.js.map → chunk-YFKR74XG.js.map} +0 -0
- package/dist/{chunk-NSRORL3O.mjs → chunk-YODUQCHJ.mjs} +2 -2
- package/dist/{chunk-NSRORL3O.mjs.map → chunk-YODUQCHJ.mjs.map} +0 -0
- package/dist/{chunk-E7LH5H76.mjs → chunk-ZVGTYLKU.mjs} +2 -2
- package/dist/chunk-ZVGTYLKU.mjs.map +1 -0
- package/dist/clients/index.d.ts +7 -8
- package/dist/clients/index.js +3 -3
- package/dist/clients/index.mjs +2 -2
- package/dist/{transactionReceipt-aed524b4.d.ts → contract-9af4608b.d.ts} +30 -18
- package/dist/{createClient-03ba79e8.d.ts → createClient-5d316c7e.d.ts} +2 -2
- package/dist/{createPublicClient-818241ba.d.ts → createPublicClient-9d2b42e1.d.ts} +3 -3
- package/dist/{createTestClient-24eb7be8.d.ts → createTestClient-79498dab.d.ts} +3 -3
- package/dist/{createWalletClient-7cbd6ad1.d.ts → createWalletClient-f69a5230.d.ts} +3 -3
- package/dist/{eip1193-71d8e27d.d.ts → eip1193-6c485d63.d.ts} +1 -1
- package/dist/index.d.ts +45 -23
- package/dist/index.js +10 -6
- package/dist/index.mjs +9 -5
- package/dist/{parseGwei-dbd12305.d.ts → parseGwei-4308ad80.d.ts} +17 -4
- package/dist/public.d.ts +9 -10
- package/dist/public.js +4 -4
- package/dist/public.mjs +3 -3
- package/dist/{sendTransaction-d09961a7.d.ts → sendTransaction-1c8290a9.d.ts} +3 -4
- package/dist/{stopImpersonatingAccount-c8a00be7.d.ts → stopImpersonatingAccount-7781842a.d.ts} +2 -2
- package/dist/test.d.ts +5 -6
- package/dist/test.js +3 -3
- package/dist/test.mjs +2 -2
- package/dist/transactionRequest-341b6ed2.d.ts +15 -0
- package/dist/utils/index.d.ts +9 -8
- package/dist/utils/index.js +22 -2
- package/dist/utils/index.mjs +25 -5
- package/dist/wallet.d.ts +7 -8
- package/dist/wallet.js +3 -3
- package/dist/wallet.mjs +2 -2
- package/dist/{watchAsset-c39645b7.d.ts → watchAsset-afaad3c7.d.ts} +3 -4
- package/dist/{watchPendingTransactions-01e6be4f.d.ts → watchPendingTransactions-3b722547.d.ts} +21 -16
- package/dist/{webSocket-3a00015b.d.ts → webSocket-b180e679.d.ts} +2 -2
- package/dist/window.d.ts +3 -2
- package/package.json +3 -3
- package/src/_test/abis.ts +188 -0
- package/src/_test/constants.ts +0 -3
- package/src/_test/index.ts +5 -2
- package/src/actions/index.test.ts +2 -0
- package/src/actions/index.ts +6 -0
- package/src/actions/public/call.ts +2 -1
- package/src/actions/public/createContractEventFilter.test.ts +119 -0
- package/src/actions/public/createContractEventFilter.ts +69 -0
- package/src/actions/public/createEventFilter.ts +5 -8
- package/src/actions/public/deployContract.ts +1 -2
- package/src/actions/public/getBlock.test.ts +3 -3
- package/src/actions/public/getBlock.ts +1 -2
- package/src/actions/public/getBlockTransactionCount.ts +1 -2
- package/src/actions/public/getFilterChanges.test.ts +9 -7
- package/src/actions/public/getFilterLogs.test.ts +7 -5
- package/src/actions/public/getLogs.test.ts +7 -5
- package/src/actions/public/getLogs.ts +3 -2
- package/src/actions/public/getTransaction.ts +1 -2
- package/src/actions/public/getTransactionConfirmations.ts +1 -2
- package/src/actions/public/getTransactionReceipt.test.ts +24 -62
- package/src/actions/public/getTransactionReceipt.ts +1 -2
- package/src/actions/public/index.test.ts +4 -0
- package/src/actions/public/index.ts +18 -0
- package/src/actions/public/readContract.ts +1 -1
- package/src/actions/public/simulateContract.ts +1 -1
- package/src/actions/public/waitForTransactionReceipt.ts +1 -2
- package/src/actions/public/watchBlocks.test.ts +27 -2
- package/src/actions/public/watchBlocks.ts +33 -14
- package/src/actions/public/watchContractEvent.test.ts +301 -0
- package/src/actions/public/watchContractEvent.ts +108 -0
- package/src/actions/public/watchEvent.test.ts +190 -0
- package/src/actions/public/watchEvent.ts +95 -0
- package/src/actions/public/watchPendingTransactions.ts +1 -0
- package/src/actions/wallet/addChain.ts +1 -1
- package/src/actions/wallet/sendTransaction.ts +2 -1
- package/src/actions/wallet/switchChain.ts +1 -1
- package/src/actions/wallet/writeContract.ts +1 -1
- package/src/chains.test.ts +830 -52
- package/src/chains.ts +90 -158
- package/src/clients/createClient.ts +1 -1
- package/src/clients/createPublicClient.ts +1 -1
- package/src/clients/createTestClient.ts +1 -1
- package/src/clients/createWalletClient.ts +1 -1
- package/src/clients/transports/createTransport.ts +1 -1
- package/src/index.test.ts +2 -0
- package/src/index.ts +10 -2
- package/src/types/chain.ts +6 -0
- package/src/types/contract.ts +17 -2
- package/src/types/formatter.ts +23 -0
- package/src/types/index.ts +6 -0
- package/src/types/transaction.ts +1 -1
- package/src/utils/abi/decodeErrorResult.ts +2 -2
- package/src/utils/abi/decodeFunctionData.ts +2 -2
- package/src/utils/abi/encodeErrorResult.ts +2 -2
- package/src/utils/abi/encodeEventTopics.ts +4 -4
- package/src/utils/abi/encodeFunctionData.ts +2 -2
- package/src/utils/abi/{formatAbiItemWithParams.test.ts → formatAbiItem.test.ts} +11 -11
- package/src/utils/abi/{formatAbiItemWithParams.ts → formatAbiItem.ts} +1 -1
- package/src/utils/abi/index.test.ts +1 -1
- package/src/utils/abi/index.ts +1 -1
- package/src/utils/chain.test.ts +43 -0
- package/src/utils/chain.ts +8 -0
- package/src/utils/contract/getContractError.ts +2 -6
- package/src/utils/formatters/block.ts +4 -3
- package/src/utils/formatters/extract.ts +1 -1
- package/src/utils/formatters/format.ts +39 -2
- package/src/utils/formatters/index.test.ts +10 -0
- package/src/utils/formatters/index.ts +15 -5
- package/src/utils/formatters/transaction.ts +16 -4
- package/src/utils/formatters/transactionReceipt.ts +13 -4
- package/src/utils/formatters/transactionRequest.ts +12 -3
- package/src/utils/hash/getEventSignature.ts +2 -2
- package/src/utils/index.test.ts +24 -1
- package/src/utils/index.ts +12 -1
- package/src/{constants.test.ts → utils/unit/constants.test.ts} +0 -5
- package/src/{constants.ts → utils/unit/constants.ts} +0 -6
- package/src/utils/unit/formatEther.ts +1 -1
- package/src/utils/unit/formatGwei.ts +1 -1
- package/src/utils/unit/index.test.ts +12 -0
- package/src/utils/unit/index.ts +1 -0
- package/src/utils/unit/parseEther.ts +1 -1
- package/src/utils/unit/parseGwei.ts +1 -1
- package/dist/chunk-24EKKMUO.js.map +0 -1
- package/dist/chunk-BZFF747V.js.map +0 -1
- package/dist/chunk-E7LH5H76.mjs.map +0 -1
- package/dist/chunk-HRE6LYF3.js.map +0 -1
- package/dist/chunk-IVWG22RQ.mjs.map +0 -1
- package/dist/chunk-NYMS65YI.js.map +0 -1
- package/dist/chunk-PHSAQQGZ.mjs.map +0 -1
- package/dist/chunk-VAT5CEF7.mjs.map +0 -1
- package/dist/transactionRequest-8e970b0e.d.ts +0 -33
@@ -0,0 +1,108 @@
|
|
1
|
+
import { Abi } from 'abitype'
|
2
|
+
import type { PublicClient } from '../../clients'
|
3
|
+
import type {
|
4
|
+
Address,
|
5
|
+
ExtractEventArgsFromAbi,
|
6
|
+
ExtractEventNameFromAbi,
|
7
|
+
Filter,
|
8
|
+
Log,
|
9
|
+
} from '../../types'
|
10
|
+
import { observe } from '../../utils/observe'
|
11
|
+
import { poll } from '../../utils/poll'
|
12
|
+
import {
|
13
|
+
createContractEventFilter,
|
14
|
+
CreateContractEventFilterArgs,
|
15
|
+
} from './createContractEventFilter'
|
16
|
+
import { getFilterChanges } from './getFilterChanges'
|
17
|
+
import { uninstallFilter } from './uninstallFilter'
|
18
|
+
|
19
|
+
export type OnLogsResponse = Log[]
|
20
|
+
export type OnLogs = (logs: OnLogsResponse) => void
|
21
|
+
|
22
|
+
export type WatchContractEventArgs<
|
23
|
+
TAbi extends Abi | readonly unknown[] = Abi,
|
24
|
+
TEventName extends string = any,
|
25
|
+
> = {
|
26
|
+
/** The address of the contract. */
|
27
|
+
address: Address | Address[]
|
28
|
+
/** Contract ABI. */
|
29
|
+
abi: TAbi
|
30
|
+
/** Whether or not the event logs should be batched on each invocation. */
|
31
|
+
batch?: boolean
|
32
|
+
/** Contract event. */
|
33
|
+
eventName?: ExtractEventNameFromAbi<TAbi, TEventName>
|
34
|
+
/** The callback to call when an error occurred when trying to get for a new block. */
|
35
|
+
onError?: (error: Error) => void
|
36
|
+
/** The callback to call when new event logs are received. */
|
37
|
+
onLogs: OnLogs
|
38
|
+
/** Polling frequency (in ms). Defaults to Client's pollingInterval config. */
|
39
|
+
pollingInterval?: number
|
40
|
+
} & ExtractEventArgsFromAbi<TAbi, TEventName>
|
41
|
+
|
42
|
+
export function watchContractEvent<
|
43
|
+
TAbi extends Abi | readonly unknown[] = Abi,
|
44
|
+
TEventName extends string = any,
|
45
|
+
>(
|
46
|
+
client: PublicClient,
|
47
|
+
{
|
48
|
+
abi,
|
49
|
+
address,
|
50
|
+
args,
|
51
|
+
batch = true,
|
52
|
+
eventName,
|
53
|
+
onError,
|
54
|
+
onLogs,
|
55
|
+
pollingInterval = client.pollingInterval,
|
56
|
+
}: WatchContractEventArgs<TAbi, TEventName>,
|
57
|
+
) {
|
58
|
+
const observerId = JSON.stringify([
|
59
|
+
'watchContractEvent',
|
60
|
+
address,
|
61
|
+
args,
|
62
|
+
batch,
|
63
|
+
client.uid,
|
64
|
+
eventName,
|
65
|
+
pollingInterval,
|
66
|
+
])
|
67
|
+
|
68
|
+
return observe(observerId, { onLogs, onError }, (emit) => {
|
69
|
+
let filter: Filter<'event'>
|
70
|
+
|
71
|
+
const unwatch = poll(
|
72
|
+
async () => {
|
73
|
+
try {
|
74
|
+
if (!filter) {
|
75
|
+
try {
|
76
|
+
filter = await createContractEventFilter(client, {
|
77
|
+
abi,
|
78
|
+
address,
|
79
|
+
args,
|
80
|
+
eventName,
|
81
|
+
} as unknown as CreateContractEventFilterArgs)
|
82
|
+
return
|
83
|
+
} catch (err) {
|
84
|
+
unwatch()
|
85
|
+
throw err
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
const logs = await getFilterChanges(client, { filter })
|
90
|
+
if (logs.length === 0) return
|
91
|
+
if (batch) emit.onLogs(logs)
|
92
|
+
else logs.forEach((log) => emit.onLogs([log]))
|
93
|
+
} catch (err) {
|
94
|
+
emit.onError?.(err as Error)
|
95
|
+
}
|
96
|
+
},
|
97
|
+
{
|
98
|
+
emitOnBegin: true,
|
99
|
+
interval: pollingInterval,
|
100
|
+
},
|
101
|
+
)
|
102
|
+
|
103
|
+
return async () => {
|
104
|
+
if (filter) await uninstallFilter(client, { filter })
|
105
|
+
unwatch()
|
106
|
+
}
|
107
|
+
})
|
108
|
+
}
|
@@ -0,0 +1,190 @@
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
|
2
|
+
import { wait } from '../../utils/wait'
|
3
|
+
import {
|
4
|
+
accounts,
|
5
|
+
publicClient,
|
6
|
+
testClient,
|
7
|
+
transfer1Data,
|
8
|
+
usdcContractConfig,
|
9
|
+
vitalikAddress,
|
10
|
+
walletClient,
|
11
|
+
} from '../../_test'
|
12
|
+
import { impersonateAccount, stopImpersonatingAccount } from '../test'
|
13
|
+
import { sendTransaction } from '../wallet'
|
14
|
+
import * as createEventFilter from './createEventFilter'
|
15
|
+
import * as getFilterChanges from './getFilterChanges'
|
16
|
+
import { OnLogsResponse, watchEvent } from './watchEvent'
|
17
|
+
|
18
|
+
beforeAll(async () => {
|
19
|
+
await impersonateAccount(testClient, {
|
20
|
+
address: vitalikAddress,
|
21
|
+
})
|
22
|
+
})
|
23
|
+
|
24
|
+
afterAll(async () => {
|
25
|
+
await stopImpersonatingAccount(testClient, {
|
26
|
+
address: vitalikAddress,
|
27
|
+
})
|
28
|
+
})
|
29
|
+
|
30
|
+
test('default', async () => {
|
31
|
+
let logs: OnLogsResponse[] = []
|
32
|
+
|
33
|
+
const unwatch = watchEvent(publicClient, {
|
34
|
+
onLogs: (logs_) => logs.push(logs_),
|
35
|
+
})
|
36
|
+
|
37
|
+
await wait(1000)
|
38
|
+
await sendTransaction(walletClient, {
|
39
|
+
from: vitalikAddress,
|
40
|
+
to: usdcContractConfig.address,
|
41
|
+
data: transfer1Data(accounts[0].address),
|
42
|
+
})
|
43
|
+
await sendTransaction(walletClient, {
|
44
|
+
from: vitalikAddress,
|
45
|
+
to: usdcContractConfig.address,
|
46
|
+
data: transfer1Data(accounts[0].address),
|
47
|
+
})
|
48
|
+
await wait(1000)
|
49
|
+
await sendTransaction(walletClient, {
|
50
|
+
from: vitalikAddress,
|
51
|
+
to: usdcContractConfig.address,
|
52
|
+
data: transfer1Data(accounts[1].address),
|
53
|
+
})
|
54
|
+
await wait(2000)
|
55
|
+
unwatch()
|
56
|
+
|
57
|
+
expect(logs.length).toBe(2)
|
58
|
+
expect(logs[0].length).toBe(2)
|
59
|
+
expect(logs[1].length).toBe(1)
|
60
|
+
})
|
61
|
+
|
62
|
+
test('args: batch', async () => {
|
63
|
+
let logs: OnLogsResponse[] = []
|
64
|
+
|
65
|
+
const unwatch = watchEvent(publicClient, {
|
66
|
+
batch: false,
|
67
|
+
onLogs: (logs_) => logs.push(logs_),
|
68
|
+
})
|
69
|
+
|
70
|
+
await wait(1000)
|
71
|
+
await sendTransaction(walletClient, {
|
72
|
+
from: vitalikAddress,
|
73
|
+
to: usdcContractConfig.address,
|
74
|
+
data: transfer1Data(accounts[0].address),
|
75
|
+
})
|
76
|
+
await sendTransaction(walletClient, {
|
77
|
+
from: vitalikAddress,
|
78
|
+
to: usdcContractConfig.address,
|
79
|
+
data: transfer1Data(accounts[0].address),
|
80
|
+
})
|
81
|
+
await wait(1000)
|
82
|
+
await sendTransaction(walletClient, {
|
83
|
+
from: vitalikAddress,
|
84
|
+
to: usdcContractConfig.address,
|
85
|
+
data: transfer1Data(accounts[1].address),
|
86
|
+
})
|
87
|
+
await wait(2000)
|
88
|
+
unwatch()
|
89
|
+
|
90
|
+
expect(logs.length).toBe(3)
|
91
|
+
expect(logs[0].length).toBe(1)
|
92
|
+
expect(logs[1].length).toBe(1)
|
93
|
+
expect(logs[2].length).toBe(1)
|
94
|
+
})
|
95
|
+
|
96
|
+
test('args: address', async () => {
|
97
|
+
let logs: OnLogsResponse[] = []
|
98
|
+
let logs2: OnLogsResponse[] = []
|
99
|
+
|
100
|
+
const unwatch = watchEvent(publicClient, {
|
101
|
+
address: usdcContractConfig.address,
|
102
|
+
onLogs: (logs_) => logs.push(logs_),
|
103
|
+
})
|
104
|
+
const unwatch2 = watchEvent(publicClient, {
|
105
|
+
address: '0x0000000000000000000000000000000000000000',
|
106
|
+
onLogs: (logs_) => logs2.push(logs_),
|
107
|
+
})
|
108
|
+
|
109
|
+
await wait(1000)
|
110
|
+
await sendTransaction(walletClient, {
|
111
|
+
from: vitalikAddress,
|
112
|
+
to: usdcContractConfig.address,
|
113
|
+
data: transfer1Data(accounts[0].address),
|
114
|
+
})
|
115
|
+
await wait(2000)
|
116
|
+
unwatch()
|
117
|
+
unwatch2()
|
118
|
+
|
119
|
+
expect(logs.length).toBe(1)
|
120
|
+
expect(logs2.length).toBe(0)
|
121
|
+
})
|
122
|
+
|
123
|
+
test('args: address + event', async () => {
|
124
|
+
let logs: OnLogsResponse[] = []
|
125
|
+
let logs2: OnLogsResponse[] = []
|
126
|
+
|
127
|
+
const unwatch = watchEvent(publicClient, {
|
128
|
+
address: usdcContractConfig.address,
|
129
|
+
event: 'Transfer(address indexed, address indexed, uint256 indexed)',
|
130
|
+
onLogs: (logs_) => logs.push(logs_),
|
131
|
+
})
|
132
|
+
const unwatch2 = watchEvent(publicClient, {
|
133
|
+
address: usdcContractConfig.address,
|
134
|
+
event: 'Approval(address indexed, address indexed)',
|
135
|
+
onLogs: (logs_) => logs2.push(logs_),
|
136
|
+
})
|
137
|
+
|
138
|
+
await wait(1000)
|
139
|
+
await sendTransaction(walletClient, {
|
140
|
+
from: vitalikAddress,
|
141
|
+
to: usdcContractConfig.address,
|
142
|
+
data: transfer1Data(accounts[0].address),
|
143
|
+
})
|
144
|
+
await wait(2000)
|
145
|
+
unwatch()
|
146
|
+
unwatch2()
|
147
|
+
|
148
|
+
expect(logs.length).toBe(1)
|
149
|
+
expect(logs2.length).toBe(0)
|
150
|
+
})
|
151
|
+
|
152
|
+
test.todo('args: args')
|
153
|
+
|
154
|
+
describe('errors', () => {
|
155
|
+
test('handles error thrown from creating filter', async () => {
|
156
|
+
vi.spyOn(createEventFilter, 'createEventFilter').mockRejectedValueOnce(
|
157
|
+
new Error('foo'),
|
158
|
+
)
|
159
|
+
|
160
|
+
let unwatch: () => void = () => null
|
161
|
+
const error = await new Promise((resolve) => {
|
162
|
+
unwatch = watchEvent(publicClient, {
|
163
|
+
onLogs: () => null,
|
164
|
+
onError: resolve,
|
165
|
+
})
|
166
|
+
})
|
167
|
+
expect(error).toMatchInlineSnapshot('[Error: foo]')
|
168
|
+
unwatch()
|
169
|
+
})
|
170
|
+
|
171
|
+
test(
|
172
|
+
'handles error thrown from filter changes',
|
173
|
+
async () => {
|
174
|
+
vi.spyOn(getFilterChanges, 'getFilterChanges').mockRejectedValueOnce(
|
175
|
+
new Error('bar'),
|
176
|
+
)
|
177
|
+
|
178
|
+
let unwatch: () => void = () => null
|
179
|
+
const error = await new Promise((resolve) => {
|
180
|
+
unwatch = watchEvent(publicClient, {
|
181
|
+
onLogs: () => null,
|
182
|
+
onError: resolve,
|
183
|
+
})
|
184
|
+
})
|
185
|
+
expect(error).toMatchInlineSnapshot('[Error: bar]')
|
186
|
+
unwatch()
|
187
|
+
},
|
188
|
+
{ retry: 3 },
|
189
|
+
)
|
190
|
+
})
|
@@ -0,0 +1,95 @@
|
|
1
|
+
import type { PublicClient } from '../../clients'
|
2
|
+
import type { Address, EventDefinition, Filter, Log } from '../../types'
|
3
|
+
import { observe } from '../../utils/observe'
|
4
|
+
import { poll } from '../../utils/poll'
|
5
|
+
import { createEventFilter, EventFilterArgs } from './createEventFilter'
|
6
|
+
import { getFilterChanges } from './getFilterChanges'
|
7
|
+
import { uninstallFilter } from './uninstallFilter'
|
8
|
+
|
9
|
+
export type OnLogsResponse = Log[]
|
10
|
+
export type OnLogs = (logs: OnLogsResponse) => void
|
11
|
+
|
12
|
+
export type WatchEventArgs<TEventDefinition extends EventDefinition> = {
|
13
|
+
/** The address of the contract. */
|
14
|
+
address?: Address | Address[]
|
15
|
+
/** Whether or not the event logs should be batched on each invocation. */
|
16
|
+
batch?: boolean
|
17
|
+
/** The callback to call when an error occurred when trying to get for a new block. */
|
18
|
+
onError?: (error: Error) => void
|
19
|
+
/** The callback to call when new event logs are received. */
|
20
|
+
onLogs: OnLogs
|
21
|
+
/** Polling frequency (in ms). Defaults to Client's pollingInterval config. */
|
22
|
+
pollingInterval?: number
|
23
|
+
} & (
|
24
|
+
| {
|
25
|
+
event: TEventDefinition
|
26
|
+
args?: EventFilterArgs<TEventDefinition>
|
27
|
+
}
|
28
|
+
| {
|
29
|
+
event?: never
|
30
|
+
args?: never
|
31
|
+
}
|
32
|
+
)
|
33
|
+
|
34
|
+
export function watchEvent<TEventDefinition extends EventDefinition>(
|
35
|
+
client: PublicClient,
|
36
|
+
{
|
37
|
+
address,
|
38
|
+
args,
|
39
|
+
batch = true,
|
40
|
+
event,
|
41
|
+
onError,
|
42
|
+
onLogs,
|
43
|
+
pollingInterval = client.pollingInterval,
|
44
|
+
}: WatchEventArgs<TEventDefinition>,
|
45
|
+
) {
|
46
|
+
const observerId = JSON.stringify([
|
47
|
+
'watchEvent',
|
48
|
+
address,
|
49
|
+
args,
|
50
|
+
batch,
|
51
|
+
client.uid,
|
52
|
+
event,
|
53
|
+
pollingInterval,
|
54
|
+
])
|
55
|
+
|
56
|
+
return observe(observerId, { onLogs, onError }, (emit) => {
|
57
|
+
let filter: Filter<'event'>
|
58
|
+
|
59
|
+
const unwatch = poll(
|
60
|
+
async () => {
|
61
|
+
try {
|
62
|
+
if (!filter) {
|
63
|
+
try {
|
64
|
+
filter = await createEventFilter(client, {
|
65
|
+
address,
|
66
|
+
args,
|
67
|
+
event: event!,
|
68
|
+
})
|
69
|
+
return
|
70
|
+
} catch (err) {
|
71
|
+
unwatch()
|
72
|
+
throw err
|
73
|
+
}
|
74
|
+
}
|
75
|
+
|
76
|
+
const logs = await getFilterChanges(client, { filter })
|
77
|
+
if (logs.length === 0) return
|
78
|
+
if (batch) emit.onLogs(logs)
|
79
|
+
else logs.forEach((log) => emit.onLogs([log]))
|
80
|
+
} catch (err) {
|
81
|
+
emit.onError?.(err as Error)
|
82
|
+
}
|
83
|
+
},
|
84
|
+
{
|
85
|
+
emitOnBegin: true,
|
86
|
+
interval: pollingInterval,
|
87
|
+
},
|
88
|
+
)
|
89
|
+
|
90
|
+
return async () => {
|
91
|
+
if (filter) await uninstallFilter(client, { filter })
|
92
|
+
unwatch()
|
93
|
+
}
|
94
|
+
})
|
95
|
+
}
|
@@ -53,6 +53,7 @@ export function watchPendingTransactions(
|
|
53
53
|
}
|
54
54
|
|
55
55
|
const hashes = await getFilterChanges(client, { filter })
|
56
|
+
if (hashes.length === 0) return
|
56
57
|
if (batch) emit.onTransactions(hashes)
|
57
58
|
else hashes.forEach((hash) => emit.onTransactions([hash]))
|
58
59
|
} catch (err) {
|