Skip to content

Commit

Permalink
[api] impl eth_blobBaseFee (#4528)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc authored Dec 31, 2024
1 parent 40838e9 commit bf606c0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
11 changes: 11 additions & 0 deletions api/web3server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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
}
Expand Down
23 changes: 23 additions & 0 deletions api/web3server_integrity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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)
}
}
5 changes: 1 addition & 4 deletions api/web3server_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bf606c0

Please sign in to comment.