forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1559.ts
50 lines (44 loc) · 1.42 KB
/
1559.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
import { createBlock } from '@ethereumjs/block'
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { createTx } from '@ethereumjs/tx'
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
const block = createBlock(
{
header: {
baseFeePerGas: BigInt(10),
gasLimit: BigInt(100),
gasUsed: BigInt(60),
},
},
{ common },
)
// Base fee will increase for next block since the
// gas used is greater than half the gas limit
console.log(Number(block.header.calcNextBaseFee())) // 11
// So for creating a block with a matching base fee in a certain
// chain context you can do:
const blockWithMatchingBaseFee = createBlock(
{
header: {
baseFeePerGas: block.header.calcNextBaseFee(),
gasLimit: BigInt(100),
gasUsed: BigInt(60),
},
},
{ common },
)
console.log(Number(blockWithMatchingBaseFee.header.baseFeePerGas)) // 11
// successful validation does not throw error
await blockWithMatchingBaseFee.validateData()
// failed validation throws error
const tx = createTx(
{ type: 2, maxFeePerGas: BigInt(20) },
{ common: new Common({ chain: Mainnet, hardfork: Hardfork.London }) },
)
blockWithMatchingBaseFee.transactions.push(tx)
console.log(blockWithMatchingBaseFee.getTransactionsValidationErrors()) // invalid transaction added to block
try {
await blockWithMatchingBaseFee.validateData()
} catch (err) {
console.log(err) // block validation fails
}