Skip to content

Commit

Permalink
clean up, all tests passing!!
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsoncusack committed Sep 2, 2023
1 parent 840e782 commit 389c42d
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 472 deletions.
2 changes: 1 addition & 1 deletion src/_test/live.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test('correctly retrieves L2 hash', async () => {
}).extend(walletL1OpStackActions)

const depositHash = await walletClient.writeUnsafeDepositTransaction({
toChainId: baseGoerli.id,
l2ChainId: baseGoerli.id,
args: {
to: account.address,
value: 1n,
Expand Down
5 changes: 3 additions & 2 deletions src/actions/public/L1/getL2HashesForDepositTx.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { publicClient } from '../../../_test/utils'
import { getL2HashesForDepositTx } from './getL2HashesForDepositTx'
import { DepositTx } from '@eth-optimism/core-utils'
import { ethers } from 'ethers'
import { base } from '@roninjin10/rollup-chains'
import { optimismPortalABI } from '@eth-optimism/contracts-ts'
import { base } from 'viem/chains'
import { mainnet } from '../../../chains/mainnet'

describe('Computes L2 hash for L1 event', () => {
bench('op-viem: `getL2HashesForDepositTx`', async () => {
Expand All @@ -20,7 +21,7 @@ describe('Computes L2 hash for L1 event', () => {
// I am not sure how to produce an event from a transaction receipt.
// Happy to update this if there is a better comparison
const contract = new ethers.Contract(
base.opContracts.OptimismPortalProxy,
mainnet.contracts.optimismPortal[base.id],
optimismPortalABI,
ethersProvider,
)
Expand Down
5 changes: 3 additions & 2 deletions src/actions/public/L1/getL2HashesForDepositTx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { test, expect } from 'vitest'
import { publicClient } from '../../../_test/utils'
import { getL2HashesForDepositTx } from './getL2HashesForDepositTx'
import { ethers } from 'ethers'
import { optimism } from '@roninjin10/rollup-chains'
import { optimismPortalABI } from '@eth-optimism/contracts-ts'
import { ethersProvider } from '../../../_test/bench'
import { DepositTx } from '@eth-optimism/core-utils'
import { optimism } from 'viem/chains'
import { mainnet } from '../../../chains/mainnet'

test('correctly retrieves L2 hash', async () => {
const hashes = await getL2HashesForDepositTx(publicClient, {
Expand All @@ -27,7 +28,7 @@ test('matches @eth-optimism/core-utils', async () => {
})

const contract = new ethers.Contract(
optimism.opContracts.OptimismPortalProxy,
mainnet.contracts.optimismPortal[optimism.id],
optimismPortalABI,
ethersProvider,
)
Expand Down
2 changes: 1 addition & 1 deletion src/actions/public/L1/getOutputForL2Block.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, expect } from 'vitest'
import { getOutputForL2Block } from './getOutputForL2Block'
import { publicClient } from '../../../_test/utils'
import { base } from 'viem/dist/types/chains'
import { base } from 'viem/chains'

test('retrieves correctly', async () => {
const result = await getOutputForL2Block(publicClient, {
Expand Down
11 changes: 7 additions & 4 deletions src/actions/public/L1/getOutputForL2Block.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Chain, Hex, PublicClient, Transport } from 'viem'
import { readContract } from 'viem/actions'
import { l2OutputOracleABI } from '@eth-optimism/contracts-ts'
import {
ActionBaseType
} from '../../../types/actions'
import { ActionBaseType } from '../../../types/actions'
import { OpStackL1Contracts } from '../../../types/opStackContracts'
import { ContractToChainAddressMapping } from '../../wallet/L1/writeUnsafeDepositTransaction'

Expand All @@ -16,7 +14,12 @@ export type Proposal = {
export type GetOutputForL2BlockParameters<
TChain extends Chain | undefined = Chain,
_contractName extends OpStackL1Contracts = OpStackL1Contracts.optimismL2OutputOracle,
> = {l2BlockNumber: bigint} & ActionBaseType<TChain, TChain, _contractName, TChain>
> = { l2BlockNumber: bigint } & ActionBaseType<
TChain,
TChain,
_contractName,
TChain
>

export type GetOutputForL2BlockReturnType = {
proposal: Proposal
Expand Down
7 changes: 4 additions & 3 deletions src/actions/public/L1/simulateDepositERC20.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { expect, test } from 'vitest'
import { publicClient, testClient } from '../../../_test/utils'
import { base } from '@roninjin10/rollup-chains'
import { simulateDepositERC20 } from './simulateDepositERC20'
import { writeContract, readContract, simulateContract } from 'viem/actions'
import { erc20ABI } from 'wagmi'
import { mainnet } from '../../../chains/mainnet'
import { base } from 'viem/chains'

const USDCL1 = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
const USDCL2 = '0x2e668bb88287675e34c8df82686dfd0b7f0c0383'
Expand All @@ -22,7 +23,7 @@ test('default', async () => {
address: USDCL1,
abi: erc20ABI,
functionName: 'approve',
args: [base.opContracts.L1StandardBridgeProxy, 10000n],
args: [mainnet.contracts.optimismL1StandardBridge[base.id], 10000n],
account: zenaddress,
})
const balanceBefore = await readContract(testClient, {
Expand All @@ -40,7 +41,7 @@ test('default', async () => {
gasLimit: 100000n,
data: '0x',
},
toChain: base,
l2ChainId: base.id,
account: zenaddress,
})

Expand Down
40 changes: 27 additions & 13 deletions src/actions/public/L1/simulateDepositERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@ import {
SimulateContractReturnType,
} from 'viem'
import { l1StandardBridgeABI } from '@eth-optimism/contracts-ts'
import { OpChainL2 } from '@roninjin10/rollup-chains'
import { simulateContract } from 'viem/actions'
import { DepositERC20Parameters } from '../../types/depositERC20Parameters'
import { DepositERC20Parameters } from '../../../types/depositERC20Parameters'
import { ResolveChain, SimulateActionBaseType } from '../../../types/actions'
import { OpStackL1Contracts } from '../../../types/opStackContracts'
import { ContractToChainAddressMapping } from '../../wallet/L1/writeUnsafeDepositTransaction'

export type SimulateDepositERC20Parameters<
TChain extends Chain | undefined = Chain,
TChainOverride extends Chain | undefined = Chain | undefined,
> = Omit<
SimulateContractParameters<
typeof l1StandardBridgeABI,
'depositERC20',
_contractName extends OpStackL1Contracts = OpStackL1Contracts.optimismL1StandardBridge,
_functionName extends string = 'depositERC20',
_resolvedChain extends Chain | undefined = ResolveChain<
TChain,
TChainOverride
>,
'abi' | 'functionName' | 'args' | 'address'
> & {
toChain: OpChainL2
args: DepositERC20Parameters
}
> = { args: DepositERC20Parameters } & SimulateActionBaseType<
TChain,
typeof l1StandardBridgeABI,
TChainOverride,
_contractName,
_functionName,
_resolvedChain
>

export type SimulateDepositERC20ReturnType<
TChain extends Chain | undefined = Chain,
Expand All @@ -43,12 +47,22 @@ export async function simulateDepositERC20<
client: PublicClient<Transport, TChain>,
{
args: { l1Token, l2Token, amount, gasLimit, data },
toChain,
l2ChainId,
optimismL1StandardBridgeAddress,
chain = client.chain,
...rest
}: SimulateDepositERC20Parameters<TChain, TChainOverride>,
): Promise<SimulateDepositERC20ReturnType<TChain, TChainOverride>> {
const contracts = chain?.contracts as
| ContractToChainAddressMapping
| undefined
const bridge =
optimismL1StandardBridgeAddress ||
(contracts && typeof l2ChainId == 'number'
? contracts[OpStackL1Contracts.optimismL1StandardBridge][l2ChainId]
: undefined)
return simulateContract(client, {
address: toChain.opContracts.L1StandardBridgeProxy,
address: bridge,
abi: l1StandardBridgeABI,
functionName: 'depositERC20',
args: [l1Token, l2Token, amount, gasLimit, data],
Expand Down
4 changes: 2 additions & 2 deletions src/actions/public/L1/simulateDepositETH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export type SimulateDepositETHParameters<
_resolvedChain extends Chain | undefined = ResolveChain<
TChain,
TChainOverride
>
> = & {
>,
> = {
args: DepositETHParameters
} & SimulateActionBaseType<
TChain,
Expand Down
58 changes: 0 additions & 58 deletions src/actions/wallet/L1/bridgeWriteContract.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/actions/wallet/L1/writeDepositERC20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { writeDepositERC20 } from './writeDepositERC20'
import { mine, writeContract } from 'viem/actions'
import { erc20ABI } from 'wagmi'
import { mainnet } from '../../../chains/mainnet'
import { base } from 'viem/dist/types/chains'
import { base } from 'viem/chains'

const USDCL1 = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
const USDCL2 = '0x2e668bb88287675e34c8df82686dfd0b7f0c0383'
Expand Down
2 changes: 1 addition & 1 deletion src/chains/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const mainnet = {
optimismPortal: {
8453: '0x49048044D57e1C92A77f79988d21Fa8fAF74E97e',
7777777: '0x1a0ad011913A150f69f6A19DF447A0CfD9551054',
10: '0xDb9F51790365e7dc196e7D072728df39Be958ACe',
10: '0xbEb5Fc579115071764c7423A4f12eDde41f106Ed',
},
optimismSystemConfig: {
8453: '0x73a79Fab69143498Ed3712e519A88a918e1f4072',
Expand Down
8 changes: 0 additions & 8 deletions src/decorators/walletL1OpStackActions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Account, Chain, Transport, WriteContractReturnType } from 'viem'
import { WalletClient } from 'viem'
import { bridgeWriteContract } from '../actions/wallet/L1/bridgeWriteContract'
import {
writeUnsafeDepositTransaction,
WriteUnsafeDepositTransactionParameters,
Expand All @@ -18,10 +17,6 @@ export type WalletL1OpStackActions<
TChain extends Chain | undefined = Chain | undefined,
TAccount extends Account | undefined = Account | undefined,
> = {
bridgeWriteContract: (
// TODO name these params
args: Parameters<typeof bridgeWriteContract>[1],
) => Promise<string>
writeDepositETH: <
TChainOverride extends Chain | undefined = Chain | undefined,
>(
Expand Down Expand Up @@ -51,9 +46,6 @@ export function walletL1OpStackActions<
client: WalletClient<TTransport, TChain, TAccount>,
): WalletL1OpStackActions<TChain, TAccount> {
return {
// TODO do better than as any
bridgeWriteContract: (args) =>
bridgeWriteContract(client as any, args as any),
writeUnsafeDepositTransaction: (args) =>
writeUnsafeDepositTransaction(client, args),
writeDepositETH: (args) => writeDepositETH(client, args),
Expand Down
16 changes: 9 additions & 7 deletions src/types/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Abi, Account, Address, Chain, SimulateContractParameters, WriteContractParameters } from 'viem'
import {
Abi,
Account,
Address,
Chain,
SimulateContractParameters,
WriteContractParameters,
} from 'viem'
import { OpStackL1Contracts } from './opStackContracts'

export type ExtractValidChainIdFromContract<
Expand Down Expand Up @@ -100,11 +107,6 @@ export type SimulateActionBaseType<
>,
> = ActionBaseType<TChain, TChainOverride, _contractName, _resolvedChain> &
Omit<
SimulateContractParameters<
TAbi,
_functionName,
TChain,
TChainOverride
>,
SimulateContractParameters<TAbi, _functionName, TChain, TChainOverride>,
'abi' | 'functionName' | 'args' | 'address' | 'chain'
>
2 changes: 1 addition & 1 deletion src/utils/getDepositEventsInfoFromTxReceipt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TransactionReceipt, decodeEventLog } from 'viem'
import { optimismPortalABI } from '../generated/contracts'
import { TransactionDepositedEvent } from '../types/depositTransaction'
import { optimismPortalABI } from '@eth-optimism/contracts-ts'

type GetDepositEventInfoFromTxReceiptParams = {
receipt: TransactionReceipt
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { optimismPortalABI } from '@eth-optimism/contracts-ts'
import { optimism } from '@roninjin10/rollup-chains'
import { BigNumber, ethers } from 'ethers'
import { expect, test } from 'vitest'
import { ethersProvider } from '../_test/bench'
Expand All @@ -10,13 +9,15 @@ import { getDepositEventsInfoFromTxReceipt } from './getDepositEventsInfoFromTxR
import { getDepositTransactionFromTransactionDepositedEvent } from './getDepositTransactionFromTransactionDepositedEvent'
import { getSourceHash } from './getSourceHash'
import { SourceHashDomain } from '../types/depositTransaction'
import { mainnet } from '../chains/mainnet'
import { optimism } from 'viem/chains'

// Simply testing against another implementation is not the best practice
// but I added these after debugging a difference. They will be useful to have
// if debugging again in the future.
test('derives same values as op-ethereum/core-utils', async () => {
const contract = new ethers.Contract(
optimism.opContracts.OptimismPortalProxy,
mainnet.contracts.optimismPortal[optimism.id],
optimismPortalABI,
ethersProvider,
)
Expand Down
5 changes: 3 additions & 2 deletions src/utils/getL2HashFromL1DepositInfo.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { ethersProvider } from '../_test/bench'
import { publicClient } from '../_test/utils'
import { DepositTx } from '@eth-optimism/core-utils'
import { ethers } from 'ethers'
import { base } from '@roninjin10/rollup-chains'
import { optimismPortalABI } from '@eth-optimism/contracts-ts'
import { getTransactionReceipt } from 'viem/actions'
import { getDepositEventsInfoFromTxReceipt } from './getDepositEventsInfoFromTxReceipt'
import { getL2HashFromL1DepositInfo } from './getL2HashFromL1DepositInfo'
import { base } from 'viem/chains'
import { mainnet } from '../chains/mainnet'

describe('Obtain L2 hash from already fetched event', async () => {
const receipt = await getTransactionReceipt(publicClient, {
Expand All @@ -23,7 +24,7 @@ describe('Obtain L2 hash from already fetched event', async () => {
})

const contract = new ethers.Contract(
base.opContracts.OptimismPortalProxy,
mainnet.contracts.optimismPortal[base.id],
optimismPortalABI,
ethersProvider,
)
Expand Down
Loading

0 comments on commit 389c42d

Please sign in to comment.