Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add upgradeExecutor integration tests #191

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/actions/buildAddExecutor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Address, Chain, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem';
import { upgradeExecutorABI } from '../contracts/UpgradeExecutor';
import {
ActionParameters,
PrepareTransactionRequestReturnTypeWithChainId,
WithAccount,
} from '../types/Actions';
import { Prettify } from '../types/utils';
import { UPGRADE_EXECUTOR_ROLE_EXECUTOR } from '../upgradeExecutorEncodeFunctionData';
import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters';

export type BuildAddExecutorParameters<Curried extends boolean = false> = Prettify<
WithAccount<
ActionParameters<
{
address: Address;
},
'upgradeExecutor',
Curried
>
>
>;

export type BuildAddExecutorReturnType = PrepareTransactionRequestReturnTypeWithChainId;

export async function buildAddExecutor<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
{ account, upgradeExecutor: upgradeExecutorAddress, params }: BuildAddExecutorParameters,
): Promise<BuildAddExecutorReturnType> {
const request = await client.prepareTransactionRequest({
chain: client.chain as Chain | undefined,
account,
...prepareUpgradeExecutorCallParameters({
to: upgradeExecutorAddress,
upgradeExecutor: upgradeExecutorAddress,
args: [UPGRADE_EXECUTOR_ROLE_EXECUTOR, params.address],
abi: upgradeExecutorABI,
functionName: 'grantRole',
}),
} satisfies PrepareTransactionRequestParameters);

return { ...request, chainId: client.chain.id };
}
43 changes: 43 additions & 0 deletions src/actions/buildRemoveExecutor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Address, Chain, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem';
import { upgradeExecutorABI } from '../contracts/UpgradeExecutor';
import {
ActionParameters,
PrepareTransactionRequestReturnTypeWithChainId,
WithAccount,
} from '../types/Actions';
import { Prettify } from '../types/utils';
import { UPGRADE_EXECUTOR_ROLE_EXECUTOR } from '../upgradeExecutorEncodeFunctionData';
import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters';

export type BuildRemoveExecutorParameters<Curried extends boolean = false> = Prettify<
WithAccount<
ActionParameters<
{
address: Address;
},
'upgradeExecutor',
Curried
>
>
>;

export type BuildRemoveExecutorReturnType = PrepareTransactionRequestReturnTypeWithChainId;

export async function buildRemoveExecutor<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
{ account, upgradeExecutor: upgradeExecutorAddress, params }: BuildRemoveExecutorParameters,
): Promise<BuildRemoveExecutorReturnType> {
const request = await client.prepareTransactionRequest({
chain: client.chain as Chain | undefined,
account,
...prepareUpgradeExecutorCallParameters({
to: upgradeExecutorAddress,
upgradeExecutor: upgradeExecutorAddress,
args: [UPGRADE_EXECUTOR_ROLE_EXECUTOR, params.address],
abi: upgradeExecutorABI,
functionName: 'revokeRole',
}),
} satisfies PrepareTransactionRequestParameters);

return { ...request, chainId: client.chain.id };
}
27 changes: 27 additions & 0 deletions src/actions/hasRole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Address, Chain, PublicClient, ReadContractReturnType, Transport } from 'viem';
import { upgradeExecutorABI } from '../contracts/UpgradeExecutor';
import { UpgradeExecutorRole } from '../upgradeExecutorEncodeFunctionData';
import { ActionParameters } from '../types/Actions';

export type HasRoleParameters<Curried extends boolean = false> = ActionParameters<
{
role: UpgradeExecutorRole;
address: Address;
},
'upgradeExecutor',
Curried
>;

export type HasRoleReturnType = ReadContractReturnType<typeof upgradeExecutorABI, 'hasRole'>;

