Categorygithub.com/Energy8Token/go-web3
modulepackage
0.0.0-20210908110635-18d23c502ef3
Repository: https://github.com/energy8token/go-web3.git
Documentation: pkg.go.dev

# README

Go-Web3

JsonRPC

package main

import (
	"fmt"
	
	web3 "github.com/Energy8Token/go-web3"
	"github.com/Energy8Token/go-web3/jsonrpc"
)

func main() {
	client, err := jsonrpc.NewClient("https://mainnet.infura.io")
	if err != nil {
		panic(err)
	}

	number, err := client.Eth().BlockNumber()
	if err != nil {
		panic(err)
	}

	header, err := client.Eth().GetBlockByNumber(web3.BlockNumber(number), true)
	if err != nil {
		panic(err)
	}

	fmt.Println(header)
}

ABI

The ABI codifier uses randomized tests with e2e integration tests with a real Geth client to ensure that the codification is correct and provides the same results as the AbiEncoder from Solidity.

To use the library import:

"github.com/Energy8Token/go-web3/abi"

Declare basic objects:

typ, err := abi.NewType("uint256")

or

typ = abi.MustNewType("uint256")

and use it to encode/decode the data:

num := big.NewInt(1)

encoded, err := typ.Encode(num)
if err != nil {
    panic(err)
}

decoded, err := typ.Decode(encoded) // decoded as interface
if err != nil {
    panic(err)
}

num2 := decoded.(*big.Int)
fmt.Println(num.Cmp(num2) == 0) // num == num2

You can also codify structs as Solidity tuples:

import (
	"fmt"
    
	web3 "github.com/Energy8Token/go-web3"
	"github.com/Energy8Token/go-web3/abi"
	"math/big"
)

func main() {
	typ := abi.MustNewType("tuple(address a, uint256 b)")

	type Obj struct {
		A web3.Address
		B *big.Int
	}
	obj := &Obj{
		A: web3.Address{0x1},
		B: big.NewInt(1),
	}

	// Encode
	encoded, err := typ.Encode(obj)
	if err != nil {
		panic(err)
	}

	// Decode output into a map
	res, err := typ.Decode(encoded)
	if err != nil {
		panic(err)
	}

	// Decode into a struct
	var obj2 Obj
	if err := typ.DecodeStruct(encoded, &obj2); err != nil {
		panic(err)
	}

	fmt.Println(res)
	fmt.Println(obj)
}

Wallet

As for now the library only provides primitive abstractions to send signed abstractions. The intended goal is to abstract the next steps inside the contract package.

// Generate a random wallet
key, _ := wallet.GenerateKey()

to := web3.Address{0x1}
transferVal := big.NewInt(1000)

// Create the transaction
txn := &web3.Transaction{
	To:    &to,
	Value: transferVal,
	Gas:   100000,
}

// Create the signer object and sign
signer := wallet.NewEIP155Signer(chainID)
txn, _ = signer.SignTx(txn, key)

// Send the signed transaction
data := txn.MarshalRLP()
hash, _ := c.Eth().SendRawTransaction(data)

ENS

Resolve names on the Ethereum Name Service registrar.

import (
    "fmt"

    web3 "github.com/Energy8Token/go-web3"
    "github.com/Energy8Token/go-web3/jsonrpc"
    "github.com/Energy8Token/go-web3/contract/builtin/ens"
)

var mainnetAddress = web3.HexToAddress("0x314159265dD8dbb310642f98f50C066173C1259b")

func main() {
	client, err := jsonrpc.NewClient("https://mainnet.infura.io")
    if err != nil {
        panic(err)
    }

	resolver := ens.NewENSResolver(mainnetAddress, client)
	addr, err := resolver.Resolve("ens_address")
	if err != nil {
		panic(err)
	}

    fmt.Println(addr)
}

Tracker

Complete example of the tracker here

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

No description provided by the author
Ether converts a value to the ether unit with 18 decimals.
Gwei converts a value to the gwei unit with 9 decimals.
HexToAddress converts an hex string value to an address object.
HexToHash converts an hex string value to a hash object.

# Constants

No description provided by the author
Goerli is the Clique testnet.
No description provided by the author
Mainnet is the mainnet network.
No description provided by the author
Rinkeby is a POW testnet.
Ropsten is the POW testnet.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

Address is an Ethereum address.
No description provided by the author
Hash is an Ethereum hash.
Network is a chain id.