Skip to content

Commit

Permalink
Feat: Add v1 publicActions for parent chain
Browse files Browse the repository at this point in the history
Closes FS-535
  • Loading branch information
chrstph-dvx committed Jul 2, 2024
1 parent 76f9c78 commit 22d8300
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 0 deletions.
161 changes: 161 additions & 0 deletions src/decorators/publicActionsParentChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import { Address, Chain, PublicClient, Transport } from 'viem';

// Getters
import {
getMaxTimeVariation,
GetMaxTimeVariationParameters,
GetMaxTimeVariationReturnType,
} from '../actions/getMaxTimeVariation';
import {
isBatchPoster,
IsBatchPosterParameters,
IsBatchPosterReturnType,
} from '../actions/isBatchPoster';
import {
isValidKeysetHash,
IsValidKeysetHashParameters,
IsValidKeysetHashReturnType,
} from '../actions/isValidKeysetHash';
// Setters
import {
invalidateKeysetHash,
InvalidateKeysetHashParameters,
InvalidateKeysetHashReturnType,
} from '../actions/invalidateKeysetHash';
import {
enableBatchPoster,
disableBatchPoster,
SetIsBatchPosterParameters,
SetIsBatchPosterReturnType,
} from '../actions/setIsbatchPoster';
import { setKeyset, SetKeysetParameters, SetKeysetReturnType } from '../actions/setKeyset';
import {
setMaxTimeVariation,
SetMaxTimeVariationParameters,
SetMaxTimeVariationReturnType,
} from '../actions/setMaxTimeVariation';

type Params = { sequencerInbox: Address } | void;

export type PublicActionsParentChain<Curried extends boolean> = {
// Getters
getMaxTimeVariation: (
parameters: GetMaxTimeVariationParameters<Curried>,
) => Promise<GetMaxTimeVariationReturnType>;
isBatchPoster: (parameters: IsBatchPosterParameters<Curried>) => Promise<IsBatchPosterReturnType>;
isValidKeysetHash: (
parameters: IsValidKeysetHashParameters<Curried>,
) => Promise<IsValidKeysetHashReturnType>;
// Setters
invalidateKeysetHash: (
parameters: InvalidateKeysetHashParameters<Curried>,
) => Promise<InvalidateKeysetHashReturnType>;
enableBatchPoster: (
parameters: SetIsBatchPosterParameters<Curried>,
) => Promise<SetIsBatchPosterReturnType>;
disableBatchPoster: (
parameters: SetIsBatchPosterParameters<Curried>,
) => Promise<SetIsBatchPosterReturnType>;
setKeyset: (parameters: SetKeysetParameters<Curried>) => Promise<SetKeysetReturnType>;
setMaxTimeVariation: (
parameters: SetMaxTimeVariationParameters<Curried>,
) => Promise<SetMaxTimeVariationReturnType>;
};

/**
* Simplifies the overall typing with curried sequencerInbox address
*
* By design, sequencerInbox is either passed initially from the decorator, or on each call
*
* Address passed through each call has the priority over the address passed to the decorator, for override
*/

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

/**
* Public actions for parent chain
*
* @example
* import { createPublicClient, http } from 'viem'
* import { publicActionsParentChain } from '@arbitrum/orbit-sdk'
* import { arbitrum } from 'viem/chains'
*
* export const publicClientParentChain = createPublicClient({
* chain: arbitrum,
* transport: http(),
* }).extend(publicActionsParentChain({
* sequencerInbox: '0x1c479675ad559DC151F6Ec7ed3FbF8ceE79582B6'
* }))
*
* const { delayBlocks, futureBlocks, delaySeconds, futureSeconds } = await publicClientParentChain.getMaxTimeVariation()
*/
export function publicActionsParentChain<
TParams extends Params = void,
TTransport extends Transport = Transport,
TChain extends Chain | undefined = Chain | undefined,
>(params: void): (client: PublicClient<TTransport, TChain>) => PublicActionsParentChain<false>;
export function publicActionsParentChain<
TParams extends Params = { sequencerInbox: Address },
TTransport extends Transport = Transport,
TChain extends Chain | undefined = Chain | undefined,
>(params: TParams): (client: PublicClient<TTransport, TChain>) => PublicActionsParentChain<true>;
export function publicActionsParentChain<
TParams extends Params,
TTransport extends Transport = Transport,
TChain extends Chain | undefined = Chain | undefined,
>(params: TParams) {
return (client: PublicClient<TTransport, TChain>) => {
// sequencerInbox is curried, sequencerInbox param is optional.
return {
// Getters
getMaxTimeVariation: (args) =>
getMaxTimeVariation(client, {
...args,
sequencerInbox: getSequencerInboxAddress(params, args),
}),
isBatchPoster: (args) =>
isBatchPoster(client, {
...args,
sequencerInbox: getSequencerInboxAddress(params, args),
}),
isValidKeysetHash: (args) =>
isValidKeysetHash(client, {
...args,
sequencerInbox: getSequencerInboxAddress(params, args),
}),
// Setters
invalidateKeysetHash: (args) =>
invalidateKeysetHash(client, {
...args,
sequencerInbox: getSequencerInboxAddress(params, args),
}),
enableBatchPoster: (args) =>
enableBatchPoster(client, {
...args,
sequencerInbox: getSequencerInboxAddress(params, args),
}),
disableBatchPoster: (args) =>
disableBatchPoster(client, {
...args,
sequencerInbox: getSequencerInboxAddress(params, args),
}),
setKeyset: (args) =>
setKeyset(client, {
...args,
sequencerInbox: getSequencerInboxAddress(params, args),
}),
setMaxTimeVariation: (args) =>
setMaxTimeVariation(client, {
...args,
sequencerInbox: getSequencerInboxAddress(params, args),
}),
} satisfies PublicActionsParentChain<
TParams extends { sequencerInbox: Address } ? true : false
>;
};
}
45 changes: 45 additions & 0 deletions src/decorators/publicActionsParentChain.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { it, expect, describe } from 'vitest';

import { createPublicClient, http, padHex, zeroAddress } from 'viem';
import { mainnet } from '../chains';
import { publicActionsParentChain } from './publicActionsParentChain';

const arbOneSequencerInbox = '0x1c479675ad559DC151F6Ec7ed3FbF8ceE79582B6';

const client = createPublicClient({
chain: mainnet,
transport: http(),
}).extend(publicActionsParentChain({ sequencerInbox: arbOneSequencerInbox }));

describe('Getters', () => {
it('[maxTimeVariation] Should return max time variation', async () => {
const maxTimeVariation = await client.getMaxTimeVariation();
expect(maxTimeVariation).toEqual({
delayBlocks: 5760n,
futureBlocks: 64n,
delaySeconds: 86400n,
futureSeconds: 768n,
});
});

it('[isBatchPoster] Should return if an address is a batch poster', async () => {
const isZeroAddressBatchPoster = await client.isBatchPoster({
batchPoster: zeroAddress,
});
expect(isZeroAddressBatchPoster).toBeFalsy();
});

it('[isValidKeysetHash] Should return if a keysetHash is a valid one', async () => {
const isEmptyHashValidKeysetHash = await client.isValidKeysetHash({
keysetHash: padHex('0x'),
});
expect(isEmptyHashValidKeysetHash).toBeFalsy();

// Test on Nova
const isAValidKeysetHashOnNova = await client.isValidKeysetHash({
keysetHash: '0x01191accc7ad5a8020e6c6d122984540e9fc48d0457bda63e0a32c8c31994f4a',
sequencerInbox: '0x211e1c4c7f1bf5351ac850ed10fd68cffcf6c21b',
});
expect(isAValidKeysetHashOnNova).toBeTruthy();
});
});

0 comments on commit 22d8300

Please sign in to comment.