Skip to content

Commit

Permalink
Feat: Add v1 upgradeExecutor setters
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstph-dvx committed Sep 25, 2024
1 parent 2f33e44 commit 086ece6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
36 changes: 36 additions & 0 deletions src/actions/buildAddExecutor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Address, Chain, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem';
import { upgradeExecutorABI } from '../contracts/UpgradeExecutor';
import { PrepareTransactionRequestReturnTypeWithChainId, WithAccount } from '../types/Actions';
import { Prettify } from '../types/utils';
import { UPGRADE_EXECUTOR_ROLE_EXECUTOR } from '../upgradeExecutorEncodeFunctionData';
import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters';

export type BuildAddExecutorParameters = Prettify<
WithAccount<{
params: {
address: Address;
};
upgradeExecutor: Address;
}>
>;

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 };
}
36 changes: 36 additions & 0 deletions src/actions/buildRemoveExecutor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Address, Chain, PrepareTransactionRequestParameters, PublicClient, Transport } from 'viem';
import { upgradeExecutorABI } from '../contracts/UpgradeExecutor';
import { PrepareTransactionRequestReturnTypeWithChainId, WithAccount } from '../types/Actions';
import { Prettify } from '../types/utils';
import { UPGRADE_EXECUTOR_ROLE_EXECUTOR } from '../upgradeExecutorEncodeFunctionData';
import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters';

export type RemoveExecutorParameters = Prettify<
WithAccount<{
params: {
address: Address;
};
upgradeExecutor: Address;
}>
>;

export type RemoveExecutorReturnType = PrepareTransactionRequestReturnTypeWithChainId;

export async function buildRemoveExecutor<TChain extends Chain>(
client: PublicClient<Transport, TChain>,
{ account, upgradeExecutor: upgradeExecutorAddress, params }: RemoveExecutorParameters,
): Promise<RemoveExecutorReturnType> {
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 };
}
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
8 changes: 4 additions & 4 deletions src/types/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type isEmptyObject<Args> = Args extends Record<string, never> ? true : false;
export type ActionParameters<Args, ContractName extends string, Curried extends boolean> = Prettify<
Curried extends false
? isEmptyObject<Args> extends true
? { [key in ContractName]: Address } // Contract wasn't curried. Args is an empty object. Only requires the contract name
: { params: Args } & { [key in ContractName]: Address } // Contract wasn't curried. Args is not empty. Requires both params and contract name
? { [key in ContractName]: Address } | { sequencerInbox: Address } // Contract wasn't curried. Args is an empty object. Only requires the contract name
: { params: Args } & ({ [key in ContractName]: Address } | { sequencerInbox: Address }) // Contract wasn't curried. Args is not empty. Requires both params and contract name
: isEmptyObject<Args> extends true
? { [key in ContractName]: Address } | void // Contract was curried. Args is empty. Only requires the contract name. Allows no parameters
: { params: Args } & { [key in ContractName]?: Address } // Contract was curried. Args is not empty. Requires params, contract name is optional
? { [key in ContractName]: Address } | { sequencerInbox: Address } | void // Contract was curried. Args is empty. Only requires the contract name. Allows no parameters
: { params: Args } & ({ [key in ContractName]?: Address } | { sequencerInbox?: Address }) // Contract was curried. Args is not empty. Requires params, contract name is optional
>;

export type WithAccount<Args> = Args & {
Expand Down

0 comments on commit 086ece6

Please sign in to comment.