Skip to content

Commit

Permalink
viem lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdiallam committed Oct 9, 2024
1 parent 3c695a2 commit b1b74ea
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 74 deletions.
47 changes: 37 additions & 10 deletions packages/viem/src/actions/relayL2ToL2Message.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { encodeFunctionData } from 'viem'
import { describe, expect, it } from 'vitest'

import { supersimL2A, supersimL2B } from '@/chains/supersim.js'
import { publicClientA, publicClientB, testAccount, walletClientA, walletClientB } from '@/test/clients.js'
import {
publicClientA,
publicClientB,
testAccount,
walletClientA,
walletClientB,
} from '@/test/clients.js'
import { ticTacToeABI, ticTacToeAddress } from '@/test/setupTicTacToe.js'
import {
createInteropSentL2ToL2Messages,
Expand All @@ -28,7 +34,10 @@ describe('relayL2ToL2Message', () => {
})

const receipt = await publicClientA.waitForTransactionReceipt({ hash })
const { sentMessages } = await createInteropSentL2ToL2Messages(publicClientA, { receipt })
const { sentMessages } = await createInteropSentL2ToL2Messages(
publicClientA,
{ receipt },
)
expect(sentMessages).length(1)

const gas = await publicClientB.estimateRelayL2ToL2MessageGas({
Expand All @@ -51,14 +60,17 @@ describe('relayL2ToL2Message', () => {
})

const receipt = await publicClientA.waitForTransactionReceipt({ hash })
const { sentMessages } = await createInteropSentL2ToL2Messages(publicClientA, { receipt })
const { sentMessages } = await createInteropSentL2ToL2Messages(
publicClientA,
{ receipt },
)
expect(sentMessages).length(1)

expect(() =>
publicClientB.simulateRelayL2ToL2Message({
account: testAccount,
sentMessageId: sentMessages[0].id,
sentMessagePayload: sentMessages[0].payload
sentMessagePayload: sentMessages[0].payload,
}),
).not.throw()
})
Expand All @@ -73,20 +85,28 @@ describe('relayL2ToL2Message', () => {
message: calldata,
})

const sendReceipt = await publicClientA.waitForTransactionReceipt({ hash: sendTxHash })
const { sentMessages } = await createInteropSentL2ToL2Messages(publicClientA, { receipt: sendReceipt })
const sendReceipt = await publicClientA.waitForTransactionReceipt({
hash: sendTxHash,
})
const { sentMessages } = await createInteropSentL2ToL2Messages(
publicClientA,
{ receipt: sendReceipt },
)
expect(sentMessages).length(1)

const relayTxHash = await walletClientB.relayL2ToL2Message({
sentMessageId: sentMessages[0].id,
sentMessagePayload: sentMessages[0].payload
sentMessagePayload: sentMessages[0].payload,
})

expect(relayTxHash).toBeDefined()

// succesfully relayed
const relayReceipt = await publicClientB.waitForTransactionReceipt({ hash: relayTxHash })
const { successfulMessages, failedMessages } = decodeRelayedL2ToL2Messages({ receipt: relayReceipt })
const relayReceipt = await publicClientB.waitForTransactionReceipt({
hash: relayTxHash,
})
const { successfulMessages, failedMessages } =
decodeRelayedL2ToL2Messages({ receipt: relayReceipt })
expect(successfulMessages).length(1)
expect(failedMessages).length(0)

Expand All @@ -95,7 +115,14 @@ describe('relayL2ToL2Message', () => {
expect(messages).length(1)

const { destination, messageNonce, sender, target, message } = messages[0]
const msgHash = hashL2ToL2Message(destination, BigInt(supersimL2A.id), messageNonce, sender, target, message)
const msgHash = hashL2ToL2Message(
destination,
BigInt(supersimL2A.id),
messageNonce,
sender,
target,
message,
)
expect(msgHash).toEqual(successfulMessages[0].messageHash)
})
})
Expand Down
5 changes: 4 additions & 1 deletion packages/viem/src/actions/relayL2ToL2Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import { estimateContractGas, simulateContract } from 'viem/actions'

