Skip to content

Commit

Permalink
Fetch rollupAddress from sequencerInbox in getters
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstph-dvx committed Jul 10, 2024
1 parent f04abba commit 221ecbf
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 18 deletions.
8 changes: 5 additions & 3 deletions src/actions/getConfirmPeriodBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Chain, PublicClient, ReadContractReturnType, Transport } from 'viem';
import { rollupAdminLogic } from '../contracts';
import { ActionParameters } from '../types/Actions';
import { WithContractAddress } from '../types/Actions';
import { getRollupAddress } from '../getRollupAddress';

export type GetConfirmPeriodBlocksParameters<Curried extends boolean = false> = ActionParameters<
export type GetConfirmPeriodBlocksParameters<Curried extends boolean = false> = WithContractAddress<
{},
'rollupAdminLogic',
Curried
Expand All @@ -17,9 +18,10 @@ export async function getConfirmPeriodBlocks<TChain extends Chain | undefined>(
client: PublicClient<Transport, TChain>,
args: GetConfirmPeriodBlocksParameters,
): Promise<GetConfirmPeriodBlocksReturnType> {
const rollupAdminLogicAddresss = await getRollupAddress(client, args);
return client.readContract({
abi: rollupAdminLogic.abi,
functionName: 'confirmPeriodBlocks',
address: args.rollupAdminLogic,
address: rollupAdminLogicAddresss,
});
}
8 changes: 5 additions & 3 deletions src/actions/getExtraChallengeTimeBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Chain, PublicClient, ReadContractReturnType, Transport } from 'viem';
import { rollupAdminLogic } from '../contracts';
import { ActionParameters } from '../types/Actions';
import { WithContractAddress } from '../types/Actions';
import { getRollupAddress } from '../getRollupAddress';

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

export type GetExtraChallengeTimeBlocksReturnType = ReadContractReturnType<
typeof rollupAdminLogic.abi,
Expand All @@ -14,9 +15,10 @@ export async function getExtraChallengeTimeBlocks<TChain extends Chain | undefin
client: PublicClient<Transport, TChain>,
args: GetExtraChallengeTimeBlocksParameters,
): Promise<GetExtraChallengeTimeBlocksReturnType> {
const rollupAdminLogicAddresss = await getRollupAddress(client, args);
return client.readContract({
abi: rollupAdminLogic.abi,
functionName: 'extraChallengeTimeBlocks',
address: args.rollupAdminLogic,
address: rollupAdminLogicAddresss,
});
}
13 changes: 6 additions & 7 deletions src/actions/getMinimumAssertionPeriod.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Chain, PublicClient, ReadContractReturnType, Transport } from 'viem';
import { rollupAdminLogic } from '../contracts';
import { ActionParameters } from '../types/Actions';
import { WithContractAddress } from '../types/Actions';
import { getRollupAddress } from '../getRollupAddress';

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

export type GetMinimumAssertionPeriodReturnType = ReadContractReturnType<
typeof rollupAdminLogic.abi,
Expand All @@ -17,9 +15,10 @@ export async function getMinimumAssertionPeriod<TChain extends Chain | undefined
client: PublicClient<Transport, TChain>,
args: GetMinimumAssertionPeriodParameters,
): Promise<GetMinimumAssertionPeriodReturnType> {
const rollupAdminLogicAddresss = await getRollupAddress(client, args);
return client.readContract({
abi: rollupAdminLogic.abi,
functionName: 'minimumAssertionPeriod',
address: args.rollupAdminLogic,
address: rollupAdminLogicAddresss,
});
}
8 changes: 5 additions & 3 deletions src/actions/getWasmModuleRoot.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Chain, PublicClient, ReadContractReturnType, Transport } from 'viem';
import { rollupAdminLogic } from '../contracts';
import { ActionParameters } from '../types/Actions';
import { WithContractAddress } from '../types/Actions';
import { getRollupAddress } from '../getRollupAddress';

export type GetWasmModuleRootParameters<Curried extends boolean = false> = ActionParameters<
export type GetWasmModuleRootParameters<Curried extends boolean = false> = WithContractAddress<
{},
'rollupAdminLogic',
Curried
Expand All @@ -17,9 +18,10 @@ export async function getWasmModuleRoot<TChain extends Chain | undefined>(
client: PublicClient<Transport, TChain>,
args: GetWasmModuleRootParameters,
): Promise<GetWasmModuleRootReturnType> {
const rollupAdminLogicAddresss = await getRollupAddress(client, args);
return client.readContract({
abi: rollupAdminLogic.abi,
functionName: 'wasmModuleRoot',
address: args.rollupAdminLogic,
address: rollupAdminLogicAddresss,
});
}
8 changes: 6 additions & 2 deletions src/decorators/publicActionsParentChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ export type PublicActionsParentChain<Curried extends boolean> = {

function getSequencerInboxAddress(
params: Params,
args: { sequencerInbox?: Address } | void,
args: { sequencerInbox?: Address } | {} | void,
): Address {
return ((args && args.sequencerInbox) ?? (params && params.sequencerInbox)) as unknown as Address;
if (args && 'sequencerInbox' in args && args.sequencerInbox) {
return args.sequencerInbox;
}

return (params && params.sequencerInbox) as Address;
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/getRollupAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Address, Chain, PublicClient, Transport } from 'viem';
import { sequencerInboxABI } from './abi';

const cache: Record<Address, Address> = {};
export async function getRollupAddress<TChain extends Chain | undefined>(
publicClient: PublicClient<Transport, TChain>,
params: { sequencerInbox: Address } | { rollupAdminLogic: Address },
): Promise<Address> {
// rollupAdminLogic was passed as an override, return directly
if ('rollupAdminLogic' in params) {
return params.rollupAdminLogic;
}

const addressFromCache = cache[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[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 @@ -24,3 +24,35 @@ export type ActionParameters<Args, ContractName extends string, Curried extends
export type WithAccount<Args> = Args & {
account: Address;
};

/**
* 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 221ecbf

Please sign in to comment.