util

package
v0.0.0-...-52fa2d9 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

util.go is a collection of utility functions to deploy and interact with a Maltcoin ERC20 token contract. The contained functions serve a simulated backend as well as an actual connection to a running blockchain node.

Index

Constants

This section is empty.

Variables

View Source
var (

	// Defines the max gas per block for the simulated backend
	MaxGasPerBlock = uint64(5000000)

	// Defines 10^18 as a big integer
	Ten18 = big.NewInt(1e18)

	// According to the go-ethereum docs, the chain ID for simulated
	// backends must be 1337
	// (https://pkg.go.dev/github.com/ethereum/go-ethereum@v1.10.19/accounts/abi/bind/backends#NewSimulatedBackend).
	TestChainID = big.NewInt(1337)
)

Functions

func DeployContractAndCommit

func DeployContractAndCommit(auth *bind.TransactOpts, client *backends.SimulatedBackend) (common.Address, *types.Transaction, *maltcoin.Maltcoin, error)

DeployContractAndCommit deploys an instance of the ERC20 token contract and commits the transaction to the simulated backend. The function returns the contract address, the transaction, and an instance of the contract binding.

func FillTransactionSignerFields

func FillTransactionSignerFields(auth *bind.TransactOpts, client *ethclient.Client, callMsg ethereum.CallMsg) (*bind.TransactOpts, error)

FillTransactionSignerFields takes the transaction signer, the client and a byte array of the data to be called in a transaction. It gathers necessary gas price, nonce and estimated gas and assigns these to the fields of the transaction signer, which the function then returns.

func GeneratePrivKeysAndAddresses

func GeneratePrivKeysAndAddresses(n uint64) ([]*ecdsa.PrivateKey, []common.Address, error)

GeneratePrivKeysAndAddresses returns a slice of private keys and addresses.

func GetCallData

func GetCallData(name string, args ...interface{}) ([]byte, error)

GetCallData returns the necessary byte array to fill an ethereum.CallMsg struct's data field. This data is a byte representation of the method call paired with its corresponding arguments.

func GetClient

func GetClient() (*ethclient.Client, error)

GetClient connects to a local blockchain node and returns the client. This is only a wrapper function to use the preconfigured blockchain URL.

func GetClientAndTransactionSigner

func GetClientAndTransactionSigner(privKey *ecdsa.PrivateKey) (*ethclient.Client, *bind.TransactOpts, error)

GetClientAndTransactionSigner connects to a local Evmos node on port 8545, queries the chain id and uses this together with the private key to create a transaction signer. The function returns the client and the transaction signer.

func GetReceipt

func GetReceipt(backend ReceiptBackend, txHashHex string) (*types.Receipt, error)

GetReceipt converts a given transaction hash in hex string format and returns the transaction receipt, if the hash is valid.

func GetSimulatedClientAndTransactionSigner

func GetSimulatedClientAndTransactionSigner(privKey *ecdsa.PrivateKey, blockGasLimit uint64, chainID *big.Int) (*backends.SimulatedBackend, *bind.TransactOpts, error)

GetSimulatedClientAndTransactionSigner establishes a new simulated backend for testing purposes. An initial token balance is assigned to the address of the given private key. The maximum gas a block can consume is defined with the blockGasLimit input. The private key and chain id are used to create a transaction signer for any transactions on the blockchain. The function returns the client and the transaction signer.

Types

type ReceiptBackend

type ReceiptBackend interface {
	TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
}

ReceiptBackend defines an interface, which can be used to get the transaction receipt for a given ethclient or simulated backend.

Jump to

Keyboard shortcuts

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