Golang client for ethereum json rpc api

Ethrpc

Tests Coverage Status Go Report Card PkgGoDev Donate with Ethereum

Golang client for ethereum JSON RPC API.

  • web3_clientVersion
  • web3_sha3
  • net_version
  • net_peerCount
  • net_listening
  • eth_protocolVersion
  • eth_syncing
  • eth_coinbase
  • eth_mining
  • eth_hashrate
  • eth_gasPrice
  • eth_accounts
  • eth_blockNumber
  • eth_getBalance
  • eth_getStorageAt
  • eth_getTransactionCount
  • eth_getBlockTransactionCountByHash
  • eth_getBlockTransactionCountByNumber
  • eth_getUncleCountByBlockHash
  • eth_getUncleCountByBlockNumber
  • eth_getCode
  • eth_sign
  • eth_sendTransaction
  • eth_sendRawTransaction
  • eth_call
  • eth_estimateGas
  • eth_getBlockByHash
  • eth_getBlockByNumber
  • eth_getTransactionByHash
  • eth_getTransactionByBlockHashAndIndex
  • eth_getTransactionByBlockNumberAndIndex
  • eth_getTransactionReceipt
  • eth_pendingTransactions
  • eth_getUncleByBlockHashAndIndex
  • eth_getUncleByBlockNumberAndIndex
  • eth_getCompilers (DEPRECATED)
  • eth_compileLLL (DEPRECATED)
  • eth_compileSolidity (DEPRECATED)
  • eth_compileSerpent (DEPRECATED)
  • eth_newFilter
  • eth_newBlockFilter
  • eth_newPendingTransactionFilter
  • eth_uninstallFilter
  • eth_getFilterChanges
  • eth_getFilterLogs
  • eth_getLogs
  • eth_getWork
  • eth_submitWork
  • eth_submitHashrate
  • eth_getProof
  • db_putString
  • db_getString
  • db_putHex
  • db_getHex
  • shh_post
  • shh_version
  • shh_newIdentity
  • shh_hasIdentity
  • shh_newGroup
  • shh_addToGroup
  • shh_newFilter
  • shh_uninstallFilter
  • shh_getFilterChanges
  • shh_getMessages
Usage:
package main

import (
    "fmt"
    "log"

    "github.com/onrik/ethrpc"
)

func main() {
    client := ethrpc.New("http://127.0.0.1:8545")

    version, err := client.Web3ClientVersion()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(version)

    // Send 1 eth
    txid, err := client.EthSendTransaction(ethrpc.T{
        From:  "0x6247cf0412c6462da2a51d05139e2a3c6c630f0a",
        To:    "0xcfa202c4268749fbb5136f2b68f7402984ed444b",
        Value: ethrpc.Eth1(),
    })
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(txid)
}

Donate with Ethereum

