Skip to content

Commit

Permalink
convert createScaffoldReadContract to new runes/function format
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteAtATime committed Apr 16, 2024
1 parent 32c4421 commit 4e3b0df
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 56 deletions.
22 changes: 9 additions & 13 deletions packages/svelte/src/lib/runes/scaffoldReadContract.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,25 @@ import type { ReadContractErrorType } from "viem";
export const createScaffoldReadContract = <
TContractName extends ContractName,
TFunctionName extends ExtractAbiFunctionNames<ContractAbi<TContractName>, "pure" | "view">,
>({
contractName,
functionName,
args,
value,
...readConfig
}: CreateScaffoldReadConfig<TContractName, TFunctionName>) => {
>(
config: CreateScaffoldReadConfig<TContractName, TFunctionName>,
) => {
const { contractName, functionName, args, value, ...readConfig } = $derived(
config instanceof Function ? config() : config,
);
const { data: deployedContract } = $derived.by(createDeployedContractInfo(contractName));
const targetNetwork = $derived.by(createTargetNetwork());

const argsVal = $derived(typeof args === "function" ? args() : args);
const valueVal = $derived(typeof value === "function" ? value() : value);

const result = $derived.by(
createReadContract({
chainId: targetNetwork.id,
functionName,
address: deployedContract?.address,
abi: deployedContract?.abi,
watch: true,
args: argsVal,
value: valueVal,
enabled: !Array.isArray(argsVal) || !argsVal.some(arg => arg === undefined),
args: args,
value: value,
enabled: !Array.isArray(args) || !args.some(arg => arg === undefined),
...(readConfig as any),
}) as () => Omit<ReturnType<CreateReadContractReturnType>, "data" | "refetch"> & {
data: AbiFunctionReturnType<ContractAbi, TFunctionName> | undefined;
Expand Down
86 changes: 43 additions & 43 deletions packages/svelte/src/lib/utils/scaffold-eth/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ export type AbiFunctionOutputs<TAbi extends Abi, TFunctionName extends string> =
export type AbiFunctionReturnType<TAbi extends Abi, TFunctionName extends string> = IsContractDeclarationMissing<
any,
AbiParametersToPrimitiveTypes<AbiFunctionOutputs<TAbi, TFunctionName>> extends readonly [any]
? AbiParametersToPrimitiveTypes<AbiFunctionOutputs<TAbi, TFunctionName>>[0]
: AbiParametersToPrimitiveTypes<AbiFunctionOutputs<TAbi, TFunctionName>>
? AbiParametersToPrimitiveTypes<AbiFunctionOutputs<TAbi, TFunctionName>>[0]
: AbiParametersToPrimitiveTypes<AbiFunctionOutputs<TAbi, TFunctionName>>
>;

export type AbiEventInputs<TAbi extends Abi, TEventName extends ExtractAbiEventNames<TAbi>> = ExtractAbiEvent<
Expand Down Expand Up @@ -159,33 +159,33 @@ type UseScaffoldArgsParam<
TFunctionName extends ExtractAbiFunctionNames<ContractAbi<TContractName>>,
> =
TFunctionName extends FunctionNamesWithInputs<TContractName>
? {
args: FuncOrValue<
OptionalTupple<UnionToIntersection<AbiFunctionArguments<ContractAbi<TContractName>, TFunctionName>>>
>;
value?: FuncOrValue<
ExtractAbiFunction<ContractAbi<TContractName>, TFunctionName>["stateMutability"] extends "payable"
? bigint | undefined
: undefined
>;
}
: {
args?: never;
value?: never;
};
? {
args: FuncOrValue<
OptionalTupple<UnionToIntersection<AbiFunctionArguments<ContractAbi<TContractName>, TFunctionName>>>
>;
value?: FuncOrValue<
ExtractAbiFunction<ContractAbi<TContractName>, TFunctionName>["stateMutability"] extends "payable"
? bigint | undefined
: undefined
>;
}
: {
args?: never;
value?: never;
};

export type CreateScaffoldReadConfig<
TContractName extends ContractName,
TFunctionName extends ExtractAbiFunctionNames<ContractAbi<TContractName>, ReadAbiStateMutability>,
> = {
> = FuncOrValue<{
contractName: TContractName;
} & IsContractDeclarationMissing<
Partial<CreateReadContractParameters>,
{
functionName: TFunctionName;
} & UseScaffoldArgsParam<TContractName, TFunctionName> &
Omit<CreateReadContractParameters, "chainId" | "abi" | "address" | "functionName" | "args">
>;
Omit<CreateReadContractParameters, "chainId" | "abi" | "address" | "functionName" | "args">
>>;

export type scaffoldWriteContractVariables<
TContractName extends ContractName,
Expand All @@ -195,7 +195,7 @@ export type scaffoldWriteContractVariables<
{
functionName: TFunctionName;
} & UseScaffoldArgsParam<TContractName, TFunctionName> &
Omit<WriteContractParameters, "chainId" | "abi" | "address" | "functionName" | "args">
Omit<WriteContractParameters, "chainId" | "abi" | "address" | "functionName" | "args">
>;

type WriteVariables = WriteContractVariables<Abi, string, any[], Config, number>;
Expand Down Expand Up @@ -224,13 +224,13 @@ export type EventFilters<
> = IsContractDeclarationMissing<
any,
IndexedEventInputs<TContractName, TEventName> extends never
? never
: {
[Key in IsContractDeclarationMissing<
any,
IndexedEventInputs<TContractName, TEventName>["name"]
>]?: AbiParameterToPrimitiveType<Extract<IndexedEventInputs<TContractName, TEventName>, { name: Key }>>;
}
? never
: {
[Key in IsContractDeclarationMissing<
any,
IndexedEventInputs<TContractName, TEventName>["name"]
>]?: AbiParameterToPrimitiveType<Extract<IndexedEventInputs<TContractName, TEventName>, { name: Key }>>;
}
>;

export type UseScaffoldEventHistoryConfig<
Expand Down Expand Up @@ -263,22 +263,22 @@ export type UseScaffoldEventHistoryData<
>,
> =
| IsContractDeclarationMissing<
any[],
{
log: Log<bigint, number, false, TEvent, false, [TEvent], TEventName>;
args: AbiParametersToPrimitiveTypes<TEvent["inputs"]> &
GetEventArgs<
ContractAbi<TContractName>,
TEventName,
{
IndexedOnly: false;
}
>;
block: TBlockData extends true ? Block<bigint, true> : null;
receipt: TReceiptData extends true ? GetTransactionReturnType : null;
transaction: TTransactionData extends true ? GetTransactionReceiptReturnType : null;
}[]
>
any[],
{
log: Log<bigint, number, false, TEvent, false, [TEvent], TEventName>;
args: AbiParametersToPrimitiveTypes<TEvent["inputs"]> &
GetEventArgs<
ContractAbi<TContractName>,
TEventName,
{
IndexedOnly: false;
}
>;
block: TBlockData extends true ? Block<bigint, true> : null;
receipt: TReceiptData extends true ? GetTransactionReturnType : null;
transaction: TTransactionData extends true ? GetTransactionReceiptReturnType : null;
}[]
>
| undefined;

export type AbiParameterTuple = Extract<AbiParameter, { type: "tuple" | `tuple[${string}]` }>;

0 comments on commit 4e3b0df

Please sign in to comment.