ethclient

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2021 License: GPL-3.0 Imports: 20 Imported by: 0

README

ethclient

Description

Extension ethclient.

Prerequisites

golang

Quick Start

package main

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

	"github.com/TheStarBoys/ethclient"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/crypto"
)

func main() {
	// The private key of your wallet address.
	privateKey, _ := crypto.HexToECDSA("9a01f5c57e377e0239e6036b7b2d700454b760b2dab51390f1eeb2f64fe98b68")

	// Dial Client.
	chainUrl := "ws://localhost:8546"
	client, err := ethclient.Dial(chainUrl)
	if err != nil {
		panic(err)
	}
	defer client.Close()

	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
	defer cancel()

	// The address your want to send to.
	to := common.HexToAddress("0x06514D014e997bcd4A9381bF0C4Dc21bD32718D4")

	// Send single transaction.
	tx, err := client.SendMsg(ctx, ethclient.Message{
		To:         &to,
		PrivateKey: privateKey,
		Value:      big.NewInt(0),
	})

	if err != nil {
		fmt.Printf("Send single message err: %v\n", err)
		return
	}

	// Waiting n confirmations.
	contains, err := client.ConfirmTx(tx.Hash(), 2, 5*time.Second)
	if err != nil {
		panic(err)
	}

	if !contains {
		fmt.Printf("The transaction %v is not contained at blockchain", tx.Hash().Hex())
	} else {
		receipt, err := client.RawClient().TransactionReceipt(ctx, tx.Hash())
		// do something.
		_, _ = receipt, err
	}

	fmt.Println("Send single message successful, txHash:", tx.Hash().Hex())

	// Send multiple transactions.
	mesgs := make(chan ethclient.Message)
	txs, errs := client.BatchSendMsg(ctx, mesgs)
	go func() {
		for i := 0; i < 5; i++ {
			mesgs <- ethclient.Message{
				PrivateKey: privateKey,
				To:         &to,
			}
		}

		close(mesgs)
	}()

	for tx := range txs {
		fmt.Printf("Send multiple message successful, txHash: %v, err: %v\n", tx.Hash().Hex(), <-errs)
	}
}

License

The ethclient library is licensed under the GNU General Public License v3.0, also included in our repository in the COPYING.LESSER file.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoAnyKeyStores       = errors.New("No any keystores")
	ErrMessagePrivateKeyNil = errors.New("PrivateKey is nil")
)

Functions

func NewTestEthBackend

func NewTestEthBackend(privateKey *ecdsa.PrivateKey, alloc core.GenesisAlloc) (*node.Node, error)

Types

type ChainSubscrier

type ChainSubscrier struct {
	// contains filtered or unexported fields
}

ChainSubscrier implements Subscriber interface

func NewChainSubscriber

func NewChainSubscriber(c *ethclient.Client) (*ChainSubscrier, error)

NewChainSubscriber .

func (*ChainSubscrier) SubscribeFilterlogs

func (cs *ChainSubscrier) SubscribeFilterlogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) error

SubscribeFilterlog .

func (*ChainSubscrier) SubscribeNewHead

func (cs *ChainSubscrier) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) error

SubscribeNewHead .

type Client

type Client struct {
	Subscriber
	// contains filtered or unexported fields
}

func Dial

func Dial(rawurl string) (*Client, error)

func NewClient

func NewClient(c *rpc.Client) (*Client, error)

func (*Client) BatchSendMsg

func (c *Client) BatchSendMsg(ctx context.Context, msgs <-chan Message) (<-chan *types.Transaction, <-chan error)

func (*Client) Close

func (c *Client) Close()

func (*Client) ConfirmTx

func (c *Client) ConfirmTx(txHash common.Hash, n uint, timeout time.Duration) (bool, error)

func (*Client) MessageToTransactOpts

func (c *Client) MessageToTransactOpts(ctx context.Context, msg Message) (*bind.TransactOpts, error)

MessageToTransactOpts . NOTE: You must provide private key for signature.

func (*Client) NewMethodData

func (c *Client) NewMethodData(a abi.ABI, methodName string, args ...interface{}) ([]byte, error)

func (*Client) NewTransaction

func (c *Client) NewTransaction(ctx context.Context, msg ethereum.CallMsg) (*types.Transaction, error)

func (*Client) RawClient

func (c *Client) RawClient() *ethclient.Client

RawClient returns ethclient

func (*Client) SendMsg

func (c *Client) SendMsg(ctx context.Context, msg Message) (*types.Transaction, error)

type ExpectedEventsFunc

type ExpectedEventsFunc func(event interface{}) bool

ExpectedEventsFunc returns true if event is expected.

type Message

type Message struct {
	From       common.Address    // the sender of the 'transaction'
	PrivateKey *ecdsa.PrivateKey // overwrite From if not nil
	To         *common.Address   // the destination contract (nil for contract creation)
	Gas        uint64            // if 0, the call executes with near-infinite gas
	GasPrice   *big.Int          // wei <-> gas exchange ratio
	Value      *big.Int          // amount of wei sent along with the call
	Data       []byte            // input data, usually an ABI-encoded contract method invocation

	AccessList types.AccessList // EIP-2930 access list.
}

type Subscriber

type Subscriber interface {
	SubscribeFilterlogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) error
	SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) error
}

Subscriber represents a set of methods about chain subscription

type TransactFunc

type TransactFunc func() (*types.Transaction, error)

TransactFunc represents the transact call of Smart Contract.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL