Skip to content

Commit

Permalink
Merge pull request #161 from base-org/export-more-types-and-actions
Browse files Browse the repository at this point in the history
Ensure export coverage
  • Loading branch information
zencephalon authored Nov 3, 2023
2 parents b2bc226 + 28ebe8f commit d15535f
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 72 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-mails-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"op-viem": minor
---

Export more types and ensure that we export every action defined.
2 changes: 1 addition & 1 deletion src/_test/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export let forkUrl: string
if (process.env.VITE_ANVIL_FORK_URL) {
forkUrl = process.env.VITE_ANVIL_FORK_URL
} else {
forkUrl = 'https://cloudflare-eth.com'
forkUrl = 'https://ethereum.publicnode.com'
warn(`\`VITE_ANVIL_FORK_URL\` not found. Falling back to \`${forkUrl}\`.`)
}

Expand Down
17 changes: 17 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
export { type AccountProof, getProof, type GetProofParameters, type StorageProof } from './public/getProof.js'

// Public L1 actions
export {
getL2HashesForDepositTx,
type GetL2HashesForDepositTxParamters,
type GetL2HashesForDepositTxReturnType,
} from './public/L1/getL2HashesForDepositTx.js'
export {
getLatestProposedL2BlockNumber,
type GetLatestProposedL2BlockNumberParameters,
type GetLatestProposedL2BlockNumberReturnType,
} from './public/L1/getLatestProposedL2BlockNumber.js'
export {
getOutputForL2Block,
type GetOutputForL2BlockParameters,
Expand Down Expand Up @@ -35,11 +42,18 @@ export {
type SimulateDepositTransactionParameters,
type SimulateDepositTransactionReturnType,
} from './public/L1/simulateDepositTransaction.js'
export {
simulateFinalizeWithdrawalTransaction,
type SimulateFinalizeWithdrawalTransactionParameters,
type SimulateFinalizeWithdrawalTransactionReturnType,
} from './public/L1/simulateFinalizeWithdrawalTransaction.js'
export {
simulateProveWithdrawalTransaction,
type SimulateProveWithdrawalTransactionParameters,
type SimulateProveWithdrawalTransactionReturnType,
} from './public/L1/simulateProveWithdrawalTransaction.js'

// Public L2 actions
export { estimateFees, type EstimateFeesParameters } from './public/L2/estimateFees.js'
export { estimateL1Fee, type EstimateL1FeeParameters } from './public/L2/estimateL1Fee.js'
export { estimateL1GasUsed, type EstimateL1GasUsedParameters } from './public/L2/estimateL1GasUsed.js'
Expand All @@ -65,6 +79,7 @@ export {
type SimulateWithdrawETHReturnType,
} from './public/L2/simulateWithdrawETH.js'

// Wallet L1 actions
export { writeContractDeposit, type WriteContractDepositParameters } from './wallet/L1/writeContractDeposit.js'
export { writeDepositERC20, type WriteDepositERC20Parameters } from './wallet/L1/writeDepositERC20.js'
export { writeDepositETH, type WriteDepositETHParameters } from './wallet/L1/writeDepositETH.js'
Expand All @@ -86,5 +101,7 @@ export {
writeSendMessage,
type WriteSendMessageParameters,
} from './wallet/L1/writeSendMessage.js'

// Wallet l2 actions
export { writeWithdrawERC20, type WriteWithdrawERC20Parameters } from './wallet/L2/writeWithdrawERC20.js'
export { writeWithdrawETH, type WriteWithdrawETHParameters } from './wallet/L2/writeWithdrawETH.js'
11 changes: 11 additions & 0 deletions src/actions/public/L1/getLatestProposedL2BlockNumber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect, test } from 'vitest'
import { publicClient } from '../../../_test/utils.js'
import { baseAddresses } from '../../../chains/index.js'
import { getLatestProposedL2BlockNumber } from './getLatestProposedL2BlockNumber.js'

test('retrieves correctly', async () => {
const result = await getLatestProposedL2BlockNumber(publicClient, {
...baseAddresses,
})
expect(result.l2BlockNumber).toBeDefined()
})
10 changes: 3 additions & 7 deletions src/actions/public/L1/getSecondsToNextL2Output.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createPublicClient, http } from 'viem'
import { base, mainnet } from 'viem/chains'
import { base } from 'viem/chains'
import { expect, test } from 'vitest'
import { publicClient } from '../../../_test/utils.js'
import { baseAddresses } from '../../../chains/base.js'
import { getSecondsToNextL2Output } from './getSecondsToNextL2Output.js'

