miguelmota / ethereum-development-with-go-book Goto Github PK
View Code? Open in Web Editor NEW📖 A little guide book on Ethereum Development with Go (golang)
Home Page: https://goethereumbook.org
License: Other
📖 A little guide book on Ethereum Development with Go (golang)
Home Page: https://goethereumbook.org
License: Other
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"
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
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.
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
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:
but I don't know what to put in the method param (w io.Writer)
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
when i follow this Address Check, i don't know why always send me back false, is it out of date?
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:
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;
}
}
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.
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!
Hi Miguel,
I want to translate this material into Spanish
I wanted to know if you had any required PullReuqest format?
Regards.
Will just park this here before it's done :)
https://github.com/miguelmota/ethereum-development-with-go-book/blob/master/code/transactions.go - this code don't show transaction's field "From".
For instance, for transaction https://etherscan.io/tx/0x5d49fcaa394c97ec8a9c3e7bd9e8388d420fb050a52083ca52ff24b3b65bc9c2 it shows nothing.
For transaction https://etherscan.io/tx/0x36368eb4665367100bcb46427e8ac39b7873abfca2015116c478f84642a8812d it shows 0x0000000000000000000000000000000000000000
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.
website doesn't work both on mobile and desktop, something about an incorrect time of my computer / mobile
doesn't build from source due to node-gyp. Tried node v8, v10, v12 and v14
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)
https://goethereumbook.org/en/ have a invalid https certificate.
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?
https://github.com/miguelmota/ethereum-development-with-go-book/tree/master/en/swarm-upload
This page code file name swarm_upload.sol maybe swarm_upload.go
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.
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:
ethereum-development-with-go-book/code/event_subscribe.go
Lines 21 to 26 in 0820058
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
Just wonder if there any bitcoin go-lang development book?
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);
}
}
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.
hello how to use HexToAddress in python
How can a transaction instruction send tokens one-to-many or collect tokens many-to-one? Is there a specific example of golang implementation?
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.
Hi.. just letting you know that the SSL cert for goethereumbook.org expired 2 days ago.
Thanks for producing such great content :)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.