Popularity
4.9
Growing
Activity
8.2
-
146
9
26

Description

Package w3 implements a blazing fast and modular Ethereum JSON RPC client with first-class ABI support.

Programming language: Go
License: MIT License
Tags: Generation & Generics     RPC     Ethereum     Web3     Solidity    

w3 alternatives and similar packages

Based on the "Generation & Generics" category.
Alternatively, view w3 alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of w3 or a related project?

Add another 'Generation & Generics' Package

README

w3

Go Reference Go Report Card

Package w3 implements a modular and fast Ethereum JSON RPC client with first-class ABI support.

  • Modular API allows to create custom RPC method integrations that can be used alongside the methods implemented by the package.
  • Batch request support significantly reduces the duration of requests to both remote and local endpoints.
  • ABI bindings are specified for individual functions using Solidity syntax. No need for abigen and ABI JSON files.

w3 is closely linked to go-ethereum and uses a variety of its types, such as common.Address or types.Transaction.

Install

go get github.com/lmittmann/w3@latest

Getting Started

Connect to an RPC endpoint via HTTP, WebSocket, or IPC using Dial or MustDial.

// Connect (or panic on error)
client := w3.MustDial("https://cloudflare-eth.com")
defer client.Close()

Batch Requests

Batch request support in the Client allows to send multiple RPC requests in a single HTTP request. The speed gains to remote endpoints are huge. Fetching 100 blocks in a single batch request with w3 is ~80x faster compared to sequential requests with ethclient.

Example: Request the nonce and balance of an address in a single request

var (
    addr = w3.A("0x000000000000000000000000000000000000c0Fe")

    nonce   uint64
    balance big.Int
)

err := client.Call(
    eth.Nonce(addr).Returns(&nonce),
    eth.Balance(addr).Returns(&balance),
)

ABI Bindings

ABI bindings in w3 are specified for individual functions using Solidity syntax and are usable for any contract that supports that function.

Example: ABI binding for the ERC20-function balanceOf

funcBalanceOf := w3.MustNewFunc("balanceOf(address)", "uint256")

A Func can be used to

Reading Contracts

Func's can be used with eth.CallFunc in the client to read contract data.

var (
    weth9 = w3.A("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2")
    dai   = w3.A("0x6B175474E89094C44Da98b954EedeAC495271d0F")

    weth9Balance big.Int
    daiBalance   big.Int
)

err := client.Call(
    eth.CallFunc(funcBalanceOf, weth9, addr).Returns(&weth9Balance),
    eth.CallFunc(funcBalanceOf, dai, addr).Returns(&daiBalance),
)

Custom RPC Methods

Custom RPC methods can be called with the w3 client by creating a core.Caller implementation. The w3/module/eth package can be used as implementation reference.

RPC Methods

List of supported RPC methods.

eth

Method Go Code
eth_blockNumber eth.BlockNumber().Returns(blockNumber *big.Int)
eth_call eth.Call(msg ethereum.CallMsg).Returns(output *[]byte)eth.CallFunc(fn core.Func, contract common.Address, args ...interface{}).Returns(returns ...interface{})
eth_chainId eth.ChainID().Returns(chainID *uint64)
eth_gasPrice eth.GasPrice().Returns(gasPrice *big.Int)
eth_getBalance eth.Balance(addr common.Address).Returns(balance *big.Int)
eth_getBlockByHash eth.BlockByHash(hash common.Hash).Returns(block *types.Block)eth.BlockByHash(hash common.Hash).ReturnsRAW(block *eth.RPCBlock) eth.HeaderByHash(hash common.Hash).Returns(header *types.Header)eth.HeaderByHash(hash common.Hash).ReturnsRAW(header *eth.RPCHeader)
eth_getBlockByNumber eth.BlockByNumber(number *big.Int).Returns(block *types.Block)eth.BlockByNumber(number *big.Int).ReturnsRAW(block *eth.RPCBlock)eth.HeaderByNumber(number *big.Int).Returns(header *types.Header)eth.HeaderByNumber(number *big.Int).ReturnsRAW(header *eth.RAWHeader)
eth_getCode eth.Code(addr common.Address).Returns(code *[]byte)
eth_getLogs eth.Logs(q ethereum.FilterQuery).Returns(logs *[]types.Log)
eth_getStorageAt eth.StorageAt(addr common.Address, slot common.Hash).Returns(storage *common.Hash)
eth_getTransactionByHash eth.TransactionByHash(hash common.Hash).Returns(tx *types.Transaction)eth.TransactionByHash(hash common.Hash).ReturnsRAW(tx *eth.RPCTransaction)
eth_getTransactionCount eth.Nonce(addr common.Address).Returns(nonce *uint64)
eth_getTransactionReceipt eth.TransactionReceipt(hash common.Hash).Returns(receipt *types.Receipt)eth.TransactionReceipt(hash common.Hash).ReturnsRAW(receipt *eth.RPCReceipt)
eth_sendRawTransaction eth.SendTransaction(tx *types.Transaction).Returns(hash *common.Hash)eth.SendRawTransaction(rawTx []byte).Returns(hash *common.Hash)

Third Party RPC Method Packages

Package Description
github.com/lmittmann/flashbots Package flashbots implements RPC API bindings for the Flashbots relay and mev-geth.