Expand All @@ -11,12 +12,7 @@ test('get seconds to next L2 output', async () => {
})
const latestL2BlockNumber = await l2Client.getBlockNumber()

const l1Client = createPublicClient({
chain: mainnet,
transport: http(),
})

const time = await getSecondsToNextL2Output(l1Client, { latestL2BlockNumber, ...baseAddresses })
const time = await getSecondsToNextL2Output(publicClient, { latestL2BlockNumber, ...baseAddresses })
expect(time).toBeDefined()
// this is too noisy to node issues,
// but I do think we should revert if latestL2BlockNumber
Expand Down
14 changes: 4 additions & 10 deletions src/actions/public/L2/getProveWithdrawalTransactionArgs.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createPublicClient, http } from 'viem'
import { base, mainnet } from 'viem/chains'
import { base } from 'viem/chains'
import { expect, test } from 'vitest'
import { accounts } from '../../../_test/constants.js'
import { walletClient } from '../../../_test/utils.js'
import { publicClient, walletClient } from '../../../_test/utils.js'
import { baseAddresses } from '../../../chains/index.js'
import { writeProveWithdrawalTransaction } from '../../index.js'
import { getLatestProposedL2BlockNumber } from '../L1/getLatestProposedL2BlockNumber.js'
Expand All @@ -21,17 +21,11 @@ test('correctly generates args', async () => {
hash: '0xd0eb2a59f3cc4c61b01c350e71e1804ad6bd776dc9abc1bdb5e2e40695ab2628',
})

// TODO: using publicClient was giving issues in CI, need to solve
const l1Client = createPublicClient({
chain: mainnet,
transport: http(),
})

