首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Go SDK中部署以太坊智能合约

如何在Go SDK中部署以太坊智能合约
EN

Stack Overflow用户
提问于 2019-01-28 21:39:07
回答 1查看 240关注 0票数 1

我正在尝试在go sdk中部署ethereum智能合约,但我收到一些错误,因为

代码语言:javascript
复制
./inbox_test.go:20:44: not enough arguments in call to backends.NewSimulatedBackend
        have (core.GenesisAlloc)
        want (core.GenesisAlloc, uint64)

我正在按照逐步指南在go中部署智能合约,但我无法做到这一点

代码语言:javascript
复制
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中部署智能合约

EN

回答 1

Stack Overflow用户

发布于 2019-04-18 17:47:14

NewSimulatedBackend的方法签名已更改。按原样:

https://github.com/ethereum/go-ethereum/blob/master/accounts/abi/bind/backends/simulated.go#L68

代码语言:javascript
复制
// 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。如下所示:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54403243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档