miguelmota / ethereum-development-with-go-book

📖 A little guide book on Ethereum Development with Go (golang)
https://goethereumbook.org
Other
1.74k stars 427 forks source link

Question if you don’t mind on using Ganache #1

Closed deckarep closed 6 years ago

deckarep commented 6 years ago

Hi @miguelmota,

Fantastic work on this book it’s written very well and so streamlined for getting up to speed.

Question for you though: do you think you can show an example of getting the Go client talking to Ganache which seems to be the de facto development version of the Blockchain?

I mean, is it as simple as changing the DNS name to point to the local instance of Ganache?

I’d like to get that working but I haven’t investigated what it would all take but thought it would be useful in this book.

What do you think? And great work.

-Deckarep (Ralph)

miguelmota commented 6 years ago

@deckarep Thanks! glad it's useful 🙂

Talking to ganche is just as simple as talking to an infura endpoint. Here are full instructions to help you get started (will include this in the book as well):

Install ganache

npm install -g ganache-cli

Run ganache cli

ganache-cli -m "much repair shock carbon improve miss forget sock include bullet interest solution"

Connect to ganache rpc host on http://localhost:8545

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/ethclient"
)

func main() {
    client, err := ethclient.Dial("http://localhost:8545")
    if err != nil {
        log.Fatal(err)
    }

    account := common.HexToAddress("0xe280029a7867ba5c9154434886c241775ea87e53")
    balance, err := client.BalanceAt(context.Background(), account, nil)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(balance) // 100000000000000000000
}

Let me know if you're having issues

deckarep commented 6 years ago

Bingo, this is what I was looking for. I still think it would be worth adding to the book as an aside...but thanks for the detailed explanation. Looks easy enough!

miguelmota commented 6 years ago

Awesome 👍 added to the setting up the client section

https://goethereumbook.org/client-setup/#using-ganache