Skip to content

Commit 22d8300

Browse files
committed
Feat: Add v1 publicActions for parent chain
Closes FS-535
1 parent 76f9c78 commit 22d8300

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { it, expect, describe } from 'vitest';
2+
3+
import { createPublicClient, http, padHex, zeroAddress } from 'viem';
4+
import { mainnet } from '../chains';
5+
import { publicActionsParentChain } from './publicActionsParentChain';
6+
7+
const arbOneSequencerInbox = '0x1c479675ad559DC151F6Ec7ed3FbF8ceE79582B6';
8+
9+
const client = createPublicClient({
10+
chain: mainnet,
11+
transport: http(),
12+
}).extend(publicActionsParentChain({ sequencerInbox: arbOneSequencerInbox }));
13+
14+
describe('Getters', () => {
15+
it('[maxTimeVariation] Should return max time variation', async () => {
16+
const maxTimeVariation = await client.getMaxTimeVariation();
17+
expect(maxTimeVariation).toEqual({
18+
delayBlocks: 5760n,
19+
futureBlocks: 64n,
20+
delaySeconds: 86400n,
21+
futureSeconds: 768n,
22+
});
23+
});
24+
25+
it('[isBatchPoster] Should return if an address is a batch poster', async () => {
26+
const isZeroAddressBatchPoster = await client.isBatchPoster({
27+
batchPoster: zeroAddress,
28+
});
29+
expect(isZeroAddressBatchPoster).toBeFalsy();
30+
});
31+
32+
it('[isValidKeysetHash] Should return if a keysetHash is a valid one', async () => {
33+
const isEmptyHashValidKeysetHash = await client.isValidKeysetHash({
34+
keysetHash: padHex('0x'),
35+
});
36+
expect(isEmptyHashValidKeysetHash).toBeFalsy();
37+
38+
// Test on Nova
39+
const isAValidKeysetHashOnNova = await client.isValidKeysetHash({
40+
keysetHash: '0x01191accc7ad5a8020e6c6d122984540e9fc48d0457bda63e0a32c8c31994f4a',
41+
sequencerInbox: '0x211e1c4c7f1bf5351ac850ed10fd68cffcf6c21b',
42+
});
43+
expect(isAValidKeysetHashOnNova).toBeTruthy();
44+
});
45+
});

0 commit comments

Comments
 (0)