Skip to content

Commit

Permalink
[api] Fix out of gas for new types tx (#4507)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc authored Nov 29, 2024
1 parent 9922923 commit 584f187
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
28 changes: 5 additions & 23 deletions api/web3server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"encoding/json"
"fmt"
"io"
"math/big"
"strconv"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/eth/tracers/logger"
Expand All @@ -32,7 +30,6 @@ import (
apitypes "github.com/iotexproject/iotex-core/v2/api/types"
"github.com/iotexproject/iotex-core/v2/pkg/log"
"github.com/iotexproject/iotex-core/v2/pkg/tracer"
"github.com/iotexproject/iotex-core/v2/pkg/util/addrutil"
)

const (
Expand Down Expand Up @@ -440,32 +437,17 @@ func (svr *web3Handler) estimateGas(in *gjson.Result) (interface{}, error) {
if err != nil {
return nil, err
}
from, to := callMsg.From, callMsg.To
var (
tx *types.Transaction
toAddr *common.Address
)
if len(to) != 0 {
addr, err := addrutil.IoAddrToEvmAddr(to)
if err != nil {
return nil, err
}
toAddr = &addr
}
tx = types.NewTx(&types.LegacyTx{
Nonce: 0,
GasPrice: big.NewInt(0),
Gas: callMsg.Gas,
To: toAddr,
Value: callMsg.Value,
Data: callMsg.Data,
})
tx, err := callMsg.toUnsignedTx(svr.coreService.EVMNetworkID())
if err != nil {
return nil, err
}
elp, err := svr.ethTxToEnvelope(tx)
if err != nil {
return nil, err
}

var estimatedGas uint64
from := callMsg.From
switch act := elp.Action().(type) {
case *action.Execution:
estimatedGas, err = svr.coreService.EstimateExecutionGasConsumption(context.Background(), elp, from)
Expand Down
1 change: 1 addition & 0 deletions api/web3server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ func TestEstimateGas(t *testing.T) {
core.EXPECT().ChainID().Return(uint32(1)).Times(2)

t.Run("estimate execution", func(t *testing.T) {
core.EXPECT().EVMNetworkID().Return(uint32(0)).AnyTimes()
core.EXPECT().Account(gomock.Any()).Return(&iotextypes.AccountMeta{IsContract: true}, nil, nil)
core.EXPECT().EstimateExecutionGasConsumption(gomock.Any(), gomock.Any(), gomock.Any()).Return(uint64(11000), nil)

Expand Down
46 changes: 46 additions & 0 deletions api/web3server_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,52 @@ func parseCallObject(in *gjson.Result) (*callMsg, error) {
}, nil
}

func (call *callMsg) toUnsignedTx(chainID uint32) (*types.Transaction, error) {
var (
tx *types.Transaction
toAddr *common.Address
)
if len(call.To) != 0 {
addr, err := addrutil.IoAddrToEvmAddr(call.To)
if err != nil {
return nil, err
}
toAddr = &addr
}
switch {
case call.GasFeeCap != nil || call.GasTipCap != nil:
tx = types.NewTx(&types.DynamicFeeTx{
ChainID: big.NewInt(int64(chainID)),
GasTipCap: big.NewInt(0),
GasFeeCap: big.NewInt(0),
Gas: call.Gas,
To: toAddr,
Value: call.Value,
Data: call.Data,
AccessList: call.AccessList,
})
case call.AccessList != nil:
tx = types.NewTx(&types.AccessListTx{
ChainID: big.NewInt(int64(chainID)),
GasPrice: big.NewInt(0),
Gas: call.Gas,
To: toAddr,
Value: call.Value,
Data: call.Data,
AccessList: call.AccessList,
})
default:
tx = types.NewTx(&types.LegacyTx{
GasPrice: big.NewInt(0),
Gas: call.Gas,
To: toAddr,
Value: call.Value,
Data: call.Data,
})
}
return tx, nil
}

func (svr *web3Handler) getLogQueryRange(fromStr, toStr string, logHeight uint64) (from uint64, to uint64, hasNewLogs bool, err error) {
if from, to, err = svr.parseBlockRange(fromStr, toStr); err != nil {
return
Expand Down

0 comments on commit 584f187

Please sign in to comment.