Giter Site home page Giter Site logo

miguelmota / ethereum-development-with-go-book Goto Github PK

View Code? Open in Web Editor NEW
1.7K 58.0 413.0 9.99 MB

📖 A little guide book on Ethereum Development with Go (golang)

Home Page: https://goethereumbook.org

License: Other

Makefile 1.66% Go 88.70% CSS 1.61% Dockerfile 1.54% Solidity 6.49%
book ethereum go golang development tutorial free examples pdf ebook

ethereum-development-with-go-book's Introduction

Book cover


Ethereum Development with Go

A little guide book on Ethereum Development with Go (golang)

License Mentioned in Awesome Go PRs Welcome

Online

https://goethereumbook.org

E-book

The e-book is avaiable in different formats.

Languages

Contents

Help & Support

Development

Install dependencies:

make install

Run gitbook server:

make serve

Generating e-book in pdf, mobi, and epub format:

make ebooks

Visit http://localhost:4000

Contributing

Pull requests are welcome!

If making general content fixes:

  • please double check for typos and cite any relevant sources in the comments.

If updating code examples:

  • make sure to update both the code in the markdown files as well as the code in the code folder.

If wanting to add a new translation, follow these instructions:

  1. Set up development environment

  2. Add language to LANGS.md

  3. Copy the the en directory and rename it with the 2 letter language code of the language you're translating to (e.g. zh)

  4. Translate content

  5. Set "root" to "./" in book.json if not already set

Thanks

Thanks to @qbig and @gzuhlwang for the Chinese translation.

And thanks to all the contributors who have contributed to this guide book.

License

Released under the CC0-1.0 license.

© Miguel Mota

ethereum-development-with-go-book's People

Contributors

acharyab15 avatar aniket223 avatar anonyma avatar arberiii avatar asidorochev avatar codydjango avatar cypherhat avatar dandanua avatar diveinto avatar feifeiiiiiiiiiii avatar fokionzervoudakis avatar iczc avatar letsila avatar letubert avatar metachris avatar miguelmota avatar mirokuratczyk avatar petuhovskiy avatar philippgille avatar trabing avatar tsuu32 avatar xwjdsh avatar zeddee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ethereum-development-with-go-book's Issues

Question if you don’t mind on using Ganache

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)

abigen for complex contract example

Hi, for all the example within the book, the contract is simple. What if the contract is a complex one(with many libraries and helper contract in it)?

Let's say, uniswap StakingRewards contract https://etherscan.io/address/0xCA35e32e7926b96A9988f61d510E038108d8068e#code

Can you demonstrate how to interact with it? Many abi and bin file will generate, but specifically, I want interactive with the contractStakingRewards.

Also, I try to interact with TetherToken https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7#code

mkdir usdt
cd usdt
# touch TetherToken.sol and put code in it
solc --abi TetherToken.sol --out abi
solc --bin TetherToken.sol --out bin
abigen --bin=bin/TetherToken.bin --abi=abi/TetherToken.abi --pkg=usdt --out=usdt.go

then I get an error

Fatal: Failed to generate ABI binding: duplicated identifier "totalSupply"(normalized "TotalSupply"), use --alias for renaming

cannot access https://etherconverter.online?

The next step is to set the amount of ETH that we'll be transferring. However we must convert ether to wei since that's what the Ethereum blockchain uses. Ether supports up to 18 decimal places so 1 ETH is 1 plus 18 zeros. Here's a little tool to help you convert between ETH and wei: [https://etherconverter.online](https://etherconverter.online])

