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 v1 RollupAdminLogic setters #148

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/actions/buildSetConfirmPeriodBlocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Chain, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem';
import { rollupABI } from '../contracts/Rollup';
import {
WithAccount,
ActionParameters,
WithUpgradeExecutor,
PrepareTransactionRequestReturnTypeWithChainId,
} from '../types/Actions';
import { Prettify } from '../types/utils';
import { getRollupAddress } from '../getRollupAddress';
import { validateParentChainPublicClient } from '../types/ParentChain';
import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters';

export type BuildSetConfirmPeriodBlocksParameters<Curried extends boolean = false> = Prettify<
WithUpgradeExecutor<
WithAccount<ActionParameters<{ newPeriod: bigint }, 'rollupAdminLogic', Curried>>
>
>;

export type BuildSetConfirmPeriodBlocksReturnType = PrepareTransactionRequestReturnTypeWithChainId;

export async function buildSetConfirmPeriodBlocks<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
{ params, account, upgradeExecutor, ...args }: BuildSetConfirmPeriodBlocksParameters,
): Promise<BuildSetConfirmPeriodBlocksReturnType> {
const validatedPublicClient = validateParentChainPublicClient(client);
const rollupAdminLogicAddress =
'sequencerInbox' in args ? await getRollupAddress(client, args) : args.rollupAdminLogic;

const request = await client.prepareTransactionRequest({
chain: client.chain as Chain | undefined,
account,
...prepareUpgradeExecutorCallParameters({
to: rollupAdminLogicAddress,
upgradeExecutor,
args: [params.newPeriod],
abi: rollupABI,
functionName: 'setConfirmPeriodBlocks',
}),
} satisfies PrepareTransactionRequestParameters);

return { ...request, chainId: validatedPublicClient.chain.id };
}
44 changes: 44 additions & 0 deletions src/actions/buildSetExtraChallengeTimeBlocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Chain, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem';
import { rollupABI } from '../contracts/Rollup';
import {
WithAccount,
ActionParameters,
WithUpgradeExecutor,
PrepareTransactionRequestReturnTypeWithChainId,
} from '../types/Actions';
import { Prettify } from '../types/utils';
import { getRollupAddress } from '../getRollupAddress';
import { validateParentChainPublicClient } from '../types/ParentChain';
import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters';

export type BuildSetExtraChallengeTimeBlocksParameters<Curried extends boolean = false> = Prettify<
WithUpgradeExecutor<
WithAccount<ActionParameters<{ newExtraTimeBlocks: bigint }, 'rollupAdminLogic', Curried>>
>
>;

export type BuildSetExtraChallengeTimeBlocksReturnType =
PrepareTransactionRequestReturnTypeWithChainId;

export async function buildSetExtraChallengeTimeBlocks<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
{ params, account, upgradeExecutor, ...args }: BuildSetExtraChallengeTimeBlocksParameters,
): Promise<BuildSetExtraChallengeTimeBlocksReturnType> {
const validatedPublicClient = validateParentChainPublicClient(client);
const rollupAdminLogicAddress =
'sequencerInbox' in args ? await getRollupAddress(client, args) : args.rollupAdminLogic;

const request = await client.prepareTransactionRequest({
chain: client.chain as Chain | undefined,
account,
...prepareUpgradeExecutorCallParameters({
to: rollupAdminLogicAddress,
upgradeExecutor,
args: [params.newExtraTimeBlocks],
abi: rollupABI,
functionName: 'setExtraChallengeTimeBlocks',
}),
} satisfies PrepareTransactionRequestParameters);

return { ...request, chainId: validatedPublicClient.chain.id };
}
44 changes: 44 additions & 0 deletions src/actions/buildSetMinimumAssertionPeriod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Chain, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem';
import { rollupABI } from '../contracts/Rollup';
import {
WithAccount,
ActionParameters,
PrepareTransactionRequestReturnTypeWithChainId,
WithUpgradeExecutor,
} from '../types/Actions';
import { Prettify } from '../types/utils';
import { getRollupAddress } from '../getRollupAddress';
import { validateParentChainPublicClient } from '../types/ParentChain';
import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters';

export type BuildSetMinimumAssertionPeriodParameters<Curried extends boolean = false> = Prettify<
WithUpgradeExecutor<
WithAccount<ActionParameters<{ newPeriod: bigint }, 'rollupAdminLogic', Curried>>
>
>;

export type BuildSetMinimumAssertionPeriodReturnType =
PrepareTransactionRequestReturnTypeWithChainId;

