Skip to content

Commit 2043107

Browse files
committed
Fix typing
Allow arbOwnerPublicActions to be either called: - without parameters (`.extend(arbOwnerPublicActions)`) - with parameters (`.extend(arbOwnerPublicActions({ arbOsVersion: X}))`)
1 parent a4bb1c2 commit 2043107

File tree

2 files changed

+85
-14
lines changed

2 files changed

+85
-14
lines changed
Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Transport, Chain, PrepareTransactionRequestReturnType, PublicClient } from 'viem';
1+
import { Transport, Chain, PrepareTransactionRequestReturnType, Client, Account } from 'viem';
22

33
import {
44
arbOwnerReadContract,
@@ -13,7 +13,7 @@ import {
1313
import { ArbOSVersions } from '../contracts';
1414

1515
export type ArbOwnerPublicActions<
16-
TArbOsVersion extends ArbOSVersions,
16+
TArbOsVersion extends ArbOSVersions = 20,
1717
TChain extends Chain | undefined = Chain | undefined,
1818
> = {
1919
arbOwnerReadContract: <TFunctionName extends ArbOwnerPublicFunctionName<TArbOsVersion>>(
@@ -24,26 +24,58 @@ export type ArbOwnerPublicActions<
2424
TFunctionName extends ArbOwnerPublicFunctionName<TArbOsVersion>,
2525
>(
2626
args: ArbOwnerPrepareTransactionRequestParameters<TArbOsVersion, TFunctionName>,
27-
) => Promise<PrepareTransactionRequestReturnType<TChain> & { chainId: number }>;
27+
) => Promise<PrepareTransactionRequestReturnType<TChain>>;
2828
};
2929

30+
const defaultArbOsVersion = 20;
31+
32+
// arbOsVersion is passed as a parameter `client.extend(arbOwnerPublicActions({ arbOsVersion: 10 }))`
3033
export function arbOwnerPublicActions<
3134
TArbOsVersion extends ArbOSVersions,
3235
TTransport extends Transport = Transport,
3336
TChain extends Chain | undefined = Chain | undefined,
34-
>({ arbOsVersion }: { arbOsVersion: TArbOsVersion }) {
35-
return (
36-
client: PublicClient<TTransport, TChain>,
37-
): ArbOwnerPublicActions<TArbOsVersion, TChain> => {
38-
return {
39-
arbOwnerReadContract: (args) => arbOwnerReadContract(client, { ...args, arbOsVersion }),
40-
37+
TAccount extends Account | undefined = Account | undefined,
38+
>(param: {
39+
arbOsVersion: TArbOsVersion;
40+
}): (client: Client) => ArbOwnerPublicActions<TArbOsVersion, TChain>;
41+
// No parameter are passed `client.extend(arbOwnerPublicActions)`
42+
export function arbOwnerPublicActions<
43+
TArbOsVersion extends ArbOSVersions,
44+
TTransport extends Transport = Transport,
45+
TChain extends Chain | undefined = Chain | undefined,
46+
TAccount extends Account | undefined = Account | undefined,
47+
>(param: Client<TTransport, TChain, TAccount>): ArbOwnerPublicActions<typeof defaultArbOsVersion, TChain>;
48+
export function arbOwnerPublicActions<
49+
TArbOsVersion extends ArbOSVersions,
50+
TTransport extends Transport = Transport,
51+
TChain extends Chain | undefined = Chain | undefined,
52+
TAccount extends Account | undefined = Account | undefined,
53+
>(paramOrClient: { arbOsVersion: TArbOsVersion } | Client<TTransport, TChain, TAccount>) {
54+
if ('arbOsVersion' in paramOrClient) {
55+
const result: (client: Client) => ArbOwnerPublicActions<TArbOsVersion, TChain> = (client) => ({
56+
arbOwnerReadContract: (args) =>
57+
arbOwnerReadContract(client, { ...args, arbOsVersion: paramOrClient.arbOsVersion }),
4158
arbOwnerPrepareTransactionRequest: (args) =>
4259
// @ts-ignore (todo: fix viem type issue)
4360
arbOwnerPrepareTransactionRequest(client, {
4461
...args,
45-
arbOsVersion,
62+
arbOsVersion: paramOrClient.arbOsVersion,
4663
}),
47-
};
64+
});
65+
66+
return result;
67+
}
68+
69+
const result: ArbOwnerPublicActions<typeof defaultArbOsVersion, TChain> = {
70+
arbOwnerReadContract: (args) =>
71+
// @ts-ignore (todo: fix viem type issue)
72+
arbOwnerReadContract(paramOrClient, { ...args, arbOsVersion: defaultArbOsVersion }),
73+
arbOwnerPrepareTransactionRequest: (args) =>
74+
// @ts-ignore (todo: fix viem type issue)
75+
arbOwnerPrepareTransactionRequest(paramOrClient, {
76+
...args,
77+
arbOsVersion: defaultArbOsVersion,
78+
}),
4879
};
80+
return result;
4981
}

src/decorators/arbOwnerPublicActions.unit.test.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { nitroTestnodeL2 } from '../chains';
55
import { arbOwnerPublicActions } from './arbOwnerPublicActions';
66
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
77

8+
const clientWithoutParam = createPublicClient({
9+
chain: nitroTestnodeL2,
10+
transport: http(),
11+
}).extend(arbOwnerPublicActions);
812
const client10 = createPublicClient({
913
chain: nitroTestnodeL2,
1014
transport: http(),
@@ -19,6 +23,10 @@ const client20 = createPublicClient({
1923
}).extend(arbOwnerPublicActions({ arbOsVersion: 20 }));
2024
const randomAccount = privateKeyToAccount(generatePrivateKey());
2125
const upgradeExecutorAddress = '0x24198F8A339cd3C47AEa3A764A20d2dDaB4D1b5b';
26+
const client = createPublicClient({
27+
chain: nitroTestnodeL2,
28+
transport: http(),
29+
})
2230

2331
describe('Accept function name based on arbOSVersion', async () => {
2432
it('Version 10', () => {
@@ -86,6 +94,31 @@ describe('Accept function name based on arbOSVersion', async () => {
8694
}),
8795
).rejects.toThrowError(AbiFunctionNotFoundError);
8896
});
97+
98+
it('Default version (20)', () => {
99+
// arbOwnerPublicActions without params is defaulted to arbOsVersion 20
100+
expectTypeOf<
101+
typeof clientWithoutParam.arbOwnerReadContract<'getInfraFeeAccount'>
102+
>().toBeCallableWith({
103+
functionName: 'getInfraFeeAccount',
104+
});
105+
106+
expectTypeOf<
107+
typeof clientWithoutParam.arbOwnerPrepareTransactionRequest<'setL1PricingRewardRecipient'>
108+
>().toBeCallableWith({
109+
functionName: 'setL1PricingRewardRecipient',
110+
account: randomAccount.address,
111+
upgradeExecutor: upgradeExecutorAddress,
112+
args: [randomAccount.address],
113+
});
114+
115+
expect(
116+
clientWithoutParam.arbOwnerReadContract({
117+
// @ts-expect-error Not available for version 20
118+
functionName: 'onlyOnArbOS10',
119+
}),
120+
).rejects.toThrowError(AbiFunctionNotFoundError);
121+
});
89122
});
90123

91124
// Those tests won't fail if the return type is wrong
@@ -98,19 +131,25 @@ describe('Type return values for function in multiple versions', () => {
98131
}),
99132
).resolves.toEqualTypeOf<`0x${string}`>();
100133
});
101-
102134
it('Version 11', () => {
103135
expectTypeOf(
104136
client11.arbOwnerReadContract({
105137
functionName: 'getAllChainOwners',
106138
}),
107139
).resolves.toEqualTypeOf<bigint>();
108140
});
109-
it('Version 11', () => {
141+
it('Version 20', () => {
110142
expectTypeOf(
111143
client20.arbOwnerReadContract({
112144
functionName: 'getAllChainOwners',
113145
}),
114146
).resolves.toEqualTypeOf<readonly `0x${string}`[]>();
115147
});
148+
it('Default version (20)', () => {
149+
expectTypeOf(
150+
clientWithoutParam.arbOwnerReadContract({
151+
functionName: 'getAllChainOwners',
152+
}),
153+
).resolves.toEqualTypeOf<readonly `0x${string}`[]>();
154+
});
116155
});

0 commit comments

Comments
 (0)