-
-
Notifications
You must be signed in to change notification settings - Fork 834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: use getAction internally #2627
Conversation
|
@holic is attempting to deploy a commit to the Wevm Team on Vercel. A member of the Team first needs to authorize it. |
bfba87f
to
e1b0ef6
Compare
dc841be
to
e96b06c
Compare
e96b06c
to
3600034
Compare
@@ -30,7 +30,6 @@ export type EstimateMaxPriorityFeePerGasErrorType = | |||
| GetBlockErrorType | |||
| HexToBigIntErrorType | |||
| RequestErrorType | |||
| GetBlockErrorType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was already in the union a few lines above
@@ -52,6 +52,7 @@ export type EstimateFeesPerGasReturnType< | |||
|
|||
export type EstimateFeesPerGasErrorType = | |||
| BaseFeeScalarErrorType | |||
| GetBlockErrorType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we call getBlock
here, so this error type should also be included here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My worry here is if this pattern is expected, that means third party actions will also need to remember to implement it.
Maybe this is a unimportant because a third party action is less likely to be overriden.
Maybe a solution to that would be to encourage folks to build actions using a defineAction(...)
utility or something?
return getAction(client, _getBlockNumber, 'getBlockNumber')(params) | ||
} | ||
|
||
export async function _getBlockNumber<chain extends Chain | undefined>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it actually necessary to export both? Shouldn't getBlockNumber always "just work"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is if I override getBlockNumber
with my own action, then inside that override call into viem's getBlockNumber
here, it'd end up resolving to my own getBlockNumber
, not viem's underlying one, because that's the one attached to the client object?
That points to a potentially deeper issue though - would this result in an infinite loop?
>( | ||
client: Client<Transport, chain, account>, | ||
params: GetBlockParameters<includeTransactions, blockTag> = {}, | ||
): Promise<GetBlockReturnType<chain, includeTransactions, blockTag>> { | ||
return getAction(client, _getBlock, 'getBlock')(params) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
>( | |
client: Client<Transport, chain, account>, | |
params: GetBlockParameters<includeTransactions, blockTag> = {}, | |
): Promise<GetBlockReturnType<chain, includeTransactions, blockTag>> { | |
return getAction(client, _getBlock, 'getBlock')(params) | |
} | |
>( | |
client: Client<Transport, chain, account>, | |
params: GetBlockParameters<includeTransactions, blockTag> = {}, | |
): Promise<GetBlockReturnType<chain, includeTransactions, blockTag>> { | |
return getAction(client, _getBlock, 'getBlock')(params) | |
} |
Might be able to do this just to save some boilerplate
const withGetAction = <TAction>(action: TAction, name: string): TAction => {
return ((client, params) => {
return getAction(client, action, name)(params)
}) as unknown as TAction
}
export const getBlock = withGetAction(_getBlock, 'getBlock')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DOn't do this it's convenient but will make stack traces suck
this is already true for |
So if I have two decorators that each override Which makes sense, because the action ultimately calls out to another lower level action or RPC method.
Nevermind, it does work like a stack of middleware. Test PR coming! |
b56e162
to
ad2831b
Compare
closing for now – will revisit later |
As an author of custom actions and action overrides, I will use viem actions internally but sometimes forget to call them
getAction
so that the actions can compose on one another.For example, maybe I'll use
getChainId
internally in my custom action, but I want this to be composable with an action that overridesgetChainId
to cache the RPC's chain ID. If I don't call this withgetAction
, I'll accidentally bypass this cache and viem will do the underlying RPC call.I've seen this pattern in both in my code, other SDKs, and within viem itself.
I am curious if, instead of requiring all action authors to use
getAction
, that viem itself exports by default thegetAction
-wrapped variant of each of these actions, while also exporting the internal implementation in case it's useful to make an explicit, direct call.I'm mocking this up here to see if this approach seems reasonable before doing more because its quite a big refactor.
PR-Codex overview
This PR focuses on refactoring functions in various action files to directly call
getBlock
,getBlockNumber
, andgetChainId
functions instead of usinggetAction
.Detailed summary
getAction
calls with direct calls togetBlock
,getBlockNumber
, andgetChainId
functions in multiple files.getAction
imports from specific files.