const { l2BlockNumber } = await getLatestProposedL2BlockNumber(l1Client, {
const { l2BlockNumber } = await getLatestProposedL2BlockNumber(publicClient, {
...baseAddresses,
})

const output = await getOutputForL2Block(l1Client, {
const output = await getOutputForL2Block(publicClient, {
l2BlockNumber,
...baseAddresses,
})
Expand Down
65 changes: 46 additions & 19 deletions src/decorators/publicL1OpStackActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import {
type GetL2HashesForDepositTxParamters,
type GetL2HashesForDepositTxReturnType,
} from '../actions/public/L1/getL2HashesForDepositTx.js'
import {
getLatestProposedL2BlockNumber,
type GetLatestProposedL2BlockNumberParameters,
type GetLatestProposedL2BlockNumberReturnType,
} from '../actions/public/L1/getLatestProposedL2BlockNumber.js'
import {
getOutputForL2Block,
type GetOutputForL2BlockParameters,
Expand Down Expand Up @@ -41,6 +46,11 @@ import {
type SimulateDepositTransactionParameters,
type SimulateDepositTransactionReturnType,
} from '../actions/public/L1/simulateDepositTransaction.js'
import {
simulateFinalizeWithdrawalTransaction,
type SimulateFinalizeWithdrawalTransactionParameters,
type SimulateFinalizeWithdrawalTransactionReturnType,
} from '../actions/public/L1/simulateFinalizeWithdrawalTransaction.js'
import {
simulateProveWithdrawalTransaction,
type SimulateProveWithdrawalTransactionParameters,
Expand All @@ -53,35 +63,46 @@ export type PublicL1OpStackActions<
getL2HashesForDepositTx: (
args: GetL2HashesForDepositTxParamters,
) => Promise<GetL2HashesForDepositTxReturnType>
simulateDepositETH: <
TChainOverride extends Chain | undefined = Chain | undefined,
>(
args: SimulateDepositETHParameters<TChain, TChainOverride>,
) => Promise<SimulateDepositETHReturnType<TChain, TChainOverride>>
getLatestProposedL2BlockNumber: (
args: GetLatestProposedL2BlockNumberParameters<TChain>,
) => Promise<GetLatestProposedL2BlockNumberReturnType>
getOutputForL2Block: (
args: GetOutputForL2BlockParameters<TChain>,
) => Promise<GetOutputForL2BlockReturnType>
getSecondsToFinalizable: (args: GetSecondsToFinalizableParameters<TChain>) => Promise<bigint>
getSecondsToNextL2Output: (
args: GetSecondsToNextL2OutputParameters<TChain>,
) => Promise<bigint>

readFinalizedWithdrawals: (args: ReadFinalizedWithdrawalsParameters<TChain>) => Promise<boolean>
readProvenWithdrawals: (args: ReadProvenWithdrawalsParameters<TChain>) => Promise<ReadProvenWithdrawalsReturnType>

simulateDepositERC20: <
TChainOverride extends Chain | undefined = Chain | undefined,
>(
args: SimulateDepositERC20Parameters<TChain, TChainOverride>,
) => Promise<SimulateDepositERC20ReturnType<TChain, TChainOverride>>
simulateDepositETH: <
TChainOverride extends Chain | undefined = Chain | undefined,
>(
args: SimulateDepositETHParameters<TChain, TChainOverride>,
) => Promise<SimulateDepositETHReturnType<TChain, TChainOverride>>
simulateDepositTransaction: <
TChainOverride extends Chain | undefined = Chain | undefined,
>(
args: SimulateDepositTransactionParameters<TChain, TChainOverride>,
) => Promise<SimulateDepositTransactionReturnType<TChain, TChainOverride>>

simulateFinalizeWithdrawalTransaction: <
TChainOverride extends Chain | undefined = Chain | undefined,
>(
args: SimulateFinalizeWithdrawalTransactionParameters<TChain, TChainOverride>,
) => Promise<SimulateFinalizeWithdrawalTransactionReturnType<TChain, TChainOverride>>
simulateProveWithdrawTransaction: <
TChainOverride extends Chain | undefined = Chain | undefined,
>(
args: SimulateProveWithdrawalTransactionParameters<TChain, TChainOverride>,
) => Promise<SimulateProveWithdrawalTransactionReturnType<TChain, TChainOverride>>
getOutputForL2Block: (
args: GetOutputForL2BlockParameters<TChain>,
) => Promise<GetOutputForL2BlockReturnType>
getSecondsToNextL2Output: (
args: GetSecondsToNextL2OutputParameters<TChain>,
) => Promise<bigint>
getSecondsToFinalizable: (args: GetSecondsToFinalizableParameters<TChain>) => Promise<bigint>
readProvenWithdrawals: (args: ReadProvenWithdrawalsParameters<TChain>) => Promise<ReadProvenWithdrawalsReturnType>
readFinalizedWithdrawals: (args: ReadFinalizedWithdrawalsParameters<TChain>) => Promise<boolean>
}

export function publicL1OpStackActions<
Expand All @@ -92,14 +113,20 @@ export function publicL1OpStackActions<
): PublicL1OpStackActions<TChain> {
return {
getL2HashesForDepositTx: (args) => getL2HashesForDepositTx(client, args),

getLatestProposedL2BlockNumber: (args) => getLatestProposedL2BlockNumber(client, args),
getOutputForL2Block: (args) => getOutputForL2Block(client, args),
getSecondsToFinalizable: (args) => getSecondsToFinalizable(client, args),
getSecondsToNextL2Output: (args) => getSecondsToNextL2Output(client, args),

simulateDepositETH: (args) => simulateDepositETH(client, args),
simulateDepositERC20: (args) => simulateDepositERC20(client, args),
simulateDepositTransaction: (args) => simulateDepositTransaction(client, args),
getOutputForL2Block: (args) => getOutputForL2Block(client, args),
simulateProveWithdrawTransaction: (args) => simulateProveWithdrawalTransaction(client, args),
getSecondsToNextL2Output: (args) => getSecondsToNextL2Output(client, args),
getSecondsToFinalizable: (args) => getSecondsToFinalizable(client, args),
readProvenWithdrawals: (args) => readProvenWithdrawals(client, args),

readFinalizedWithdrawals: (args) => readFinalizedWithdrawals(client, args),
readProvenWithdrawals: (args) => readProvenWithdrawals(client, args),

simulateFinalizeWithdrawalTransaction: (args) => simulateFinalizeWithdrawalTransaction(client, args),
simulateProveWithdrawTransaction: (args) => simulateProveWithdrawalTransaction(client, args),
}
}
66 changes: 52 additions & 14 deletions src/decorators/publicL2OpStackActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ import {
type GetWithdrawalMessagesParameters,
type GetWithdrawalMessagesReturnType,
} from '../actions/public/L2/getWithdrawalMessages.js'
import {
simulateWithdrawERC20,
type SimulateWithdrawERC20Parameters,
type SimulateWithdrawERC20ReturnType,
} from '../actions/public/L2/simulateWithdrawERC20.js'
import {
simulateWithdrawETH,
type SimulateWithdrawETHParameters,
type SimulateWithdrawETHReturnType,
} from '../actions/public/L2/simulateWithdrawETH.js'

import { type OracleTransactionParameters } from '../types/gasPriceOracle.js'

export type PublicL2OpStackActions = {
getWithdrawalMessages: (
args: GetWithdrawalMessagesParameters,
) => Promise<GetWithdrawalMessagesReturnType>
getProveWithdrawalTransactionArgs: (
args: GetProveWithdrawalTransactionArgsParams,
) => Promise<GetProveWithdrawalTransactionArgsReturnType>
export type PublicL2OpStackActions<TChain extends Chain | undefined = Chain | undefined> = {
/**
* Estimate the l1 gas price portion for a transaction
* @example
Expand All @@ -29,28 +34,61 @@ export type PublicL2OpStackActions = {
* blockTag,
* });
*/
estimateFees: <TAbi extends Abi | readonly unknown[], TFunctionName extends string | undefined = undefined>(
args: EstimateFeesParameters<TAbi, TFunctionName>,
) => Promise<bigint>
estimateL1Fee: <TAbi extends Abi | readonly unknown[], TFunctionName extends string | undefined = undefined>(
args: OracleTransactionParameters<TAbi, TFunctionName>,
) => Promise<bigint>
estimateL1GasUsed: <TAbi extends Abi | readonly unknown[], TFunctionName extends string | undefined = undefined>(
args: OracleTransactionParameters<TAbi, TFunctionName>,
) => Promise<bigint>
estimateFees: <TAbi extends Abi | readonly unknown[], TFunctionName extends string | undefined = undefined>(
args: EstimateFeesParameters<TAbi, TFunctionName>,
) => Promise<bigint>

getProveWithdrawalTransactionArgs: (
args: GetProveWithdrawalTransactionArgsParams,
) => Promise<GetProveWithdrawalTransactionArgsReturnType>
getWithdrawalMessages: (
args: GetWithdrawalMessagesParameters,
) => Promise<GetWithdrawalMessagesReturnType>

simulateWithdrawERC20: <
TChainOverride extends Chain | undefined = undefined,
>(
args: SimulateWithdrawERC20Parameters<TChain, TChainOverride>,
) => Promise<
SimulateWithdrawERC20ReturnType<
TChain,
TChainOverride
>
>

simulateWithdrawETH: <
TChainOverride extends Chain | undefined = undefined,
>(
args: SimulateWithdrawETHParameters<TChain, TChainOverride>,
) => Promise<
SimulateWithdrawETHReturnType<
TChain,
TChainOverride
>
>
}

export function publicL2OpStackActions<
TTransport extends Transport = Transport,
TChain extends Chain | undefined = Chain | undefined,
>(
client: PublicClient<TTransport, TChain>,
): PublicL2OpStackActions {
): PublicL2OpStackActions<TChain> {
return {
getWithdrawalMessages: (args) => getWithdrawalMessages(client, args),
getProveWithdrawalTransactionArgs: (args) => getProveWithdrawalTransactionArgs(client, args),
estimateFees: (args) => estimateFees(client, args),
estimateL1Fee: (args) => estimateL1Fee(client, args),
estimateL1GasUsed: (args) => estimateL1GasUsed(client, args),
estimateFees: (args) => estimateFees(client, args),

getProveWithdrawalTransactionArgs: (args) => getProveWithdrawalTransactionArgs(client, args),
getWithdrawalMessages: (args) => getWithdrawalMessages(client, args),

simulateWithdrawERC20: (args) => simulateWithdrawERC20(client, args),
simulateWithdrawETH: (args) => simulateWithdrawETH(client, args),
}
}
Loading

0 comments on commit d15535f

Please sign in to comment.