Skip to content

Commit

Permalink
reverts to using bare functions
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Dec 27, 2023
1 parent e0b2cf0 commit 333655d
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 101 deletions.
5 changes: 2 additions & 3 deletions examples/create-rollup-custom-fee-token/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -8,7 +8,6 @@ import {
createRollupPrepareCustomFeeTokenApprovalTransactionRequest,
createRollupPrepareTransactionRequest,
createRollupPrepareTransactionReceipt,
createOrbitClient,
} from '@arbitrum/orbit-sdk';
import { generateChainId } from '@arbitrum/orbit-sdk/utils';

Expand Down Expand Up @@ -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(),
});
Expand Down
19 changes: 6 additions & 13 deletions examples/create-rollup-eth/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Chain, http } from 'viem';
import { Chain, createPublicClient, http } from 'viem';
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import { arbitrumSepolia } from 'viem/chains';
import {
createRollupPrepareConfig,
prepareChainConfig,
createRollupPrepareTransactionRequest,
createRollupPrepareTransactionReceipt,
createOrbitClient,
} from '@arbitrum/orbit-sdk';
import { generateChainId } from '@arbitrum/orbit-sdk/utils';

Expand Down Expand Up @@ -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));
Expand All @@ -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
Expand All @@ -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}`);
Expand Down
5 changes: 2 additions & 3 deletions examples/prepare-node-config/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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(),
});
Expand Down
16 changes: 5 additions & 11 deletions examples/set-valid-keyset/index.ts
Original file line number Diff line number Diff line change
@@ -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')) {
Expand All @@ -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));
Expand All @@ -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}`,
Expand Down
13 changes: 6 additions & 7 deletions src/createRollup.integration.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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(),
});
Expand Down Expand Up @@ -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
Expand All @@ -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');
Expand Down
21 changes: 13 additions & 8 deletions src/createRollup.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand All @@ -25,16 +26,20 @@ export type CreateRollupParams = Pick<CreateRollupFunctionInputs[0], RequiredKey

export async function createRollup({
params,
orbitClient,
publicClient,
walletClient,
}: {
params: CreateRollupParams;
orbitClient: OrbitClient;
publicClient: PublicClient;
walletClient: WalletClient;
}): Promise<CreateRollupTransactionReceipt> {
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');
}
Expand All @@ -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],
Expand All @@ -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);
}
14 changes: 8 additions & 6 deletions src/createRollupEnoughCustomFeeTokenAllowance.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 };
Expand Down
17 changes: 9 additions & 8 deletions src/createRollupPrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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);

Expand All @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -77,7 +76,4 @@ export {
NodeConfigChainInfoJson,
prepareNodeConfig,
utils,
createOrbitClient,
};

export type { OrbitClient };
Loading

0 comments on commit 333655d

Please sign in to comment.