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 getters #144

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
29 changes: 29 additions & 0 deletions src/actions/getConfirmPeriodBlocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Chain, PublicClient, ReadContractReturnType, Transport } from 'viem';
import { rollupABI } from '../contracts/Rollup';
import { WithContractAddress } from '../types/Actions';
import { getRollupAddress } from '../getRollupAddress';

export type GetConfirmPeriodBlocksParameters<Curried extends boolean = false> = WithContractAddress<
{},
'rollupAdminLogic',
Curried
>;

export type GetConfirmPeriodBlocksReturnType = ReadContractReturnType<
typeof rollupABI,
'confirmPeriodBlocks'
>;

export async function getConfirmPeriodBlocks<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
args: GetConfirmPeriodBlocksParameters,
): Promise<GetConfirmPeriodBlocksReturnType> {
const rollupAdminLogicAddress =
'sequencerInbox' in args ? await getRollupAddress(client, args) : args.rollupAdminLogic;

return client.readContract({
abi: rollupABI,
functionName: 'confirmPeriodBlocks',
address: rollupAdminLogicAddress,
});
}
25 changes: 25 additions & 0 deletions src/actions/getExtraChallengeTimeBlocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Chain, PublicClient, ReadContractReturnType, Transport } from 'viem';
import { rollupABI } from '../contracts/Rollup';
import { WithContractAddress } from '../types/Actions';
import { getRollupAddress } from '../getRollupAddress';

export type GetExtraChallengeTimeBlocksParameters<Curried extends boolean = false> =
WithContractAddress<{}, 'rollupAdminLogic', Curried>;

export type GetExtraChallengeTimeBlocksReturnType = ReadContractReturnType<
typeof rollupABI,
'extraChallengeTimeBlocks'
>;

export async function getExtraChallengeTimeBlocks<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
args: GetExtraChallengeTimeBlocksParameters,
): Promise<GetExtraChallengeTimeBlocksReturnType> {
const rollupAdminLogicAddress =
'sequencerInbox' in args ? await getRollupAddress(client, args) : args.rollupAdminLogic;
return client.readContract({
abi: rollupABI,
functionName: 'extraChallengeTimeBlocks',
address: rollupAdminLogicAddress,
});
}
25 changes: 25 additions & 0 deletions src/actions/getMinimumAssertionPeriod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Chain, PublicClient, ReadContractReturnType, Transport } from 'viem';
import { rollupABI } from '../contracts/Rollup';
import { WithContractAddress } from '../types/Actions';
import { getRollupAddress } from '../getRollupAddress';

export type GetMinimumAssertionPeriodParameters<Curried extends boolean = false> =
WithContractAddress<{}, 'rollupAdminLogic', Curried>;

export type GetMinimumAssertionPeriodReturnType = ReadContractReturnType<
typeof rollupABI,
'minimumAssertionPeriod'
>;

export async function getMinimumAssertionPeriod<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
args: GetMinimumAssertionPeriodParameters,
): Promise<GetMinimumAssertionPeriodReturnType> {
const rollupAdminLogicAddress =
'sequencerInbox' in args ? await getRollupAddress(client, args) : args.rollupAdminLogic;
return client.readContract({
abi: rollupABI,
functionName: 'minimumAssertionPeriod',
address: rollupAdminLogicAddress,
});
}
28 changes: 28 additions & 0 deletions src/actions/getWasmModuleRoot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Chain, PublicClient, ReadContractReturnType, Transport } from 'viem';
import { rollupABI } from '../contracts/Rollup';
import { WithContractAddress } from '../types/Actions';
import { getRollupAddress } from '../getRollupAddress';

export type GetWasmModuleRootParameters<Curried extends boolean = false> = WithContractAddress<
{},
'rollupAdminLogic',
Curried
>;

export type GetWasmModuleRootReturnType = ReadContractReturnType<
typeof rollupABI,
'wasmModuleRoot'
>;

export async function getWasmModuleRoot<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
args: GetWasmModuleRootParameters,
): Promise<GetWasmModuleRootReturnType> {
const rollupAdminLogicAddress =
'sequencerInbox' in args ? await getRollupAddress(client, args) : args.rollupAdminLogic;
return client.readContract({
abi: rollupABI,
functionName: 'wasmModuleRoot',
address: rollupAdminLogicAddress,
});
}
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) {
fionnachan marked this conversation as resolved.
Show resolved Hide resolved
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;
}
32 changes: 32 additions & 0 deletions src/types/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,35 @@ export type WithUpgradeExecutor<Args> = Args & {
export type PrepareTransactionRequestReturnTypeWithChainId = PrepareTransactionRequestReturnType & {
chainId: number;
};

/**
* Some actions require a different contract than sequencerInbox.
* We either accept sequencerInbox to fetch the contract address
* or an override
*/
export type WithContractAddress<
Args,
ContractName extends string,
Curried extends boolean,
> = Curried extends true
? /**
* If sequencerInbox was passed to the decorator. We accept the contract address,
* an sequencerInbox override, or no parameters
*/
| (Args & {
sequencerInbox?: Address;
})
| (Args & {
[key in ContractName]?: Address;
})
| void
: /**
* If sequencerInbox wasn't passed to the decorator. We need one of the address to be passed
* We either accept the contract address or an sequencerInbox override
*/
| (Args & {
sequencerInbox: Address;
})
| (Args & {
[key in ContractName]: Address;
});