diff --git a/src/actions/buildInvalidateKeysetHash.ts b/src/actions/buildInvalidateKeysetHash.ts new file mode 100644 index 00000000..30b55039 --- /dev/null +++ b/src/actions/buildInvalidateKeysetHash.ts @@ -0,0 +1,49 @@ +import { Chain, Hex, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem'; +import { sequencerInboxABI } from '../contracts/SequencerInbox'; +import { + ActionParameters, + PrepareTransactionRequestReturnTypeWithChainId, + WithAccount, + WithUpgradeExecutor, +} from '../types/Actions'; +import { Prettify } from '../types/utils'; +import { validateParentChainPublicClient } from '../types/ParentChain'; +import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters'; + +export type BuildInvalidateKeysetHashParameters = Prettify< + WithUpgradeExecutor< + WithAccount< + ActionParameters< + { + keysetHash: Hex; + }, + 'sequencerInbox', + Curried + > + > + > +>; + +export type BuildInvalidateKeysetHashReturnType = PrepareTransactionRequestReturnTypeWithChainId; + +export async function buildInvalidateKeysetHash( + client: PublicClient, + params: BuildInvalidateKeysetHashParameters, +): Promise { + const validatedPublicClient = validateParentChainPublicClient(client); + const { account, upgradeExecutor, sequencerInbox: sequencerInboxAddress, ...args } = params; + + const request = await client.prepareTransactionRequest({ + chain: client.chain, + account, + ...prepareUpgradeExecutorCallParameters({ + to: sequencerInboxAddress, + upgradeExecutor, + args: [args.keysetHash], + abi: sequencerInboxABI, + functionName: 'setValidKeyset', + }), + } satisfies PrepareTransactionRequestParameters); + + return { ...request, chainId: validatedPublicClient.chain.id }; +} diff --git a/src/actions/buildSetIsBatchPoster.ts b/src/actions/buildSetIsBatchPoster.ts new file mode 100644 index 00000000..209eef67 --- /dev/null +++ b/src/actions/buildSetIsBatchPoster.ts @@ -0,0 +1,63 @@ +import { Address, Chain, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem'; +import { sequencerInboxABI } from '../contracts/SequencerInbox'; +import { + ActionParameters, + PrepareTransactionRequestReturnTypeWithChainId, + WithAccount, + WithUpgradeExecutor, +} from '../types/Actions'; +import { Prettify } from '../types/utils'; +import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters'; +import { validateParentChainPublicClient } from '../types/ParentChain'; + +type Args = { + batchPoster: Address; +}; + +export type BuildSetIsBatchPosterParameters = Prettify< + WithUpgradeExecutor>> +>; + +export type BuildSetIsBatchPosterReturnType = PrepareTransactionRequestReturnTypeWithChainId; + +async function buildSetIsBatchPoster( + client: PublicClient, + params: BuildSetIsBatchPosterParameters & { enable: boolean }, +): Promise { + const validatedPublicClient = validateParentChainPublicClient(client); + const { account, upgradeExecutor, sequencerInbox: sequencerInboxAddress, ...args } = params; + + const request = await client.prepareTransactionRequest({ + chain: client.chain, + account, + ...prepareUpgradeExecutorCallParameters({ + to: sequencerInboxAddress, + upgradeExecutor, + args: [args.batchPoster, args.enable], + abi: sequencerInboxABI, + functionName: 'setIsBatchPoster', + }), + } satisfies PrepareTransactionRequestParameters); + + return { ...request, chainId: validatedPublicClient.chain.id }; +} + +export async function buildEnableBatchPoster( + client: PublicClient, + args: BuildSetIsBatchPosterParameters, +): Promise { + return buildSetIsBatchPoster(client, { + ...args, + enable: true, + }); +} + +export async function buildDisableBatchPoster( + client: PublicClient, + args: BuildSetIsBatchPosterParameters, +): Promise { + return buildSetIsBatchPoster(client, { + ...args, + enable: false, + }); +} diff --git a/src/actions/buildSetMaxTimeVariation.ts b/src/actions/buildSetMaxTimeVariation.ts new file mode 100644 index 00000000..aef110fa --- /dev/null +++ b/src/actions/buildSetMaxTimeVariation.ts @@ -0,0 +1,45 @@ +import { Chain, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem'; +import { sequencerInboxABI } from '../contracts/SequencerInbox'; +import { + ActionParameters, + PrepareTransactionRequestReturnTypeWithChainId, + WithAccount, + WithUpgradeExecutor, +} from '../types/Actions'; +import { Prettify } from '../types/utils'; +import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters'; +import { validateParentChainPublicClient } from '../types/ParentChain'; + +type Args = { + delayBlocks: bigint; + futureBlocks: bigint; + delaySeconds: bigint; + futureSeconds: bigint; +}; +export type BuildSetMaxTimeVariationParameters = Prettify< + WithUpgradeExecutor>> +>; + +export type BuildSetMaxTimeVariationReturnType = PrepareTransactionRequestReturnTypeWithChainId; + +export async function buildSetMaxTimeVariation( + client: PublicClient, + params: BuildSetMaxTimeVariationParameters, +): Promise { + const validatedPublicClient = validateParentChainPublicClient(client); + const { account, upgradeExecutor, sequencerInbox: sequencerInboxAddress, ...args } = params; + + const request = await client.prepareTransactionRequest({ + chain: client.chain, + account, + ...prepareUpgradeExecutorCallParameters({ + to: sequencerInboxAddress, + upgradeExecutor, + args: [args], + abi: sequencerInboxABI, + functionName: 'setMaxTimeVariation', + }), + } satisfies PrepareTransactionRequestParameters); + + return { ...request, chainId: validatedPublicClient.chain.id }; +} diff --git a/src/actions/buildSetValidKeyset.ts b/src/actions/buildSetValidKeyset.ts new file mode 100644 index 00000000..c690a41c --- /dev/null +++ b/src/actions/buildSetValidKeyset.ts @@ -0,0 +1,49 @@ +import { Chain, Hex, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem'; +import { sequencerInboxABI } from '../contracts/SequencerInbox'; +import { + ActionParameters, + PrepareTransactionRequestReturnTypeWithChainId, + WithAccount, + WithUpgradeExecutor, +} from '../types/Actions'; +import { Prettify } from '../types/utils'; +import { validateParentChainPublicClient } from '../types/ParentChain'; +import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters'; + +export type BuildSetValidKeysetParameters = Prettify< + WithUpgradeExecutor< + WithAccount< + ActionParameters< + { + keyset: Hex; + }, + 'sequencerInbox', + Curried + > + > + > +>; + +export type BuildSetValidKeysetReturnType = PrepareTransactionRequestReturnTypeWithChainId; + +export async function buildSetValidKeyset( + client: PublicClient, + params: BuildSetValidKeysetParameters, +): Promise { + const validatedPublicClient = validateParentChainPublicClient(client); + const { account, upgradeExecutor, sequencerInbox: sequencerInboxAddress, ...args } = params; + + const request = await client.prepareTransactionRequest({ + chain: client.chain, + account, + ...prepareUpgradeExecutorCallParameters({ + to: sequencerInboxAddress, + upgradeExecutor, + args: [args.keyset], + abi: sequencerInboxABI, + functionName: 'setValidKeyset', + }), + } satisfies PrepareTransactionRequestParameters); + + return { ...request, chainId: validatedPublicClient.chain.id }; +} diff --git a/src/actions/index.ts b/src/actions/index.ts index 63164981..28c51440 100644 --- a/src/actions/index.ts +++ b/src/actions/index.ts @@ -1,3 +1,7 @@ export * from './getMaxTimeVariation'; export * from './isBatchPoster'; export * from './isValidKeysetHash'; +export * from './buildInvalidateKeysetHash'; +export * from './buildSetIsBatchPoster'; +export * from './buildSetValidKeyset'; +export * from './buildSetMaxTimeVariation'; diff --git a/src/prepareUpgradeExecutorCallParameters.ts b/src/prepareUpgradeExecutorCallParameters.ts new file mode 100644 index 00000000..dcde21c0 --- /dev/null +++ b/src/prepareUpgradeExecutorCallParameters.ts @@ -0,0 +1,76 @@ +import { + Address, + encodeFunctionData as viemEncodeFunctionData, + EncodeFunctionDataParameters as ViemEncodeFunctionDataParameters, +} from 'viem'; +import { GetFunctionName } from './types/utils'; +import { sequencerInboxABI } from './contracts/SequencerInbox'; +import { arbOwnerABI } from './contracts/ArbOwner'; +import { + upgradeExecutorEncodeFunctionData, + UpgradeExecutorFunctionName, +} from './upgradeExecutorEncodeFunctionData'; + +type ABIs = typeof sequencerInboxABI | typeof arbOwnerABI; +type FunctionName = GetFunctionName; + +type EncodeFunctionDataParameters< + TAbi extends ABIs, + TFunctionName extends FunctionName, +> = ViemEncodeFunctionDataParameters; + +function encodeFunctionData>({ + abi, + functionName, + args, +}: EncodeFunctionDataParameters) { + return viemEncodeFunctionData({ + abi, + functionName, + args, + } as unknown as ViemEncodeFunctionDataParameters); +} + +export function prepareUpgradeExecutorCallParameters< + TAbi extends ABIs, + TFunctionName extends FunctionName, +>( + params: EncodeFunctionDataParameters & + ( + | { + to: Address; + upgradeExecutor: false; + value?: bigint; + } + | { + to: Address; + upgradeExecutor: Address; + value?: bigint; + upgradeExecutorFunctionName?: Extract< + UpgradeExecutorFunctionName, + 'execute' | 'executeCall' + >; + } + ), +) { + const { upgradeExecutor, value = BigInt(0) } = params; + if (!upgradeExecutor) { + return { + to: params.to, + data: encodeFunctionData(params), + value, + }; + } + + return { + to: upgradeExecutor, + data: upgradeExecutorEncodeFunctionData({ + functionName: params.upgradeExecutorFunctionName ?? 'executeCall', + args: [ + params.to, // target + encodeFunctionData(params), // targetCallData + ], + }), + value, + }; +} diff --git a/src/types/Actions.ts b/src/types/Actions.ts index 0327ffba..ba515953 100644 --- a/src/types/Actions.ts +++ b/src/types/Actions.ts @@ -1,4 +1,4 @@ -import { Address } from 'viem'; +import { Address, PrepareTransactionRequestReturnType } from 'viem'; import { Prettify } from './utils'; /** @@ -20,3 +20,15 @@ export type ActionParameters; + +export type WithAccount = Args & { + account: Address; +}; + +export type WithUpgradeExecutor = Args & { + upgradeExecutor: Address | false; +}; + +export type PrepareTransactionRequestReturnTypeWithChainId = PrepareTransactionRequestReturnType & { + chainId: number; +};