Skip to content

Commit bf606c0

Browse files
authored
[api] impl eth_blobBaseFee (#4528)
1 parent 40838e9 commit bf606c0

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

api/web3server.go

+11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"google.golang.org/grpc/status"
2727

2828
"github.com/iotexproject/iotex-core/v2/action"
29+
"github.com/iotexproject/iotex-core/v2/action/protocol"
2930
rewardingabi "github.com/iotexproject/iotex-core/v2/action/protocol/rewarding/ethabi"
3031
stakingabi "github.com/iotexproject/iotex-core/v2/action/protocol/staking/ethabi"
3132
apitypes "github.com/iotexproject/iotex-core/v2/api/types"
@@ -170,6 +171,8 @@ func (svr *web3Handler) handleWeb3Req(ctx context.Context, web3Req *gjson.Result
170171
res, err = svr.maxPriorityFee()
171172
case "eth_feeHistory":
172173
res, err = svr.feeHistory(ctx, web3Req)
174+
case "eth_blobBaseFee":
175+
res, err = svr.blobBaseFee()
173176
case "eth_getBlockByHash":
174177
res, err = svr.getBlockByHash(web3Req)
175178
case "eth_chainId":
@@ -368,6 +371,14 @@ func (svr *web3Handler) getChainID() (interface{}, error) {
368371
return uint64ToHex(uint64(svr.coreService.EVMNetworkID())), nil
369372
}
370373

374+
func (svr *web3Handler) blobBaseFee() (interface{}, error) {
375+
blk, err := svr.coreService.BlockByHeight(svr.coreService.TipHeight())
376+
if err != nil {
377+
return nil, err
378+
}
379+
return bigIntToHex(protocol.CalcBlobFee(protocol.CalcExcessBlobGas(blk.Block.ExcessBlobGas(), blk.Block.BlobGasUsed()))), nil
380+
}
381+
371382
func (svr *web3Handler) getBlockNumber() (interface{}, error) {
372383
return uint64ToHex(svr.coreService.TipHeight()), nil
373384
}

api/web3server_integrity_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ func TestWeb3ServerIntegrity(t *testing.T) {
161161
t.Run("eth_feeHistory", func(t *testing.T) {
162162
feeHistory(t, handler, bc, dao, actPool)
163163
})
164+
165+
t.Run("eth_blobBaseFee", func(t *testing.T) {
166+
blobBaseFee(t, handler, bc, dao, actPool)
167+
})
164168
}
165169

166170
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
849853
}`, oldnest), string(actual))
850854
}
851855
}
856+
857+
func blobBaseFee(t *testing.T, handler *hTTPHandler, bc blockchain.Blockchain, dao blockdao.BlockDAO, actPool actpool.ActPool) {
858+
require := require.New(t)
859+
for _, test := range []struct {
860+
params string
861+
expected int
862+
}{
863+
{`[]`, 1},
864+
} {
865+
result := serveTestHTTP(require, handler, "eth_blobBaseFee", test.params)
866+
if test.expected == 0 {
867+
require.Nil(result)
868+
continue
869+
}
870+
actual, ok := result.(string)
871+
require.True(ok)
872+
require.Equal("0x1", actual)
873+
}
874+
}

api/web3server_utils.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ func uint64ToHex(val uint64) string {
6060
}
6161

6262
func bigIntToHex(b *big.Int) string {
63-
if b == nil {
64-
return "0x0"
65-
}
66-
if b.Sign() == 0 {
63+
if b == nil || b.Sign() == 0 {
6764
return "0x0"
6865
}
6966
return "0x" + b.Text(16)

0 commit comments

Comments
 (0)