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 Jun 27, 2024
1 parent 3db265f commit 6cd3648
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 5 deletions.
162 changes: 162 additions & 0 deletions src/decorators/publicActionsParentChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import { Address, Chain, PublicClient, Transport } from 'viem';

// Getters
import {
getMaxTimeVariation,
GetMaxTimeVariationActionParameters,

Check failure on line 6 in src/decorators/publicActionsParentChain.ts

View workflow job for this annotation

GitHub Actions / Test (Unit)

'"../actions/getMaxTimeVariation"' has no exported member named 'GetMaxTimeVariationActionParameters'. Did you mean 'GetMaxTimeVariationParameters'?
GetMaxTimeVariationReturnType,
} from '../actions/getMaxTimeVariation';
import {
isBatchPoster,
IsBatchPosterActionParameters,

Check failure on line 11 in src/decorators/publicActionsParentChain.ts

View workflow job for this annotation

GitHub Actions / Test (Unit)

'"../actions/isBatchPoster"' has no exported member named 'IsBatchPosterActionParameters'. Did you mean 'IsBatchPosterParameters'?
IsBatchPosterReturnType,
} from '../actions/isBatchPoster';
import {
isValidKeysetHash,
IsValidKeysetHashActionParameters,

Check failure on line 16 in src/decorators/publicActionsParentChain.ts

View workflow job for this annotation

GitHub Actions / Test (Unit)

'"../actions/isValidKeysetHash"' has no exported member named 'IsValidKeysetHashActionParameters'. Did you mean 'IsValidKeysetHashParameters'?
IsValidKeysetHashReturnType,
} from '../actions/isValidKeysetHash';
// Setters
import {
invalidateKeysetHash,
InvalidateKeysetHashActionParameters,

Check failure on line 22 in src/decorators/publicActionsParentChain.ts

View workflow job for this annotation

GitHub Actions / Test (Unit)

'"../actions/invalidateKeysetHash"' has no exported member named 'InvalidateKeysetHashActionParameters'. Did you mean 'InvalidateKeysetHashParameters'?
InvalidateKeysetHashReturnType,
} from '../actions/invalidateKeysetHash';
import {
enableBatchPoster,
disableBatchPoster,
SetIsBatchPosterActionParameters,

Check failure on line 28 in src/decorators/publicActionsParentChain.ts

View workflow job for this annotation

GitHub Actions / Test (Unit)

'"../actions/setIsbatchPoster"' has no exported member named 'SetIsBatchPosterActionParameters'. Did you mean 'SetIsBatchPosterParameters'?
SetIsBatchPosterReturnType,
} from '../actions/setIsbatchPoster';
import { setKeyset, SetKeysetActionParameters, SetKeysetReturnType } from '../actions/setKeyset';

Check failure on line 31 in src/decorators/publicActionsParentChain.ts

View workflow job for this annotation

GitHub Actions / Test (Unit)

'"../actions/setKeyset"' has no exported member named 'SetKeysetActionParameters'. Did you mean 'SetKeysetParameters'?
import {
setMaxTimeVariation,
SetMaxTimeVariationActionParameters,
SetMaxTimeVariationReturnType,
} from '../actions/setMaxTimeVariation';

type Params = { sequencerInbox: Address } | void;

export type PublicActionsParentChain<Curried extends boolean> = {
// Getters
getMaxTimeVariation: (
parameters: GetMaxTimeVariationActionParameters<Curried>,
) => Promise<GetMaxTimeVariationReturnType>;
isBatchPoster: (
parameters: IsBatchPosterActionParameters<Curried>,
) => Promise<IsBatchPosterReturnType>;
isValidKeysetHash: (
parameters: IsValidKeysetHashActionParameters<Curried>,
) => Promise<IsValidKeysetHashReturnType>;
// Setters
invalidateKeysetHash: (
parameters: InvalidateKeysetHashActionParameters<Curried>,
) => Promise<InvalidateKeysetHashReturnType>;
enableBatchPoster: (
parameters: SetIsBatchPosterActionParameters<Curried>,
) => Promise<SetIsBatchPosterReturnType>;
disableBatchPoster: (
parameters: SetIsBatchPosterActionParameters<Curried>,
) => Promise<SetIsBatchPosterReturnType>;
setKeyset: (parameters: SetKeysetActionParameters<Curried>) => Promise<SetKeysetReturnType>;
setMaxTimeVariation: (
parameters: SetMaxTimeVariationActionParameters<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 { 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 }