Comments
  • EthGetBlockByHash always return response

    EthGetBlockByHash always return response

    Example:eth_getBlockByHash rpc call return null, if block was not found, which might happen when node is de-synchronised. In this case EthGetBlockByHash api method will return nil error, and empty response. Expected behaviour in this case would be return nil in response.

  • Can you give me EthCall example

    Can you give me EthCall example

    Summary

    I love your tool. It is very convenient and easy to use. Can you give me an example of EthCall. I got error like the following. The below ethcall is for getting ERC20 token name with the contract address.

    Steps to Reproduce

    1. run EthCall()
    • test.go
    var client *ethrpc.EthRPC
    
    func EthCall() string {
    	result, err := client.EthCall(ethrpc.T{To: "0x41e5560054824ea6b0732e656e3ad64e20e94e45", Data: "0x06fdde03"}, "latest")
    	if err != nil {
    		panic(err)
    	}
    	return result
    }
    

    Expected Results

    Return the result same as normal JSON RPC

    $ curl -X POST --data '{"jsonrpc":"2.0","method":"eth_call","params":[{"to":"0x41e5560054824ea6b0732e656e3ad64e20e94e45","data":"0x06fdde03"}, "latest"],"id":83}' http://localhost:xxxx
    
    {"jsonrpc":"2.0","id":83,"result":"0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000054369766963000000000000000000000000000000000000000000000000000000"}
    

    Actual Results

    Error occurred.

    panic: Error -32602 (invalid argument 0: hex string has length 0, want 40 for common.Address)
    
  • Make From param optional in Transaction object

    Make From param optional in Transaction object

    In eth_call the from field is optional (unlike for instance in eth_sendTransaction).

    However, the current code always fills in the from field with an empty string if no actual value is provided, which results in the following error:

    invalid argument 0: hex string has length 0, want 40 for common.Address

    when using eth_call with no from field provided.

    This fix makes the from field optional so that the rpc json request gets correctly serialized.

  • EthGetBlockByNumber has first parameter of type INT

    EthGetBlockByNumber has first parameter of type INT

    The EthGetBlockByNumber method has the first parameter of type INT. The first parameter takes a block number. In the future, this type will create problems with large numbers of blocks that are constantly increasing. My advice is to use UINT64 or implement your own bigInt type.

  • TransactionReceipt.Status throws with Parity

    TransactionReceipt.Status throws with Parity

    Parity returns status: null for transactions before Byzantium. Current code doesn't handle this and will throw calling strconv.ParseInt with null. Maybe we should change this field into *int.

    Parity ref: https://wiki.parity.io/JSONRPC-eth-module#eth_gettransactionreceipt Sample data:

    {
    	"jsonrpc": "2.0",
    	"result": {
    		"blockHash": "0xd99ad9a1142e57fbd0aea432bd76ef46c0af1ef71d80b6986b945fce95ce9d34",
    		"blockNumber": "0x1e8485",
    		"contractAddress": null,
    		"cumulativeGasUsed": "0x16d15",
    		"gasUsed": "0xc905",
    		"logs": [
    			{
    				"address": "0xe0b7927c4af23765cb51314a0e0521a9645f0e2a",
    				"blockHash": "0xd99ad9a1142e57fbd0aea432bd76ef46c0af1ef71d80b6986b945fce95ce9d34",
    				"blockNumber": "0x1e8485",
    				"data": "0x0000000000000000000000000000000000000000000000000000002e90edd000",
    				"logIndex": "0x0",
    				"topics": [
    					"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
    					"0x000000000000000000000000147af46ae9ccd18bb35ca01b353b51990e49dce1",
    					"0x00000000000000000000000045d01cc478a3dcfd66607726dc50780f3eb4d7bc"
    				],
    				"transactionHash": "0x6994e58ca3816889184186ad8883e20277883959084c72a9fe12b92bea6422e2",
    				"transactionIndex": "0x2",
    				"transactionLogIndex": "0x0",
    				"type": "mined"
    			}
    		],
    		"logsBloom": "0x00000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000040000000008000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000400010000000000000000000000000000000000000000000000000000000000004000000000000000000800000000000000000000000000000000000000000000000000000000004000002000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000",
    		"root": "0x5b694615e7e1ff4857d3c0714a4b70133ec2c161e1808029828ef979e5551347",
    		"status": null,
    		"transactionHash": "0x6994e58ca3816889184186ad8883e20277883959084c72a9fe12b92bea6422e2",
    		"transactionIndex": "0x2"
    	},
    	"id": 1
    }
    
  • Handling locked accounts

    Handling locked accounts

    Disclaimer - I'm not super familiar with the Ethereum network

    Trying to send a transaction on a private network with EthSendTransaction but getting the following error

    Error -32000 (authentication needed: password or unlock)

    Is there another function that I should be using that takes a password?

  • Support custom http.Client

    Support custom http.Client

    In most cases, using http.Client is not a problem. However, you need to explicitly specify http.Client in certain cases (for example, when running on the Google App Engine). With this change, you can use any http.Client.

  • Example inputs for transaction{} object used in (*EthRPC).EthSendTransactionl()

    Example inputs for transaction{} object used in (*EthRPC).EthSendTransactionl()

    Trying to use the EthSendTransaction() function and having some trouble with the inputs used in the transaction{} object.

    It would help alot if you could add an example to the README demonstrating the values used in the transaction{} object and what they represent.

  • On Active Release of TCP Connections

    On Active Release of TCP Connections

    TCP will not be released immediately after the end of the access, so when the client opens multiple threads to access at the same time, there will be an error of "onnectex: only one usage of each socket address (protocol/network address/port) is normally allowed." Can we adjust "TIME-WAIT"?

  • Fix query balance of contract token

    Fix query balance of contract token

    when I try use

    client.EthCall(ethrpc.T{
                To:   contractAddress,
                Data: data,
            }, "latest")
    

    to query balance of erc20 token

    type T struct {
    	From     string
    	To       string
    	Gas      int
    	GasPrice *big.Int
    	Value    *big.Int
    	Data     string
    	Nonce    int
    }
    
    func (t T) MarshalJSON() ([]byte, error) {
    	params := map[string]interface{}{
    		"from": t.From,
    	}
    	if t.To != "" {
    		params["to"] = t.To
    	}
    	if t.Gas > 0 {
    		params["gas"] = IntToHex(t.Gas)
    	}
    	if t.GasPrice != nil {
    		params["gasPrice"] = BigToHex(*t.GasPrice)
    	}
    	if t.Value != nil {
    		params["value"] = BigToHex(*t.Value)
    	}
    	if t.Data != "" {
    		params["data"] = t.Data
    	}
    	if t.Nonce > 0 {
    		params["nonce"] = IntToHex(t.Nonce)
    	}
    
    	return json.Marshal(params)
    }
    

    this let t.From = "" and get this json string {"id":1,"jsonrpc":"2.0","method":"eth_call","params":[{"data":"0x70a08231000000000000000000000000fe2e424e2ef9b929157dd525c414f4b128ee1bfb","from":"","to":"0xf4c90e18727c5c76499ea6369c856a6d61d3e92e"},"latest"]} after marshal

    finnal error appear

    invalid argument 0: hex string has length 0, want 40 for common.Address
    
  • EthGetBlockByHash The value returned is not correct

    EthGetBlockByHash The value returned is not correct

    client := ethrpc.New("https://ropsten.infura.io") hash := "0x931084475948fa771429ea3a5c39650e83a95059eff624d2089cc64c21e62915" bl ,err :=client.EthGetBlockByHash(hash,true) if err != nil{ log.Print("err", err) } fmt.Print("bl---",bl)

    _20181220144747

  • types: allow nonce to be zero

    types: allow nonce to be zero

    As far as I understand nonce might be zero, but current version of library doesn't allow it, and just do not place "nonce" in json request, which later interpreted by daemon that nonce specified at all, and request is not valid.

    Not sure for 100% but that seems like this bug caused our transaction stuck on queue pool of ethereum daemon.

