使用Go-Ethereum部署Solidity合约报transaction type not supported错误如何解决
错误根因
- 你使用的go-ethereum v1.10.15+版本默认启用EIP-1559交易类型,abigen生成的绑定代码默认会尝试发送EIP-1559类型交易,但你初始化的
bind.TransactOpts未配置EIP-1559要求的手续费参数,同时你的代码存在笔误,导致交易校验失败返回transaction type not supported错误。
解决方案
有两种修复方式,选择任意一种即可:
方案1:强制使用Legacy旧类型交易
在生成auth对象后添加以下配置,强制使用兼容旧版本的Legacy交易:
auth.GasPrice = big.NewInt(1000000000) // 模拟环境可随意设置gas价格 auth.Type = bind.LegacyTxType
方案2:补全EIP-1559交易的手续费参数
按照EIP-1559规范补充小费和最高gas费配置:
auth.GasTipCap = big.NewInt(1000000000) // 优先级小费 auth.GasFeeCap = big.NewInt(2000000000) // 每单位gas最高费用
修复代码笔误
你当前代码中定义的模拟后端变量名为sim,但提交交易时调用的是blockchain.Commit(),属于变量名拼写错误,修改为:
sim.Commit()
修改后完整测试代码
package Contract import ( "testing" "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/core" "math/big" ) // 测试合约部署逻辑 func TestMain(t *testing.T) { // 初始化模拟区块链 key, _ := crypto.GenerateKey() auth := bind.NewKeyedTransactor(key) // 新增配置:强制使用Legacy交易(或替换为EIP-1559参数配置) auth.GasPrice = big.NewInt(1e9) auth.Type = bind.LegacyTxType alloc := make(core.GenesisAlloc) alloc[auth.From] = core.GenesisAccount{Balance: big.NewInt(1337000000000000000)} // 建议余额设大一点避免gas不足 gasLimit := uint64(3000000) // 调高gas limit避免部署失败 sim := backends.NewSimulatedBackend(alloc, gasLimit) // 部署合约,函数名替换为abigen实际生成的部署函数名称 address, _, _, err := DeploySendMSG( auth, sim, ) // 提交所有待处理交易 sim.Commit() if err != nil { t.Fatalf("合约部署失败: %v", err) } t.Logf("合约部署成功,地址:%s", address.Hex()) }
内容的提问来源于stack exchange,提问作者Kevin Jiang
相关产品推荐
相关产品推荐

