-
Notifications
You must be signed in to change notification settings - Fork 55
/
interop.spec.ts
249 lines (207 loc) · 7.4 KB
/
interop.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { encodeFunctionData, parseAbi } from 'viem'
import { beforeAll, describe, expect, it } from 'vitest'
import { superchainWETHABI } from '@/abis.js'
import { supersimL2B } from '@/chains/supersim.js'
import { contracts } from '@/contracts.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 { SUPERSIM_SUPERC20_ADDRESS } from '../supERC20.js'
describe('Generic Interop Flow', () => {
const calldata = encodeFunctionData({
abi: ticTacToeABI,
functionName: 'createGame',
args: [testAccount.address],
})
it('should send and relay cross chain message', async () => {
const sentMessageTxHash = await walletClientA.sendL2ToL2Message({
account: testAccount.address,
destinationChainId: supersimL2B.id,
target: ticTacToeAddress,
message: calldata,
})
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({
account: testAccount.address,
sentMessageId: sentMessages[0].id,
sentMessagePayload: sentMessages[0].payload,
})
const relayMessageReceipt = await publicClientB.waitForTransactionReceipt({
hash: relayMessageTxHash,
})
const { successfulMessages } = decodeRelayedL2ToL2Messages({
receipt: relayMessageReceipt,
})
expect(successfulMessages).length(1)
})
})
describe('SuperchainERC20 Flow', () => {
const balanceOfABI = parseAbi([
'function balanceOf(address account) view returns (uint256)',
])
beforeAll(async () => {
const hash = await walletClientA.writeContract({
address: SUPERSIM_SUPERC20_ADDRESS,
abi: parseAbi(['function mint(address to, uint256 amount)']),
functionName: 'mint',
args: [testAccount.address, 1000n],
})
await publicClientA.waitForTransactionReceipt({ hash })
})
it('should send supERC20 and relay cross chain message to burn/mint tokens', async () => {
const startingBalance = await publicClientB.readContract({
address: SUPERSIM_SUPERC20_ADDRESS,
abi: balanceOfABI,
functionName: 'balanceOf',
args: [testAccount.address],
})
const hash = await walletClientA.sendSupERC20({
tokenAddress: SUPERSIM_SUPERC20_ADDRESS,
to: testAccount.address,
amount: 10n,
chainId: supersimL2B.id,
})
const receipt = await publicClientA.waitForTransactionReceipt({ hash })
const { sentMessages } = await createInteropSentL2ToL2Messages(
publicClientA,
{ receipt },
)
expect(sentMessages).toHaveLength(1)
const relayMessageTxHash = await walletClientB.relayL2ToL2Message({
account: testAccount.address,
sentMessageId: sentMessages[0].id,
sentMessagePayload: sentMessages[0].payload,
})
const relayMessageReceipt = await publicClientB.waitForTransactionReceipt({
hash: relayMessageTxHash,
})
const { successfulMessages } = decodeRelayedL2ToL2Messages({
receipt: relayMessageReceipt,
})
expect(successfulMessages).length(1)
const endingBalance = await publicClientB.readContract({
address: SUPERSIM_SUPERC20_ADDRESS,
abi: balanceOfABI,
functionName: 'balanceOf',
args: [testAccount.address],
})
expect(endingBalance).toEqual(startingBalance + 10n)
})
})
describe('SuperchainWETH Flow', () => {
const AMOUNT_TO_SEND = 10n
beforeAll(async () => {
const hash = await walletClientA.depositSuperchainWETH({
value: 1000n,
})
await publicClientA.waitForTransactionReceipt({ hash })
})
it('should send SuperchainWETH and relay cross chain message to burn/mint tokens', async () => {
const startingWETHBalance = await publicClientB.readContract({
address: contracts.superchainWETH.address,
abi: superchainWETHABI,
functionName: 'balanceOf',
args: [testAccount.address],
})
const hash = await walletClientA.sendSuperchainWETH({
to: testAccount.address,
amount: AMOUNT_TO_SEND,
chainId: supersimL2B.id,
})
const receipt = await publicClientA.waitForTransactionReceipt({ hash })
const { sentMessages } = await createInteropSentL2ToL2Messages(
publicClientA,
{ receipt },
)
expect(sentMessages).toHaveLength(1)
const relayMessageTxHash = await walletClientB.relayL2ToL2Message({
account: testAccount.address,
sentMessageId: sentMessages[0].id,
sentMessagePayload: sentMessages[0].payload,
})
const relayMessageReceipt = await publicClientB.waitForTransactionReceipt({
hash: relayMessageTxHash,
})
const { successfulMessages } = decodeRelayedL2ToL2Messages({
receipt: relayMessageReceipt,
})
expect(successfulMessages).length(1)
const endingWETHBalance = await publicClientB.readContract({
address: contracts.superchainWETH.address,
abi: superchainWETHABI,
functionName: 'balanceOf',
args: [testAccount.address],
})
expect(endingWETHBalance).toEqual(startingWETHBalance + AMOUNT_TO_SEND)
const startingETHBalance = await publicClientB.getBalance({
address: testAccount.address,
})
const withdrawHash = await walletClientB.withdrawSuperchainWETH({
amount: AMOUNT_TO_SEND,
})
const withdrawReceipt = await publicClientB.waitForTransactionReceipt({
hash: withdrawHash,
})
const gasPaid = withdrawReceipt.gasUsed * withdrawReceipt.effectiveGasPrice
const endingETHBalance = await publicClientB.getBalance({
address: testAccount.address,
})
expect(endingETHBalance).toEqual(
startingETHBalance + AMOUNT_TO_SEND - gasPaid,
)
})
})
describe('Cross chain ETH transfer', () => {
const AMOUNT_TO_SEND = 10n
it('should send native ETH from source chain to destination chain', async () => {
const startingBalance = await publicClientB.getBalance({
address: testAccount.address,
})
const hash = await walletClientA.crossChainSendETH({
to: testAccount.address,
value: AMOUNT_TO_SEND,
chainId: supersimL2B.id,
})
const receipt = await publicClientA.waitForTransactionReceipt({ hash })
const { sentMessages } = await createInteropSentL2ToL2Messages(
publicClientA,
{ receipt },
)
expect(sentMessages).toHaveLength(1)
const relayMessageTxHash = await walletClientB.relayL2ToL2Message({
account: testAccount.address,
sentMessageId: sentMessages[0].id,
sentMessagePayload: sentMessages[0].payload,
})
const relayMessageReceipt = await publicClientB.waitForTransactionReceipt({
hash: relayMessageTxHash,
})
const { successfulMessages } = decodeRelayedL2ToL2Messages({
receipt: relayMessageReceipt,
})
expect(successfulMessages).length(1)
const gasPaid =
relayMessageReceipt.gasUsed * relayMessageReceipt.effectiveGasPrice
const endingBalance = await publicClientB.getBalance({
address: testAccount.address,
})
expect(endingBalance).toEqual(startingBalance + AMOUNT_TO_SEND - gasPaid)
})
})