I found a typo.
[https://etherconverter.online](https://etherconverter.online]) should be
[https://etherconverter.online](https://etherconverter.online).
But I cannot access https://etherconverter.online (dead link?).
How about changing to alternative one?

Can't run client.go due to secp256k1.h missing

When trying to run client.go this error pops out:

# vendor/github.com/ethereum/go-ethereum/crypto/secp256k1
..\vendor\github.com\ethereum\go-ethereum\crypto\secp256k1\curve.go:42:10: fatal error: libsecp256k1/include/secp256k1.h: No such file or directory
   42 | #include "libsecp256k1/include/secp256k1.h"

My previous steps were:

go get github.com/ethereum/go-ethereum/ethclient
go mod vendor

Unpack parameters has changed

In the ebook, ABI.Unpack takes three arguments: interface{}, string and []byte. This seems to have changed in the recent versions of geth, which now has Unpack not take an interface, and instead returns a []interface{} representing the event's data.

Invalid sender

Good day. I run the code from page but have the answer like:

0x0c98048d
0x0000000000000000000000000725097f0677c83d099dfce69fab02d5e762bec5
0x000000000000000000000000000000000000000000000000000000174876e800
23064
2020/02/06 18:58:33 invalid sender

I check, i write correct private key, address of token and toAddress. I use a private blockchain with geth. Thanks a lot if anyone can help me.

code out dated

hi Miguel,

The below method does not exist anymore for types.Transaction

	ts := types.Transactions{signedTx}
	rawTxBytes := ts.GetRlp(0)

so, this file should update

I test with go ethereum v1.10.2

I find there are another two functions should do this:

https://github.com/ethereum/go-ethereum/blob/beee6b77a0ca8c26881dc2bb17c58e996c90a0c6/core/types/transaction.go#L119

https://github.com/ethereum/go-ethereum/blob/beee6b77a0ca8c26881dc2bb17c58e996c90a0c6/core/types/transaction.go#L86

but I don't know what to put in the method param (w io.Writer)

SubscribeFilterLogs: The method eth_logs does not exist/is not available

Hello @miguelmota I am following up your awesome book.
My requirement is to read the event log of a Transfer(from, to, value) function. I am using SubscribeFilterLogs for continuous log streaming and updating my other dbs.

But I am little bit stuck head over to this obfuscating error msg:

The method eth_logs does not exist/is not available

I have posted the issuse on slack:golang#ethereum room

I am quiet sure, error is pooping from this line:

query := ethereum.FilterQuery{
Addresses: []common.Address{contractAddress},
}
logs := make(chan types.Log)
sub, err := client.SubscribeFilterLogs(context.Background(), query, logs)

But couldn't figure out why though. The closest google match relation I found is on this thread: ethereum/go-ethereum#310 (comment)

Am I missing something import here. I have just started the websocket with admin.startWS("0.0.0.0", 8546, "*","*") , I am sure, I can telnet to that port.

/cc @miguelmota

transfer_token.go outdated, needs update, example provided

Current transfer_token.go is outdated. Unsure of when this happened but the current method of transacting ERC-20 tokens looks something like this:

https://gist.github.com/ckruger097/a156d04a5f3542eac20e6eea5129946d

Note: This is done from my own research, this may not be best practice but it has worked as a PoC. It may also be worth mentioning I created the token mentioned in the comments using https://vittominacori.github.io/erc20-generator/ & transferred to MetaMask first. Then sent the tokens to my local geth account running on a light node. Hopefully this helps someone.

Expired SSL Certificate

Hi.. just letting you know that the SSL cert for goethereumbook.org expired 2 days ago.

Thanks for producing such great content :)

the method shh_newKeyPair does not exist/is not available

Hey, I'm getting the above error for the following code

Geth version : 1.9.11-stable
Golang version : go1.13.6 windows/amd64

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/ethereum/go-ethereum/whisper/shhclient"
)



func main() {
    client, err := shhclient.Dial("ws://127.0.0.1:8546")
    if err != nil {
        log.Fatal(err)
    }
	keyID, err := client.NewKeyPair(context.Background())
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(keyID) // 0ec5cfe4e215239756054992dbc2e10f011db1cdfc88b9ba6301e2f9ea1b58d2
    
}


This is the exact error

2020/03/11 16:54:48 the method shh_newSymKey does not exist/is not available
exit status 1

This is what is showing in geth console

