@effectstream/utils 0.100.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/src/viem.ts ADDED
@@ -0,0 +1,41 @@
1
+ import type {
2
+ Account,
3
+ Address,
4
+ HttpTransport,
5
+ ParseAccount,
6
+ PublicClient,
7
+ PublicClientConfig,
8
+ RpcSchema,
9
+ Transport,
10
+ } from "viem";
11
+ import { type Chain, createPublicClient, http } from "viem";
12
+ import type { Evm4ByteSelector, EvmSelector } from "./types/nominal.ts";
13
+
14
+ export function truncateSelector(fullSelector: EvmSelector): Evm4ByteSelector {
15
+ return fullSelector.slice(0, 10) as Evm4ByteSelector;
16
+ }
17
+
18
+ export function createViemPublicClient<
19
+ chain extends Chain,
20
+ transport extends Transport = HttpTransport,
21
+ accountOrAddress extends Account | Address | undefined = undefined,
22
+ rpcSchema extends RpcSchema | undefined = undefined,
23
+ >(
24
+ chain: chain,
25
+ override?: Partial<
26
+ Omit<
27
+ PublicClientConfig<transport, chain, accountOrAddress, rpcSchema>,
28
+ "chain"
29
+ >
30
+ >,
31
+ ): PublicClient<transport, chain, ParseAccount<accountOrAddress>, rpcSchema> {
32
+ return createPublicClient({
33
+ chain,
34
+ transport: http(chain.rpcUrls.default.http[0], {
35
+ // batch is needed for sync protocols to be efficient. Does this ever cause issues in other scenarios?
36
+ batch: true,
37
+ // TODO: transport options should come from the sync protocol configuration
38
+ }),
39
+ ...override,
40
+ }) as any;
41
+ }