Skip to content

Commit

Permalink
Feat: Add v1 RollupAdminLogic getters
Browse files Browse the repository at this point in the history
Closes FS-620
  • Loading branch information
chrstph-dvx committed Sep 25, 2024
1 parent 2f33e44 commit e4bef41
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 0 deletions.
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) {
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;
});

0 comments on commit e4bef41

Please sign in to comment.