Skip to content

Commit

Permalink
Feat: Add v1 SequencerInbox getters
Browse files Browse the repository at this point in the history
Closes FS-535
  • Loading branch information
chrstph-dvx committed Jun 27, 2024
1 parent 4fedd67 commit 534cfd6
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/actions/getMaxTimeVariation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Address, Chain, PublicClient, Transport } from 'viem';
import { sequencerInbox } from '../contracts';
import { ActionParameters } from '../types/Actions';

export type GetMaxTimeVariationActionParameters<Curried extends boolean> = ActionParameters<
{},
'sequencerInbox',
Curried
>;

export type GetMaxTimeVariationReturnType = {
delayBlocks: bigint;
futureBlocks: bigint;
delaySeconds: bigint;
futureSeconds: bigint;
};

export async function getMaxTimeVariation<TChain extends Chain | undefined>(
client: PublicClient<Transport, TChain>,
args: {
sequencerInbox: Address;
},
): Promise<GetMaxTimeVariationReturnType> {
const [delayBlocks, futureBlocks, delaySeconds, futureSeconds] = await client.readContract({
abi: sequencerInbox.abi,
functionName: 'maxTimeVariation',
address: args.sequencerInbox,
});
return {
delayBlocks,
futureBlocks,
delaySeconds,
futureSeconds,
};
}
32 changes: 32 additions & 0 deletions src/actions/isBatchPoster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Address, Chain, PublicClient, ReadContractReturnType, Transport } from 'viem';
import { sequencerInbox } from '../contracts';
import { ActionParameters, WithContractAddress } from '../types/Actions';

type Args = {
batchPoster: Address;
};
type SequencerInboxABI = typeof sequencerInbox.abi;
type IsBatchPosterParameters<Curried extends boolean = false> = WithContractAddress<
Args,
'sequencerInbox',
Curried
>;
export type IsBatchPosterActionParameters<Curried extends boolean> = ActionParameters<
Args,
'sequencerInbox',
Curried
>;

export type IsBatchPosterReturnType = ReadContractReturnType<SequencerInboxABI, 'isBatchPoster'>;

export async function isBatchPoster<TChain extends Chain | undefined>(
client: PublicClient<Transport, TChain>,
args: IsBatchPosterParameters,
): Promise<IsBatchPosterReturnType> {
return client.readContract({
abi: sequencerInbox.abi,
functionName: 'isBatchPoster',
address: args.sequencerInbox,
args: [args.batchPoster],
});
}
35 changes: 35 additions & 0 deletions src/actions/isValidKeysetHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Chain, Hex, PublicClient, ReadContractReturnType, Transport } from 'viem';
import { sequencerInbox } from '../contracts';
import { ActionParameters, WithContractAddress } from '../types/Actions';

type Args = {
keysetHash: Hex;
};
type SequencerInboxABI = typeof sequencerInbox.abi;
type IsValidKeysetHashParameters<Curried extends boolean = false> = WithContractAddress<
Args,
'sequencerInbox',
Curried
>;
export type IsValidKeysetHashActionParameters<Curried extends boolean> = ActionParameters<
Args,
'sequencerInbox',
Curried
>;

export type IsValidKeysetHashReturnType = ReadContractReturnType<
SequencerInboxABI,
'isValidKeysetHash'
>;

export async function isValidKeysetHash<TChain extends Chain | undefined>(
client: PublicClient<Transport, TChain>,
args: IsValidKeysetHashParameters,
): Promise<IsValidKeysetHashReturnType> {
return client.readContract({
abi: sequencerInbox.abi,
functionName: 'isValidKeysetHash',
address: args.sequencerInbox,
args: [args.keysetHash],
});
}
45 changes: 45 additions & 0 deletions src/types/Actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 & { [key in ContractName]: Address }>
: Args extends { args?: undefined }
?
| {
[key in ContractName]: Address;
}
| void
: Args & {
[key in ContractName]?: Address;
}
>;

0 comments on commit 534cfd6

Please sign in to comment.