ethereum

package module
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: Apache-2.0 Imports: 18 Imported by: 37

README

Ethereum

CircleCI Go Report Card go.mod Go version

Copyright 2022, Ardan Labs
hello@ardanlabs.com

Ethereum provides higher level Go API's for writing applications and smart contracts on the Ethereum blockchain.

Licensing

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Learn More

Reach out about corporate training events, open enrollment live training sessions, and on-demand learning options.

Ardan Labs (www.ardanlabs.com)
hello@ardanlabs.com

To attend any of our high-performance tranings check out this link:
https://www.ardanlabs.com/training

Examples

To see example for using these API's check out these two projects.

Ultimate Go: Smart Contract
This project provides material to help you learn the basics of writing, debuging, and maintaining Ethereum smart contracts. This repository has several smart contracts of increasing complexity to showcase different aspects of smart contract development.

Ultimate Go: Liar's Dice
This project implements a game of Liar's Dice where a crypto wallet is used for authentication and a smart contract is used to maintain the bank. Once a player is authenticated, they bet money for each game played and that money comes from their crypto wallet.

For advanced general blockchain knowledge check out this project.

Ultimate Go: Advanced Engineering
This project implements a semantically correct blockchain in Go. The implementation of the Ardan blockchain takes inspiration from both Bitcoin and Ethereum. This does not incorporate any smart contract development, but is good for advanced learning.

Documentation

Overview

Package ethereum higher level Go API's for writing applications and smart contracts on the Ethereum blockchain.

Index

Constants

View Source
const (
	NetworkHTTPLocalhost = "http://localhost:8545"
	NetworkLocalhost     = "zarf/ethereum/geth.ipc"
	NetworkGoerli        = "https://rpc.ankr.com/eth_goerli"
)

Set of networks supported by the smart package.

View Source
const ZeroHash string = "0x0000000000000000000000000000000000000000000000000000000000000000"

ZeroHash represents a hash code of zeros.

Variables

This section is empty.

Functions

func FromAddressAny added in v0.5.0

func FromAddressAny(value any, signature string) (string, error)

FromAddressAny marshals the specified value into JSON to create an array of bytes. Then uses FromAddressBytes function to extract the address.

func FromAddressBytes added in v0.5.0

func FromAddressBytes(bytes []byte, signature string) (string, error)

FromAddressBytes extracts the address for the account that signed the data. The signature must be provided as a hexadecimal string.

func PrivateKeyByKeyFile

func PrivateKeyByKeyFile(keyFile string, passPhrase string) (*ecdsa.PrivateKey, error)

PrivateKeyByKeyFile opens a key file for the private key.

func SignAny added in v0.5.0

func SignAny(value any, privateKey *ecdsa.PrivateKey) (signature string, err error)

SignAny marshals the specified value into JSON to create an array of bytes. Then the SignBytes function is used to sign the data.

func SignBytes added in v0.5.0

func SignBytes(bytes []byte, privateKey *ecdsa.PrivateKey) (signature string, err error)

SignBytes takes the specified bytes and first hashes them using Keccak256 to create a 32 byte array of data. Then those bytes are salted with the Ethereum stamp and signed using the specified private key. The signature is returned as a hexadecimal string.

Types

type Backend added in v0.8.0

type Backend interface {
	bind.ContractBackend
	bind.DeployBackend
	TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error)
	TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
	BalanceAt(ctx context.Context, contract common.Address, blockNumber *big.Int) (*big.Int, error)
	Network() string
	ChainID() *big.Int
}

Backend represents behavior for interacting with an ethereum node.

type Client added in v0.8.0

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

Client provides an API for working with smart contracts.

func NewClient added in v0.8.0

func NewClient(backend Backend, privateKey *ecdsa.PrivateKey) (*Client, error)

NewClient provides an API for accessing an Ethereum node to perform blockchain related operations. It required the private key you want to use for this instance when accessing the node.

func (*Client) Address added in v0.8.0

func (clt *Client) Address() common.Address

Address returns the current address calculated from the private key.

func (*Client) Balance added in v0.8.0

func (clt *Client) Balance(ctx context.Context) (wei *big.Int, err error)

Balance retrieves the current balance for the client account.