WARN [03-11|16:49:49.314] Served shh_newKeyPair                    reqid=1 t=0s err="the method shh_newKeyPair does not exist/is not available"

Simulated backend fails to run

I am trying to run the client_simulated.go and got an error:

github.com/ethereum/go-ethereum/accounts/abi/bind/backends.(*SimulatedBackend).SendTransaction(0xc0002a0800, 0x48c5a40, 0xc00019a000, 0xc0002b2990, 0x0, 0x0)
	/Users/billy/gocode/pkg/mod/github.com/ethereum/[email protected]/accounts/abi/bind/backends/simulated.go:562 +0x5cd
main.main()
	/Users/billy/Desktop/study/online-course/ethereum-intro/goethereumbook/code/client_simulated.go:60 +0x56b
exit status 2

I am using geth 1.9.8. It seems like the chainID is not defined when we create the simulated backend.

I am not sure if we can fix this by modifying your code since there are only 2 functions that create a simulated backend NewSimulatedBackendWithDatabase and NewSimulatedBackend and they don't allow you to define chainID

Just a question on token event reading.

Hi, @miguelmota

I am reading this github as I need to do something similar on ethereum, and I have to say this is a wonderful project and helps me a lot. 👍
Here I have a question while reading "Reading ERC-20 Token Event Logs" section. There we have code as below:

  	err := contractAbi.Unpack(&transferEvent, "Transfer", vLog.Data)
  	if err != nil {
  		log.Fatal(err)
  	}
  	
  	transferEvent.From = common.HexToAddress(vLog.Topics[1].Hex()) // ?
  	transferEvent.To = common.HexToAddress(vLog.Topics[2].Hex())     // ?
  	
  	fmt.Printf("From: %s\n", transferEvent.From.Hex())
  	fmt.Printf("To: %s\n", transferEvent.To.Hex())
  	fmt.Printf("Tokens: %s\n", transferEvent.Tokens.String()

My question is as this line
err := contractAbi.Unpack(&transferEvent, "Transfer", vLog.Data)
already parsed/unpacked the data (from/to fields from message), why we still need to get Hex data from vLog.Topics as below code shows ?

  	transferEvent.From = common.HexToAddress(vLog.Topics[1].Hex())
  	transferEvent.To = common.HexToAddress(vLog.Topics[2].Hex())

Based on my understanding, Topics stores the message type Hash, we may not get the correct from/to by convert Topics[*].Hex to address, correct me if I am wrong. :)
Thanks!

subscribe event logs code is outdated

when i follow this tutorial, and this code will generate an error when i miss Topics parameter:

ethereum.FilterQuery{
   	Topics:    [][]common.Hash{}, //comment this line and an error will occur
   	Addresses: []common.Address{i.contract},
   }

Errors encountered in param 1: Invalid value null supplied to : (RpcFilterRequest | undefined)/0: RpcFilterRequest/topics: (Array<(null | HASH | Array<(null | HASH)>)> | undefined)/0: Array<(null | HASH | Array<(null | HASH)>)>, Invalid value null supplied to : (RpcFilterRequest | undefined)/0: RpcFilterRequest/topics: (Array<(null | HASH | Array<(null | HASH)>)> | undefined)/1: undefined, Invalid value {"address":["0xa82ff9afd8f496c3d6ac40e2a0f282e47488cfc9"],"fromBlock":"0x0","toBlock":"latest","topics":null} supplied to : (RpcFilterRequest | undefined)/1: undefined

and when i test the code when i add Topics parameter in ethereum.FilterQuery{}, it still can‘t print any event logs here, below is my code:

package contracts

import (
	"context"
	"fmt"
	_ "fmt"
	//	"math/big"

	"github.com/ethereum/go-ethereum"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/golang/glog"
	"github.com/spf13/viper"
)

type ItemsObserver struct {
	client   *ethclient.Client
	contract common.Address
}

