diff --git a/src/withUpgradeExecutor.ts b/src/withUpgradeExecutor.ts new file mode 100644 index 00000000..901ea589 --- /dev/null +++ b/src/withUpgradeExecutor.ts @@ -0,0 +1,56 @@ +import { + Address, + encodeFunctionData as viemEncodeFunctionData, + EncodeFunctionDataParameters as ViemEncodeFunctionDataParameters, +} from 'viem'; +import { GetFunctionName } from './types/utils'; +import { arbOwner, sequencerInbox } from './contracts'; +import { upgradeExecutorEncodeFunctionData } from './upgradeExecutorEncodeFunctionData'; + +type ABIs = typeof sequencerInbox.abi | typeof arbOwner.abi; +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 withUpgradeExecutor>( + params: EncodeFunctionDataParameters & { + to: Address; + upgradeExecutor: false | Address; + }, +) { + const { upgradeExecutor } = params; + if (!upgradeExecutor) { + return { + to: params.to, + data: encodeFunctionData(params), + value: BigInt(0), + }; + } + + return { + to: upgradeExecutor, + data: upgradeExecutorEncodeFunctionData({ + functionName: 'executeCall', + args: [ + params.to, // target + encodeFunctionData(params), // targetCallData + ], + }), + value: BigInt(0), + }; +}