diff --git a/src/actions/addSequencerL2Batch.ts b/src/actions/addSequencerL2Batch.ts index c288862c..37477077 100644 --- a/src/actions/addSequencerL2Batch.ts +++ b/src/actions/addSequencerL2Batch.ts @@ -9,14 +9,15 @@ import { } from 'viem'; import { sequencerInbox } from '../contracts'; import { WithContractAddress, ActionParameters, WithAccount } from '../types/Actions'; +import { Prettify } from '../types/utils'; type Args = GetFunctionArgs; type SequencerInboxABI = typeof sequencerInbox.abi; -type BuildAddSequencerL2BatchParameters = WithAccount< - WithContractAddress +type BuildAddSequencerL2BatchParameters = Prettify< + WithAccount> >; -export type BuildAddSequencerL2BatchActionParameters = WithAccount< - ActionParameters +export type BuildAddSequencerL2BatchActionParameters = Prettify< + WithAccount> >; export type BuildAddSequencerL2BatchReturnType = PrepareTransactionRequestReturnType; diff --git a/src/types/Actions.ts b/src/types/Actions.ts new file mode 100644 index 00000000..c1bc3edc --- /dev/null +++ b/src/types/Actions.ts @@ -0,0 +1,49 @@ +import { Address } from 'viem'; +import { Prettify } from './utils'; + +type RemoveUndefinedArgs = T extends { args?: undefined } ? Omit : T; + +/** + * If the function has no args, `GetFunctionArgs` returns `{ args?: undefined }` + * we remove args from the returned type + * + * Contract address is required if no contract address was passed to the actions, otherwise it's optional + */ +export type WithContractAddress< + Args, + ContractName extends string, + Curried extends boolean = false, +> = Prettify< + RemoveUndefinedArgs< + Args & + (Curried extends true + ? { + [key in ContractName]?: Address; + } + : { [key in ContractName]: Address }) + > +>; + +/** + * Actions require contract address, but as part of decorators, the address might have been passed already to the decorator. + * + * If the address was passed to the decorator, it's now optional (we still allow overrides of the address per action). + * If the action doesn't have any other parameters beside the contract address, then parameters can either be { contract: address } or void + */ +export type ActionParameters = Prettify< + Curried extends false + ? RemoveUndefinedArgs + : Args extends { args?: undefined } + ? + | { + [key in ContractName]: Address; + } + | void + : Args & { + [key in ContractName]?: Address; + } +>; + +export type WithAccount = Args & { + account: Address; +};