9

I found the following example for working with the SimulatedBackend. Until recently that worked fine, however this code now results in a panic (panic: max fee per gas less than block base fee: address 0xbc8153EE0b1E9B1f1E8153945400dc38EDbD8638, maxFeePerGas: 1 baseFee: 875000000). I suspect it's related to the London update.

package eth

import (
    "context"
    "math/big"
    "testing"

    "github.com/ethereum/go-ethereum/accounts/abi/bind"
    "github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/crypto"
)

func TestSimulatedBackend(t *testing.T) {
    privateKey, err := crypto.GenerateKey()
    if err != nil {
        t.Fatalf("failed to generate key: %v", err)
    }

    auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(1337))
    if err != nil {
        t.Fatalf("failed to generate transaction: %v", err)
    }

    balance := new(big.Int)
    balance.SetString("10000000000000000000", 10) // 10 eth in wei

    address := auth.From
    genesisAlloc := map[common.Address]core.GenesisAccount{
        address: {
            Balance: balance,
        },
    }

    blockGasLimit := uint64(4712388)
    client := backends.NewSimulatedBackend(genesisAlloc, blockGasLimit)

    fromAddress := auth.From
    nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
    if err != nil {
        t.Fatalf("failed to generate nonce: %v", err)
    }

    value := big.NewInt(1000000000000000000) // in wei (1 eth)
    gasLimit := uint64(21000)                // in units
    gasPrice, err := client.SuggestGasPrice(context.Background())
    if err != nil {
        t.Fatalf("failed to generate suggested gas price: %v", err)
    }

    toAddress := common.HexToAddress("0x4592d8f8d7b001e72cb26a73e4fa1806a51ac79d")
    var data []byte
    tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data)

    chainID := big.NewInt(1337)
    signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
    if err != nil {
        t.Fatalf("failed to sign transaction: %v", err)
    }

    err = client.SendTransaction(context.Background(), signedTx)
    if err != nil {
        t.Fatalf("failed to send transaction: %v", err)
    }

    t.Logf("tx sent: %s\n", signedTx.Hash().Hex()) // tx sent: 0xec3ceb05642c61d33fa6c951b54080d1953ac8227be81e7b5e4e2cfed69eeb51

    client.Commit()

    receipt, err := client.TransactionReceipt(context.Background(), signedTx.Hash())
    if err != nil {
        t.Fatalf("failed to create transaction receipt: %v", err)
    }
    if receipt == nil {
        t.Fatal("receipient is empty")
    }

    t.Logf("status: %v\n", receipt.Status) // status: 1
}

Even if I add auth.GasFeeCap = big.NewInt(875000000) I still get the same panic. I have no idea where else I'm supposed to to set this.

Does anyone have a working example or anyone able to explain me what I'm doing wrong?

Ruli
  • 2,592
  • 12
  • 30
  • 40
Arjan van Eersel
  • 119
  • 1
  • 1
  • 6
  • 2
    Did you find answer to this? I am struggling with similar issue. Please post your response if you already found the solution. Thanks. – Maddy Nov 02 '21 at 17:51

2 Answers2

1

The simulated backend is returning 1 which is the number of gwei it sets the initial base fee to, but the base fee and gas price actually expects units of wei. So just set the gas price to 1000000000 when using the simulated backend.

Cryptonium
  • 11
  • 1
-1

print out the payload , and check is the parameter is correct.

in my case, the from address is 0x0000... and the gas price is actually very low. so the network compliants for this error message.

just increate the gas , and problem solved.

Siwei
  • 19,858
  • 7
  • 75
  • 95
  • Can you explain how to increase the gas? somebody? thanks! – staminna Sep 12 '22 at 23:30
  • in the origin question you can find this line of code: `gasPrice, err := client.SuggestGasPrice(context.Background())` just simply increase this variable , will work. if you are using another programming language such as javascript, you can use `ethers` lib and there are ton of examples out there. – Siwei Sep 14 '22 at 00:16