登录
首页 >  Golang >  Go问答

在Go SDK中进行以太坊智能合约部署指南

来源:stackoverflow

时间:2024-02-28 16:27:28 213浏览 收藏

golang学习网今天将给大家带来《在Go SDK中进行以太坊智能合约部署指南》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习Golang或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!

问题内容

我正在尝试在 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)

今天关于《在Go SDK中进行以太坊智能合约部署指南》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>