Skip to content

Commit

Permalink
Fix typing
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstph-dvx committed Jun 10, 2024
1 parent 488e15f commit 760d796
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/actions/addSequencerL2Batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SequencerInboxABI, 'addSequencerL2Batch'>;
type SequencerInboxABI = typeof sequencerInbox.abi;
type BuildAddSequencerL2BatchParameters<Curried extends boolean = false> = WithAccount<
WithContractAddress<Args, 'sequencerInbox', Curried>
type BuildAddSequencerL2BatchParameters<Curried extends boolean = false> = Prettify<
WithAccount<WithContractAddress<Args, 'sequencerInbox', Curried>>
>;
export type BuildAddSequencerL2BatchActionParameters<Curried extends boolean> = WithAccount<
ActionParameters<Args, 'sequencerInbox', Curried>
export type BuildAddSequencerL2BatchActionParameters<Curried extends boolean> = Prettify<
WithAccount<ActionParameters<Args, 'sequencerInbox', Curried>>
>;

export type BuildAddSequencerL2BatchReturnType = PrepareTransactionRequestReturnType;
Expand Down
49 changes: 49 additions & 0 deletions src/types/Actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Address } from 'viem';
import { Prettify } from './utils';

type RemoveUndefinedArgs<T> = T extends { args?: undefined } ? Omit<T, 'args'> : 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<Args, ContractName extends string, Curried extends boolean> = Prettify<
Curried extends false
? RemoveUndefinedArgs<Args & { sequencerInbox: Address }>
: Args extends { args?: undefined }
?
| {
[key in ContractName]: Address;
}
| void
: Args & {
[key in ContractName]?: Address;
}
>;

export type WithAccount<Args> = Args & {
account: Address;
};

0 comments on commit 760d796

Please sign in to comment.