Skip to content

Commit

Permalink
Add withUpgradeExecutor file
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstph-dvx committed Jul 15, 2024
1 parent ba1f66c commit 9ffac6f
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/withUpgradeExecutor.ts
Original file line number Diff line number Diff line change
@@ -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<TAbi extends ABIs> = GetFunctionName<TAbi>;

type EncodeFunctionDataParameters<
TAbi extends ABIs,
TFunctionName extends FunctionName<TAbi>,
> = ViemEncodeFunctionDataParameters<TAbi, TFunctionName>;

function encodeFunctionData<TAbi extends ABIs, TFunctionName extends GetFunctionName<TAbi>>({
abi,
functionName,
args,
}: EncodeFunctionDataParameters<TAbi, TFunctionName>) {
return viemEncodeFunctionData({
abi,
functionName,
args,
} as unknown as ViemEncodeFunctionDataParameters<TAbi, TFunctionName>);
}

export function withUpgradeExecutor<TAbi extends ABIs, TFunctionName extends FunctionName<TAbi>>(
params: EncodeFunctionDataParameters<TAbi, TFunctionName> & {
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),
};
}

0 comments on commit 9ffac6f

Please sign in to comment.