Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(taiko-client): introduce TaikoL2.GetBasefeeV2 #18660

Merged
merged 4 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions packages/protocol/contracts/layer2/based/TaikoL2Deprecated.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,45 @@ abstract contract TaikoL2Deprecated {
}

function anchor(
bytes32, /*_l1BlockHash*/
bytes32, /*_l1StateRoot*/
uint64, /*_l1BlockId*/
uint32 /*_parentGasUsed */
bytes32 _l1BlockHash,
bytes32 _l1StateRoot,
uint64 _l1BlockId,
uint32 _parentGasUsed
)
external
deprecated
{ }

function getBasefee(
uint64, /*_anchorBlockId*/
uint32 /*_parentGasUsed*/
uint64 _anchorBlockId,
uint32 _parentGasUsed
)
public
pure
deprecated
returns (uint256, /*basefee_*/ uint64 /*parentGasExcess_*/ )
returns (uint256 basefee_, uint64 parentGasExcess_)
{ }

function adjustExcess(
uint64, /*_currGasExcess*/
uint64, /*_currGasTarget*/
uint64 /*_newGasTarget*/
uint64 _currGasExcess,
uint64 _currGasTarget,
uint64 _newGasTarget
)
public
pure
deprecated
returns (uint64 /*newGasExcess_*/ )
returns (uint64 newGasExcess_)
{ }

function calculateBaseFee(
LibSharedData.BaseFeeConfig calldata, /*_baseFeeConfig*/
uint64, /*_blocktime*/
uint64, /*_parentGasExcess*/
uint32 /*_parentGasUsed*/
LibSharedData.BaseFeeConfig calldata _baseFeeConfig,
uint64 _blocktime,
uint64 _parentGasExcess,
uint32 _parentGasUsed
)
public
pure
deprecated
returns (uint256, /*basefee_*/ uint64 /*parentGasExcess_*/ )
returns (uint256 basefee_, uint64 parentGasExcess_)
{ }
}
2 changes: 1 addition & 1 deletion packages/taiko-client/bindings/.githead
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ab50d406172e879e7436c70a41bda674bfc6e441
8d2cca11180bd615420e9bb3287f31a9ddf18df8
241 changes: 199 additions & 42 deletions packages/taiko-client/bindings/gen_taiko_l2.go

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions packages/taiko-client/bindings/gen_tier_provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 73 additions & 49 deletions packages/taiko-client/pkg/rpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/big"
"strings"
"time"

