|
| 1 | +import { Address, Chain, PublicClient, Transport } from 'viem'; |
| 2 | + |
| 3 | +// Getters |
| 4 | +import { |
| 5 | + getMaxTimeVariation, |
| 6 | + GetMaxTimeVariationParameters, |
| 7 | + GetMaxTimeVariationReturnType, |
| 8 | +} from '../actions/getMaxTimeVariation'; |
| 9 | +import { |
| 10 | + isBatchPoster, |
| 11 | + IsBatchPosterParameters, |
| 12 | + IsBatchPosterReturnType, |
| 13 | +} from '../actions/isBatchPoster'; |
| 14 | +import { |
| 15 | + isValidKeysetHash, |
| 16 | + IsValidKeysetHashParameters, |
| 17 | + IsValidKeysetHashReturnType, |
| 18 | +} from '../actions/isValidKeysetHash'; |
| 19 | +// Setters |
| 20 | +import { |
| 21 | + invalidateKeysetHash, |
| 22 | + InvalidateKeysetHashParameters, |
| 23 | + InvalidateKeysetHashReturnType, |
| 24 | +} from '../actions/invalidateKeysetHash'; |
| 25 | +import { |
| 26 | + enableBatchPoster, |
| 27 | + disableBatchPoster, |
| 28 | + SetIsBatchPosterParameters, |
| 29 | + SetIsBatchPosterReturnType, |
| 30 | +} from '../actions/setIsbatchPoster'; |
| 31 | +import { setKeyset, SetKeysetParameters, SetKeysetReturnType } from '../actions/setKeyset'; |
| 32 | +import { |
| 33 | + setMaxTimeVariation, |
| 34 | + SetMaxTimeVariationParameters, |
| 35 | + SetMaxTimeVariationReturnType, |
| 36 | +} from '../actions/setMaxTimeVariation'; |
| 37 | + |
| 38 | +type Params = { sequencerInbox: Address } | void; |
| 39 | + |
| 40 | +export type PublicActionsParentChain<Curried extends boolean> = { |
| 41 | + // Getters |
| 42 | + getMaxTimeVariation: ( |
| 43 | + parameters: GetMaxTimeVariationParameters<Curried>, |
| 44 | + ) => Promise<GetMaxTimeVariationReturnType>; |
| 45 | + isBatchPoster: (parameters: IsBatchPosterParameters<Curried>) => Promise<IsBatchPosterReturnType>; |
| 46 | + isValidKeysetHash: ( |
| 47 | + parameters: IsValidKeysetHashParameters<Curried>, |
| 48 | + ) => Promise<IsValidKeysetHashReturnType>; |
| 49 | + // Setters |
| 50 | + invalidateKeysetHash: ( |
| 51 | + parameters: InvalidateKeysetHashParameters<Curried>, |
| 52 | + ) => Promise<InvalidateKeysetHashReturnType>; |
| 53 | + enableBatchPoster: ( |
| 54 | + parameters: SetIsBatchPosterParameters<Curried>, |
| 55 | + ) => Promise<SetIsBatchPosterReturnType>; |
| 56 | + disableBatchPoster: ( |
| 57 | + parameters: SetIsBatchPosterParameters<Curried>, |
| 58 | + ) => Promise<SetIsBatchPosterReturnType>; |
| 59 | + setKeyset: (parameters: SetKeysetParameters<Curried>) => Promise<SetKeysetReturnType>; |
| 60 | + setMaxTimeVariation: ( |
| 61 | + parameters: SetMaxTimeVariationParameters<Curried>, |
| 62 | + ) => Promise<SetMaxTimeVariationReturnType>; |
| 63 | +}; |
| 64 | + |
| 65 | +/** |
| 66 | + * Simplifies the overall typing with curried sequencerInbox address |
| 67 | + * |
| 68 | + * By design, sequencerInbox is either passed initially from the decorator, or on each call |
| 69 | + * |
| 70 | + * Address passed through each call has the priority over the address passed to the decorator, for override |
| 71 | + */ |
| 72 | + |
| 73 | +function getSequencerInboxAddress( |
| 74 | + params: Params, |
| 75 | + args: { sequencerInbox?: Address } | void, |
| 76 | +): Address { |
| 77 | + return ((args && args.sequencerInbox) ?? (params && params.sequencerInbox)) as unknown as Address; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Public actions for parent chain |
| 82 | + * |
| 83 | + * @example |
| 84 | + * import { createPublicClient, http } from 'viem' |
| 85 | + * import { publicActionsParentChain } from '@arbitrum/orbit-sdk' |
| 86 | + * import { arbitrum } from 'viem/chains' |
| 87 | + * |
| 88 | + * export const publicClientParentChain = createPublicClient({ |
| 89 | + * chain: arbitrum, |
| 90 | + * transport: http(), |
| 91 | + * }).extend(publicActionsParentChain({ |
| 92 | + * sequencerInbox: '0x1c479675ad559DC151F6Ec7ed3FbF8ceE79582B6' |
| 93 | + * })) |
| 94 | + * |
| 95 | + * const { delayBlocks, futureBlocks, delaySeconds, futureSeconds } = await publicClientParentChain.getMaxTimeVariation() |
| 96 | + */ |
| 97 | +export function publicActionsParentChain< |
| 98 | + TParams extends Params = void, |
| 99 | + TTransport extends Transport = Transport, |
| 100 | + TChain extends Chain | undefined = Chain | undefined, |
| 101 | +>(params: void): (client: PublicClient<TTransport, TChain>) => PublicActionsParentChain<false>; |
| 102 | +export function publicActionsParentChain< |
| 103 | + TParams extends Params = { sequencerInbox: Address }, |
| 104 | + TTransport extends Transport = Transport, |
| 105 | + TChain extends Chain | undefined = Chain | undefined, |
| 106 | +>(params: TParams): (client: PublicClient<TTransport, TChain>) => PublicActionsParentChain<true>; |
| 107 | +export function publicActionsParentChain< |
| 108 | + TParams extends Params, |
| 109 | + TTransport extends Transport = Transport, |
| 110 | + TChain extends Chain | undefined = Chain | undefined, |
| 111 | +>(params: TParams) { |
| 112 | + return (client: PublicClient<TTransport, TChain>) => { |
| 113 | + // sequencerInbox is curried, sequencerInbox param is optional. |
| 114 | + return { |
| 115 | + // Getters |
| 116 | + getMaxTimeVariation: (args) => |
| 117 | + getMaxTimeVariation(client, { |
| 118 | + ...args, |
| 119 | + sequencerInbox: getSequencerInboxAddress(params, args), |
| 120 | + }), |
| 121 | + isBatchPoster: (args) => |
| 122 | + isBatchPoster(client, { |
| 123 | + ...args, |
| 124 | + sequencerInbox: getSequencerInboxAddress(params, args), |
| 125 | + }), |
| 126 | + isValidKeysetHash: (args) => |
| 127 | + isValidKeysetHash(client, { |
| 128 | + ...args, |
| 129 | + sequencerInbox: getSequencerInboxAddress(params, args), |
| 130 | + }), |
| 131 | + // Setters |
| 132 | + invalidateKeysetHash: (args) => |
| 133 | + invalidateKeysetHash(client, { |
| 134 | + ...args, |
| 135 | + sequencerInbox: getSequencerInboxAddress(params, args), |
| 136 | + }), |
| 137 | + enableBatchPoster: (args) => |
| 138 | + enableBatchPoster(client, { |
| 139 | + ...args, |
| 140 | + sequencerInbox: getSequencerInboxAddress(params, args), |
| 141 | + }), |
| 142 | + disableBatchPoster: (args) => |
| 143 | + disableBatchPoster(client, { |
| 144 | + ...args, |
| 145 | + sequencerInbox: getSequencerInboxAddress(params, args), |
| 146 | + }), |
| 147 | + setKeyset: (args) => |
| 148 | + setKeyset(client, { |
| 149 | + ...args, |
| 150 | + sequencerInbox: getSequencerInboxAddress(params, args), |
| 151 | + }), |
| 152 | + setMaxTimeVariation: (args) => |
| 153 | + setMaxTimeVariation(client, { |
| 154 | + ...args, |
| 155 | + sequencerInbox: getSequencerInboxAddress(params, args), |
| 156 | + }), |
| 157 | + } satisfies PublicActionsParentChain< |
| 158 | + TParams extends { sequencerInbox: Address } ? true : false |
| 159 | + >; |
| 160 | + }; |
| 161 | +} |
0 commit comments