From 333655d9a11971a9e60679bb8801348421bc8844 Mon Sep 17 00:00:00 2001 From: Doug <4741454+douglance@users.noreply.github.com> Date: Wed, 27 Dec 2023 13:49:51 -0500 Subject: [PATCH] reverts to using bare functions --- .../create-rollup-custom-fee-token/index.ts | 5 ++-- examples/create-rollup-eth/index.ts | 19 ++++-------- examples/prepare-node-config/index.ts | 5 ++-- examples/set-valid-keyset/index.ts | 16 ++++------ src/createRollup.integration.test.ts | 13 ++++----- src/createRollup.ts | 21 +++++++++----- ...eateRollupEnoughCustomFeeTokenAllowance.ts | 14 +++++---- ...ustomFeeTokenApprovalTransactionRequest.ts | 18 ++++++------ src/createRollupPrepareTransactionRequest.ts | 17 ++++++----- src/index.ts | 4 --- src/orbitClient.ts | 29 ------------------- src/utils/getters.ts | 19 ++++++++++++ 12 files changed, 79 insertions(+), 101 deletions(-) delete mode 100644 src/orbitClient.ts create mode 100644 src/utils/getters.ts diff --git a/examples/create-rollup-custom-fee-token/index.ts b/examples/create-rollup-custom-fee-token/index.ts index c78bd36d..29fef7fa 100644 --- a/examples/create-rollup-custom-fee-token/index.ts +++ b/examples/create-rollup-custom-fee-token/index.ts @@ -1,4 +1,4 @@ -import { Chain, http, Address } from 'viem'; +import { Chain, createPublicClient, http, Address } from 'viem'; import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; import { arbitrumSepolia } from 'viem/chains'; import { @@ -8,7 +8,6 @@ import { createRollupPrepareCustomFeeTokenApprovalTransactionRequest, createRollupPrepareTransactionRequest, createRollupPrepareTransactionReceipt, - createOrbitClient, } from '@arbitrum/orbit-sdk'; import { generateChainId } from '@arbitrum/orbit-sdk/utils'; @@ -46,7 +45,7 @@ const validator = privateKeyToAccount(validatorPrivateKey).address; // set the parent chain and create a public client for it const parentChain = arbitrumSepolia; -const parentChainPublicClient = createOrbitClient({ +const parentChainPublicClient = createPublicClient({ chain: parentChain, transport: http(), }); diff --git a/examples/create-rollup-eth/index.ts b/examples/create-rollup-eth/index.ts index 23bf2e39..dd982ec6 100644 --- a/examples/create-rollup-eth/index.ts +++ b/examples/create-rollup-eth/index.ts @@ -1,4 +1,4 @@ -import { Chain, http } from 'viem'; +import { Chain, createPublicClient, http } from 'viem'; import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; import { arbitrumSepolia } from 'viem/chains'; import { @@ -6,7 +6,6 @@ import { prepareChainConfig, createRollupPrepareTransactionRequest, createRollupPrepareTransactionReceipt, - createOrbitClient, } from '@arbitrum/orbit-sdk'; import { generateChainId } from '@arbitrum/orbit-sdk/utils'; @@ -44,10 +43,7 @@ const validator = privateKeyToAccount(validatorPrivateKey).address; // set the parent chain and create a public client for it const parentChain = arbitrumSepolia; -const parentChainOrbitClient = createOrbitClient({ - chain: parentChain, - transport: http(), -}); +const parentChainPublicClient = createPublicClient({ chain: parentChain, transport: http() }); // load the deployer account const deployer = privateKeyToAccount(sanitizePrivateKey(process.env.DEPLOYER_PRIVATE_KEY)); @@ -59,10 +55,7 @@ async function main() { // create the chain config const chainConfig = prepareChainConfig({ chainId, - arbitrum: { - InitialChainOwner: deployer.address, - DataAvailabilityCommittee: true, - }, + arbitrum: { InitialChainOwner: deployer.address, DataAvailabilityCommittee: true }, }); // prepare the transaction for deploying the core contracts @@ -77,17 +70,17 @@ async function main() { validators: [validator], }, account: deployer.address, - publicClient: parentChainOrbitClient, + publicClient: parentChainPublicClient, }); // sign and send the transaction - const txHash = await parentChainOrbitClient.sendRawTransaction({ + const txHash = await parentChainPublicClient.sendRawTransaction({ serializedTransaction: await deployer.signTransaction(request), }); // get the transaction receipt after waiting for the transaction to complete const txReceipt = createRollupPrepareTransactionReceipt( - await parentChainOrbitClient.waitForTransactionReceipt({ hash: txHash }), + await parentChainPublicClient.waitForTransactionReceipt({ hash: txHash }), ); console.log(`Deployed in ${getBlockExplorerUrl(parentChain)}/tx/${txReceipt.transactionHash}`); diff --git a/examples/prepare-node-config/index.ts b/examples/prepare-node-config/index.ts index f769b482..fe3a2f71 100644 --- a/examples/prepare-node-config/index.ts +++ b/examples/prepare-node-config/index.ts @@ -1,11 +1,10 @@ -import { Chain, http } from 'viem'; +import { Chain, createPublicClient, http } from 'viem'; import { arbitrumSepolia } from 'viem/chains'; import { ChainConfig, createRollupPrepareTransaction, createRollupPrepareTransactionReceipt, prepareNodeConfig, - createOrbitClient, } from '@arbitrum/orbit-sdk'; import { writeFile } from 'fs/promises'; @@ -16,7 +15,7 @@ function getRpcUrl(chain: Chain) { // set the parent chain and create a public client for it const parentChain = arbitrumSepolia; -const parentChainPublicClient = createOrbitClient({ +const parentChainPublicClient = createPublicClient({ chain: parentChain, transport: http(), }); diff --git a/examples/set-valid-keyset/index.ts b/examples/set-valid-keyset/index.ts index f9e04613..154c3f5e 100644 --- a/examples/set-valid-keyset/index.ts +++ b/examples/set-valid-keyset/index.ts @@ -1,8 +1,7 @@ -import { Chain, http } from 'viem'; +import { Chain, createPublicClient, http } from 'viem'; import { privateKeyToAccount } from 'viem/accounts'; import { arbitrumSepolia } from 'viem/chains'; import { setValidKeysetPrepareTransactionRequest } from '@arbitrum/orbit-sdk'; -import { createOrbitClient } from '@arbitrum/orbit-sdk/dist'; function sanitizePrivateKey(privateKey: string): `0x${string}` { if (!privateKey.startsWith('0x')) { @@ -25,10 +24,7 @@ const keyset = // set the parent chain and create a public client for it const parentChain = arbitrumSepolia; -const parentChainOrbitClient = createOrbitClient({ - chain: parentChain, - transport: http(), -}); +const parentChainPublicClient = createPublicClient({ chain: parentChain, transport: http() }); // load the deployer account const deployer = privateKeyToAccount(sanitizePrivateKey(process.env.DEPLOYER_PRIVATE_KEY)); @@ -42,18 +38,16 @@ async function main() { }, keyset, account: deployer.address, - publicClient: parentChainOrbitClient, + publicClient: parentChainPublicClient, }); // sign and send the transaction - const txHash = await parentChainOrbitClient.sendRawTransaction({ + const txHash = await parentChainPublicClient.sendRawTransaction({ serializedTransaction: await deployer.signTransaction(txRequest), }); // wait for the transaction receipt - const txReceipt = await parentChainOrbitClient.waitForTransactionReceipt({ - hash: txHash, - }); + const txReceipt = await parentChainPublicClient.waitForTransactionReceipt({ hash: txHash }); console.log( `Keyset updated in ${getBlockExplorerUrl(parentChain)}/tx/${txReceipt.transactionHash}`, diff --git a/src/createRollup.integration.test.ts b/src/createRollup.integration.test.ts index 8dfe3995..15893e18 100644 --- a/src/createRollup.integration.test.ts +++ b/src/createRollup.integration.test.ts @@ -1,5 +1,5 @@ import { it, expect } from 'vitest'; -import { http, parseGwei, zeroAddress } from 'viem'; +import { createPublicClient, http, parseGwei, zeroAddress } from 'viem'; import { nitroTestnodeL2 } from './chains'; import { generateChainId } from './utils'; @@ -10,14 +10,13 @@ import { createRollupPrepareTransactionRequest } from './createRollupPrepareTran import { createRollupPrepareTransactionReceipt } from './createRollupPrepareTransactionReceipt'; import { getTestPrivateKeyAccount } from './testHelpers'; -import { createOrbitClient } from './orbitClient'; const deployer = getTestPrivateKeyAccount(); const batchPoster = deployer.address; const validators = [deployer.address]; -const orbitClient = createOrbitClient({ +const publicClient = createPublicClient({ chain: nitroTestnodeL2, transport: http(), }); @@ -46,16 +45,16 @@ it(`successfully deploys core contracts through rollup creator`, async () => { validators, }, account: deployer.address, - orbitClient: orbitClient, + publicClient, }); // sign and send the transaction - const txHash = await orbitClient.sendRawTransaction({ + const txHash = await publicClient.sendRawTransaction({ serializedTransaction: await deployer.signTransaction(request), }); // get the transaction - const tx = createRollupPrepareTransaction(await orbitClient.getTransaction({ hash: txHash })); + const tx = createRollupPrepareTransaction(await publicClient.getTransaction({ hash: txHash })); const [arg] = tx.getInputs(); // assert all inputs are correct @@ -69,7 +68,7 @@ it(`successfully deploys core contracts through rollup creator`, async () => { // get the transaction receipt after waiting for the transaction to complete const txReceipt = createRollupPrepareTransactionReceipt( - await orbitClient.waitForTransactionReceipt({ hash: txHash }), + await publicClient.waitForTransactionReceipt({ hash: txHash }), ); expect(txReceipt.status).toEqual('success'); diff --git a/src/createRollup.ts b/src/createRollup.ts index 922cd77f..33731443 100644 --- a/src/createRollup.ts +++ b/src/createRollup.ts @@ -1,6 +1,7 @@ -import { WalletClient, GetFunctionArgs } from 'viem'; +import { PublicClient, WalletClient, GetFunctionArgs } from 'viem'; import { rollupCreator } from './contracts'; +import { validParentChainId } from './types/ParentChain'; import { defaults } from './createRollupDefaults'; import { createRollupGetCallValue } from './createRollupGetCallValue'; import { createRollupGetMaxDataSize } from './createRollupGetMaxDataSize'; @@ -11,7 +12,7 @@ import { import { isCustomFeeTokenAddress } from './utils/isCustomFeeTokenAddress'; import { ChainConfig } from './types/ChainConfig'; import { isAnyTrustChainConfig } from './utils/isAnyTrustChainConfig'; -import type { OrbitClient } from './orbitClient'; +import { getValidChainId } from './utils/getters'; export type CreateRollupFunctionInputs = GetFunctionArgs< typeof rollupCreator.abi, @@ -25,16 +26,20 @@ export type CreateRollupParams = Pick { - const chainId = orbitClient.getValidChainId(); + const chainId = getValidChainId(publicClient); const account = walletClient.account?.address; + if (!validParentChainId(chainId)) { + throw new Error('chainId is undefined'); + } + if (typeof account === 'undefined') { throw new Error('account is undefined'); } @@ -50,8 +55,8 @@ export async function createRollup({ const maxDataSize = createRollupGetMaxDataSize(chainId); const paramsWithDefaults = { ...defaults, ...params, maxDataSize }; - const { request } = await orbitClient.simulateContract({ - address: orbitClient.getRollupCreatorAddress(), + const { request } = await publicClient.simulateContract({ + address: rollupCreator.address[chainId], abi: rollupCreator.abi, functionName: 'createRollup', args: [paramsWithDefaults], @@ -60,7 +65,7 @@ export async function createRollup({ }); const hash = await walletClient.writeContract(request); - const txReceipt = await orbitClient.waitForTransactionReceipt({ hash }); + const txReceipt = await publicClient.waitForTransactionReceipt({ hash }); return createRollupPrepareTransactionReceipt(txReceipt); } diff --git a/src/createRollupEnoughCustomFeeTokenAllowance.ts b/src/createRollupEnoughCustomFeeTokenAllowance.ts index 3dd434e6..7ff05eb7 100644 --- a/src/createRollupEnoughCustomFeeTokenAllowance.ts +++ b/src/createRollupEnoughCustomFeeTokenAllowance.ts @@ -1,25 +1,27 @@ -import { Address } from 'viem'; +import { Address, PublicClient } from 'viem'; +import { rollupCreator } from './contracts'; +import { validParentChainId } from './types/ParentChain'; import { fetchAllowance } from './utils/erc20'; import { deterministicFactoriesDeploymentEstimatedFees } from './constants'; -import { OrbitClient } from './orbitClient'; +import { getRollupCreatorAddress, getValidChainId } from './utils/getters'; export type CreateRollupEnoughCustomFeeTokenAllowanceParams = { nativeToken: Address; account: Address; - orbitClient: OrbitClient; + publicClient: PublicClient; }; export async function createRollupEnoughCustomFeeTokenAllowance({ nativeToken, account, - orbitClient, + publicClient, }: CreateRollupEnoughCustomFeeTokenAllowanceParams) { const allowance = await fetchAllowance({ address: nativeToken, owner: account, - spender: orbitClient.getRollupCreatorAddress(), - publicClient: orbitClient, + spender: getRollupCreatorAddress(publicClient), + publicClient, }); return allowance >= deterministicFactoriesDeploymentEstimatedFees; diff --git a/src/createRollupPrepareCustomFeeTokenApprovalTransactionRequest.ts b/src/createRollupPrepareCustomFeeTokenApprovalTransactionRequest.ts index 371a6049..0093f96a 100644 --- a/src/createRollupPrepareCustomFeeTokenApprovalTransactionRequest.ts +++ b/src/createRollupPrepareCustomFeeTokenApprovalTransactionRequest.ts @@ -1,30 +1,30 @@ -import { Address, maxInt256 } from 'viem'; +import { Address, PublicClient, maxInt256 } from 'viem'; -import { OrbitClient } from './orbitClient'; import { approvePrepareTransactionRequest } from './utils/erc20'; +import { validParentChainId } from './types/ParentChain'; +import { rollupCreator } from './contracts'; +import { getRollupCreatorAddress, getValidChainId } from './utils/getters'; export type CreateRollupPrepareCustomFeeTokenApprovalTransactionRequestParams = { amount?: bigint; nativeToken: Address; account: Address; - orbitClient: OrbitClient; + publicClient: PublicClient; }; export async function createRollupPrepareCustomFeeTokenApprovalTransactionRequest({ amount = maxInt256, nativeToken, account, - orbitClient, + publicClient, }: CreateRollupPrepareCustomFeeTokenApprovalTransactionRequestParams) { - const chainId = orbitClient.getValidChainId(); - const spender = orbitClient.getRollupCreatorAddress(); - + const chainId = getValidChainId(publicClient); const request = await approvePrepareTransactionRequest({ address: nativeToken, owner: account, - spender, + spender: getRollupCreatorAddress(publicClient), amount, - publicClient: orbitClient, + publicClient, }); return { ...request, chainId }; diff --git a/src/createRollupPrepareTransactionRequest.ts b/src/createRollupPrepareTransactionRequest.ts index e6161a2c..d6ab160a 100644 --- a/src/createRollupPrepareTransactionRequest.ts +++ b/src/createRollupPrepareTransactionRequest.ts @@ -1,14 +1,15 @@ -import { Address, encodeFunctionData } from 'viem'; +import { Address, PublicClient, encodeFunctionData } from 'viem'; import { CreateRollupFunctionInputs, CreateRollupParams } from './createRollup'; import { defaults } from './createRollupDefaults'; import { createRollupGetCallValue } from './createRollupGetCallValue'; import { createRollupGetMaxDataSize } from './createRollupGetMaxDataSize'; import { rollupCreator } from './contracts'; +import { validParentChainId } from './types/ParentChain'; import { isCustomFeeTokenAddress } from './utils/isCustomFeeTokenAddress'; import { ChainConfig } from './types/ChainConfig'; import { isAnyTrustChainConfig } from './utils/isAnyTrustChainConfig'; -import { OrbitClient } from './orbitClient'; +import { getRollupCreatorAddress, getValidChainId } from './utils/getters'; function createRollupEncodeFunctionData(args: CreateRollupFunctionInputs) { return encodeFunctionData({ @@ -21,13 +22,13 @@ function createRollupEncodeFunctionData(args: CreateRollupFunctionInputs) { export async function createRollupPrepareTransactionRequest({ params, account, - orbitClient, + publicClient, }: { params: CreateRollupParams; account: Address; - orbitClient: OrbitClient; + publicClient: PublicClient; }) { - const chainId = orbitClient.getValidChainId(); + const chainId = getValidChainId(publicClient); const chainConfig: ChainConfig = JSON.parse(params.config.chainConfig); @@ -40,9 +41,9 @@ export async function createRollupPrepareTransactionRequest({ const maxDataSize = createRollupGetMaxDataSize(chainId); const paramsWithDefaults = { ...defaults, ...params, maxDataSize }; - const request = await orbitClient.prepareTransactionRequest({ - chain: orbitClient.chain, - to: orbitClient.getRollupCreatorAddress(), + const request = await publicClient.prepareTransactionRequest({ + chain: publicClient.chain, + to: getRollupCreatorAddress(publicClient), data: createRollupEncodeFunctionData([paramsWithDefaults]), value: createRollupGetCallValue(paramsWithDefaults), account, diff --git a/src/index.ts b/src/index.ts index 7097b6e6..0d8e8142 100644 --- a/src/index.ts +++ b/src/index.ts @@ -40,7 +40,6 @@ import { ParentChain, ParentChainId } from './types/ParentChain'; import { NodeConfig, NodeConfigChainInfoJson } from './types/NodeConfig'; import { prepareNodeConfig } from './prepareNodeConfig'; import * as utils from './utils'; -import { createOrbitClient, OrbitClient } from './orbitClient'; export { createRollup, @@ -77,7 +76,4 @@ export { NodeConfigChainInfoJson, prepareNodeConfig, utils, - createOrbitClient, }; - -export type { OrbitClient }; diff --git a/src/orbitClient.ts b/src/orbitClient.ts deleted file mode 100644 index 3440c230..00000000 --- a/src/orbitClient.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { PublicClient, PublicClientConfig, createPublicClient } from 'viem'; -import { rollupCreator } from './contracts'; -import { ParentChainId, validParentChainId } from './types/ParentChain'; - -export interface OrbitClient extends PublicClient { - getRollupCreatorAddress(): `0x${string}`; - getValidChainId(): ParentChainId; -} - -const validateClientChainId = (client: PublicClient) => { - const chainId = client.chain?.id; - if (!validParentChainId(chainId)) { - throw new Error('chainId is undefined'); - } - return chainId; -}; - -export function createOrbitClient({ chain, transport }: PublicClientConfig): OrbitClient { - return createPublicClient({ - chain, - transport, - }).extend((client: PublicClient) => ({ - getRollupCreatorAddress: () => { - const chainId = validateClientChainId(client); - return rollupCreator.address[chainId]; - }, - getValidChainId: () => validateClientChainId(client), - })); -} diff --git a/src/utils/getters.ts b/src/utils/getters.ts new file mode 100644 index 00000000..c12e2a9d --- /dev/null +++ b/src/utils/getters.ts @@ -0,0 +1,19 @@ +import { PublicClient } from 'viem'; +import { rollupCreator } from '../contracts'; +import { validParentChainId } from '../types/ParentChain'; + +export const getValidChainId = (client: PublicClient) => { + const chainId = client.chain?.id; + if (!validParentChainId(chainId)) { + throw new Error('chainId is undefined'); + } + return chainId; +}; + +export const getRollupCreatorAddress = (client: PublicClient) => { + const chainId = getValidChainId(client); + if (!rollupCreator.address[chainId]) { + throw new Error(`Invalid chainId or chainId not supported: ${chainId}`); + } + return rollupCreator.address[chainId]; +};