Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add SequencerInbox setters #117

Merged
merged 15 commits into from
Aug 27, 2024
Merged
49 changes: 49 additions & 0 deletions src/actions/buildInvalidateKeysetHash.ts
Original file line number Diff line number Diff line change
@@ -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<Curried extends boolean = false> = Prettify<
WithUpgradeExecutor<
WithAccount<
ActionParameters<
{
keysetHash: Hex;
},
'sequencerInbox',
Curried
>
>
>
>;

export type BuildInvalidateKeysetHashReturnType = PrepareTransactionRequestReturnTypeWithChainId;

export async function buildInvalidateKeysetHash<TChain extends Chain | undefined>(
client: PublicClient<Transport, TChain>,
params: BuildInvalidateKeysetHashParameters,
): Promise<BuildInvalidateKeysetHashReturnType> {
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 };
}
63 changes: 63 additions & 0 deletions src/actions/buildSetIsBatchPoster.ts
Original file line number Diff line number Diff line change
@@ -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<Curried extends boolean = false> = Prettify<
WithUpgradeExecutor<WithAccount<ActionParameters<Args, 'sequencerInbox', Curried>>>
>;

export type BuildSetIsBatchPosterReturnType = PrepareTransactionRequestReturnTypeWithChainId;

async function buildSetIsBatchPoster<TChain extends Chain | undefined>(
client: PublicClient<Transport, TChain>,
params: BuildSetIsBatchPosterParameters & { enable: boolean },
): Promise<BuildSetIsBatchPosterReturnType> {
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<TChain extends Chain | undefined>(
client: PublicClient<Transport, TChain>,
args: BuildSetIsBatchPosterParameters,
): Promise<BuildSetIsBatchPosterReturnType> {
return buildSetIsBatchPoster(client, {
...args,
enable: true,
});
}

export async function buildDisableBatchPoster<TChain extends Chain | undefined>(
client: PublicClient<Transport, TChain>,
args: BuildSetIsBatchPosterParameters,
): Promise<BuildSetIsBatchPosterReturnType> {
return buildSetIsBatchPoster(client, {
...args,
enable: false,
});
}
45 changes: 45 additions & 0 deletions src/actions/buildSetMaxTimeVariation.ts
Original file line number Diff line number Diff line change
@@ -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<Curried extends boolean = false> = Prettify<
WithUpgradeExecutor<WithAccount<ActionParameters<Args, 'sequencerInbox', Curried>>>
>;

export type BuildSetMaxTimeVariationReturnType = PrepareTransactionRequestReturnTypeWithChainId;

export async function buildSetMaxTimeVariation<TChain extends Chain | undefined>(
client: PublicClient<Transport, TChain>,
params: BuildSetMaxTimeVariationParameters,
): Promise<BuildSetMaxTimeVariationReturnType> {
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 };
}
49 changes: 49 additions & 0 deletions src/actions/buildSetValidKeyset.ts
Original file line number Diff line number Diff line change
@@ -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<Curried extends boolean = false> = Prettify<
WithUpgradeExecutor<
WithAccount<
ActionParameters<
{
keyset: Hex;
},
'sequencerInbox',
Curried
>
>
>
>;

export type BuildSetValidKeysetReturnType = PrepareTransactionRequestReturnTypeWithChainId;

export async function buildSetValidKeyset<TChain extends Chain | undefined>(
client: PublicClient<Transport, TChain>,
params: BuildSetValidKeysetParameters,
): Promise<BuildSetValidKeysetReturnType> {
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 };
}
4 changes: 4 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -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';
76 changes: 76 additions & 0 deletions src/prepareUpgradeExecutorCallParameters.ts
Original file line number Diff line number Diff line change
@@ -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<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 prepareUpgradeExecutorCallParameters<
TAbi extends ABIs,
TFunctionName extends FunctionName<TAbi>,
>(
params: EncodeFunctionDataParameters<TAbi, TFunctionName> &
(
| {
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,
};
}
14 changes: 13 additions & 1 deletion src/types/Actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address } from 'viem';
import { Address, PrepareTransactionRequestReturnType } from 'viem';
import { Prettify } from './utils';

/**
Expand All @@ -20,3 +20,15 @@ export type ActionParameters<Args, ContractName extends string, Curried extends
[key in ContractName]?: Address;
}
>;

export type WithAccount<Args> = Args & {
account: Address;
};

export type WithUpgradeExecutor<Args> = Args & {
upgradeExecutor: Address | false;
};

export type PrepareTransactionRequestReturnTypeWithChainId = PrepareTransactionRequestReturnType & {
chainId: number;
};