golang geth test

deploy

go
func main (){
    pk, err := keyStoreToPrivateKey("/root/golang/mvc/UTC--2020-01-03T05-58-33.219268500Z--21d10c5e114a959e6e92c6f5f0ffcbe7df7785f1", "654321")
    if err != nil {
        log.Fatal(err)
    }
    privateKey, err := crypto.HexToECDSA(pk)
    if err != nil {
        log.Fatal("privateKey ", err)

    }

    publicKey := privateKey.Public()

    publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
    if !ok {
        log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
    }

    fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)

    nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
    if err != nil {
        log.Fatal(err)
    }

    gasPrice, err := client.SuggestGasPrice(context.Background())
    if err != nil {
        log.Fatal(err)
    }

    auth := bind.NewKeyedTransactor(privateKey)
    auth.Nonce = big.NewInt(int64(nonce))
    auth.Value = big.NewInt(0)     // in wei
    auth.GasLimit = uint64(300000) // in units
    auth.GasPrice = gasPrice

    input := "1.0"
    address, tx, instance, err := DeployStore(auth, client, input)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(address.Hex())   // 0x147B8eb97fD247D06C4006D269c90C1908Fb5D54
    fmt.Println(tx.Hash().Hex()) // 0xdae8ba5444eefdc99f4d45cd0c4f24056cba6a02cefbf78066ef9f4188ff7dc0
    fmt.Println(instance)

}

load contract

go
    address := common.HexToAddress("0x3484dfb6aCED276ABC3b9824266f03295139507A")
    instance, err := NewStore(address, client)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("contract is loaded")
    fmt.Println(instance)

golang ganache test

那麼要加速一個合約的部屬與debug我們來開始學習新的ganache 這樣我們就不用像在geth 要在挖礦讓我們的合約才能執行。 我們是run 在wsl 來重選一下網路 確實連接到

https://medium.com/@daniel.mars622/%E9%96%8B%E7%99%BC%E8%88%87%E6%B8%AC%E8%A9%A6dapp%E5%85%8D%E8%A3%9D%E7%AF%80%E9%BB%9E-c9f446128da4 https://medium.com/my-blockchain-development-daily-journey/%E4%BB%8B%E7%B4%B9-truffle-suite-%E5%92%8Cdapp-development-7d747d13cf2b

那麼照前面的合約部屬我們要換成

privateKey

go
func main (){
    client, err := ethclient.Dial("http://172.20.64.1:7545")
    if err != nil {
        log.Fatal(err)
    }
    privateKey, err := crypto.HexToECDSA("a1fbeac5c525dbe3c1bac85bd0f5ba369ec693282e22eb89e36e26059ad36e18")
    if err != nil {
        log.Fatal(err)
    }

    publicKey := privateKey.Public()
    publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
    if !ok {
        log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
    }
    gasPrice, err := client.SuggestGasPrice(context.Background())
    if err != nil {
        log.Fatal(err)
    }

    auth := bind.NewKeyedTransactor(privateKey)
    auth.Nonce = big.NewInt(int64(nonce))
    auth.Value = big.NewInt(0) // in wei
    auth.GasLimit = uint64(0)  // in units
    auth.GasPrice = gasPrice

    input := "1.0"
    address, tx, instance, err := DeployStore(auth, client, input)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(address.Hex())   // 0x3B394d5cb1e395744DD99433619556351BE34689
    fmt.Println(tx.Hash().Hex()) // 0x3232ae7f29e6fb43aec8f49a8a4221a40a4854cc826cc90e556991efb7d4757e
    fmt.Println(instance)

}

部屬完可以看到 eth變了

load smart contr

go
    address := common.HexToAddress("0x3B394d5cb1e395744DD99433619556351BE34689")
    instance, err := NewStore(address, client)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("contract is loaded")

get contract value

go
    address := common.HexToAddress("0x3B394d5cb1e395744DD99433619556351BE34689")
    instance, err := NewStore(address, client)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("contract is loaded")
    version, err := instance.Version(nil)
    if err != nil {
        log.Fatal(err)
    }
    // // _ = instance
    fmt.Println(instance)
    fmt.Println(version)

write smart contract

換個人 PRIVATE KEY

go
    client, err := ethclient.Dial("http://172.20.64.1:7545")
    if err != nil {
        log.Fatal(err)
    }
    privateKey, err := crypto.HexToECDSA("38a30755101fb988c5568d4b50655249245bdf28d0ffae12d726a0d5654d0c2f")
    if err != nil {
        log.Fatal(err)
    }

    publicKey := privateKey.Public()
    publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
    if !ok {
        log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
    }

    fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
    nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
    if err != nil {
        log.Fatal(err)
    }
    gasPrice, err := client.SuggestGasPrice(context.Background())
    if err != nil {
        log.Fatal(err)
    }

    auth := bind.NewKeyedTransactor(privateKey)
    auth.Nonce = big.NewInt(int64(nonce))
    auth.Value = big.NewInt(0) // in wei
    auth.GasLimit = uint64(0)  // in units
    auth.GasPrice = gasPrice

    address := common.HexToAddress("0x3B394d5cb1e395744DD99433619556351BE34689")
    instance, err := NewStore(address, client)
    if err != nil {
        log.Fatal(err)
    }

    key := [32]byte{}
    value := [32]byte{}
    copy(key[:], []byte("foo"))
    copy(value[:], []byte("bar"))

    tx, err := instance.SetItem(auth, key, value)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("tx sent: %s", tx.Hash().Hex()) 

    result, err := instance.Items(nil, key)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(string(result[:])) // "bar"

get smart contract binary code

go
    contractAddress := common.HexToAddress("0x3B394d5cb1e395744DD99433619556351BE34689")
    bytecode, err := client.CodeAt(context.Background(), contractAddress, nil) // nil is latest block
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(hex.EncodeToString(bytecode)) // 60806...10029