-
Notifications
You must be signed in to change notification settings - Fork 171
feat: add use send calls hook #1649
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
base: moldy/wallet-api-base
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { | ||
clientHeaderTrack, | ||
type EntryPointVersion, | ||
type UserOperationRequest, | ||
} from "@aa-sdk/core"; | ||
import type { GetSmartWalletClientResult } from "@account-kit/core/experimental"; | ||
import type { SmartWalletClient } from "@account-kit/wallet-client"; | ||
import { | ||
useMutation, | ||
type UseMutateAsyncFunction, | ||
type UseMutateFunction, | ||
} from "@tanstack/react-query"; | ||
import { sendTransaction as wagmi_sendTransaction } from "@wagmi/core"; | ||
import { fromHex, type Address, type Hex, type JsonRpcAccount } from "viem"; | ||
import { useAccount as wagmi_useAccount } from "wagmi"; | ||
import { | ||
ClientUndefinedHookError, | ||
UnsupportedEOAActionError, | ||
} from "../../errors.js"; | ||
import { useAlchemyAccountContext } from "../../hooks/useAlchemyAccountContext.js"; | ||
import { ReactLogger } from "../../metrics.js"; | ||
|
||
export type UseSendCallsParams = { | ||
client?: GetSmartWalletClientResult<Address>; | ||
}; | ||
|
||
type MutationResult< | ||
TEntryPointVersion extends EntryPointVersion = EntryPointVersion, | ||
> = { | ||
id: Hex; | ||
// TODO: deprecate this | ||
request?: UserOperationRequest<TEntryPointVersion>; | ||
}; | ||
|
||
export type UseSendCallsResult< | ||
TEntryPointVersion extends EntryPointVersion = EntryPointVersion, | ||
> = { | ||
sendCalls: UseMutateFunction< | ||
MutationResult<TEntryPointVersion>, | ||
Error, | ||
Parameters<SmartWalletClient<JsonRpcAccount<Address>>["prepareCalls"]>[0], | ||
unknown | ||
>; | ||
sendCallsAsync: UseMutateAsyncFunction< | ||
MutationResult<TEntryPointVersion>, | ||
Error, | ||
Parameters<SmartWalletClient<JsonRpcAccount<Address>>["prepareCalls"]>[0], | ||
unknown | ||
>; | ||
sendCallsResult: MutationResult | undefined; | ||
isSendingCalls: boolean; | ||
sendCallsError: Error | null; | ||
}; | ||
|
||
// TODO: remove the entrypoint version generic when we don't need it anymore | ||
export function useSendCalls< | ||
TEntryPointVersion extends EntryPointVersion = EntryPointVersion, | ||
>(params: UseSendCallsParams): UseSendCallsResult<TEntryPointVersion> { | ||
const { client: _client } = params; | ||
const { | ||
queryClient, | ||
config: { | ||
_internal: { wagmiConfig }, | ||
}, | ||
} = useAlchemyAccountContext(); | ||
const { isConnected } = wagmi_useAccount({ config: wagmiConfig }); | ||
|
||
const { | ||
mutate: sendCalls, | ||
mutateAsync: sendCallsAsync, | ||
data: sendCallsResult, | ||
isPending: isSendingCalls, | ||
error: sendCallsError, | ||
} = useMutation( | ||
{ | ||
mutationFn: async ( | ||
params: Parameters< | ||
SmartWalletClient<JsonRpcAccount<Address>>["prepareCalls"] | ||
>[0], | ||
): Promise<MutationResult<TEntryPointVersion>> => { | ||
if (isConnected) { | ||
console.warn( | ||
"useSendCalls: connected to an EOA, sending as a transaction instead", | ||
); | ||
if (params.calls.length !== 1) { | ||
throw new UnsupportedEOAActionError( | ||
"useSendCalls", | ||
"batch execute", | ||
); | ||
} | ||
|
||
const [call] = params.calls; | ||
|
||
const tx = await wagmi_sendTransaction(wagmiConfig, { | ||
to: call.to, | ||
data: call.data, | ||
value: call.value ? fromHex(call.value, "bigint") : undefined, | ||
}); | ||
|
||
return { | ||
id: tx, | ||
}; | ||
} | ||
|
||
if (!_client) { | ||
throw new ClientUndefinedHookError("useSendCalls"); | ||
} | ||
|
||
const client = clientHeaderTrack(_client, "reactUseSendCalls"); | ||
|
||
const preparedCalls = await client.prepareCalls(params); | ||
|
||
const signature = await client.signSignatureRequest( | ||
preparedCalls.signatureRequest, | ||
); | ||
|
||
const { preparedCallIds } = await client.sendPreparedCalls({ | ||
...preparedCalls, | ||
signature, | ||
}); | ||
Comment on lines
+113
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i assume we are going to need to handle the 7702 stuff in here too, and include the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we end up doing the |
||
|
||
return { | ||
id: preparedCallIds[0], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this might be a problem if we introduce sending multiple UOs in one request, as proposed here: https://github.com/alchemyplatform/wallet-server/pull/199/files#diff-c00cc16915cfc4387914060aa01512d771fe33d56927f69c95111ceebebc45abR123 |
||
request: { | ||
...preparedCalls.data, | ||
signature: signature.signature, | ||
} as UserOperationRequest<TEntryPointVersion>, | ||
}; | ||
}, | ||
}, | ||
queryClient, | ||
); | ||
|
||
return { | ||
sendCalls: ReactLogger.profiled("sendCalls", sendCalls), | ||
sendCallsAsync: ReactLogger.profiled("sendCallsAsync", sendCallsAsync), | ||
sendCallsResult, | ||
isSendingCalls, | ||
sendCallsError, | ||
}; | ||
} |
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.
actually... not sure if we should deprecate this. we will need it for drop and replace unless we come up with a better API for drop and replace