func (*Client) BaseFee added in v0.8.0

func (clt *Client) BaseFee(receipt *types.Receipt) (wei *big.Int)

BaseFee calculates the base fee from the block for this receipt.

func (*Client) ChainID added in v0.8.0

func (clt *Client) ChainID() int

ChainID returns the chain information for the connected network.

func (*Client) Network added in v0.11.0

func (clt *Client) Network() string

Network returns the network information for the connected network.

func (*Client) NewCallOpts added in v0.8.0

func (clt *Client) NewCallOpts(ctx context.Context) (*bind.CallOpts, error)

NewCallOpts constructs a new CallOpts which is used to call contract methods that does not require a transaction.

func (*Client) NewTransactOpts added in v0.8.0

func (clt *Client) NewTransactOpts(ctx context.Context, gasLimit uint64, gasPrice *big.Int, valueGWei *big.Float) (*bind.TransactOpts, error)

NewTransactOpts constructs a new TransactOpts which is the collection of authorization data required to create a valid Ethereum transaction. If the gasLimit is set to 0, an estimate will be made for the amount of gas needed. If the gasPrice is set to 0, then the connected geth service is consulted for the suggested gas price.

func (*Client) PrivateKey added in v0.8.0

func (clt *Client) PrivateKey() *ecdsa.PrivateKey

PrivateKey returns the private key being used.

func (*Client) SendTransaction added in v0.8.0

func (clt *Client) SendTransaction(ctx context.Context, address common.Address, value *big.Int, gasLimit uint64) error

SendTransaction sends a transaction to the specified address for the specified amount. The function will wait for the transaction to be mined based on the timeout value specified in the context.

func (*Client) WaitMined added in v0.8.0

func (clt *Client) WaitMined(ctx context.Context, tx *types.Transaction) (*types.Receipt, error)

WaitMined will wait for the transaction to be minded and return a receipt.

type DialedBackend added in v0.8.0

type DialedBackend struct {
	*ethclient.Client
	// contains filtered or unexported fields
}

DialedBackend represents a dialied connection to an ethereum node.

func CreateDialedBackend added in v0.8.0

func CreateDialedBackend(ctx context.Context, network string) (*DialedBackend, error)

CreateDialedBackend constructs an ethereum client value for the specified network and attempts to establish a connection.

func (*DialedBackend) ChainID added in v0.8.0

func (b *DialedBackend) ChainID() *big.Int

ChainID returns the chain id the backend is connected to.

func (*DialedBackend) Network added in v0.8.0

func (b *DialedBackend) Network() string

Network returns the network the backend is connected to.

type SimulatedBackend added in v0.8.0

type SimulatedBackend struct {
	simulated.Client
	*simulated.Backend
	AutoCommit  bool
	PrivateKeys []*ecdsa.PrivateKey
	// contains filtered or unexported fields
}

SimulatedBackend represents a simulated connection to an ethereum node.

func CreateSimulatedBackend added in v0.8.0

func CreateSimulatedBackend(numAccounts int, autoCommit bool, accountBalance *big.Int) (*SimulatedBackend, error)

CreateSimulatedBackend constructs a simulated backend and a set of private keys registered to the backend with a balance of 100 ETH. Use these private keys with the NewSimulation call to get an Ethereum API value.

func (*SimulatedBackend) ChainID added in v0.8.0

func (b *SimulatedBackend) ChainID() *big.Int

ChainID returns the chain id the backend is connected to.

func (*SimulatedBackend) Network added in v0.11.0

func (b *SimulatedBackend) Network() string

Network returns the network the backend is connected to.

func (*SimulatedBackend) SendTransaction added in v0.8.0

func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error

SendTransaction functions pipes its parameters to the embedded backend and also calls Commit() if sb.AutoCommit==true.

func (*SimulatedBackend) SetTime added in v0.9.0

func (b *SimulatedBackend) SetTime(t time.Time)

SetTime creates a time shift to the simulated clock. It can only be called on empty blocks.

Directories

Path Synopsis
Package currency provides information on the price of ETH.
Package currency provides information on the price of ETH.
Package etherscan provides Go access to the Etherscan API.
Package etherscan provides Go access to the Etherscan API.

Jump to

Keyboard shortcuts

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