"github.com/cenkalti/backoff/v4"
Expand Down Expand Up @@ -281,66 +282,24 @@ func (c *Client) CalculateBaseFee(
currentTimestamp uint64,
) (*big.Int, error) {
var (
baseFeeInfo struct {
Basefee *big.Int
ParentGasExcess uint64
}
err error
baseFee *big.Int
err error
)

if isOntake {
var (
newGasTarget = uint64(baseFeeConfig.GasIssuancePerSecond) * uint64(baseFeeConfig.AdjustmentQuotient)
parentGasExcess uint64
)
parentGasTarget, err := c.TaikoL2.ParentGasTarget(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
)
if err != nil {
return nil, fmt.Errorf("failed to fetch parent gas target: %w", err)
}

if newGasTarget != parentGasTarget && parentGasTarget != 0 {
oldParentGasExcess, err := c.TaikoL2.ParentGasExcess(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
)
if err != nil {
return nil, fmt.Errorf("failed to fetch old parent gas excess: %w", err)
}
if parentGasExcess, err = c.TaikoL2.AdjustExcess(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
oldParentGasExcess,
parentGasTarget,
newGasTarget,
); err != nil {
return nil, fmt.Errorf("failed to adjust parent gas excess: %w", err)
}
} else {
if parentGasExcess, err = c.TaikoL2.ParentGasExcess(&bind.CallOpts{
BlockNumber: l2Head.Number, Context: ctx,
}); err != nil {
return nil, fmt.Errorf("failed to fetch parent gas excess: %w", err)
}
}
baseFeeInfo, err = c.TaikoL2.CalculateBaseFee(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
*baseFeeConfig,
currentTimestamp-l2Head.Time,
parentGasExcess,
uint32(l2Head.GasUsed),
)
if err != nil {
if baseFee, err = c.calculateBaseFeeOntake(ctx, l2Head, currentTimestamp, baseFeeConfig); err != nil {
return nil, err
}
} else {
baseFeeInfo, err = c.TaikoL2.GetBasefee(
baseFeeInfo, err := c.TaikoL2.GetBasefee(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
anchorBlockID.Uint64(),
uint32(l2Head.GasUsed),
)
if err != nil {
return nil, err
}
baseFee = baseFeeInfo.Basefee
}

if err != nil {
Expand All @@ -349,13 +308,13 @@ func (c *Client) CalculateBaseFee(

log.Info(
"Base fee information",
"fee", utils.WeiToGWei(baseFeeInfo.Basefee),
"fee", utils.WeiToGWei(baseFee),
"l2Head", l2Head.Number,
"anchorBlockID", anchorBlockID,
"isOntake", isOntake,
)

return baseFeeInfo.Basefee, nil
return baseFee, nil
}

// GetPoolContent fetches the transactions list from L2 execution engine's transactions pool with given
Expand Down Expand Up @@ -914,3 +873,68 @@ func (c *Client) WaitL1NewPendingTransaction(

return nil
}

// CalculateBaseFeeOnTake calculates the base fee after ontake fork from the L2 protocol.
func (c *Client) calculateBaseFeeOntake(
ctx context.Context,
l2Head *types.Header,
currentTimestamp uint64,
baseFeeConfig *bindings.LibSharedDataBaseFeeConfig,
) (*big.Int, error) {
var (
newGasTarget = uint64(baseFeeConfig.GasIssuancePerSecond) * uint64(baseFeeConfig.AdjustmentQuotient)
parentGasExcess uint64
)
parentGasTarget, err := c.TaikoL2.ParentGasTarget(&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx})
if err != nil {
return nil, fmt.Errorf("failed to fetch parent gas target: %w", err)
}

if newGasTarget != parentGasTarget && parentGasTarget != 0 {
oldParentGasExcess, err := c.TaikoL2.ParentGasExcess(&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx})
if err != nil {
return nil, fmt.Errorf("failed to fetch old parent gas excess: %w", err)
}
if parentGasExcess, err = c.TaikoL2.AdjustExcess(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
oldParentGasExcess,
parentGasTarget,
newGasTarget,
); err != nil {
// If the `calculateBaseFee()` method is deprecated, we will use the new method `getBasefeeV2()`
// to calculate the base fee.
if strings.Contains(encoding.TryParsingCustomError(err).Error(), "L2_DEPRECATED_METHOD") {
baseFeeInfo, err := c.TaikoL2.GetBasefeeV2(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
0,
*baseFeeConfig,
)
if err != nil {
return nil, fmt.Errorf("failed to calculate base fee by GetBasefeeV2: %w", err)
}
return baseFeeInfo.Basefee, nil
}

return nil, fmt.Errorf("failed to adjust parent gas excess: %w", err)
}
} else {
if parentGasExcess, err = c.TaikoL2.ParentGasExcess(&bind.CallOpts{
BlockNumber: l2Head.Number, Context: ctx,
}); err != nil {
return nil, fmt.Errorf("failed to fetch parent gas excess: %w", err)
}
}

baseFeeInfo, err := c.TaikoL2.CalculateBaseFee(
&bind.CallOpts{BlockNumber: l2Head.Number, Context: ctx},
*baseFeeConfig,
currentTimestamp-l2Head.Time,
parentGasExcess,
uint32(l2Head.GasUsed),
)
if err != nil {
return nil, err
}

return baseFeeInfo.Basefee, nil
}
Loading