dongwuzun4630 2019-01-28 13:39
浏览 500

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

I am trying to deploy ethereum smart contracts in go sdk but i am getting some error as

./inbox_test.go:20:44: not enough arguments in call to backends.NewSimulatedBackend
        have (core.GenesisAlloc)
        want (core.GenesisAlloc, uint64)

I am following step by step guide to deploy smart contract in go and i am not able to do this

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")
    }

}

This code should deploy smart contract in go sdk

  • 写回答

1条回答 默认 最新

  • dougongyou7364 2019-04-18 09:47
    关注

    The method signature for NewSimulatedBackend has changed. As is:

    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
    }
    

    You need also pass gasLimit along. Something like this:

    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)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)