export async function hasRole<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
{ upgradeExecutor, params }: HasRoleParameters,
): Promise<HasRoleReturnType> {
return client.readContract({
abi: upgradeExecutorABI,
functionName: 'hasRole',
address: upgradeExecutor,
args: [params.role, params.address],
});
}
93 changes: 93 additions & 0 deletions src/actions/upgradeExecutor.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { describe, it, expect } from 'vitest';
import { getInformationFromTestnode, getNitroTestnodePrivateKeyAccounts } from '../testHelpers';
import { Address, createPublicClient, http } from 'viem';
import { nitroTestnodeL2 } from '../chains';
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import { upgradeExecutorFetchPrivilegedAccounts } from '../upgradeExecutorFetchPrivilegedAccounts';
import { buildAddExecutor } from './buildAddExecutor';
import { UPGRADE_EXECUTOR_ROLE_EXECUTOR } from '../upgradeExecutorEncodeFunctionData';
import { buildRemoveExecutor } from './buildRemoveExecutor';
import { hasRole } from './hasRole';

const { l3UpgradeExecutor } = getInformationFromTestnode();
const { l3RollupOwner } = getNitroTestnodePrivateKeyAccounts();

const client = createPublicClient({
chain: nitroTestnodeL2,
transport: http(),
});

describe('Upgrade executor', () => {
it('buildAddExecutor and buildRemoveExecutor update upgradeExecutor', async () => {
async function changeUpgradeExecutor(address: Address, add: boolean) {
const fn = add ? buildAddExecutor : buildRemoveExecutor;
const transactionRequest = await fn(client, {
account: l3RollupOwner.address,
upgradeExecutor: l3UpgradeExecutor,
params: {
address,
},
});
const txHash = await client.sendRawTransaction({
serializedTransaction: await l3RollupOwner.signTransaction(transactionRequest),
});
await client.waitForTransactionReceipt({ hash: txHash });
}
const addUpgradeExecutor = (address: Address) => changeUpgradeExecutor(address, true);
const removeUpgradeExecutor = (address: Address) => changeUpgradeExecutor(address, false);

const randomAddress = privateKeyToAccount(generatePrivateKey()).address;

const initialState = await upgradeExecutorFetchPrivilegedAccounts({
upgradeExecutorAddress: l3UpgradeExecutor,
publicClient: client,
});

expect(
await hasRole(client, {
params: {
role: UPGRADE_EXECUTOR_ROLE_EXECUTOR,
address: randomAddress,
},
upgradeExecutor: l3UpgradeExecutor,
}),
).toBeFalsy();

await addUpgradeExecutor(randomAddress);
expect(
await upgradeExecutorFetchPrivilegedAccounts({
upgradeExecutorAddress: l3UpgradeExecutor,
publicClient: client,
}),
).toEqual({
...initialState,
[randomAddress]: [UPGRADE_EXECUTOR_ROLE_EXECUTOR],
});
expect(
await hasRole(client, {
params: {
role: UPGRADE_EXECUTOR_ROLE_EXECUTOR,
address: randomAddress,
},
upgradeExecutor: l3UpgradeExecutor,
}),
).toBeTruthy();

await removeUpgradeExecutor(randomAddress);
expect(
await upgradeExecutorFetchPrivilegedAccounts({
upgradeExecutorAddress: l3UpgradeExecutor,
publicClient: client,
}),
).toEqual(initialState);
expect(
await hasRole(client, {
params: {
role: UPGRADE_EXECUTOR_ROLE_EXECUTOR,
address: randomAddress,
},
upgradeExecutor: l3UpgradeExecutor,
}),
).toBeFalsy();
});
});
3 changes: 2 additions & 1 deletion src/prepareUpgradeExecutorCallParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import {
upgradeExecutorEncodeFunctionData,
UpgradeExecutorFunctionName,
} from './upgradeExecutorEncodeFunctionData';
import { upgradeExecutorABI } from './contracts/UpgradeExecutor';

type ABIs = typeof sequencerInboxABI | typeof arbOwnerABI;
type ABIs = typeof sequencerInboxABI | typeof arbOwnerABI | typeof upgradeExecutorABI;
type FunctionName<TAbi extends ABIs> = GetFunctionName<TAbi>;

type EncodeFunctionDataParameters<
Expand Down
Loading