Skip to content

Commit e063eae

Browse files
committed
Allow actions to be called manually
1 parent 1cacecc commit e063eae

File tree

2 files changed

+109
-16
lines changed

2 files changed

+109
-16
lines changed

src/decorators/arbOwnerPublicActions.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,34 @@ export type ArbOwnerPublicActions<
2424
TFunctionName extends ArbOwnerPublicFunctionName<TArbOsVersion>,
2525
>(
2626
args: ArbOwnerPrepareTransactionRequestParameters<TArbOsVersion, TFunctionName>,
27-
) => Promise<PrepareTransactionRequestReturnType<TChain>>;
27+
) => Promise<PrepareTransactionRequestReturnType<TChain> & { chainId: number }>;
2828
};
2929

3030
const defaultArbOsVersion = 20;
3131

32-
// arbOsVersion is passed as a parameter `client.extend(arbOwnerPublicActions({ arbOsVersion: 10 }))`
32+
// Client is passed explicitly
33+
// `arbOwnerPublicActions(client)`, `arbOwnerPublicActions(client, { arbOsVersion: 20 })`
34+
export function arbOwnerPublicActions<
35+
TArbOsVersion extends ArbOSVersions,
36+
TTransport extends Transport = Transport,
37+
TChain extends Chain | undefined = Chain | undefined,
38+
TAccount extends Account | undefined = Account | undefined,
39+
>(
40+
client: Client<TTransport, TChain, TAccount>,
41+
{ arbOsVersion }: { arbOsVersion: TArbOsVersion },
42+
): ArbOwnerPublicActions<TArbOsVersion, TChain>;
43+
// arbOsVersion is passed as a parameter
44+
// `client.extend(arbOwnerPublicActions({ arbOsVersion: 10 }))`
3345
export function arbOwnerPublicActions<
3446
TArbOsVersion extends ArbOSVersions,
3547
TTransport extends Transport = Transport,
3648
TChain extends Chain | undefined = Chain | undefined,
3749
TAccount extends Account | undefined = Account | undefined,
3850
>(param: {
3951
arbOsVersion: TArbOsVersion;
40-
}): (client: Client) => ArbOwnerPublicActions<TArbOsVersion, TChain>;
41-
// No parameter are passed `client.extend(arbOwnerPublicActions)`
52+
}): (client: Client<TTransport, TChain, TAccount>) => ArbOwnerPublicActions<TArbOsVersion, TChain>;
53+
// No parameter are passed
54+
// `client.extend(arbOwnerPublicActions)`
4255
export function arbOwnerPublicActions<
4356
TArbOsVersion extends ArbOSVersions,
4457
TTransport extends Transport = Transport,
@@ -52,7 +65,11 @@ export function arbOwnerPublicActions<
5265
TTransport extends Transport = Transport,
5366
TChain extends Chain | undefined = Chain | undefined,
5467
TAccount extends Account | undefined = Account | undefined,
55-
>(paramOrClient: { arbOsVersion: TArbOsVersion } | Client<TTransport, TChain, TAccount>) {
68+
>(
69+
paramOrClient: { arbOsVersion: TArbOsVersion } | Client<TTransport, TChain, TAccount>,
70+
options?: { arbOsVersion: TArbOsVersion },
71+
) {
72+
// arbOsVersion is passed as a parameter, return actions with curried arbOsVersion
5673
if ('arbOsVersion' in paramOrClient) {
5774
const result: (
5875
client: Client<TTransport, TChain, TAccount>,
@@ -70,15 +87,24 @@ export function arbOwnerPublicActions<
7087
return result;
7188
}
7289

73-
const result: ArbOwnerPublicActions<typeof defaultArbOsVersion, TChain> = {
90+
/**
91+
* Parameter is a client, we either have:
92+
* - client.extend(arbOwnerPublicActions)
93+
* - arbOwnerPublicActions(client)
94+
* - arbOwnerPublicActions(client, { arbOsVersion: X })
95+
*
96+
* If we don't have arbOsVersion (the 2 first cases), default the version to defaultArbOsVersion
97+
*/
98+
const version = options?.arbOsVersion ?? defaultArbOsVersion;
99+
const result: ArbOwnerPublicActions<typeof version, TChain> = {
74100
arbOwnerReadContract: (args) =>
75101
// @ts-ignore (todo: fix viem type issue)
76-
arbOwnerReadContract(paramOrClient, { ...args, arbOsVersion: defaultArbOsVersion }),
102+
arbOwnerReadContract(paramOrClient, { ...args, arbOsVersion: version }),
77103
arbOwnerPrepareTransactionRequest: (args) =>
78104
// @ts-ignore (todo: fix viem type issue)
79105
arbOwnerPrepareTransactionRequest(paramOrClient, {
80106
...args,
81-
arbOsVersion: defaultArbOsVersion,
107+
arbOsVersion: version,
82108
}),
83109
};
84110
return result;

src/decorators/arbOwnerPublicActions.unit.test.ts

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ const client20 = createPublicClient({
2424
const randomAccount = privateKeyToAccount(generatePrivateKey());
2525
const upgradeExecutorAddress = '0x24198F8A339cd3C47AEa3A764A20d2dDaB4D1b5b';
2626

27+
const client = createPublicClient({
28+
chain: nitroTestnodeL2,
29+
transport: http(),
30+
});
31+
const actionsWithVersion = arbOwnerPublicActions(client, { arbOsVersion: 11 });
32+
const actionsWithDefaultVersion = arbOwnerPublicActions(client);
33+
2734
describe('Accept function name based on arbOSVersion', async () => {
28-
it('Version 10', () => {
35+
it('Client with actions (version 10)', () => {
2936
expectTypeOf<typeof client10.arbOwnerReadContract<'onlyOnArbOS10'>>().toBeCallableWith({
3037
functionName: 'onlyOnArbOS10',
3138
});
@@ -46,7 +53,7 @@ describe('Accept function name based on arbOSVersion', async () => {
4653
}),
4754
).rejects.toThrowError(AbiFunctionNotFoundError);
4855
});
49-
it('Version 11', () => {
56+
it('Client with actions (version 11)', () => {
5057
expectTypeOf<typeof client11.arbOwnerReadContract<'onlyOnArbOS11'>>().toBeCallableWith({
5158
functionName: 'onlyOnArbOS11',
5259
});
@@ -67,7 +74,7 @@ describe('Accept function name based on arbOSVersion', async () => {
6774
}),
6875
).rejects.toThrowError(AbiFunctionNotFoundError);
6976
});
70-
it('Version 20', () => {
77+
it('Client with actions (version 20)', () => {
7178
expectTypeOf<typeof client20.arbOwnerReadContract<'getInfraFeeAccount'>>().toBeCallableWith({
7279
functionName: 'getInfraFeeAccount',
7380
});
@@ -88,7 +95,7 @@ describe('Accept function name based on arbOSVersion', async () => {
8895
}),
8996
).rejects.toThrowError(AbiFunctionNotFoundError);
9097
});
91-
it('Default version (20)', () => {
98+
it('Client with actions (default version)', () => {
9299
// arbOwnerPublicActions without params is defaulted to arbOsVersion 20
93100
expectTypeOf<
94101
typeof clientWithoutParam.arbOwnerReadContract<'getInfraFeeAccount'>
@@ -112,37 +119,97 @@ describe('Accept function name based on arbOSVersion', async () => {
112119
}),
113120
).rejects.toThrowError(AbiFunctionNotFoundError);
114121
});
122+
it('Standalone actions (version 11) ', () => {
123+
expectTypeOf<
124+
typeof actionsWithVersion.arbOwnerReadContract<'onlyOnArbOS11'>
125+
>().toBeCallableWith({
126+
functionName: 'onlyOnArbOS11',
127+
});
128+
129+
expectTypeOf<
130+
typeof actionsWithVersion.arbOwnerPrepareTransactionRequest<'setL1PricingRewardRecipient'>
131+
>().toBeCallableWith({
132+
functionName: 'setL1PricingRewardRecipient',
133+
account: randomAccount.address,
134+
upgradeExecutor: upgradeExecutorAddress,
135+
args: [100n],
136+
});
137+
138+
expect(
139+
actionsWithVersion.arbOwnerReadContract({
140+
// @ts-expect-error Not available for version 11
141+
functionName: 'onlyOnArbOS20',
142+
}),
143+
).rejects.toThrowError(AbiFunctionNotFoundError);
144+
});
145+
it('Standalone actions (default version) ', () => {
146+
expectTypeOf<
147+
typeof actionsWithDefaultVersion.arbOwnerReadContract<'getInfraFeeAccount'>
148+
>().toBeCallableWith({
149+
functionName: 'getInfraFeeAccount',
150+
});
151+
152+
expectTypeOf<
153+
typeof actionsWithDefaultVersion.arbOwnerPrepareTransactionRequest<'setL1PricingRewardRecipient'>
154+
>().toBeCallableWith({
155+
functionName: 'setL1PricingRewardRecipient',
156+
account: randomAccount.address,
157+
upgradeExecutor: upgradeExecutorAddress,
158+
args: [randomAccount.address],
159+
});
160+
161+
expect(
162+
actionsWithDefaultVersion.arbOwnerReadContract({
163+
// @ts-expect-error Not available for version 20
164+
functionName: 'onlyOnArbOS10',
165+
}),
166+
).rejects.toThrowError(AbiFunctionNotFoundError);
167+
});
115168
});
116169

117170
// Those tests won't fail if the return type is wrong
118171
// But they will display an error in the IDE
119172
describe('Type return values for function in multiple versions', () => {
120-
it('Version 10', () => {
173+
it('Client with actions (Version 10)', () => {
121174
expectTypeOf(
122175
client10.arbOwnerReadContract({
123176
functionName: 'getAllChainOwners',
124177
}),
125178
).resolves.toEqualTypeOf<`0x${string}`>();
126179
});
127-
it('Version 11', () => {
180+
it('Client with actions (Version 11)', () => {
128181
expectTypeOf(
129182
client11.arbOwnerReadContract({
130183
functionName: 'getAllChainOwners',
131184
}),
132185
).resolves.toEqualTypeOf<bigint>();
133186
});
134-
it('Version 20', () => {
187+
it('Client with actions (Version 20)', () => {
135188
expectTypeOf(
136189
client20.arbOwnerReadContract({
137190
functionName: 'getAllChainOwners',
138191
}),
139192
).resolves.toEqualTypeOf<readonly `0x${string}`[]>();
140193
});
141-
it('Default version (20)', () => {
194+
it('Client with actions (default version)', () => {
142195
expectTypeOf(
143196
clientWithoutParam.arbOwnerReadContract({
144197
functionName: 'getAllChainOwners',
145198
}),
146199
).resolves.toEqualTypeOf<readonly `0x${string}`[]>();
147200
});
201+
it('Standalone actions (version 11)', () => {
202+
expectTypeOf(
203+
actionsWithVersion.arbOwnerReadContract({
204+
functionName: 'getAllChainOwners',
205+
}),
206+
).resolves.toEqualTypeOf<bigint>();
207+
});
208+
it('Standalone actions (default version)', () => {
209+
expectTypeOf(
210+
actionsWithDefaultVersion.arbOwnerReadContract({
211+
functionName: 'getAllChainOwners',
212+
}),
213+
).resolves.toEqualTypeOf<readonly `0x${string}`[]>();
214+
});
148215
});

0 commit comments

Comments
 (0)