export async function buildSetMinimumAssertionPeriod<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
{ params, account, upgradeExecutor, ...args }: BuildSetMinimumAssertionPeriodParameters,
): Promise<BuildSetMinimumAssertionPeriodReturnType> {
const validatedPublicClient = validateParentChainPublicClient(client);
const rollupAdminLogicAddress =
'sequencerInbox' in args ? await getRollupAddress(client, args) : args.rollupAdminLogic;

const request = await client.prepareTransactionRequest({
chain: client.chain as Chain | undefined,
account,
...prepareUpgradeExecutorCallParameters({
to: rollupAdminLogicAddress,
upgradeExecutor,
args: [params.newPeriod],
abi: rollupABI,
functionName: 'setMinimumAssertionPeriod',
}),
} satisfies PrepareTransactionRequestParameters);

return { ...request, chainId: validatedPublicClient.chain.id };
}
56 changes: 56 additions & 0 deletions src/actions/buildSetValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Address, Chain, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem';
import { rollupABI } from '../contracts/Rollup';
import {
WithAccount,
ActionParameters,
WithUpgradeExecutor,
PrepareTransactionRequestReturnTypeWithChainId,
} from '../types/Actions';
import { Prettify } from '../types/utils';
import { getRollupAddress } from '../getRollupAddress';
import { validateParentChainPublicClient } from '../types/ParentChain';
import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters';

export type BuildSetIsValidatorParameters<Curried extends boolean = false> = Prettify<
WithUpgradeExecutor<
WithAccount<
ActionParameters<
{
add: Address[];
remove: Address[];
},
'rollupAdminLogic',
Curried
>
>
>
>;

export type BuildSetIsValidatorReturnType = PrepareTransactionRequestReturnTypeWithChainId;

export async function buildSetValidators<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
{ account, upgradeExecutor, params, ...args }: BuildSetIsValidatorParameters,
): Promise<BuildSetIsValidatorReturnType> {
const validatedPublicClient = validateParentChainPublicClient(client);
const rollupAdminLogicAddress =
'sequencerInbox' in args ? await getRollupAddress(client, args) : args.rollupAdminLogic;
const { add: addressesToAdd, remove: addressesToRemove } = params;

const addState: boolean[] = new Array(addressesToAdd.length).fill(true);
const removeState: boolean[] = new Array(addressesToRemove.length).fill(false);

const request = await client.prepareTransactionRequest({
chain: client.chain as Chain | undefined,
account,
...prepareUpgradeExecutorCallParameters({
to: rollupAdminLogicAddress,
upgradeExecutor,
args: [addressesToAdd.concat(addressesToRemove), addState.concat(removeState)],
abi: rollupABI,
functionName: 'setValidator',
}),
} satisfies PrepareTransactionRequestParameters);

return { ...request, chainId: validatedPublicClient.chain.id };
}
70 changes: 70 additions & 0 deletions src/actions/buildSetValidatorWhitelistDisabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Chain, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem';
import { rollupABI } from '../contracts/Rollup';
import {
WithAccount,
ActionParameters,
WithUpgradeExecutor,
PrepareTransactionRequestReturnTypeWithChainId,
} from '../types/Actions';
import { Prettify } from '../types/utils';
import { getRollupAddress } from '../getRollupAddress';
import { validateParentChainPublicClient } from '../types/ParentChain';
import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters';

export type BuildSetValidatorWhitelistDisabledParameters<Curried extends boolean = false> =
Prettify<WithUpgradeExecutor<WithAccount<ActionParameters<{}, 'rollupAdminLogic', Curried>>>>;

export type BuildSetValidatorWhitelistDisabledReturnType =
PrepareTransactionRequestReturnTypeWithChainId;

export async function buildSetValidatorWhitelistDisabled<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
{
account,
upgradeExecutor,
params,
...args
}: BuildSetValidatorWhitelistDisabledParameters & { params: { enable: boolean } },
): Promise<BuildSetValidatorWhitelistDisabledReturnType> {
const validatedPublicClient = validateParentChainPublicClient(client);
const rollupAdminLogicAddress =
'sequencerInbox' in args ? await getRollupAddress(client, args) : args.rollupAdminLogic;

const request = await client.prepareTransactionRequest({
chain: client.chain as Chain | undefined,
account,
...prepareUpgradeExecutorCallParameters({
to: rollupAdminLogicAddress,
upgradeExecutor,
args: [params.enable],
abi: rollupABI,
functionName: 'setValidatorWhitelistDisabled',
}),
} satisfies PrepareTransactionRequestParameters);

return { ...request, chainId: validatedPublicClient.chain.id };
}

export async function buildEnableValidatorWhitelist<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
args: BuildSetValidatorWhitelistDisabledParameters,
): Promise<BuildSetValidatorWhitelistDisabledReturnType> {
return buildSetValidatorWhitelistDisabled(client, {
...args,
params: {
enable: true,
},
});
}

