From 280ae7ddd73abea443e677d900287b4fb305ffde Mon Sep 17 00:00:00 2001 From: Christophe Date: Mon, 10 Jun 2024 09:45:27 +0000 Subject: [PATCH] Add Actions type --- src/types/Actions.ts | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/types/Actions.ts diff --git a/src/types/Actions.ts b/src/types/Actions.ts new file mode 100644 index 00000000..5bf794cd --- /dev/null +++ b/src/types/Actions.ts @@ -0,0 +1,45 @@ +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; + } +>;