From 8f3b0a5b7f2c3d9a3d7e697178520e99f834f23d Mon Sep 17 00:00:00 2001 From: wilsoncusack Date: Mon, 28 Aug 2023 13:08:02 -0400 Subject: [PATCH] fix types for unsafeDeposit and add test (#24) * fix types for unsafeDeposit and add test * clean up decorator types * cleanup --- .../public/getL2HashesForDepositTx.test.ts | 3 ++ .../writeUnsafeDepositTransaction.test.ts | 51 ++++++++++++++++--- .../wallet/writeUnsafeDepositTransaction.ts | 26 ++++------ src/decorators/walletOpStack.ts | 7 +-- .../getDepositEventsInfoFromTxReceipt.ts | 5 +- 5 files changed, 64 insertions(+), 28 deletions(-) diff --git a/src/actions/public/getL2HashesForDepositTx.test.ts b/src/actions/public/getL2HashesForDepositTx.test.ts index 44c8575d..4dc7ab25 100644 --- a/src/actions/public/getL2HashesForDepositTx.test.ts +++ b/src/actions/public/getL2HashesForDepositTx.test.ts @@ -2,6 +2,9 @@ import { test, expect } from 'vitest' import { createPublicClient, http } from 'viem' import { mainnet } from '@wagmi/chains' import { publicOpStackActions } from '../../decorators/publicOpStack' +import { optimismPortalABI } from '../../generated/contracts' +import { base } from '@roninjin10/rollup-chains' +import { publicClient } from '../../_test/utils' test('correctly retrieves L2 hash', async () => { const client = createPublicClient({ diff --git a/src/actions/wallet/writeUnsafeDepositTransaction.test.ts b/src/actions/wallet/writeUnsafeDepositTransaction.test.ts index 8f963880..2ffe4809 100644 --- a/src/actions/wallet/writeUnsafeDepositTransaction.test.ts +++ b/src/actions/wallet/writeUnsafeDepositTransaction.test.ts @@ -7,8 +7,10 @@ import { import { base } from '@roninjin10/rollup-chains' import { accounts } from '../../_test/constants' import { mine } from 'viem/actions' -import { encodeFunctionData } from 'viem' +import { decodeEventLog, encodeFunctionData, encodePacked } from 'viem' import { optimismPortalABI } from '@eth-optimism/contracts-ts' +import { getDepositEventsInfoFromTxReceipt } from '../../utils/getDepositEventsInfoFromTxReceipt' +import { TransactionDepositedEvent } from '../../types/depositTx' test('default', async () => { expect( @@ -16,10 +18,11 @@ test('default', async () => { args: { to: '0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb', value: 1n, - gasLimit: 1n, + gasLimit: 25000n, data: '0x', isCreation: false, }, + value: 0n, toChain: base, account: accounts[0].address, }), @@ -31,10 +34,11 @@ test('sends transaction to correct address', async () => { args: { to: '0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb', value: 1n, - gasLimit: 1n, + gasLimit: 25000n, data: '0x', isCreation: false, }, + value: 1n, toChain: base, account: accounts[0].address, }) @@ -45,13 +49,46 @@ test('sends transaction to correct address', async () => { expect(r.to).toEqual(base.opContracts.OptimismPortalProxy.toLocaleLowerCase()) }) -// TODO(wilson): consider deploying OP stack contracts to our anvil so that we can have better tests +test('creates correct deposit transaction', async () => { + const args: DepositTransactionParameters = { + to: '0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb', + value: 1n, + gasLimit: 25000n, + data: '0x', + isCreation: false, + } + const hash = await writeUnsafeDepositTransaction(walletClient, { + args, + value: args.value!, + toChain: base, + account: accounts[0].address, + }) + + await mine(testClient, { blocks: 1 }) + + const r = await publicClient.getTransactionReceipt({ hash }) + expect(r.logs.length).toEqual(1) + const depositEvent = decodeEventLog({ + abi: optimismPortalABI, + data: r.logs[0].data, + topics: r.logs[0].topics, + }) + expect(depositEvent.eventName).toEqual('TransactionDeposited') + const deposit = depositEvent as TransactionDepositedEvent + expect(deposit.args.from.toLowerCase()).toEqual(accounts[0].address) + expect(deposit.args.to.toLowerCase()).toEqual(args.to) + const expectOpaqueData = encodePacked( + ['uint', 'uint', 'uint64', 'bool', 'bytes'], + [args.value!, args.value!, args.gasLimit, args.isCreation!, args.data!], + ) + expect(deposit.args.opaqueData).toEqual(expectOpaqueData) +}) test('correctly passes arugments', async () => { const args: DepositTransactionParameters = { to: '0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb', value: 1n, - gasLimit: 1n, + gasLimit: 25000n, data: '0x', isCreation: false, } @@ -60,6 +97,7 @@ test('correctly passes arugments', async () => { args, toChain: base, account: accounts[0].address, + value: 0n, }) await mine(testClient, { blocks: 1 }) @@ -77,13 +115,14 @@ test('correctly passes arugments', async () => { test('uses defaults for data, isCreation, and value', async () => { const args: DepositTransactionParameters = { to: '0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb', - gasLimit: 1n, + gasLimit: 25000n, } const hash = await writeUnsafeDepositTransaction(walletClient, { args, toChain: base, account: accounts[0].address, + value: 0n, }) await mine(testClient, { blocks: 1 }) diff --git a/src/actions/wallet/writeUnsafeDepositTransaction.ts b/src/actions/wallet/writeUnsafeDepositTransaction.ts index 8e517262..5b054e67 100644 --- a/src/actions/wallet/writeUnsafeDepositTransaction.ts +++ b/src/actions/wallet/writeUnsafeDepositTransaction.ts @@ -22,15 +22,13 @@ export type DepositTransactionParameters = { } export type WriteUnsafeDepositTransactionParameters< - TAbi extends Abi | readonly unknown[] = typeof optimismPortalABI, - TFunctionName extends string = 'depositTransaction', TChain extends Chain | undefined = Chain, TAccount extends Account | undefined = Account | undefined, TChainOverride extends Chain | undefined = Chain | undefined, > = Omit< WriteContractParameters< - TAbi, - TFunctionName, + typeof optimismPortalABI, + 'depositTransaction', TChain, TAccount, TChainOverride @@ -44,8 +42,6 @@ export type WriteUnsafeDepositTransactionParameters< export async function writeUnsafeDepositTransaction< TChain extends Chain | undefined, TAccount extends Account | undefined, - const TAbi extends Abi | readonly unknown[], - TFunctionName extends string, TChainOverride extends Chain | undefined, >( client: WalletClient, @@ -53,19 +49,19 @@ export async function writeUnsafeDepositTransaction< args: { to, value, gasLimit, isCreation, data }, toChain, ...rest - }: WriteUnsafeDepositTransactionParameters< - TAbi, - TFunctionName, - TChain, - TAccount, - TChainOverride - >, + }: WriteUnsafeDepositTransactionParameters, ): Promise { return writeContract(client, { address: toChain.opContracts.OptimismPortalProxy, abi: optimismPortalABI, - functionName: 'depositTransaction', + functionName: 'depositTransaction' as any, args: [to, value || 0n, gasLimit, isCreation || false, data || '0x'], ...rest, - } as any) + } as unknown as WriteContractParameters< + typeof optimismPortalABI, + 'depositTransaction', + TChain, + TAccount, + TChainOverride + >) } diff --git a/src/decorators/walletOpStack.ts b/src/decorators/walletOpStack.ts index 66ffb4b8..cc4990db 100644 --- a/src/decorators/walletOpStack.ts +++ b/src/decorators/walletOpStack.ts @@ -1,11 +1,10 @@ -import { Abi, Account, Chain, Transport, WriteContractReturnType } from 'viem' +import { Account, Chain, Transport, WriteContractReturnType } from 'viem' import { WalletClient } from 'wagmi' import { bridgeWriteContract } from '../actions/wallet/bridgeWriteContract' import { WriteUnsafeDepositTransactionParameters, writeUnsafeDepositTransaction, } from '../actions/wallet/writeUnsafeDepositTransaction' -import { optimismPortalABI } from '@eth-optimism/contracts-ts' /// NOTE We don't currently need account for exisiting actions but keeping in case // TODO need to add generics @@ -18,13 +17,9 @@ export type WalletOpStackActions< args: Parameters[1], ) => Promise writeUnsafeDepositTransaction: < - TAbi extends Abi | readonly unknown[] = typeof optimismPortalABI, - TFunctionName extends string = 'depositTransaction', TChainOverride extends Chain | undefined = Chain | undefined, >( args: WriteUnsafeDepositTransactionParameters< - TAbi, - TFunctionName, TChain, TAccount, TChainOverride diff --git a/src/utils/getDepositEventsInfoFromTxReceipt.ts b/src/utils/getDepositEventsInfoFromTxReceipt.ts index 3ae2488c..0e0622be 100644 --- a/src/utils/getDepositEventsInfoFromTxReceipt.ts +++ b/src/utils/getDepositEventsInfoFromTxReceipt.ts @@ -27,7 +27,10 @@ export function getDepositEventsInfoFromTxReceipt({ data: l.data, topics: l.topics, }) - if (l.logIndex && event.eventName === 'TransactionDeposited') { + if (event.eventName === 'TransactionDeposited') { + if (!l.logIndex) { + throw new Error('Found TransactionDeposited by logIndex undefined') + } depositEvents.push({ event, logIndex: l.logIndex }) } }