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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With