Skip to content

Commit

Permalink
feat: Add setters for public chain
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstph-dvx committed Jun 6, 2024
1 parent cae2f23 commit 72263f7
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/actions/addSequencerL2Batch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
Chain,
GetFunctionArgs,
PrepareTransactionRequestParameters,
PrepareTransactionRequestReturnType,
PublicClient,
Transport,
encodeFunctionData,
} from 'viem';
import { sequencerInbox } from '../contracts';
import { WithContractAddress, ActionParameters, WithAccount } from '../types/Actions';

type Args = GetFunctionArgs<SequencerInboxABI, 'addSequencerL2Batch'>;
type SequencerInboxABI = typeof sequencerInbox.abi;
type BuildAddSequencerL2BatchParameters<Curried extends boolean = false> = WithAccount<
WithContractAddress<Args, 'sequencerInbox', Curried>
>;
export type BuildAddSequencerL2BatchActionParameters<Curried extends boolean> = WithAccount<
ActionParameters<Args, 'sequencerInbox', Curried>
>;

export type BuildAddSequencerL2BatchReturnType = PrepareTransactionRequestReturnType;

function sequencerInboxFunctionData({ args }: BuildAddSequencerL2BatchParameters) {
return encodeFunctionData({
abi: sequencerInbox.abi,
functionName: 'addSequencerL2Batch',
args,
});
}

export async function buildAddSequencerL2Batch<TChain extends Chain | undefined>(
client: PublicClient<Transport, TChain>,
args: BuildAddSequencerL2BatchParameters,
): Promise<BuildAddSequencerL2BatchReturnType> {
const data = sequencerInboxFunctionData(args);

return client.prepareTransactionRequest({
to: args.sequencerInbox,
value: BigInt(0),
chain: client.chain,
data,
account: args.account,
} satisfies PrepareTransactionRequestParameters);
}
13 changes: 13 additions & 0 deletions src/decorators/publicActionsParentChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ import {
} from '../actions/isValidKeysetHash';

import { getRollup, GetRollupActionParameters, GetRollupReturnType } from '../actions/getRollup';
import {
buildAddSequencerL2Batch,
BuildAddSequencerL2BatchActionParameters,
BuildAddSequencerL2BatchReturnType,
} from '../actions/addSequencerL2Batch';

type Params = { sequencerInbox: Address } | void;

Expand Down Expand Up @@ -88,6 +93,9 @@ export type PublicActionsParentChain<Curried extends boolean> = {
isValidKeysetHash: (
parameters: IsValidKeysetHashActionParameters<Curried>,
) => Promise<IsValidKeysetHashReturnType>;
buildAddSequencerL2Batch: (
parameters: BuildAddSequencerL2BatchActionParameters<Curried>,
) => Promise<BuildAddSequencerL2BatchReturnType>;
};

/**
Expand Down Expand Up @@ -198,6 +206,11 @@ export function publicActionsParentChain<
...args,
sequencerInbox: getSequencerInboxAddress(params, args),
}),
buildAddSequencerL2Batch: (args) =>
buildAddSequencerL2Batch(client, {
...args,
sequencerInbox: getSequencerInboxAddress(params, args),
}),
} satisfies PublicActionsParentChain<
TParams extends { sequencerInbox: Address } ? true : false
>;
Expand Down
16 changes: 16 additions & 0 deletions src/decorators/sequencerInboxActions.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../testHelpers';
import { sequencerInboxActions } from './sequencerInboxActions';
import { sequencerInboxABI } from '../abi/sequencerInboxABI';
import { publicActionsParentChain } from './publicActionsParentChain';

const { l3RollupOwner, l3TokenBridgeDeployer, deployer } = getNitroTestnodePrivateKeyAccounts();

Expand All @@ -23,6 +24,15 @@ const client = createPublicClient({
transport: http(),
}).extend(sequencerInboxActions({ sequencerInbox: l3SequencerInbox }));

const client3 = createPublicClient({
chain: nitroTestnodeL2,
transport: http(),
}).extend(
publicActionsParentChain({
sequencerInbox: '0xaaa',
}),
);

describe('sequencerInboxReadContract', () => {
it('successfully fetches batchCount', async () => {
const batchCount = await client.sequencerInboxReadContract({
Expand Down Expand Up @@ -141,6 +151,12 @@ describe('sequencerInboxPrepareTransactionRequest', () => {
account: l3TokenBridgeDeployer.address,
upgradeExecutor: upgradeExecutor,
});

const a = client3.buildAddSequencerL2Batch({
args: [10n, '0xa', 10n, '0xaa', 5n, 5n],
account: '0xacc',
});

const transactionHash = await client.sendRawTransaction({
serializedTransaction: await l3TokenBridgeDeployer.signTransaction(transactionRequest),
});
Expand Down
46 changes: 46 additions & 0 deletions src/decorators/sequencerInboxActions.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { nitroTestnodeL2 } from '../chains';
import { sequencerInboxActions } from './sequencerInboxActions';
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import { publicActionsParentChain } from './publicActionsParentChain';

const l3SequencerInbox = '0x42b5da0625cf278067955f07045f63cafd79274f';

Expand Down Expand Up @@ -108,3 +109,48 @@ it('Infer parameters based on function name', async () => {
}),
).rejects.toThrowError(AbiFunctionNotFoundError);
});

const client2 = createPublicClient({
chain: nitroTestnodeL2,
transport: http(),
}).extend(publicActionsParentChain());
// Required (should error)
client2.getBatchCount();
// should work
client2.getBatchCount({
sequencerInbox: '0xaa',
});
client2.isBatchPoster({
args: ['0xaa'],
sequencerInbox: '0xbbb',
});

const client3 = createPublicClient({
chain: nitroTestnodeL2,
transport: http(),
}).extend(
publicActionsParentChain({
sequencerInbox: '0xaaa',
}),
);
// Should allow it
client3.getBatchCount({
sequencerInbox: '0xb',
});
// Should work
client3.getBatchCount();

// Should allow it
client3.isBatchPoster({
sequencerInbox: '0xb',
args: ['0xaa'],
});
// Should work
client3.isBatchPoster({
args: ['0xbbb'],
});

client3.buildAddSequencerL2Batch({
args: [10n, '0xa', 10n, '0xaa', 5n, 5n],
account: '0xacc',
});
4 changes: 4 additions & 0 deletions src/types/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ export type ActionParameters<Args, ContractName extends string, Curried extends
[key in ContractName]?: Address;
}
>;

export type WithAccount<Args> = Args & {
account: Address;
};

0 comments on commit 72263f7

Please sign in to comment.