-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy pathBaseFeeCalculator.cs
67 lines (62 loc) · 2.73 KB
/
BaseFeeCalculator.cs
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
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//
using Nethermind.Core.Specs;
using Nethermind.Int256;
namespace Nethermind.Core
{
/// <summary>Calculate BaseFee based on block parent and release spec.</summary>
public static class BaseFeeCalculator
{
public static UInt256 Calculate(BlockHeader parent, IReleaseSpec spec)
{
UInt256 expectedBaseFee = UInt256.Zero;
if (spec.IsEip1559Enabled)
{
UInt256 parentBaseFee = parent.BaseFeePerGas;
long gasDelta;
UInt256 feeDelta;
bool isForkBlockNumber = spec.Eip1559TransitionBlock == parent.Number + 1;
long parentGasTarget = parent.GasLimit / Eip1559Constants.ElasticityMultiplier;
if (isForkBlockNumber)
parentGasTarget = parent.GasLimit;
if (parent.GasUsed == parentGasTarget)
{
expectedBaseFee = parent.BaseFeePerGas;
}
else if (parent.GasUsed > parentGasTarget)
{
gasDelta = parent.GasUsed - parentGasTarget;
feeDelta = UInt256.Max(
parentBaseFee * (UInt256) gasDelta / (UInt256) parentGasTarget / Eip1559Constants.BaseFeeMaxChangeDenominator,
UInt256.One);
expectedBaseFee = parentBaseFee + feeDelta;
}
else
{
gasDelta = parentGasTarget - parent.GasUsed;
feeDelta = parentBaseFee * (UInt256) gasDelta / (UInt256) parentGasTarget / Eip1559Constants.BaseFeeMaxChangeDenominator;
expectedBaseFee = UInt256.Max(parentBaseFee - feeDelta, 0);
}
if (isForkBlockNumber)
{
expectedBaseFee = Eip1559Constants.ForkBaseFee;
}
}
return expectedBaseFee;
}
}
}