go - 如何在 Go SDK 中部署以太坊智能合约
问题描述
我正在尝试在 go sdk 中部署以太坊智能合约,但我遇到了一些错误
./inbox_test.go:20:44: not enough arguments in call to backends.NewSimulatedBackend
have (core.GenesisAlloc)
want (core.GenesisAlloc, uint64)
我正在按照分步指南在 go 中部署智能合约,但我无法做到这一点
func TestDeployInbox(t *testing.T) {
//Setup simulated block chain
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactor(key)
alloc := make(core.GenesisAlloc)
alloc[auth.From] = core.GenesisAccount{Balance: big.NewInt(1000000000)}
blockchain := backends.NewSimulatedBackend(alloc)
//Deploy contract
address, _, _, err := DeployInbox(
auth,
blockchain,
"Hello World",
)
// commit all pending transactions
blockchain.Commit()
if err != nil {
t.Fatalf("Failed to deploy the Inbox contract: %v", err)
}
if len(address.Bytes()) == 0 {
t.Error("Expected a valid deployment address. Received empty address byte array instead")
}
}
此代码应在 go sdk 中部署智能合约
解决方案
的方法签名NewSimulatedBackend
已更改。原样:
https://github.com/ethereum/go-ethereum/blob/master/accounts/abi/bind/backends/simulated.go#L68
// NewSimulatedBackend creates a new binding backend using a simulated blockchain
// for testing purposes.
func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
database := rawdb.NewMemoryDatabase()
genesis := core.Genesis{Config: params.AllEthashProtocolChanges, GasLimit: gasLimit, Alloc: alloc}
genesis.MustCommit(database)
blockchain, _ := core.NewBlockChain(database, nil, genesis.Config, ethash.NewFaker(), vm.Config{}, nil)
backend := &SimulatedBackend{
database: database,
blockchain: blockchain,
config: genesis.Config,
events: filters.NewEventSystem(new(event.TypeMux), &filterBackend{database, blockchain}, false),
}
backend.rollback()
return backend
}
你也需要传递gasLimit
。像这样的东西:
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactor(key)
auth.GasLimit = uint64(300000) // in units
auth.GasPrice = gasPrice
alloc := make(core.GenesisAlloc)
alloc[auth.From] = core.GenesisAccount{Balance: big.NewInt(1000000000)}
blockchain := backends.NewSimulatedBackend(alloc, auth.GasLimit)
推荐阅读
- flutter - Flutter setState 更改状态,但不会重新渲染
- xml - 复制节点后创建元素
- html - 使用 HTML 使表格行不可点击
- xml - 使用 XPath 获取 XML 部分
- haskell - 当 newtype 是 Functor 时,是否会删除对 fmap 的调用?
- docker - Docker不断创建具有root权限的文件夹
- python - 如何在 Django 模型中使用查询?
- python - 如何减少这个嵌套 for 循环的运行时间?
- javascript - process.env 返回未定义
- reactjs - React:我可以使用 refs 从父级修改子级的功能吗?