-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
native-eth.spec.ts
358 lines (308 loc) · 10.4 KB
/
native-eth.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import { expect } from 'chai'
/* Imports: External */
import { Wallet, utils, BigNumber } from 'ethers'
import { serialize } from '@ethersproject/transactions'
import { predeploys } from '@eth-optimism/contracts'
/* Imports: Internal */
import { Direction } from './shared/watcher-utils'
import {
expectApprox,
fundUser,
PROXY_SEQUENCER_ENTRYPOINT_ADDRESS,
} from './shared/utils'
import { OptimismEnv, useDynamicTimeoutForWithdrawals } from './shared/env'
const DEFAULT_TEST_GAS_L1 = 330_000
const DEFAULT_TEST_GAS_L2 = 1_300_000
// TX size enforced by CTC:
const MAX_ROLLUP_TX_SIZE = 50_000
describe('Native ETH Integration Tests', async () => {
let env: OptimismEnv
let l1Bob: Wallet
let l2Bob: Wallet
const getBalances = async (_env: OptimismEnv) => {
const l1UserBalance = await _env.l1Wallet.getBalance()
const l2UserBalance = await _env.l2Wallet.getBalance()
const l1BobBalance = await l1Bob.getBalance()
const l2BobBalance = await l2Bob.getBalance()
const sequencerBalance = await _env.ovmEth.balanceOf(
PROXY_SEQUENCER_ENTRYPOINT_ADDRESS
)
const l1BridgeBalance = await _env.l1Wallet.provider.getBalance(
_env.l1Bridge.address
)
return {
l1UserBalance,
l2UserBalance,
l1BobBalance,
l2BobBalance,
l1BridgeBalance,
sequencerBalance,
}
}
before(async () => {
env = await OptimismEnv.new()
l1Bob = Wallet.createRandom().connect(env.l1Wallet.provider)
l2Bob = l1Bob.connect(env.l2Wallet.provider)
})
describe('estimateGas', () => {
it('Should estimate gas for ETH withdraw', async () => {
const amount = utils.parseEther('0.0000001')
const gas = await env.l2Bridge.estimateGas.withdraw(
predeploys.OVM_ETH,
amount,
0,
'0xFFFF'
)
// Expect gas to be less than or equal to the target plus 1%
expectApprox(gas, 6700060, { upperPercentDeviation: 1 })
})
})
it('receive', async () => {
const depositAmount = 10
const preBalances = await getBalances(env)
const { tx, receipt } = await env.waitForXDomainTransaction(
env.l1Wallet.sendTransaction({
to: env.l1Bridge.address,
value: depositAmount,
gasLimit: DEFAULT_TEST_GAS_L1,
}),
Direction.L1ToL2
)
const l1FeePaid = receipt.gasUsed.mul(tx.gasPrice)
const postBalances = await getBalances(env)
expect(postBalances.l1BridgeBalance).to.deep.eq(
preBalances.l1BridgeBalance.add(depositAmount)
)
expect(postBalances.l2UserBalance).to.deep.eq(
preBalances.l2UserBalance.add(depositAmount)
)
expect(postBalances.l1UserBalance).to.deep.eq(
preBalances.l1UserBalance.sub(l1FeePaid.add(depositAmount))
)
})
it('depositETH', async () => {
const depositAmount = 10
const preBalances = await getBalances(env)
const { tx, receipt } = await env.waitForXDomainTransaction(
env.l1Bridge.depositETH(DEFAULT_TEST_GAS_L2, '0xFFFF', {
value: depositAmount,
gasLimit: DEFAULT_TEST_GAS_L1,
}),
Direction.L1ToL2
)
const l1FeePaid = receipt.gasUsed.mul(tx.gasPrice)
const postBalances = await getBalances(env)
expect(postBalances.l1BridgeBalance).to.deep.eq(
preBalances.l1BridgeBalance.add(depositAmount)
)
expect(postBalances.l2UserBalance).to.deep.eq(
preBalances.l2UserBalance.add(depositAmount)
)
expect(postBalances.l1UserBalance).to.deep.eq(
preBalances.l1UserBalance.sub(l1FeePaid.add(depositAmount))
)
})
it('depositETHTo', async () => {
const depositAmount = 10
const preBalances = await getBalances(env)
const depositReceipts = await env.waitForXDomainTransaction(
env.l1Bridge.depositETHTo(l2Bob.address, DEFAULT_TEST_GAS_L2, '0xFFFF', {
value: depositAmount,
gasLimit: DEFAULT_TEST_GAS_L1,
}),
Direction.L1ToL2
)
const l1FeePaid = depositReceipts.receipt.gasUsed.mul(
depositReceipts.tx.gasPrice
)
const postBalances = await getBalances(env)
expect(postBalances.l1BridgeBalance).to.deep.eq(
preBalances.l1BridgeBalance.add(depositAmount)
)
expect(postBalances.l2BobBalance).to.deep.eq(
preBalances.l2BobBalance.add(depositAmount)
)
expect(postBalances.l1UserBalance).to.deep.eq(
preBalances.l1UserBalance.sub(l1FeePaid.add(depositAmount))
)
})
it('deposit passes with a large data argument', async () => {
const ASSUMED_L2_GAS_LIMIT = 8_000_000
const depositAmount = 10
const preBalances = await getBalances(env)
// Set data length slightly less than MAX_ROLLUP_TX_SIZE
// to allow for encoding and other arguments
const data = `0x` + 'ab'.repeat(MAX_ROLLUP_TX_SIZE - 500)
const { tx, receipt } = await env.waitForXDomainTransaction(
env.l1Bridge.depositETH(ASSUMED_L2_GAS_LIMIT, data, {
value: depositAmount,
gasLimit: 4_000_000,
}),
Direction.L1ToL2
)
const l1FeePaid = receipt.gasUsed.mul(tx.gasPrice)
const postBalances = await getBalances(env)
expect(postBalances.l1BridgeBalance).to.deep.eq(
preBalances.l1BridgeBalance.add(depositAmount)
)
expect(postBalances.l2UserBalance).to.deep.eq(
preBalances.l2UserBalance.add(depositAmount)
)
expect(postBalances.l1UserBalance).to.deep.eq(
preBalances.l1UserBalance.sub(l1FeePaid.add(depositAmount))
)
})
it('depositETH fails with a TOO large data argument', async () => {
const depositAmount = 10
const data = `0x` + 'ab'.repeat(MAX_ROLLUP_TX_SIZE + 1)
await expect(
env.l1Bridge.depositETH(DEFAULT_TEST_GAS_L2, data, {
value: depositAmount,
})
).to.be.reverted
})
it('withdraw', async function () {
await useDynamicTimeoutForWithdrawals(this, env)
const withdrawAmount = BigNumber.from(3)
const preBalances = await getBalances(env)
expect(
preBalances.l2UserBalance.gt(0),
'Cannot run withdrawal test before any deposits...'
)
const transaction = await env.l2Bridge.withdraw(
predeploys.OVM_ETH,
withdrawAmount,
DEFAULT_TEST_GAS_L2,
'0xFFFF'
)
await transaction.wait()
await env.relayXDomainMessages(transaction)
const receipts = await env.waitForXDomainTransaction(
transaction,
Direction.L2ToL1
)
const fee = receipts.tx.gasLimit.mul(receipts.tx.gasPrice)
const postBalances = await getBalances(env)
// Approximate because there's a fee related to relaying the L2 => L1 message and it throws off the math.
expectApprox(
postBalances.l1BridgeBalance,
preBalances.l1BridgeBalance.sub(withdrawAmount),
{ upperPercentDeviation: 1 }
)
expectApprox(
postBalances.l2UserBalance,
preBalances.l2UserBalance.sub(withdrawAmount.add(fee)),
{ upperPercentDeviation: 1 }
)
expectApprox(
postBalances.l1UserBalance,
preBalances.l1UserBalance.add(withdrawAmount),
{ upperPercentDeviation: 1 }
)
})
it('withdrawTo', async function () {
await useDynamicTimeoutForWithdrawals(this, env)
const withdrawAmount = BigNumber.from(3)
const preBalances = await getBalances(env)
expect(
preBalances.l2UserBalance.gt(0),
'Cannot run withdrawal test before any deposits...'
)
const transaction = await env.l2Bridge.withdrawTo(
predeploys.OVM_ETH,
l1Bob.address,
withdrawAmount,
DEFAULT_TEST_GAS_L2,
'0xFFFF'
)
await transaction.wait()
await env.relayXDomainMessages(transaction)
const receipts = await env.waitForXDomainTransaction(
transaction,
Direction.L2ToL1
)
const l2Fee = receipts.tx.gasPrice.mul(receipts.receipt.gasUsed)
// Calculate the L1 portion of the fee
const raw = serialize({
nonce: transaction.nonce,
value: transaction.value,
gasPrice: transaction.gasPrice,
gasLimit: transaction.gasLimit,
to: transaction.to,
data: transaction.data,
})
const l1Fee = await env.gasPriceOracle.getL1Fee(raw)
const fee = l2Fee.add(l1Fee)
const postBalances = await getBalances(env)
expect(postBalances.l1BridgeBalance).to.deep.eq(
preBalances.l1BridgeBalance.sub(withdrawAmount),
'L1 Bridge Balance Mismatch'
)
expect(postBalances.l2UserBalance).to.deep.eq(
preBalances.l2UserBalance.sub(withdrawAmount.add(fee)),
'L2 User Balance Mismatch'
)
expect(postBalances.l1BobBalance).to.deep.eq(
preBalances.l1BobBalance.add(withdrawAmount),
'L1 User Balance Mismatch'
)
})
it('deposit, transfer, withdraw', async function () {
await useDynamicTimeoutForWithdrawals(this, env)
// 1. deposit
const amount = utils.parseEther('1')
await env.waitForXDomainTransaction(
env.l1Bridge.depositETH(DEFAULT_TEST_GAS_L2, '0xFFFF', {
value: amount,
gasLimit: DEFAULT_TEST_GAS_L1,
}),
Direction.L1ToL2
)
// 2. transfer to another address
const other = Wallet.createRandom().connect(env.l2Wallet.provider)
const tx = await env.l2Wallet.sendTransaction({
to: other.address,
value: amount,
})
await tx.wait()
const l1BalanceBefore = await other
.connect(env.l1Wallet.provider)
.getBalance()
// 3. do withdrawal
const withdrawnAmount = utils.parseEther('0.95')
const transaction = await env.l2Bridge
.connect(other)
.withdraw(
predeploys.OVM_ETH,
withdrawnAmount,
DEFAULT_TEST_GAS_L1,
'0xFFFF'
)
await transaction.wait()
await env.relayXDomainMessages(transaction)
const receipts = await env.waitForXDomainTransaction(
transaction,
Direction.L2ToL1
)
// Compute the L1 portion of the fee
const l1Fee = await env.gasPriceOracle.getL1Fee(
serialize({
nonce: transaction.nonce,
value: transaction.value,
gasPrice: transaction.gasPrice,
gasLimit: transaction.gasLimit,
to: transaction.to,
data: transaction.data,
})
)
// check that correct amount was withdrawn and that fee was charged
const l2Fee = receipts.tx.gasPrice.mul(receipts.receipt.gasUsed)
const fee = l1Fee.add(l2Fee)
const l1BalanceAfter = await other
.connect(env.l1Wallet.provider)
.getBalance()
const l2BalanceAfter = await other.getBalance()
expect(l1BalanceAfter).to.deep.eq(l1BalanceBefore.add(withdrawnAmount))
expect(l2BalanceAfter).to.deep.eq(amount.sub(withdrawnAmount).sub(fee))
})
})