Clusterpedia-client - clusterpedia-client supports the use of native client-go mode to call the clusterpedia API

clusterpedia-client supports the use of native client-go mode to call the cluste

Jan 7, 2022
Client-go - Clusterpedia-client supports the use of native client-go mode to call the clusterpedia API

clusterpedia-client supports the use of native client-go mode to call the cluste

Dec 5, 2022
High-level YugabyteDB RPC and command line client

YugabyteDB Client API YugabyteDB Client API provides high level API operations and command line interface for YugabyteDB RPC. Command line interface i

Jan 8, 2022
A Go client implementing a client-side distributed consumer group client for Amazon Kinesis.
A Go client implementing a client-side distributed consumer group client for Amazon Kinesis.

Kinesumer is a Go client implementing a client-side distributed consumer group client for Amazon Kinesis.

Jan 5, 2023
Instances-api - A JSON API to fetch Piped instances

Instances-Api Hosting it yourself docker-compose up -d You can now access the AP

Dec 27, 2022
Go-serverless-eth-event-listener - Go serverless, ethereum contract event listener with a sample contract

go-serverless-eth-event-listener This repository is for showing how to listen sm

May 19, 2022
An API client for the Notion API implemented in Golang

An API client for the Notion API implemented in Golang

Dec 30, 2022
Nutanix-client-go - Go client for the Nutanix Prism V3 API

nutanix-client-go This repository contains portions of the Nutanix API client code in nutanix/terraform-provider-nutanix. It has been extracted to red

Jan 6, 2022
Client-server-golang-sqs - Client Server with SQS and golang

Client Server with SQS and golang Multi-threaded client-server demo with Go What

Feb 14, 2022
Substrate rpc go sdk

Substrate rpc go sdk 简介 这个包其实是使用[itering/scale.go](https://github.com/itering/scale.go)这个包去代替[JFJun/go-substrate-rpc-client](https://github.com/JFJun/

Nov 29, 2021
I try to make a Restfull-API with golang and interaction with json.file

{ "users": [ { "name": "Elliot", "type": "Reader", "age": 23, "social": { "facebook": "https://facebook.com",

Jan 5, 2022
Simple-Weather-API - Simple weather api app created using golang and Open Weather API key
Simple-Weather-API - Simple weather api app created using golang and Open Weather API key

Simple Weather API Simple weather api app created using golang and Open Weather

Feb 6, 2022
Go client for the YNAB API. Unofficial. It covers 100% of the resources made available by the YNAB API.

YNAB API Go Library This is an UNOFFICIAL Go client for the YNAB API. It covers 100% of the resources made available by the YNAB API. Installation go

Oct 6, 2022
A Wrapper Client for Google Spreadsheet API (Sheets API)

Senmai A Wrapper Client for Google Spreadsheet API (Sheets API) PREPARATION Service Account and Key File Create a service account on Google Cloud Plat

Nov 5, 2021
Go-http-client: An enhanced http client for Golang
Go-http-client: An enhanced http client for Golang

go-http-client An enhanced http client for Golang Documentation on go.dev ?? This package provides you a http client package for your http requests. Y

Jan 7, 2023
Light JSON API for storing user ratings of NASA's Astronomy Picture of the Day (APOD).
Light JSON API for storing user ratings of NASA's Astronomy Picture of the Day (APOD).

nasa-apod-api-go Light JSON API for storing user ratings of NASA's Astronomy Picture of the Day (APOD). To run this server you must have access to a N

Oct 26, 2021
An API for generating JSON based STIX objects and TAXII messages with the Go

libstix2 an API for generating JSON based STIX objects and TAXII messages with the Go (Golang) programming language. Please see the examples directory and the README files in each of the sub packages for more information. This API is built to support STIX 2.x and TAXII 2.x.

Dec 21, 2021
A tiny JSON rule based API Mocker

API Mocker A tiny JSON rule based API mocker. The API Mocker intends to simulate an API behavior, supported by a group of rules written as a JSON file

Sep 11, 2022
Convert IBM stanza-style files to json and publish as an API

stanza_to_json Convert IBM stanza-style files to json and publish as an API Background IBM Stanza files have been extended to be used in other ways, b

Feb 2, 2022