From ba1f66c48c1ebfe8aae235d798be54855657d88e Mon Sep 17 00:00:00 2001 From: Christophe Date: Mon, 15 Jul 2024 11:29:21 +0000 Subject: [PATCH] Add upgradeExecutor flag --- src/actions/setIsbatchPoster.ts | 56 ++++++++++++------------- src/actions/setKeyset.ts | 66 +++++++++++++++--------------- src/actions/setMaxTimeVariation.ts | 51 +++++++++++------------ src/types/Actions.ts | 10 ++++- 4 files changed, 93 insertions(+), 90 deletions(-) diff --git a/src/actions/setIsbatchPoster.ts b/src/actions/setIsbatchPoster.ts index eef471d3..040bb72b 100644 --- a/src/actions/setIsbatchPoster.ts +++ b/src/actions/setIsbatchPoster.ts @@ -1,49 +1,45 @@ -import { - Address, - Chain, - PrepareTransactionRequestParameters, - PrepareTransactionRequestReturnType, - PublicClient, - Transport, - encodeFunctionData, -} from 'viem'; +import { Address, Chain, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem'; import { sequencerInbox } from '../contracts'; -import { ActionParameters, WithAccount } from '../types/Actions'; +import { + ActionParameters, + PrepareTransactionRequestReturnTypeWithChainId, + WithAccount, + WithUpgradeExecutor, +} from '../types/Actions'; import { Prettify } from '../types/utils'; +import { withUpgradeExecutor } from '../withUpgradeExecutor'; +import { validateParentChainPublicClient } from '../types/ParentChain'; type Args = { batchPoster: Address; }; export type SetIsBatchPosterParameters = Prettify< - WithAccount> + WithUpgradeExecutor>> >; -export type SetIsBatchPosterReturnType = PrepareTransactionRequestReturnType; - -function sequencerInboxFunctionData({ - batchPoster, - enable, -}: SetIsBatchPosterParameters & { enable: boolean }) { - return encodeFunctionData({ - abi: sequencerInbox.abi, - functionName: 'setIsBatchPoster', - args: [batchPoster, enable], - }); -} +export type SetIsBatchPosterReturnType = PrepareTransactionRequestReturnTypeWithChainId; async function setIsBatchPoster( client: PublicClient, - args: SetIsBatchPosterParameters & { enable: boolean }, + params: SetIsBatchPosterParameters & { enable: boolean }, ): Promise { - const data = sequencerInboxFunctionData(args); - return client.prepareTransactionRequest({ - to: args.sequencerInbox, - value: BigInt(0), + const validatedPublicClient = validateParentChainPublicClient(client); + const { account, upgradeExecutor, sequencerInbox: sequencerInboxAddress, ...args } = params; + + const request = await client.prepareTransactionRequest({ chain: client.chain, - data, - account: args.account, + account, + ...withUpgradeExecutor({ + to: sequencerInboxAddress, + upgradeExecutor, + args: [args.batchPoster, args.enable], + abi: sequencerInbox.abi, + functionName: 'setIsBatchPoster', + }), } satisfies PrepareTransactionRequestParameters); + + return { ...request, chainId: validatedPublicClient.chain.id }; } export async function enableBatchPoster( diff --git a/src/actions/setKeyset.ts b/src/actions/setKeyset.ts index d71c1212..0a9b5eb7 100644 --- a/src/actions/setKeyset.ts +++ b/src/actions/setKeyset.ts @@ -1,49 +1,49 @@ -import { - Chain, - Hex, - PrepareTransactionRequestParameters, - PrepareTransactionRequestReturnType, - PublicClient, - Transport, - encodeFunctionData, -} from 'viem'; +import { Chain, Hex, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem'; import { sequencerInbox } from '../contracts'; -import { ActionParameters, WithAccount } from '../types/Actions'; +import { + ActionParameters, + PrepareTransactionRequestReturnTypeWithChainId, + WithAccount, + WithUpgradeExecutor, +} from '../types/Actions'; import { Prettify } from '../types/utils'; +import { validateParentChainPublicClient } from '../types/ParentChain'; +import { withUpgradeExecutor } from '../withUpgradeExecutor'; export type SetKeysetParameters = Prettify< - WithAccount< - ActionParameters< - { - keyset: Hex; - }, - 'sequencerInbox', - Curried + WithUpgradeExecutor< + WithAccount< + ActionParameters< + { + keyset: Hex; + }, + 'sequencerInbox', + Curried + > > > >; -export type SetKeysetReturnType = PrepareTransactionRequestReturnType; - -function sequencerInboxFunctionData({ keyset }: SetKeysetParameters) { - return encodeFunctionData({ - abi: sequencerInbox.abi, - functionName: 'setValidKeyset', - args: [keyset], - }); -} +export type SetKeysetReturnType = PrepareTransactionRequestReturnTypeWithChainId; export async function setKeyset( client: PublicClient, - args: SetKeysetParameters, + params: SetKeysetParameters, ): Promise { - const data = sequencerInboxFunctionData(args); + const validatedPublicClient = validateParentChainPublicClient(client); + const { account, upgradeExecutor, sequencerInbox: sequencerInboxAddress, ...args } = params; - return client.prepareTransactionRequest({ - to: args.sequencerInbox, - value: BigInt(0), + const request = await client.prepareTransactionRequest({ chain: client.chain, - data, - account: args.account, + account, + ...withUpgradeExecutor({ + to: sequencerInboxAddress, + upgradeExecutor, + args: [args.keyset], + abi: sequencerInbox.abi, + functionName: 'setValidKeyset', + }), } satisfies PrepareTransactionRequestParameters); + + return { ...request, chainId: validatedPublicClient.chain.id }; } diff --git a/src/actions/setMaxTimeVariation.ts b/src/actions/setMaxTimeVariation.ts index 7f763d84..9e0b2386 100644 --- a/src/actions/setMaxTimeVariation.ts +++ b/src/actions/setMaxTimeVariation.ts @@ -1,14 +1,14 @@ -import { - Chain, - PrepareTransactionRequestParameters, - PrepareTransactionRequestReturnType, - PublicClient, - Transport, - encodeFunctionData, -} from 'viem'; +import { Chain, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem'; import { sequencerInbox } from '../contracts'; -import { ActionParameters, WithAccount } from '../types/Actions'; +import { + ActionParameters, + PrepareTransactionRequestReturnTypeWithChainId, + WithAccount, + WithUpgradeExecutor, +} from '../types/Actions'; import { Prettify } from '../types/utils'; +import { withUpgradeExecutor } from '../withUpgradeExecutor'; +import { validateParentChainPublicClient } from '../types/ParentChain'; type Args = { delayBlocks: bigint; @@ -17,30 +17,29 @@ type Args = { futureSeconds: bigint; }; export type SetMaxTimeVariationParameters = Prettify< - WithAccount> + WithUpgradeExecutor>> >; -export type SetMaxTimeVariationReturnType = PrepareTransactionRequestReturnType; - -function sequencerInboxFunctionData(args: SetMaxTimeVariationParameters) { - return encodeFunctionData({ - abi: sequencerInbox.abi, - functionName: 'setMaxTimeVariation', - args: [args], - }); -} +export type SetMaxTimeVariationReturnType = PrepareTransactionRequestReturnTypeWithChainId; export async function setMaxTimeVariation( client: PublicClient, - args: SetMaxTimeVariationParameters, + params: SetMaxTimeVariationParameters, ): Promise { - const data = sequencerInboxFunctionData(args); + const validatedPublicClient = validateParentChainPublicClient(client); + const { account, upgradeExecutor, sequencerInbox: sequencerInboxAddress, ...args } = params; - return client.prepareTransactionRequest({ - to: args.sequencerInbox, - value: BigInt(0), + const request = await client.prepareTransactionRequest({ chain: client.chain, - data, - account: args.account, + account, + ...withUpgradeExecutor({ + to: sequencerInboxAddress, + upgradeExecutor, + args: [args], + abi: sequencerInbox.abi, + functionName: 'setMaxTimeVariation', + }), } satisfies PrepareTransactionRequestParameters); + + return { ...request, chainId: validatedPublicClient.chain.id }; } diff --git a/src/types/Actions.ts b/src/types/Actions.ts index e61648f0..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'; /** @@ -24,3 +24,11 @@ export type ActionParameters = Args & { account: Address; }; + +export type WithUpgradeExecutor = Args & { + upgradeExecutor: Address | false; +}; + +export type PrepareTransactionRequestReturnTypeWithChainId = PrepareTransactionRequestReturnType & { + chainId: number; +};