Skip to content

Commit

Permalink
refactor folders and decorators to be L1 and L2 specific (#40)
Browse files Browse the repository at this point in the history
* refactor folders and decorators to be L1 and L2 specific

* file new functions and fix test
  • Loading branch information
wilsoncusack authored Aug 31, 2023
1 parent c481a35 commit 8c06655
Show file tree
Hide file tree
Showing 25 changed files with 98 additions and 63 deletions.
8 changes: 4 additions & 4 deletions src/_test/live.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { test, expect } from 'vitest'
import { Hex, createPublicClient, createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { goerli, mainnet } from '@wagmi/chains'
import { walletOpStackActions } from '../decorators/walletOpStack'
import { baseGoerli } from '@roninjin10/rollup-chains'
import { publicOpStackActions } from '../decorators/publicOpStack'
import { walletL1OpStackActions } from '../decorators/walletL1OpStackActions'
import { publicL1OpStackActions } from '../decorators/publicL1OpStackActions'

test('correctly retrieves L2 hash', async () => {
return
Expand All @@ -18,7 +18,7 @@ test('correctly retrieves L2 hash', async () => {
account,
chain: goerli,
transport: http(),
}).extend(walletOpStackActions)
}).extend(walletL1OpStackActions)

const depositHash = await walletClient.writeUnsafeDepositTransaction({
toChain: baseGoerli,
Expand All @@ -37,7 +37,7 @@ test('correctly retrieves L2 hash', async () => {
const mainnetPublicClient = createPublicClient({
chain: goerli,
transport: http(),
}).extend(publicOpStackActions)
}).extend(publicL1OpStackActions)

await mainnetPublicClient.waitForTransactionReceipt({ hash: depositHash })

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bench, describe } from 'vitest'
import { ethersProvider } from '../../_test/bench'
import { publicClient } from '../../_test/utils'
import { ethersProvider } from '../../../_test/bench'
import { publicClient } from '../../../_test/utils'
import { getL2HashesForDepositTx } from './getL2HashesForDepositTx'
import { DepositTx } from '@eth-optimism/core-utils'
import { ethers } from 'ethers'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from 'vitest'
import { publicClient } from '../../_test/utils'
import { publicClient } from '../../../_test/utils'
import { getL2HashesForDepositTx } from './getL2HashesForDepositTx'

test('correctly retrieves L2 hash', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type PublicClient, Chain, Transport, Hash } from 'viem'
import { getDepositEventsInfoFromTxReceipt } from '../../utils/getDepositEventsInfoFromTxReceipt'
import { getL2HashFromL1DepositInfo } from '../../utils/getL2HashFromL1DepositInfo'
import { getDepositEventsInfoFromTxReceipt } from '../../../utils/getDepositEventsInfoFromTxReceipt'
import { getL2HashFromL1DepositInfo } from '../../../utils/getL2HashFromL1DepositInfo'

export type GetL2HashesForDepositTxParamters = {
l1TxHash: Hash
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, expect } from 'vitest'
import { getOutputForL2Block } from './getOutputForL2Block'
import { base } from '@roninjin10/rollup-chains'
import { publicClient } from '../../_test/utils'
import { publicClient } from '../../../_test/utils'

test('retrieves correctly', async () => {
const result = await getOutputForL2Block(publicClient, {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { expect, test } from 'vitest'
import { publicClient, testClient } from '../../_test/utils'
import { publicClient, testClient } from '../../../_test/utils'
import { base } from '@roninjin10/rollup-chains'
import { simulateDepositERC20 } from './simulateDepositERC20'
import { writeContract, readContract } from 'viem/actions'
import { writeContract, readContract, simulateContract } from 'viem/actions'
import { erc20ABI } from 'wagmi'

const USDCL1 = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
Expand All @@ -15,8 +15,10 @@ test('default', async () => {
})
await testClient.setBalance({
address: zenaddress,
value: 1000000000000000000n,
value: 10n ** 22n,
})


await writeContract(testClient, {
address: USDCL1,
abi: erc20ABI,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { expect, test } from 'vitest'
import { walletClient, publicClient } from '../../_test/utils'
import { walletClient, publicClient } from '../../../_test/utils'
import { base } from '@roninjin10/rollup-chains'
import { simulateDepositETH } from './simulateDepositETH'
import { writeContract } from 'viem/actions'
import { accounts } from '../../_test/constants'
import { accounts } from '../../../_test/constants'

test('default', async () => {
const { request } = await simulateDepositETH(publicClient, {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { bench, describe } from 'vitest'
import { createPublicClient, http } from 'viem'
import { getWithdrawalMessages } from './getWithdrawalMessages'
import { getOutputForL2Block } from './getOutputForL2Block'
import { getProveArgsForWithdrawal } from './getProveArgsForWithdrawal'
import { getOutputForL2Block } from '../L1/getOutputForL2Block'
import { getProveWithdrawalTransactionArgs } from './getProveWithdrawalTransactionArgs'
import { base } from '@roninjin10/rollup-chains'
import { mainnet } from '@wagmi/chains'
import { providers } from 'ethers'
Expand Down Expand Up @@ -33,7 +33,7 @@ describe('Computes L1 prove args from L2 tx hash', () => {
rollup: base,
})

await getProveArgsForWithdrawal(client, {
await getProveWithdrawalTransactionArgs(client, {
message: withdrawalMessages.messages[0],
output: output,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { test, expect } from 'vitest'
import { createPublicClient, http } from 'viem'
import { getWithdrawalMessages } from './getWithdrawalMessages'
import { base } from '@roninjin10/rollup-chains'
import { getOutputForL2Block } from './getOutputForL2Block'
import { getProveArgsForWithdrawal } from './getProveArgsForWithdrawal'
import { getOutputForL2Block } from '../L1/getOutputForL2Block'
import { getProveWithdrawalTransactionArgs } from './getProveWithdrawalTransactionArgs'
import { mainnet } from '@wagmi/chains'

// from OP SDK getMessageBedrockOutput
Expand Down Expand Up @@ -49,7 +49,8 @@ test('correctly generates args', async () => {
rollup: base,
})

const args = await getProveArgsForWithdrawal(client, {
// TODO(wilson): We should simplify these test to not require so much setup ^
const args = await getProveWithdrawalTransactionArgs(client, {
message: withdrawalMessages.messages[0],
output: output,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Chain, Hex, PublicClient, Transport, toHex } from 'viem'
import { getBlock } from 'viem/actions'
import { getWithdrawalMessageStorageSlot } from '../../utils/getWithdrawalMessageStorageSlot'
import { getWithdrawalMessageStorageSlot } from '../../../utils/getWithdrawalMessageStorageSlot'
import { MessagePassedEvent } from './getWithdrawalMessages'
import { getProof } from './getProof'
import { GetOutputForL2BlockReturnType } from './getOutputForL2Block'
import { getProof } from '../getProof'
import { GetOutputForL2BlockReturnType } from '../L1/getOutputForL2Block'

export type OutputRootProof = {
version: Hex
Expand All @@ -15,12 +15,12 @@ export type OutputRootProof = {
const L2_TO_L1_MESSAGE_PASSER = '0x4200000000000000000000000000000000000016'
const OUTPUT_ROOT_PROOF_VERSION = 0n

export type GetProofsForWithdrawalParams = {
export type getProveWithdrawalTransactionArgsParams = {
message: MessagePassedEvent
output: GetOutputForL2BlockReturnType
}

export type GetProofsForWithdrawalReturnType = {
export type getProveWithdrawalTransactionArgsReturnType = {
withdrawalTransaction: Omit<MessagePassedEvent, 'withdrawalHash'>
outputRootProof: OutputRootProof
withdrawalProof: Hex[]
Expand All @@ -35,12 +35,12 @@ export type GetProofsForWithdrawalReturnType = {
* @param output the output proposal and index for the L2 block that contained the withdrawal transaction
* @returns The arguments required by proveWithdrawalTransaction
*/
export async function getProveArgsForWithdrawal<
export async function getProveWithdrawalTransactionArgs<
TChain extends Chain | undefined,
>(
client: PublicClient<Transport, TChain>,
{ message, output }: GetProofsForWithdrawalParams,
): Promise<GetProofsForWithdrawalReturnType> {
{ message, output }: getProveWithdrawalTransactionArgsParams,
): Promise<getProveWithdrawalTransactionArgsReturnType> {
const slot = getWithdrawalMessageStorageSlot(message.withdrawalHash)
const block = await getBlock(client, {
blockNumber: output.proposal.l2BlockNumber,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from 'vitest'
import { rollupPublicClient } from '../../_test/utils'
import { rollupPublicClient } from '../../../_test/utils'
import { getWithdrawalMessages } from './getWithdrawalMessages'

test('correctly retrieves L2 hash', async () => {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest'
import { publicClient, testClient, walletClient } from '../../_test/utils'
import { walletClient, testClient, publicClient } from '../../../_test/utils'
import { base } from '@roninjin10/rollup-chains'
import { writeDepositERC20 } from './writeDepositERC20'
import { mine, writeContract } from 'viem/actions'
Expand All @@ -15,7 +15,7 @@ test('default', async () => {
})
await testClient.setBalance({
address: zenaddress,
value: 1000000000000000000n,
value: 10n ** 22n,
})
await writeContract(testClient, {
address: USDCL1,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test } from 'vitest'
import { walletClient } from '../../_test/utils'
import { walletClient } from '../../../_test/utils'
import { base } from '@roninjin10/rollup-chains'
import { accounts } from '../../_test/constants'
import { accounts } from '../../../_test/constants'
import { writeDepositETH } from './writeDepositETH'

test('default', async () => {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { expect, test } from 'vitest'
import { publicClient, walletClient, testClient } from '../../_test/utils'
import { publicClient, walletClient, testClient } from '../../../_test/utils'
import {
DepositTransactionParameters,
writeUnsafeDepositTransaction,
} from './writeUnsafeDepositTransaction'
import { base } from '@roninjin10/rollup-chains'
import { accounts } from '../../_test/constants'
import { accounts } from '../../../_test/constants'
import { mine } from 'viem/actions'
import { decodeEventLog, encodeFunctionData, encodePacked } from 'viem'
import { optimismPortalABI } from '@eth-optimism/contracts-ts'
import { getDepositEventsInfoFromTxReceipt } from '../../utils/getDepositEventsInfoFromTxReceipt'
import { TransactionDepositedEvent } from '../../types/depositTx'
import { TransactionDepositedEvent } from '../../../types/depositTx'

test('default', async () => {
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,30 @@ import {
GetL2HashesForDepositTxParamters,
GetL2HashesForDepositTxReturnType,
getL2HashesForDepositTx,
} from '../actions/public/getL2HashesForDepositTx'
import {
GetWithdrawalMessagesParameters,
GetWithdrawalMessagesReturnType,
getWithdrawalMessages,
} from '../actions/public/getWithdrawalMessages'
} from '../actions/public/L1/getL2HashesForDepositTx'
import {
simulateDepositETH,
SimulateDepositETHParameters,
SimulateDepositETHReturnType,
} from '../actions/public/simulateDepositETH'
} from '../actions/public/L1/simulateDepositETH'
import {
simulateDepositERC20,
SimulateDepositERC20Parameters,
SimulateDepositERC20ReturnType,
} from '../actions/public/simulateDepositERC20'
} from '../actions/public/L1/simulateDepositERC20'
import {
GetOutputForL2BlockParameters,
GetOutputForL2BlockReturnType,
getOutputForL2Block,
} from '../actions/public/L1/getOutputForL2Block'

/// NOTE We don't currently need account for exisiting actions but keeping in case
export type PublicOpStackActions<
export type PublicL1OpStackActions<
TTransport extends Transport = Transport,
TChain extends Chain | undefined = Chain | undefined,
> = {
getL2HashesForDepositTx: (
args: GetL2HashesForDepositTxParamters,
) => Promise<GetL2HashesForDepositTxReturnType>
getWithdrawalMessages: (
args: GetWithdrawalMessagesParameters,
) => Promise<GetWithdrawalMessagesReturnType>
simulateDepositETH: <
TChainOverride extends Chain | undefined = Chain | undefined,
>(
Expand All @@ -41,18 +37,21 @@ export type PublicOpStackActions<
>(
args: SimulateDepositERC20Parameters<TChain, TChainOverride>,
) => Promise<SimulateDepositERC20ReturnType<TChain, TChainOverride>>
getOutputForL2Block: (
args: GetOutputForL2BlockParameters,
) => Promise<GetOutputForL2BlockReturnType>
}

export function publicOpStackActions<
export function publicL1OpStackActions<
TTransport extends Transport = Transport,
TChain extends Chain | undefined = Chain | undefined,
>(
client: PublicClient<TTransport, TChain>,
): PublicOpStackActions<TTransport, TChain> {
): PublicL1OpStackActions<TTransport, TChain> {
return {
getL2HashesForDepositTx: (args) => getL2HashesForDepositTx(client, args),
getWithdrawalMessages: (args) => getWithdrawalMessages(client, args),
simulateDepositETH: (args) => simulateDepositETH(client, args),
simulateDepositERC20: (args) => simulateDepositERC20(client, args),
getOutputForL2Block: (args) => getOutputForL2Block(client, args),
}
}
36 changes: 36 additions & 0 deletions src/decorators/publicL2OpStackActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Chain, type PublicClient, Transport } from 'viem'
import {
GetWithdrawalMessagesParameters,
GetWithdrawalMessagesReturnType,
getWithdrawalMessages,
} from '../actions/public/L2/getWithdrawalMessages'
import {
getProveWithdrawalTransactionArgsParams,
getProveWithdrawalTransactionArgsReturnType,
getProveWithdrawalTransactionArgs,
} from '../actions/public/L2/getProveWithdrawalTransactionArgs'

export type PublicL2OpStackActions<
TTransport extends Transport = Transport,
TChain extends Chain | undefined = Chain | undefined,
> = {
getWithdrawalMessages: (
args: GetWithdrawalMessagesParameters,
) => Promise<GetWithdrawalMessagesReturnType>
getProveWithdrawalTransactionArgs: (
args: getProveWithdrawalTransactionArgsParams,
) => Promise<getProveWithdrawalTransactionArgsReturnType>
}

export function publicL2OpStackActions<
TTransport extends Transport = Transport,
TChain extends Chain | undefined = Chain | undefined,
>(
client: PublicClient<TTransport, TChain>,
): PublicL2OpStackActions<TTransport, TChain> {
return {
getWithdrawalMessages: (args) => getWithdrawalMessages(client, args),
getProveWithdrawalTransactionArgs: (args) =>
getProveWithdrawalTransactionArgs(client, args),
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { Account, Chain, Transport, WriteContractReturnType } from 'viem'
import { WalletClient } from 'wagmi'
import { bridgeWriteContract } from '../actions/wallet/bridgeWriteContract'
import { bridgeWriteContract } from '../actions/wallet/L1/bridgeWriteContract'
import {
writeUnsafeDepositTransaction,
WriteUnsafeDepositTransactionParameters,
} from '../actions/wallet/writeUnsafeDepositTransaction'
} from '../actions/wallet/L1/writeUnsafeDepositTransaction'
import {
writeDepositETH,
WriteDepositETHParameters,
} from '../actions/wallet/writeDepositETH'
} from '../actions/wallet/L1/writeDepositETH'
import {
writeDepositERC20,
WriteDepositERC20Parameters,
} from '../actions/wallet/writeDepositERC20'
} from '../actions/wallet/L1/writeDepositERC20'

/// NOTE We don't currently need account for exisiting actions but keeping in case
// TODO need to add generics
export type WalletOpStackActions<
export type WalletL1OpStackActions<
TChain extends Chain | undefined = Chain | undefined,
TAccount extends Account | undefined = Account | undefined,
> = {
Expand Down Expand Up @@ -45,13 +43,13 @@ export type WalletOpStackActions<
) => Promise<WriteContractReturnType>
}

export function walletOpStackActions<
export function walletL1OpStackActions<
TTransport extends Transport = Transport,
TChain extends Chain = Chain,
TAccount extends Account = Account,
>(
client: WalletClient<TTransport, TChain, TAccount>,
): WalletOpStackActions<TChain, TAccount> {
): WalletL1OpStackActions<TChain, TAccount> {
return {
// TODO do better than as any
bridgeWriteContract: (args) => bridgeWriteContract(client as any, args),
Expand Down

0 comments on commit 8c06655

Please sign in to comment.