From f7067c25d91a3e46a13d0707b6ccff1f5b130aae Mon Sep 17 00:00:00 2001 From: Doug Lance <4741454+douglance@users.noreply.github.com> Date: Mon, 18 Nov 2024 16:04:51 -0500 Subject: [PATCH] adds createArbitrumClient function --- .../arbitrumDeposit/createArbitrumClient.ts | 42 +++++++++ tests/integration/arbitrumDeposit.test.ts | 87 +++++++++---------- 2 files changed, 85 insertions(+), 44 deletions(-) create mode 100644 src/experimental/arbitrumDeposit/createArbitrumClient.ts diff --git a/src/experimental/arbitrumDeposit/createArbitrumClient.ts b/src/experimental/arbitrumDeposit/createArbitrumClient.ts new file mode 100644 index 000000000..0fb8568e6 --- /dev/null +++ b/src/experimental/arbitrumDeposit/createArbitrumClient.ts @@ -0,0 +1,42 @@ +import { Chain, PublicClient, createPublicClient, http } from 'viem' +import { ArbitrumNetwork } from '../../lib/dataEntities/networks' +import { arbitrumDepositActions } from './actions' + +export type ArbitrumChain = Chain & ArbitrumNetwork + +export type ArbitrumClients = { + parentChainPublicClient: PublicClient + childChainPublicClient: PublicClient & + ReturnType +} +export type ChildChainPublicClient = PublicClient & + ReturnType + +export type CreateArbitrumClientParams = { + parentChain: Chain + childChain: ArbitrumChain + parentRpcUrl?: string + childRpcUrl?: string +} + +export function createArbitrumClient({ + parentChain, + childChain, + parentRpcUrl, + childRpcUrl, +}: CreateArbitrumClientParams): ArbitrumClients { + const parentChainPublicClient = createPublicClient({ + chain: parentChain, + transport: http(parentRpcUrl || parentChain.rpcUrls.default.http[0]), + }) + + const childChainPublicClient = createPublicClient({ + chain: childChain, + transport: http(childRpcUrl || childChain.rpcUrls.default.http[0]), + }).extend(arbitrumDepositActions()) + + return { + parentChainPublicClient, + childChainPublicClient, + } as any as ArbitrumClients +} diff --git a/tests/integration/arbitrumDeposit.test.ts b/tests/integration/arbitrumDeposit.test.ts index f448b6397..e99df74b8 100644 --- a/tests/integration/arbitrumDeposit.test.ts +++ b/tests/integration/arbitrumDeposit.test.ts @@ -1,15 +1,9 @@ import { expect } from 'chai' -import { - createWalletClient, - createPublicClient, - http, - parseEther, - type PublicClient, -} from 'viem' +import { createWalletClient, http, parseEther } from 'viem' import { privateKeyToAccount } from 'viem/accounts' import { config, testSetup } from '../../scripts/testSetup' -import { arbitrumDepositActions } from '../../src/experimental/arbitrumDeposit/actions' import { localEthChain, localArbChain } from '../../src/experimental/chains' +import { createArbitrumClient } from '../../src/experimental/arbitrumDeposit/createArbitrumClient' describe('arbitrumDepositActions', function () { before(async function () { @@ -20,39 +14,42 @@ describe('arbitrumDepositActions', function () { const account = privateKeyToAccount(`0x${config.ethKey}` as `0x${string}`) const depositAmount = parseEther('0.01') - // Create L1 clients + // Create L1 wallet client const parentWalletClient = createWalletClient({ account, chain: localEthChain, transport: http(config.ethUrl), }) - const parentPublicClient = createPublicClient({ - chain: localEthChain, - transport: http(config.ethUrl), - }) - - // Create L2 client and extend with deposit actions - const childPublicClient = createPublicClient({ - chain: localArbChain, - transport: http(config.arbUrl), - }).extend(arbitrumDepositActions()) + // Create public clients using helper + const { parentChainPublicClient, childChainPublicClient } = + createArbitrumClient({ + parentChain: localEthChain, + // @ts-expect-error + childChain: localArbChain, + parentRpcUrl: config.ethUrl, + childRpcUrl: config.arbUrl, + }) // Get initial L2 balance - const initialBalance = await childPublicClient.getBalance({ + const initialBalance = await childChainPublicClient.getBalance({ address: account.address, }) // Prepare and send deposit transaction - const request = await childPublicClient.prepareDepositEthTransaction({ + // @ts-expect-error + const request = await childChainPublicClient.prepareDepositEthTransaction({ amount: depositAmount, account, }) - const hash = await parentWalletClient.sendTransaction(request) + const hash = await parentWalletClient.sendTransaction({ + ...request, + chain: localEthChain, + }) // Wait for L1 transaction - const receipt = await parentPublicClient.waitForTransactionReceipt({ + const receipt = await parentChainPublicClient.waitForTransactionReceipt({ hash, }) @@ -66,7 +63,7 @@ describe('arbitrumDepositActions', function () { while (attempts < maxAttempts) { await new Promise(resolve => setTimeout(resolve, 3000)) - const currentBalance = await childPublicClient.getBalance({ + const currentBalance = await childChainPublicClient.getBalance({ address: account.address, }) @@ -88,36 +85,38 @@ describe('arbitrumDepositActions', function () { '0x1234567890123456789012345678901234567890' as `0x${string}` const depositAmount = parseEther('0.01') - // Create L1 clients + // Create L1 wallet client const parentWalletClient = createWalletClient({ account, chain: localEthChain, transport: http(config.ethUrl), }) - const parentPublicClient = createPublicClient({ - chain: localEthChain, - transport: http(config.ethUrl), - }) - - // Create L2 client and extend with deposit actions - const childPublicClient = createPublicClient({ - chain: localArbChain, - transport: http(config.arbUrl), - }).extend(arbitrumDepositActions()) + // Create public clients using helper + const { parentChainPublicClient, childChainPublicClient } = + createArbitrumClient({ + parentChain: localEthChain, + // @ts-expect-error + childChain: localArbChain, + parentRpcUrl: config.ethUrl, + childRpcUrl: config.arbUrl, + }) // Get initial destination balance - const initialBalance = await childPublicClient.getBalance({ + const initialBalance = await childChainPublicClient.getBalance({ address: destinationAddress, }) // Prepare and send deposit transaction - const request = await childPublicClient.prepareDepositEthToTransaction({ - amount: depositAmount, - account: account.address, - destinationAddress, - parentPublicClient, - }) + // @ts-expect-error + const request = await childChainPublicClient.prepareDepositEthToTransaction( + { + amount: depositAmount, + account: account.address, + destinationAddress, + parentPublicClient: parentChainPublicClient, + } + ) const hash = await parentWalletClient.sendTransaction({ ...request, @@ -125,7 +124,7 @@ describe('arbitrumDepositActions', function () { }) // Wait for L1 transaction - const receipt = await parentPublicClient.waitForTransactionReceipt({ + const receipt = await parentChainPublicClient.waitForTransactionReceipt({ hash, }) @@ -139,7 +138,7 @@ describe('arbitrumDepositActions', function () { while (attempts < maxAttempts) { await new Promise(resolve => setTimeout(resolve, 3000)) - const currentBalance = await childPublicClient.getBalance({ + const currentBalance = await childChainPublicClient.getBalance({ address: destinationAddress, })