diff --git a/api/web3server.go b/api/web3server.go index 414e1d009e..df14d98b45 100644 --- a/api/web3server.go +++ b/api/web3server.go @@ -26,6 +26,7 @@ import ( "google.golang.org/grpc/status" "github.com/iotexproject/iotex-core/v2/action" + "github.com/iotexproject/iotex-core/v2/action/protocol" rewardingabi "github.com/iotexproject/iotex-core/v2/action/protocol/rewarding/ethabi" stakingabi "github.com/iotexproject/iotex-core/v2/action/protocol/staking/ethabi" apitypes "github.com/iotexproject/iotex-core/v2/api/types" @@ -170,6 +171,8 @@ func (svr *web3Handler) handleWeb3Req(ctx context.Context, web3Req *gjson.Result res, err = svr.maxPriorityFee() case "eth_feeHistory": res, err = svr.feeHistory(ctx, web3Req) + case "eth_blobBaseFee": + res, err = svr.blobBaseFee() case "eth_getBlockByHash": res, err = svr.getBlockByHash(web3Req) case "eth_chainId": @@ -368,6 +371,14 @@ func (svr *web3Handler) getChainID() (interface{}, error) { return uint64ToHex(uint64(svr.coreService.EVMNetworkID())), nil } +func (svr *web3Handler) blobBaseFee() (interface{}, error) { + blk, err := svr.coreService.BlockByHeight(svr.coreService.TipHeight()) + if err != nil { + return nil, err + } + return bigIntToHex(protocol.CalcBlobFee(protocol.CalcExcessBlobGas(blk.Block.ExcessBlobGas(), blk.Block.BlobGasUsed()))), nil +} + func (svr *web3Handler) getBlockNumber() (interface{}, error) { return uint64ToHex(svr.coreService.TipHeight()), nil } diff --git a/api/web3server_integrity_test.go b/api/web3server_integrity_test.go index 8c3dc89938..65dab79027 100644 --- a/api/web3server_integrity_test.go +++ b/api/web3server_integrity_test.go @@ -161,6 +161,10 @@ func TestWeb3ServerIntegrity(t *testing.T) { t.Run("eth_feeHistory", func(t *testing.T) { feeHistory(t, handler, bc, dao, actPool) }) + + t.Run("eth_blobBaseFee", func(t *testing.T) { + blobBaseFee(t, handler, bc, dao, actPool) + }) } func setupTestServer() (*ServerV2, blockchain.Blockchain, blockdao.BlockDAO, actpool.ActPool, func()) { @@ -849,3 +853,22 @@ func feeHistory(t *testing.T, handler *hTTPHandler, bc blockchain.Blockchain, da }`, oldnest), string(actual)) } } + +func blobBaseFee(t *testing.T, handler *hTTPHandler, bc blockchain.Blockchain, dao blockdao.BlockDAO, actPool actpool.ActPool) { + require := require.New(t) + for _, test := range []struct { + params string + expected int + }{ + {`[]`, 1}, + } { + result := serveTestHTTP(require, handler, "eth_blobBaseFee", test.params) + if test.expected == 0 { + require.Nil(result) + continue + } + actual, ok := result.(string) + require.True(ok) + require.Equal("0x1", actual) + } +} diff --git a/api/web3server_utils.go b/api/web3server_utils.go index 8e1ad917d5..6b07df3c63 100644 --- a/api/web3server_utils.go +++ b/api/web3server_utils.go @@ -60,10 +60,7 @@ func uint64ToHex(val uint64) string { } func bigIntToHex(b *big.Int) string { - if b == nil { - return "0x0" - } - if b.Sign() == 0 { + if b == nil || b.Sign() == 0 { return "0x0" } return "0x" + b.Text(16)