import { l2ToL2CrossDomainMessengerABI } from '@/abis.js'
import { contracts } from '@/contracts.js'
import { baseWriteAction, type BaseWriteContractActionParameters } from '@/core/baseWriteAction.js'
import {
baseWriteAction,
type BaseWriteContractActionParameters,
} from '@/core/baseWriteAction.js'
import type { MessageIdentifier, MessagePayload } from '@/types/interop.js'
import type { ErrorType } from '@/types/utils.js'

Expand Down
4 changes: 3 additions & 1 deletion packages/viem/src/actions/sendL2ToL2Message.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ describe('sendL2ToL2Message', () => {
expect(txHash).toBeDefined()

// SentMessage event
const receipt = await publicClientA.waitForTransactionReceipt({ hash: txHash })
const receipt = await publicClientA.waitForTransactionReceipt({
hash: txHash,
})
const { messages } = decodeSentL2ToL2Messages({ receipt })
expect(messages).length(1)

Expand Down
24 changes: 10 additions & 14 deletions packages/viem/src/decorators/publicL2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ export type PublicActionsL2<
estimateRelayL2ToL2MessageGas: <
TChainOverride extends Chain | undefined = undefined,
>(
parameters: RelayL2ToL2MessageParameters<
TChain,
TAccount,
TChainOverride
>,
parameters: RelayL2ToL2MessageParameters<TChain, TAccount, TChainOverride>,
) => Promise<bigint>

simulateSendL2ToL2Message: <
Expand All @@ -48,11 +44,7 @@ export type PublicActionsL2<
simulateRelayL2ToL2Message: <
TChainOverride extends Chain | undefined = undefined,
>(
parameters: RelayL2ToL2MessageParameters<
TChain,
TAccount,
TChainOverride
>,
parameters: RelayL2ToL2MessageParameters<TChain, TAccount, TChainOverride>,
) => Promise<RelayL2ToL2MessageContractReturnType>
}

Expand All @@ -66,10 +58,14 @@ export function publicActionsL2() {
) => {
return {
...upstreamPublicActionsL2(),
estimateSendL2ToL2MessageGas: (args) => estimateSendL2ToL2MessageGas(client, args),
estimateRelayL2ToL2MessageGas: (args) => estimateRelayL2ToL2MessageGas(client, args),
simulateSendL2ToL2Message: (args) => simulateSendL2ToL2Message(client, args),
simulateRelayL2ToL2Message: (args) => simulateRelayL2ToL2Message(client, args),
estimateSendL2ToL2MessageGas: (args) =>
estimateSendL2ToL2MessageGas(client, args),
estimateRelayL2ToL2MessageGas: (args) =>
estimateRelayL2ToL2MessageGas(client, args),
simulateSendL2ToL2Message: (args) =>
simulateSendL2ToL2Message(client, args),
simulateRelayL2ToL2Message: (args) =>
simulateRelayL2ToL2Message(client, args),
} as PublicActionsL2<TChain, TAccount>
}
}
2 changes: 1 addition & 1 deletion packages/viem/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export type {
DecodeSentL2ToL2MessagesParameters,
DecodeSentL2ToL2MessagesReturnType,
} from '@/utils/l2ToL2CrossDomainMessenger.js'
export {
export {
createInteropSentL2ToL2Messages,
decodeRelayedL2ToL2Messages,
decodeSentL2ToL2Messages,
Expand Down
2 changes: 1 addition & 1 deletion packages/viem/src/test/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const testAccount = privateKeyToAccount(
'0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',
)

/** Chain A **/
/** Chain A **/

export const publicClientA = createPublicClient({
chain: supersimL2A,
Expand Down
32 changes: 25 additions & 7 deletions packages/viem/src/test/e2e/interop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ import { encodeFunctionData } from 'viem'
import { describe, expect, it } from 'vitest'

import { supersimL2B } from '@/chains/supersim.js'
import { publicClientA, publicClientB, testAccount, walletClientA, walletClientB } from '@/test/clients.js'
import {
publicClientA,
publicClientB,
testAccount,
walletClientA,
walletClientB,
} from '@/test/clients.js'
import { ticTacToeABI, ticTacToeAddress } from '@/test/setupTicTacToe.js'
import { createInteropSentL2ToL2Messages, decodeRelayedL2ToL2Messages } from '@/utils/l2ToL2CrossDomainMessenger.js'
import {
createInteropSentL2ToL2Messages,
decodeRelayedL2ToL2Messages,
} from '@/utils/l2ToL2CrossDomainMessenger.js'

describe('Generic Interop Flow', () => {
it('should send and execute cross chain message', async () => {
Expand All @@ -21,18 +30,27 @@ describe('Generic Interop Flow', () => {
message: calldata,
})

const receipt = await publicClientA.waitForTransactionReceipt({ hash: sentMessageTxHash })
const { sentMessages } = await createInteropSentL2ToL2Messages(publicClientA, { receipt })
const receipt = await publicClientA.waitForTransactionReceipt({
hash: sentMessageTxHash,
})
const { sentMessages } = await createInteropSentL2ToL2Messages(
publicClientA,
{ receipt },
)
expect(sentMessages).length(1)

// message was relayed on the other side
const relayMessageTxHash = await walletClientB.relayL2ToL2Message({
sentMessageId: sentMessages[0].id,
sentMessagePayload: sentMessages[0].payload
sentMessagePayload: sentMessages[0].payload,
})

const relayMessageReceipt = await publicClientB.waitForTransactionReceipt({ hash: relayMessageTxHash })
const { successfulMessages, failedMessages } = decodeRelayedL2ToL2Messages({ receipt: relayMessageReceipt })
const relayMessageReceipt = await publicClientB.waitForTransactionReceipt({
hash: relayMessageTxHash,
})
const { successfulMessages, failedMessages } = decodeRelayedL2ToL2Messages({
receipt: relayMessageReceipt,
})
expect(successfulMessages).length(1)
expect(failedMessages).length(0)
})
Expand Down
10 changes: 8 additions & 2 deletions packages/viem/src/test/setupTicTacToe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ export const ticTacToeABI = [
] as const

export async function setupTicTacToe() {
await testClientA.setCode({ address: ticTacToeAddress, bytecode: ticTacToeBytecode })
await testClientB.setCode({ address: ticTacToeAddress, bytecode: ticTacToeBytecode })
await testClientA.setCode({
address: ticTacToeAddress,
bytecode: ticTacToeBytecode,
})
await testClientB.setCode({
address: ticTacToeAddress,
bytecode: ticTacToeBytecode,
})
}
14 changes: 8 additions & 6 deletions packages/viem/src/utils/interop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type CreateInteropMessageReturnType = {

export type DecodeExecutingMessagesParameters = { receipt: TransactionReceipt }
export type DecodeExecutingMessagesReturnType = {
executingMessages: Array<{ id: MessageIdentifier, msgHash: Hash, log: Log }>
executingMessages: Array<{ id: MessageIdentifier; msgHash: Hash; log: Log }>
}

/**
Expand All @@ -34,14 +34,14 @@ export async function createInteropMessage<
account extends Account | undefined,
>(
client: PublicClient<Transport, chain, account>,
params: CreateInteropMessageParameters
params: CreateInteropMessageParameters,
): Promise<CreateInteropMessageReturnType> {
const { log } = params
if (log.blockNumber === undefined || log.logIndex === undefined) {
throw new Error("pending log cannot be constructed into an interop message")
throw new Error('pending log cannot be constructed into an interop message')
}
if (!client.chain) {
throw new Error("define chain required to construct an interop message")
throw new Error('define chain required to construct an interop message')
}

const block = await client.getBlock({ blockHash: log.blockHash as Hash })
Expand All @@ -64,14 +64,16 @@ export async function createInteropMessage<
* @returns Decoded cross-chain calls {@link DecodeExecutingMessagesReturnType }
*/
export function decodeExecutingMessages(
params: DecodeExecutingMessagesParameters
params: DecodeExecutingMessagesParameters,
): DecodeExecutingMessagesReturnType {
const logs = parseEventLogs({
abi: crossL2InboxABI,
eventName: 'ExecutingMessage',
logs: params.receipt.logs,
})

const executingMessages = logs.map((log) => { return { ...log.args, log }})
const executingMessages = logs.map((log) => {
return { ...log.args, log }
})
return { executingMessages }
}
Loading

0 comments on commit b1b74ea

Please sign in to comment.