export async function buildDisableValidatorWhitelist<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
args: BuildSetValidatorWhitelistDisabledParameters,
): Promise<BuildSetValidatorWhitelistDisabledReturnType> {
return buildSetValidatorWhitelistDisabled(client, {
...args,
params: {
enable: false,
},
});
}
51 changes: 51 additions & 0 deletions src/actions/buildSetWasmModuleRoot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Chain, Hex, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem';
import { rollupABI } from '../contracts/Rollup';
import {
WithAccount,
ActionParameters,
WithUpgradeExecutor,
PrepareTransactionRequestReturnTypeWithChainId,
} from '../types/Actions';
import { Prettify } from '../types/utils';
import { getRollupAddress } from '../getRollupAddress';
import { validateParentChainPublicClient } from '../types/ParentChain';
import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters';

export type BuildSetWasmModuleRootParameters<Curried extends boolean = false> = Prettify<
WithUpgradeExecutor<
WithAccount<
ActionParameters<
{
newWasmModuleRoot: Hex;
},
'rollupAdminLogic',
Curried
>
>
>
>;

export type BuildSetWasmModuleRootReturnType = PrepareTransactionRequestReturnTypeWithChainId;

export async function buildSetWasmModuleRoot<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
{ account, upgradeExecutor, params, ...args }: BuildSetWasmModuleRootParameters,
): Promise<BuildSetWasmModuleRootReturnType> {
const validatedPublicClient = validateParentChainPublicClient(client);
const rollupAdminLogicAddress =
'sequencerInbox' in args ? await getRollupAddress(client, args) : args.rollupAdminLogic;

const request = await client.prepareTransactionRequest({
chain: client.chain as Chain | undefined,
account,
...prepareUpgradeExecutorCallParameters({
to: rollupAdminLogicAddress,
upgradeExecutor,
args: [params.newWasmModuleRoot],
abi: rollupABI,
functionName: 'setWasmModuleRoot',
}),
} satisfies PrepareTransactionRequestParameters);

return { ...request, chainId: validatedPublicClient.chain.id };
}
22 changes: 22 additions & 0 deletions src/getRollupAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Address, Chain, PublicClient, Transport } from 'viem';
import { sequencerInboxABI } from './contracts/SequencerInbox';

const cache: Record<string, Address> = {};
export async function getRollupAddress<TChain extends Chain>(
publicClient: PublicClient<Transport, TChain>,
params: { sequencerInbox: Address },
): Promise<Address> {
const addressFromCache = cache[`${publicClient.chain.id}_${params.sequencerInbox}`];
if (addressFromCache) {
return addressFromCache;
}

// Otherwise, fetch the rollup address from sequencerInbox contract
const rollupAddress = await publicClient.readContract({
functionName: 'rollup',
address: params.sequencerInbox,
abi: sequencerInboxABI,
});
cache[`${publicClient.chain.id}_${params.sequencerInbox}`] = rollupAddress;
return rollupAddress;
}
3 changes: 2 additions & 1 deletion src/prepareUpgradeExecutorCallParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import {
} from 'viem';
import { GetFunctionName } from './types/utils';
import { sequencerInboxABI } from './contracts/SequencerInbox';
import { rollupABI } from './contracts/Rollup';
import { arbOwnerABI } from './contracts/ArbOwner';
import {
upgradeExecutorEncodeFunctionData,
UpgradeExecutorFunctionName,
} from './upgradeExecutorEncodeFunctionData';

type ABIs = typeof sequencerInboxABI | typeof arbOwnerABI;
type ABIs = typeof sequencerInboxABI | typeof arbOwnerABI | typeof rollupABI;
type FunctionName<TAbi extends ABIs> = GetFunctionName<TAbi>;

type EncodeFunctionDataParameters<
Expand Down
8 changes: 4 additions & 4 deletions src/types/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type isEmptyObject<Args> = Args extends Record<string, never> ? true : false;
export type ActionParameters<Args, ContractName extends string, Curried extends boolean> = Prettify<
Curried extends false
? isEmptyObject<Args> extends true
? { [key in ContractName]: Address } // Contract wasn't curried. Args is an empty object. Only requires the contract name
: { params: Args } & { [key in ContractName]: Address } // Contract wasn't curried. Args is not empty. Requires both params and contract name
? { [key in ContractName]: Address } | { sequencerInbox: Address } // Contract wasn't curried. Args is an empty object. Only requires the contract name
: { params: Args } & ({ [key in ContractName]: Address } | { sequencerInbox: Address }) // Contract wasn't curried. Args is not empty. Requires both params and contract name
: isEmptyObject<Args> extends true
? { [key in ContractName]: Address } | void // Contract was curried. Args is empty. Only requires the contract name. Allows no parameters
: { params: Args } & { [key in ContractName]?: Address } // Contract was curried. Args is not empty. Requires params, contract name is optional
? { [key in ContractName]: Address } | { sequencerInbox: Address } | void // Contract was curried. Args is empty. Only requires the contract name. Allows no parameters
: { params: Args } & ({ [key in ContractName]?: Address } | { sequencerInbox?: Address }) // Contract was curried. Args is not empty. Requires params, contract name is optional
>;

export type WithAccount<Args> = Args & {
Expand Down