moonchute
Version:
React hook for account abstraction
190 lines (175 loc) • 8.74 kB
TypeScript
import * as _tanstack_react_query from '@tanstack/react-query';
import { UseQueryOptions, QueryObserverResult, QueryClient } from '@tanstack/react-query';
import { Abi } from 'abitype';
import { SimulateContractParameters, Address, GetFunctionArgs } from 'viem';
import * as _tanstack_query_core from '@tanstack/query-core';
import { Persister } from '@tanstack/react-query-persist-client';
import { Mutate, StoreApi } from 'zustand/vanilla';
import * as React from 'react';
type PartialBy$3<TType, TKeys extends keyof TType> = Partial<Pick<TType, TKeys>> & Omit<TType, TKeys>;
type CreateSmartAccountConfig<TAbi extends Abi | readonly unknown[] = Abi, TFunctionName extends string = string> = PartialBy$3<Omit<SimulateContractParameters<TAbi, TFunctionName>, "chain">, "functionName" | "address"> & {
owner: Address;
chainId: number;
appId: string;
};
type CreateSmartAccountResult = {
userOpHash: `0x${string}`;
};
type Omit$4<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type PartialBy$2<TType, TKeys extends keyof TType> = Partial<Pick<TType, TKeys>> & Omit$4<TType, TKeys>;
type UseCreateSmartAccountArgs<TAbi extends Abi | readonly unknown[] = Abi, TFunctionName extends string = string> = PartialBy$2<CreateSmartAccountConfig, "abi" | "address" | "functionName" | "appId" | "chainId" | "owner"> & Partial<GetFunctionArgs<TAbi, TFunctionName>>;
type UseCreateSmartAccountConfig<TAbi extends Abi | readonly unknown[] = Abi, TFunctionName extends string = string> = Omit$4<UseCreateSmartAccountArgs<TAbi, TFunctionName>, "abi"> & {
abi?: TAbi;
};
type MutationFn$1<TReturnType> = (() => TReturnType) | undefined;
declare function useCreateSmartAccount<TAbi extends Abi | readonly unknown[], TFunctionName extends string>(config: UseCreateSmartAccountConfig<TAbi, TFunctionName>): {
data: CreateSmartAccountResult | undefined;
error: unknown;
isError: boolean;
isIdle: boolean;
isLoading: boolean;
isSuccess: boolean;
mutate: _tanstack_react_query.UseMutateFunction<CreateSmartAccountResult, unknown, UseCreateSmartAccountArgs<Abi, string>, unknown>;
mutateAsync: _tanstack_react_query.UseMutateAsyncFunction<CreateSmartAccountResult, unknown, UseCreateSmartAccountArgs<Abi, string>, unknown>;
reset: () => void;
status: "error" | "idle" | "loading" | "success";
variables: UseCreateSmartAccountArgs<Abi, string> | undefined;
write: MutationFn$1<CreateSmartAccountResult>;
};
type QueryConfig<TData, TError, TSelectData = TData> = Pick<UseQueryOptions<TData, TError, TSelectData>, "cacheTime" | "enabled" | "isDataEqual" | "staleTime" | "structuralSharing" | "suspense" | "onError" | "onSettled" | "onSuccess"> & {
/** Scope the cache to a given context. */
scopeKey?: string;
};
type userOperationType = {
sender: `0x${string}`;
nonce: bigint | string;
initCode: `0x${string}`;
callData: `0x${string}`;
callGasLimit: bigint | string;
verificationGasLimit: bigint | string;
preVerificationGas: bigint | string;
maxFeePerGas: bigint | string;
maxPriorityFeePerGas: bigint | string;
paymasterAndData: `0x${string}`;
signature: `0x${string}`;
};
type PartialBy$1<TType, TKeys extends keyof TType> = Partial<Pick<TType, TKeys>> & Omit<TType, TKeys>;
type GetUnsignedUserOperationConfig<TAbi extends Abi | readonly unknown[] = Abi, TFunctionName extends string = string> = PartialBy$1<Omit<SimulateContractParameters<TAbi, TFunctionName>, "chain" | "account" | "value">, "functionName" | "address"> & {
account?: string;
owner?: string;
chainId: number;
appId: string;
value?: bigint;
};
type GetUnsignedUserOperationResult = {
userOpHash: `0x${string}`;
accountType: number;
userOp: userOperationType;
chainId: number;
};
type Omit$3<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type PartialBy<TType, TKeys extends keyof TType> = Partial<Pick<TType, TKeys>> & Omit$3<TType, TKeys>;
type UsePrepareUserOperationConfig<TAbi extends Abi | readonly unknown[] = Abi, TFunctionName extends string = string> = PartialBy<Omit$3<GetUnsignedUserOperationConfig<TAbi, TFunctionName>, "appId" | "chainId" | "owner">, "abi" | "address" | "functionName"> & Partial<GetFunctionArgs<TAbi, TFunctionName>> & QueryConfig<GetUnsignedUserOperationResult, Error>;
declare function usePrepareUserOperation<TAbi extends Abi | readonly unknown[]>({ account, address, value, abi, functionName, args, }: UsePrepareUserOperationConfig<TAbi>): Pick<_tanstack_query_core.QueryObserverResult<GetUnsignedUserOperationResult, unknown>, "data" | "error" | "isError" | "isLoading" | "isSuccess" | "isFetched" | "isFetchedAfterMount" | "isFetching" | "isRefetching" | "refetch" | "fetchStatus"> & {
isIdle: boolean;
status: "error" | "idle" | "loading" | "success";
internal: Pick<_tanstack_query_core.QueryObserverResult, "failureCount" | "isPaused" | "isLoadingError" | "isRefetchError" | "dataUpdatedAt" | "errorUpdatedAt" | "isPlaceholderData" | "isPreviousData" | "isStale" | "remove">;
} & {
config: GetUnsignedUserOperationResult;
};
type SendUserOperationArgs = {
userOp: userOperationType;
userOpHash: `0x${string}`;
chainId: number;
accountType: number;
appId: string;
};
type SendUserOperationResult = {
userOpHash: `0x${string}`;
};
type Omit$2<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type WithOptional$1<T, K extends keyof T> = Omit$2<T, K> & Partial<Pick<T, K>>;
type UseSendUserOperationArgs = Partial<WithOptional$1<SendUserOperationArgs, "appId"> | undefined>;
type UseSendUserOperationConfig = Partial<GetUnsignedUserOperationResult>;
type MutationFn<TReturnType> = (() => TReturnType) | undefined;
declare function useSendUserOperation(config: UseSendUserOperationConfig): {
data: SendUserOperationResult | undefined;
error: unknown;
isError: boolean;
isIdle: boolean;
isLoading: boolean;
isSuccess: boolean;
reset: () => void;
status: "error" | "idle" | "loading" | "success";
variables: UseSendUserOperationArgs;
write: MutationFn<SendUserOperationResult>;
};
type UseQueryResult<TData, TError> = Pick<QueryObserverResult<TData, TError>, "data" | "error" | "fetchStatus" | "isError" | "isFetched" | "isFetchedAfterMount" | "isFetching" | "isLoading" | "isRefetching" | "isSuccess" | "refetch"> & {
isIdle: boolean;
status: "idle" | "loading" | "success" | "error";
internal: Pick<QueryObserverResult, "dataUpdatedAt" | "errorUpdatedAt" | "failureCount" | "isLoadingError" | "isPaused" | "isPlaceholderData" | "isPreviousData" | "isRefetchError" | "isStale" | "remove">;
};
type SmartAccount = {
address: string;
factoryAddress: string;
validator: string | null;
provider: string;
};
type FetchSmartAccountsArgs = {
address?: string;
chainId?: number;
appId: string | undefined;
};
type FetchSmartAccountsResult = {
smartAccount: SmartAccount[] | undefined;
};
type Omit$1<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type WithOptional<T, K extends keyof T> = Omit$1<T, K> & Partial<Pick<T, K>>;
type UseSmartAccountsArgs = WithOptional<FetchSmartAccountsArgs, "appId">;
declare function useSmartAccounts({ address, chainId }: UseSmartAccountsArgs): UseQueryResult<FetchSmartAccountsResult, unknown>;
type ClientStorage = {
getItem<T>(key: string, defaultState?: T | null): T | null;
setItem<T>(key: string, value: T | null): void;
removeItem(key: string): void;
};
type CreateConfigParameters = {
appId: string;
queryClient?: QueryClient;
persister?: Persister;
storage?: ClientStorage;
};
type Data = {};
type State = {
data?: Data;
appId?: string;
error?: Error;
status: "idle" | "loading" | "success" | "error";
};
declare class Config {
storage: ClientStorage;
store: Mutate<StoreApi<State>, [
[
"zustand/subscribeWithSelector",
never
],
[
"zustand/persist",
Partial<State>
]
]>;
constructor({ storage, appId, }: {
storage?: ClientStorage;
appId: string;
});
get appId(): string | undefined;
}
declare function createMoonChuteConfig({ queryClient, storage, persister, ...args }: CreateConfigParameters): Config & {
queryClient: QueryClient;
};
type ChuteConfigProps = {
config: Config & {
queryClient: QueryClient;
};
};
declare function MoonChuteConfig({ children, config, }: React.PropsWithChildren<ChuteConfigProps>): React.FunctionComponentElement<React.ProviderProps<Config | undefined>>;
export { MoonChuteConfig, createMoonChuteConfig, useCreateSmartAccount, usePrepareUserOperation, useSendUserOperation, useSmartAccounts };