func NewItemsObserver(network, contract string) ItemsObserver {
	fmt.Printf("network: %s, contract: %s\n", network, contract)
	client, err := ethclient.Dial(network)
	if err != nil {
		glog.Fatalf("Fatal error ethclient, err: %s \n", err)
	}
	return ItemsObserver{
		client:   client,
		contract: common.HexToAddress(contract),
	}
}

func (i ItemsObserver) Start() {
	if i.client == nil {
		network := viper.GetString("network.localhost")
		fmt.Println("network:", network)
		client, err := ethclient.Dial(network)
		if err != nil {
			glog.Fatalf("Fatal error ")
		}
		i.client = client
	}
	if i.contract == common.HexToAddress("") {
		contract := viper.GetString("contracts.itemsDiamond")
		i.contract = common.HexToAddress(contract)
	}
	go i.run()
}

func (i ItemsObserver) run() {
	query := ethereum.FilterQuery{
		/*
			FromBlock: big.NewInt(0),
			ToBlock:   big.NewInt(1024),
			Topics:    [][]common.Hash{},
		*/
		Addresses: []common.Address{i.contract},
	}
	logs := make(chan types.Log)
	defer close(logs)
	sub, err := i.client.SubscribeFilterLogs(context.Background(), query, logs)
	if err != nil {
		glog.Fatal(err)
	}
	for {
		select {
		case err := <-sub.Err():
			glog.Fatal(err)
		case vLog := <-logs:
			fmt.Println(vLog)
		}
	}
}

event.sol:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;

contract JustTest {
    event T(uint256 indexed x);

    function eventTest() external {
        emit T(2222);
    }
}

deploy contract and call a function in one go

hi Miguel,

Usually, the process is to deploy contact first and then call the function.

But now the cost for deploy a contract is high.

I wonder if I can deploy a contract and call a function within one transaction?

You may think why I want to do this because it's a special temporary contract, I just need to call the function once in mainnet , and what's more, I don't really need to submit it to chain, I just want to estimate gas for that function.

Suggestion: Inline solidity compilation

I find the Smart Contract Compilation & ABI could use inline solidity compilation using go instead of using external command and use abi file. Here's an example:

  • Counter.sol
pragma solidity >=0.6.0;

contract Counter {
    uint256 x;

    constructor() public {
        x = 42;
    }

    function add(uint256 y) public returns (uint256) {
        x = x + y;
        return x;
    }
}
  • test_counter.go
	import (
		"github.com/ethereum/go-ethereum/common"
		"github.com/ethereum/go-ethereum/common/compiler"
	)
..........
	counterSrc, err := filepath.Abs(PATH_TO_SOL_FILE)
	if err != nil {
		t.Fatal(err)
	}
	contracts, err := compiler.CompileSolidity("", counterSrc)
	if err != nil {
		t.Fatal(err)
	}
	contract, _ := contracts[fmt.Sprintf("%s:%s", source, "Counter")]
	code := common.Hex2Bytes(contract.Code[2:])

	nonce := uint64(0)
	tx := types.NewContractCreation(nonce, big.NewInt(0), uint64(gasLimit), gasPrice, code)
	signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID),  privateKey)

Please node that you can cache the code if contract does not change so you don't have to compile again. This removes one additional step for beginner and make sure that our go code always works with most up-to-date solidity file.

User still needs to install solc though since the go compiler still call solc externally.

"403 Forbidden" when using "client.BalanceAt(context.Background(), account, blockNumber)"

I run the code in this page. The logs are:

13565093091594031083
2019/12/09 16:21:15 403 Forbidden {"jsonrpc":"2.0","id":2,"error":{"code":-32002,"message":"project ID is required to access archive state","data":{"see":"https://infura.io/dashboard"}}}

It shows that client.BalanceAt(context.Background(), account, nil) return the balance, but balanceAt, err := client.BalanceAt(context.Background(), account, blockNumber) not.

I wonder what's the reason here and how to fix it. Thanks a lot if anyone can help me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.