Skip to content

Commit

Permalink
Merge pull request #99 from onflow/gregor/gas-estimate-fix
Browse files Browse the repository at this point in the history
Fix gas estimation with configured account
  • Loading branch information
sideninja authored Feb 28, 2024
2 parents 92b83d3 + 8892da5 commit 4a6f380
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 83 deletions.
23 changes: 15 additions & 8 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,30 +503,37 @@ func (b *BlockChainAPI) EstimateGas(
data = *args.Input
}

eoa := newEOATestAccount(b.config.COAKey.String()[2:])
// Provide a high enough gas for the tx to be able to execute
gas := uint64(15_000_000)
// provide a high enough gas for the tx to be able to execute
defaultGasLimit := uint64(15_000_000)
if args.Gas != nil {
gas = uint64(*args.Gas)
defaultGasLimit = uint64(*args.Gas)
}

txData := eoa.PrepareSignAndEncodeTx(
txData, err := signGasEstimationTx(
args.To,
data,
(*big.Int)(args.Value),
gas,
defaultGasLimit,
(*big.Int)(args.GasPrice),
)
if err != nil {
b.logger.Error().Err(err).Msg("failed to sign transaction for gas estimate")
return hexutil.Uint64(defaultGasLimit), nil // return default gas limit
}

gas, err := b.evm.EstimateGas(ctx, txData)
estimatedGas, err := b.evm.EstimateGas(ctx, txData)
if err != nil {
b.logger.Error().Err(err).Msg("failed to estimate gas")
return hexutil.Uint64(0), err
}

return hexutil.Uint64(gas), nil
return hexutil.Uint64(estimatedGas), nil
}

// handleError takes in an error and in case the error is of type ErrNotFound
// it returns nil instead of an error since that is according to the API spec,
// if the error is not of type ErrNotFound it will return the error and the generic
// empty type.
func handleError[T any](log zerolog.Logger, err error) (T, error) {
var zero T
if errors.Is(err, storageErrs.ErrNotFound) {
Expand Down
75 changes: 0 additions & 75 deletions api/eoa_test_account.go

This file was deleted.

42 changes: 42 additions & 0 deletions api/gas_estimation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package api

import (
"fmt"
"math/big"

gethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
gethCrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/onflow/flow-go/fvm/evm/emulator"
)

var key, _ = gethCrypto.HexToECDSA("45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8")
var signer = emulator.GetDefaultSigner()

// signGasEstimationTx will create a transaction and sign it with a test account
// used only for gas estimation, since gas estimation is run inside a script
// and does not change the state this account is not important it just has to be valid.
func signGasEstimationTx(
to *gethCommon.Address,
data []byte,
amount *big.Int,
gasLimit uint64,
gasPrice *big.Int,
) ([]byte, error) {
tx := types.NewTx(
&types.LegacyTx{
Nonce: 0,
To: to,
Value: amount,
Gas: gasLimit,
GasPrice: gasPrice,
Data: data,
},
)
tx, err := types.SignTx(tx, signer, key)
if err != nil {
return nil, fmt.Errorf("failed to sign tx for gas estimation: %w", err)
}

return tx.MarshalBinary()
}

0 comments on commit 4a6f380

Please sign in to comment.