Skip to content

Commit

Permalink
adds createArbitrumClient function
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Nov 18, 2024
1 parent f0c6ca2 commit f7067c2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 44 deletions.
42 changes: 42 additions & 0 deletions src/experimental/arbitrumDeposit/createArbitrumClient.ts
Original file line number Diff line number Diff line change
@@ -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<typeof arbitrumDepositActions>
}
export type ChildChainPublicClient = PublicClient &
ReturnType<typeof arbitrumDepositActions>

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
}
87 changes: 43 additions & 44 deletions tests/integration/arbitrumDeposit.test.ts
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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,
})

Expand All @@ -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,
})

Expand All @@ -88,44 +85,46 @@ 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,
chain: localEthChain,
})

// Wait for L1 transaction
const receipt = await parentPublicClient.waitForTransactionReceipt({
const receipt = await parentChainPublicClient.waitForTransactionReceipt({
hash,
})

Expand All @@ -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,
})

Expand Down

0 comments on commit f7067c2

Please sign in to comment.