From 910cae3040707752843ad99176d028d79a67574e Mon Sep 17 00:00:00 2001 From: Christophe Date: Thu, 27 Jun 2024 18:22:50 +0000 Subject: [PATCH] Feat: Add v1 SequencerInbox getters Closes FS-535 --- src/actions/getMaxTimeVariation.ts | 33 ++++++++++++++++++++++++++++++ src/actions/isBatchPoster.ts | 27 ++++++++++++++++++++++++ src/actions/isValidKeysetHash.ts | 31 ++++++++++++++++++++++++++++ src/types/Actions.ts | 24 ++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 src/actions/getMaxTimeVariation.ts create mode 100644 src/actions/isBatchPoster.ts create mode 100644 src/actions/isValidKeysetHash.ts create mode 100644 src/types/Actions.ts diff --git a/src/actions/getMaxTimeVariation.ts b/src/actions/getMaxTimeVariation.ts new file mode 100644 index 00000000..617b8a1d --- /dev/null +++ b/src/actions/getMaxTimeVariation.ts @@ -0,0 +1,33 @@ +import { Chain, PublicClient, Transport } from 'viem'; +import { sequencerInbox } from '../contracts'; +import { ActionParameters } from '../types/Actions'; + +export type GetMaxTimeVariationParameters = ActionParameters< + {}, + 'sequencerInbox', + Curried +>; + +export type GetMaxTimeVariationReturnType = { + delayBlocks: bigint; + futureBlocks: bigint; + delaySeconds: bigint; + futureSeconds: bigint; +}; + +export async function getMaxTimeVariation( + client: PublicClient, + args: GetMaxTimeVariationParameters, +): Promise { + const [delayBlocks, futureBlocks, delaySeconds, futureSeconds] = await client.readContract({ + abi: sequencerInbox.abi, + functionName: 'maxTimeVariation', + address: args.sequencerInbox, + }); + return { + delayBlocks, + futureBlocks, + delaySeconds, + futureSeconds, + }; +} diff --git a/src/actions/isBatchPoster.ts b/src/actions/isBatchPoster.ts new file mode 100644 index 00000000..e878735a --- /dev/null +++ b/src/actions/isBatchPoster.ts @@ -0,0 +1,27 @@ +import { Address, Chain, PublicClient, ReadContractReturnType, Transport } from 'viem'; +import { sequencerInbox } from '../contracts'; +import { ActionParameters } from '../types/Actions'; + +type Args = { + batchPoster: Address; +}; +type SequencerInboxABI = typeof sequencerInbox.abi; +export type IsBatchPosterParameters = ActionParameters< + Args, + 'sequencerInbox', + Curried +>; + +export type IsBatchPosterReturnType = ReadContractReturnType; + +export async function isBatchPoster( + client: PublicClient, + args: IsBatchPosterParameters, +): Promise { + return client.readContract({ + abi: sequencerInbox.abi, + functionName: 'isBatchPoster', + address: args.sequencerInbox, + args: [args.batchPoster], + }); +} diff --git a/src/actions/isValidKeysetHash.ts b/src/actions/isValidKeysetHash.ts new file mode 100644 index 00000000..b1896205 --- /dev/null +++ b/src/actions/isValidKeysetHash.ts @@ -0,0 +1,31 @@ +import { Chain, Hex, PublicClient, ReadContractReturnType, Transport } from 'viem'; +import { sequencerInbox } from '../contracts'; +import { ActionParameters } from '../types/Actions'; + +type Args = { + keysetHash: Hex; +}; +type SequencerInboxABI = typeof sequencerInbox.abi; + +export type IsValidKeysetHashParameters = ActionParameters< + Args, + 'sequencerInbox', + Curried +>; + +export type IsValidKeysetHashReturnType = ReadContractReturnType< + SequencerInboxABI, + 'isValidKeysetHash' +>; + +export async function isValidKeysetHash( + client: PublicClient, + args: IsValidKeysetHashParameters, +): Promise { + return client.readContract({ + abi: sequencerInbox.abi, + functionName: 'isValidKeysetHash', + address: args.sequencerInbox, + args: [args.keysetHash], + }); +} diff --git a/src/types/Actions.ts b/src/types/Actions.ts new file mode 100644 index 00000000..9072c656 --- /dev/null +++ b/src/types/Actions.ts @@ -0,0 +1,24 @@ +import { Address } from 'viem'; +import { Prettify } from './utils'; + +type RemoveUndefinedArgs = T extends { args?: undefined } ? Omit : T; + +/** + * 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; + } +>;