Skip to content

Commit

Permalink
fix types for unsafeDeposit and add test (#24)
Browse files Browse the repository at this point in the history
* fix types for unsafeDeposit and add test

* clean up decorator types

* cleanup
  • Loading branch information
wilsoncusack authored Aug 28, 2023
1 parent 6c37f3e commit 8f3b0a5
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/actions/public/getL2HashesForDepositTx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
51 changes: 45 additions & 6 deletions src/actions/wallet/writeUnsafeDepositTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ 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(
await writeUnsafeDepositTransaction(walletClient, {
args: {
to: '0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb',
value: 1n,
gasLimit: 1n,
gasLimit: 25000n,
data: '0x',
isCreation: false,
},
value: 0n,
toChain: base,
account: accounts[0].address,
}),
Expand All @@ -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,
})
Expand All @@ -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,
}
Expand All @@ -60,6 +97,7 @@ test('correctly passes arugments', async () => {
args,
toChain: base,
account: accounts[0].address,
value: 0n,
})

await mine(testClient, { blocks: 1 })
Expand All @@ -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 })
Expand Down
26 changes: 11 additions & 15 deletions src/actions/wallet/writeUnsafeDepositTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,28 +42,26 @@ 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<Transport, TChain, TAccount>,
{
args: { to, value, gasLimit, isCreation, data },
toChain,
...rest
}: WriteUnsafeDepositTransactionParameters<
TAbi,
TFunctionName,
TChain,
TAccount,
TChainOverride
>,
}: WriteUnsafeDepositTransactionParameters<TChain, TAccount, TChainOverride>,
): Promise<WriteContractReturnType> {
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
>)
}
7 changes: 1 addition & 6 deletions src/decorators/walletOpStack.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,13 +17,9 @@ export type WalletOpStackActions<
args: Parameters<typeof bridgeWriteContract>[1],
) => Promise<string>
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
Expand Down
5 changes: 4 additions & 1 deletion src/utils/getDepositEventsInfoFromTxReceipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
}
Expand Down

0 comments on commit 8f3b0a5

Please sign in to comment.