-
Similar to import { createPublicClient, http } from 'viem'
import { base } from 'viem/chains'
import { publicActionsL2 } from 'viem/op-stack'
const client = createPublicClient({
chain: base,
transport: http(),
}).extend(publicActionsL2()) I am trying to extend viem with a custom Viem action. Here's the simplified version of my code - See typescript playground This version is simplified because it is split into different files. The goal (for us) is to define custom functions in a file, then do something like // Here functions such as getHeaderN are exported, and nothing else
import * as actions from "../actions/file"
const actionsToDecorator = function <
TActions extends Actions<TTransport, TChain>,
TTransport extends Transport = Transport,
TChain extends Chain | undefined = Chain | undefined
>(
actions: TActions
): (
client: PublicClient<TTransport, TChain>
) => DecoratedActions<TActions, TTransport, TChain> {
return function (
client: PublicClient<TTransport, TChain>
): DecoratedActions<TActions, TTransport, TChain> {
return Object.fromEntries(
Object.keys(actions).map((action) => [
action,
(properties: Parameters<TActions[typeof action]>[1]) =>
actions[action](client, properties),
])
) as DecoratedActions<TActions, TTransport, TChain>
}
}
const decorated = <
TTransport extends Transport = Transport,
TChain extends Chain | undefined = Chain | undefined
>() => actionsToDecorator<typeof actions, TTransport, TChain>(actions)
const client = createPublicClient(parameters)
.extend(decorated())
client.getHeaderN({ height: 1 }) While that seems to work (and I get the proper typing on The idea is to use Any help or ideas? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I was able to solve this - here is the updated playground. The trick was to use |
Beta Was this translation helpful? Give feedback.
I was able to solve this - here is the updated playground. The trick was to use
type Client
which is exported fromviem
, instead oftype PublicClient
. It seemsextend()
expects theviem
client instance is of typeClient
rather thanPublicClient
.