Skip to content
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

fix(contract.ts): improve missing config check #372

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions packages/nextjs/utils/scaffold-eth/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ export type GenericContractsDeclaration = {

export const contracts = contractsData as GenericContractsDeclaration | null;

type IsContractsFileMissing<TYes, TNo> = typeof contractsData extends null ? TYes : TNo;
type ContractsDeclaration = IsContractsFileMissing<GenericContractsDeclaration, typeof contractsData>;
type ConfiguredChainId = (typeof scaffoldConfig)["targetNetwork"]["id"];

export type Chain = keyof ContractsDeclaration;
type IsContractDeclarationMissing<TYes, TNo> = typeof contractsData extends { [key in ConfiguredChainId]: any }
? TNo
: TYes;

type ContractsDeclaration = IsContractDeclarationMissing<GenericContractsDeclaration, typeof contractsData>;

type SelectedChainId = IsContractsFileMissing<number, (typeof scaffoldConfig)["targetNetwork"]["id"]>;
export type Chain = keyof ContractsDeclaration;

type Contracts = ContractsDeclaration[SelectedChainId][0]["contracts"];
type Contracts = ContractsDeclaration[ConfiguredChainId][0]["contracts"];

export type ContractName = keyof Contracts;

Expand All @@ -58,7 +61,7 @@ export type AbiFunctionOutputs<TAbi extends Abi, TFunctionName extends string> =
TFunctionName
>["outputs"];

export type AbiFunctionReturnType<TAbi extends Abi, TFunctionName extends string> = IsContractsFileMissing<
export type AbiFunctionReturnType<TAbi extends Abi, TFunctionName extends string> = IsContractDeclarationMissing<
any,
AbiParametersToPrimitiveTypes<AbiFunctionOutputs<TAbi, TFunctionName>>[0]
>;
Expand Down Expand Up @@ -144,7 +147,7 @@ export type UseScaffoldReadConfig<
TFunctionName extends ExtractAbiFunctionNames<ContractAbi<TContractName>, ReadAbiStateMutability>,
> = {
contractName: TContractName;
} & IsContractsFileMissing<
} & IsContractDeclarationMissing<
Partial<UseContractReadConfig>,
{
functionName: TFunctionName;
Expand All @@ -160,7 +163,7 @@ export type UseScaffoldWriteConfig<
value?: string;
onBlockConfirmation?: (txnReceipt: TransactionReceipt) => void;
blockConfirmations?: number;
} & IsContractsFileMissing<
} & IsContractDeclarationMissing<
Partial<UseContractWriteConfig<"recklesslyUnprepared">> & { args?: unknown[] },
{
functionName: TFunctionName;
Expand All @@ -174,7 +177,7 @@ export type UseScaffoldEventConfig<
TEventInputs extends AbiEventArgs<ContractAbi<TContractName>, TEventName> & any[],
> = {
contractName: TContractName;
} & IsContractsFileMissing<
} & IsContractDeclarationMissing<
UseContractEventConfig,
{
eventName: TEventName;
Expand All @@ -191,12 +194,12 @@ type IndexedEventInputs<
export type EventFilters<
TContractName extends ContractName,
TEventName extends ExtractAbiEventNames<ContractAbi<TContractName>>,
> = IsContractsFileMissing<
> = IsContractDeclarationMissing<
any,
IndexedEventInputs<TContractName, TEventName> extends never
? never
: {
[Key in IsContractsFileMissing<
[Key in IsContractDeclarationMissing<
any,
IndexedEventInputs<TContractName, TEventName>["name"]
>]?: AbiParameterToPrimitiveType<Extract<IndexedEventInputs<TContractName, TEventName>, { name: Key }>>;
Expand All @@ -208,7 +211,7 @@ export type UseScaffoldEventHistoryConfig<
TEventName extends ExtractAbiEventNames<ContractAbi<TContractName>>,
> = {
contractName: TContractName;
eventName: IsContractsFileMissing<string, TEventName>;
eventName: IsContractDeclarationMissing<string, TEventName>;
fromBlock: number;
filters?: EventFilters<TContractName, TEventName>;
blockData?: boolean;
Expand Down