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 { withUpgradeExecutor } from '../withUpgradeExecutor';

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,
...withUpgradeExecutor({
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
spsjvc marked this conversation as resolved.
Show resolved Hide resolved
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 { withUpgradeExecutor } from '../withUpgradeExecutor';
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,
...withUpgradeExecutor({
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,
});
}
49 changes: 49 additions & 0 deletions src/actions/buildSetKeyset.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 { withUpgradeExecutor } from '../withUpgradeExecutor';

export type BuildSetKeysetParameters<Curried extends boolean = false> = Prettify<
WithUpgradeExecutor<
WithAccount<
ActionParameters<
{
keyset: Hex;
},
'sequencerInbox',
Curried
>
>
>
>;

export type BuildSetKeysetReturnType = PrepareTransactionRequestReturnTypeWithChainId;

export async function buildSetKeyset<TChain extends Chain | undefined>(
client: PublicClient<Transport, TChain>,
params: BuildSetKeysetParameters,
): Promise<BuildSetKeysetReturnType> {
const validatedPublicClient = validateParentChainPublicClient(client);
const { account, upgradeExecutor, sequencerInbox: sequencerInboxAddress, ...args } = params;

const request = await client.prepareTransactionRequest({
chain: client.chain,
account,
...withUpgradeExecutor({
to: sequencerInboxAddress,
upgradeExecutor,
args: [args.keyset],
abi: sequencerInboxABI,
functionName: 'setValidKeyset',
}),
} satisfies PrepareTransactionRequestParameters);

return { ...request, chainId: validatedPublicClient.chain.id };
}
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 { withUpgradeExecutor } from '../withUpgradeExecutor';
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,
...withUpgradeExecutor({
to: sequencerInboxAddress,
upgradeExecutor,
args: [args],
abi: sequencerInboxABI,
functionName: 'setMaxTimeVariation',
}),
} 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 './buildSetKeyset';
export * from './buildSetMaxTimeVariation';
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;
};
57 changes: 57 additions & 0 deletions src/withUpgradeExecutor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 } 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 withUpgradeExecutor<TAbi extends ABIs, TFunctionName extends FunctionName<TAbi>>(
spsjvc marked this conversation as resolved.
Show resolved Hide resolved
params: EncodeFunctionDataParameters<TAbi, TFunctionName> & {
to: Address;
upgradeExecutor: false | Address;
},
) {
const { upgradeExecutor } = params;
if (!upgradeExecutor) {
return {
to: params.to,
data: encodeFunctionData(params),
value: BigInt(0),
spsjvc marked this conversation as resolved.
Show resolved Hide resolved
};
}

return {
to: upgradeExecutor,
data: upgradeExecutorEncodeFunctionData({
functionName: 'executeCall',
spsjvc marked this conversation as resolved.
Show resolved Hide resolved
args: [
params.to, // target
encodeFunctionData(params), // targetCallData
],
}),
value: BigInt(0),
spsjvc marked this conversation as resolved.
Show resolved Hide resolved
};
}