Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Nov 19, 2024
1 parent f42cb70 commit 48555db
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 572 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"prepare": "yarn run gen:abi",
"gen:abi": "node ./scripts/genAbi.js",
"gen:network": "ts-node ./scripts/genNetwork.ts",
"gen:wagmi": "wagmi generate",
"prepublishOnly": "yarn build && yarn format",
"preversion": "yarn lint",
"prebuild": "yarn gen:abi",
Expand Down Expand Up @@ -67,7 +66,6 @@
"@typescript-eslint/eslint-plugin": "^5.14.0",
"@typescript-eslint/eslint-plugin-tslint": "^5.27.1",
"@typescript-eslint/parser": "^5.14.0",
"@wagmi/cli": "^2.1.18",
"audit-ci": "^6.3.0",
"axios": "^1.7.4",
"chai": "^4.2.0",
Expand Down
44 changes: 44 additions & 0 deletions scripts/testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
isArbitrumNetworkWithCustomFeeToken,
} from '../tests/integration/custom-fee-token/customFeeTokenTestHelpers'
import { fundParentSigner } from '../tests/integration/testHelpers'
import { Chain } from 'viem'

dotenv.config()

Expand Down Expand Up @@ -85,6 +86,8 @@ export const testSetup = async (): Promise<{
inboxTools: InboxTools
parentDeployer: Signer
childDeployer: Signer
localEthChain: Chain
localArbChain: Chain
}> => {
const ethProvider = new JsonRpcProvider(config.ethUrl)
const arbProvider = new JsonRpcProvider(config.arbUrl)
Expand Down Expand Up @@ -113,6 +116,23 @@ export const testSetup = async (): Promise<{

assertArbitrumNetworkHasTokenBridge(setChildChain)

// Generate Viem chains using the network data we already have
const localEthChain = generateViemChain(
{
chainId: setChildChain.parentChainId,
name: 'EthLocal',
},
config.ethUrl
)

const localArbChain = generateViemChain(
{
chainId: setChildChain.chainId,
name: setChildChain.name,
},
config.arbUrl
)

const erc20Bridger = new Erc20Bridger(setChildChain)
const adminErc20Bridger = new AdminErc20Bridger(setChildChain)
const ethBridger = new EthBridger(setChildChain)
Expand All @@ -136,6 +156,8 @@ export const testSetup = async (): Promise<{
inboxTools,
parentDeployer,
childDeployer,
localEthChain,
localArbChain,
}
}

Expand All @@ -153,3 +175,25 @@ export function getLocalNetworksFromFile(): {

return { l2Network: localL2, l3Network: localL3 }
}

function generateViemChain(
networkData: {
chainId: number
name: string
},
rpcUrl: string
): Chain {
return {
id: networkData.chainId,
name: networkData.name,
nativeCurrency: {
decimals: 18,
name: 'Ether',
symbol: 'ETH',
},
rpcUrls: {
default: { http: [rpcUrl] },
public: { http: [rpcUrl] },
}
} as const satisfies Chain
}
45 changes: 24 additions & 21 deletions src/experimental/actions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { BigNumber } from 'ethers'
import {
Account,
Address,
Client,
type PublicClient,
TransactionRequest,
} from 'viem'
import { Account, Address, Chain, PublicClient, TransactionRequest } from 'viem'
import { EthBridger } from '../lib/assetBridger/ethBridger'
import { transformPublicClientToProvider } from './transformViemToEthers'

Expand All @@ -14,13 +8,24 @@ export type PrepareDepositEthParameters = {
account: Account | Address
}

export type PrepareDepositEthToParameters = PrepareDepositEthParameters & {
export type PrepareDepositEthToParameters = {
amount: bigint
account: Address
destinationAddress: Address
parentPublicClient: PublicClient
}

export async function prepareDepositEthTransaction(
client: Client,
export type ArbitrumDepositActions = {
prepareDepositEthTransaction: (
params: PrepareDepositEthParameters
) => Promise<TransactionRequest>
prepareDepositEthToTransaction: (
params: PrepareDepositEthToParameters
) => Promise<TransactionRequest>
}

async function prepareDepositEthTransaction(
client: PublicClient,
{ amount, account }: PrepareDepositEthParameters
): Promise<TransactionRequest> {
const provider = transformPublicClientToProvider(client)
Expand All @@ -37,8 +42,8 @@ export async function prepareDepositEthTransaction(
}
}

export async function prepareDepositEthToTransaction(
client: Client,
async function prepareDepositEthToTransaction(
client: PublicClient,
{
amount,
account,
Expand All @@ -53,7 +58,7 @@ export async function prepareDepositEthToTransaction(
const request = await ethBridger.getDepositToRequest({
amount: BigNumber.from(amount),
destinationAddress,
from: typeof account === 'string' ? account : account.address,
from: account,
parentProvider,
childProvider,
})
Expand All @@ -66,12 +71,10 @@ export async function prepareDepositEthToTransaction(
}

export function arbitrumDepositActions() {
return function (client: Client) {
return {
prepareDepositEthTransaction: (args: PrepareDepositEthParameters) =>
prepareDepositEthTransaction(client, args),
prepareDepositEthToTransaction: (args: PrepareDepositEthToParameters) =>
prepareDepositEthToTransaction(client, args),
}
}
return (client: PublicClient): ArbitrumDepositActions => ({
prepareDepositEthTransaction: params =>
prepareDepositEthTransaction(client, params),
prepareDepositEthToTransaction: params =>
prepareDepositEthToTransaction(client, params),
})
}
39 changes: 0 additions & 39 deletions src/experimental/chains.ts

This file was deleted.

13 changes: 4 additions & 9 deletions src/experimental/createArbitrumClient.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { Chain, PublicClient, createPublicClient, http } from 'viem'
import { ArbitrumNetwork } from '../lib/dataEntities/networks'
import { arbitrumDepositActions } from './actions'

export type ArbitrumChain = Chain & ArbitrumNetwork
import { arbitrumDepositActions, ArbitrumDepositActions } from './actions'

export type ArbitrumClients = {
parentPublicClient: PublicClient
childPublicClient: PublicClient & ReturnType<typeof arbitrumDepositActions>
childPublicClient: PublicClient & ArbitrumDepositActions
}
export type ChildChainPublicClient = PublicClient &
ReturnType<typeof arbitrumDepositActions>

export type CreateArbitrumClientParams = {
parentChain: Chain
childChain: ArbitrumChain
childChain: Chain
parentRpcUrl?: string
childRpcUrl?: string
}
Expand All @@ -37,5 +32,5 @@ export function createArbitrumClient({
return {
parentPublicClient,
childPublicClient,
} as any as ArbitrumClients
}
}
34 changes: 13 additions & 21 deletions tests/integration/arbitrumDepositActions.test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
import { expect } from 'chai'
import { createWalletClient, http, parseEther } from 'viem'
import { createWalletClient, http, parseEther, type Chain } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { config, testSetup } from '../../scripts/testSetup'
import { localEthChain, localArbChain } from '../../src/experimental/chains'
import { createArbitrumClient } from '../../src/experimental/createArbitrumClient'

describe('arbitrumDepositActions', function () {
let localEthChain: Chain
let localArbChain: Chain

before(async function () {
await testSetup()
const setup = await testSetup()
localEthChain = setup.localEthChain
localArbChain = setup.localArbChain
})

it('deposits ETH from parent to child', async function () {
const account = privateKeyToAccount(`0x${config.ethKey}` as `0x${string}`)
const depositAmount = parseEther('0.01')

// Create parent wallet client
const parentWalletClient = createWalletClient({
account,
chain: localEthChain,
transport: http(config.ethUrl),
})

// Create public clients using helper
const { parentPublicClient, childPublicClient } = createArbitrumClient({
parentChain: localEthChain,
// @ts-expect-error
childChain: localArbChain,
parentRpcUrl: config.ethUrl,
childRpcUrl: config.arbUrl,
})

// Get initial child balance
const initialBalance = await childPublicClient.getBalance({
address: account.address,
})

// Prepare and send deposit transaction
// @ts-expect-error
const request = await childPublicClient.prepareDepositEthTransaction({
amount: depositAmount,
account,
Expand All @@ -45,16 +43,16 @@ describe('arbitrumDepositActions', function () {
const hash = await parentWalletClient.sendTransaction({
...request,
chain: localEthChain,
})
account,
kzg: undefined,
} as const)

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

expect(receipt.status).to.equal('success')

// Wait for child balance to increase
let finalBalance = initialBalance
let attempts = 0
const maxAttempts = 12
Expand Down Expand Up @@ -84,29 +82,23 @@ describe('arbitrumDepositActions', function () {
'0x1234567890123456789012345678901234567890' as `0x${string}`
const depositAmount = parseEther('0.01')

// Create parent wallet client
const parentWalletClient = createWalletClient({
account,
chain: localEthChain,
transport: http(config.ethUrl),
})

// Create public clients using helper
const { parentPublicClient, childPublicClient } = createArbitrumClient({
parentChain: localEthChain,
// @ts-expect-error
childChain: localArbChain,
parentRpcUrl: config.ethUrl,
childRpcUrl: config.arbUrl,
})

// Get initial destination balance
const initialBalance = await childPublicClient.getBalance({
address: destinationAddress,
})

// Prepare and send deposit transaction
// @ts-expect-error
const request = await childPublicClient.prepareDepositEthToTransaction({
amount: depositAmount,
account: account.address,
Expand All @@ -117,16 +109,16 @@ describe('arbitrumDepositActions', function () {
const hash = await parentWalletClient.sendTransaction({
...request,
chain: localEthChain,
})
account,
kzg: undefined,
} as const)

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

expect(receipt.status).to.equal('success')

// Wait for child balance to increase
let finalBalance = initialBalance
let attempts = 0
const maxAttempts = 12
Expand Down
Loading

0 comments on commit 48555db

Please sign in to comment.