ethtest

package
v0.0.0-...-2597ab7 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package ethtest provides functionality for Ethereum testing.

The ethtest package provides helpers for testing Ethereum smart contracts.

Index

Constants

View Source
const (
	OpenSea   = MockedEntity("OpenSea")
	Chainlink = MockedEntity("Chainlink")
	Ethier    = MockedEntity("Ethier")
	WETH      = MockedEntity("wETH")
)

Mocked entities.

Variables

This section is empty.

Functions

func NewBlock

func NewBlock(num int64, time uint64) *types.Block

NewBlock returns a new Block with only the number and time populated.

Types

type BlockTimes

type BlockTimes []uint64

BlockTimes implements eth.BlockFetcher, returning Blocks with nothing but a mining time.

func (BlockTimes) BlockByNumber

func (t BlockTimes) BlockByNumber(ctx context.Context, num *big.Int) (*types.Block, error)

BlockByNumber returns the requested block.

func (BlockTimes) BlockNumber

func (t BlockTimes) BlockNumber(ctx context.Context) (uint64, error)

BlockNumber returns len(t)-1.

type MockedEntity

type MockedEntity string

A MockedEntity mocks a real-world entity such as Uniswap or Opensea with deterministically generated accounts (and therefore contract addresses).

type RPCStub

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

An RPCStub implements stubbed eth_* RPC methods.

func NewRPCStub

func NewRPCStub(chainID, blockNum uint64) *RPCStub

NewRPCStub returns a new RPCStub.

func (*RPCStub) AddBlocks

func (s *RPCStub) AddBlocks(tb testing.TB, bs ...*types.Block)

AddBlocks adds all Blocks to the stub such that they will be returned by an RPC call to GetBlockBy{Number,Hash}(). If said methods are called with a Block <= s.BlockNum but it hasn't been added by this method, an empty Block is created and returned.

func (*RPCStub) ReplaceBlocks

func (s *RPCStub) ReplaceBlocks(tb testing.TB, bs ...*types.Block)

ReplaceBlocks replaces Blocks added by AddBlocks(). An explicit replacement is required to ensure deliberate behaviour in tests.

Blocks are replaced by both number and hash.

func (*RPCStub) ServeHTTP

func (s *RPCStub) ServeHTTP(tb testing.TB) string

ServeHTTP starts an HTTP server that propagates all eth_* RPC requests to a new RPC server backed by s. It returns the HTTP server's address, with http:// prefix included.

func (*RPCStub) SetBlockNumber

func (s *RPCStub) SetBlockNumber(n uint64)

SetBlockNumber sets the stub's current block number.

type SimulatedBackend

type SimulatedBackend struct {
	*backends.SimulatedBackend

	AutoCommit bool
	// contains filtered or unexported fields
}

A SimulatedBackend embeds a go-ethereum SimulatedBackend and extends its functionality to simplify standard testing.

func NewSimulatedBackend

func NewSimulatedBackend(numAccounts int) (*SimulatedBackend, error)

NewSimulatedBackend returns a new simulated ETH backend with the specified number of accounts. Transactions are automatically committed unless. Close() must be called to free resources after use.

Accounts are deterministically generated so have identical addresses between backends, but balances are coupled to the specific instance of the backend.

func NewSimulatedBackendTB

func NewSimulatedBackendTB(tb testing.TB, numAccounts int) *SimulatedBackend

NewSimulatedBackendTB calls NewSimulatedBackend(), reports any errors with tb.Fatal, and calls Close() with tb.Cleanup().

func (*SimulatedBackend) Acc

func (sb *SimulatedBackend) Acc(account int) *bind.TransactOpts

Acc returns a TransactOpts signing as the specified account number.

func (*SimulatedBackend) Addr

func (sb *SimulatedBackend) Addr(account int) common.Address

Addr returns the Address of the specified account number.

func (*SimulatedBackend) AsMockedEntity

func (sb *SimulatedBackend) AsMockedEntity(mock MockedEntity, fn func(*bind.TransactOpts) error) error

AsMockedEntity calls the provided function with the mocked entity's account if it is supported, propagating any returned error.

MockedEntity accounts SHOULD NOT be used in general tests; prefer provided packages like openseatest to using AsMockedEntity() directly.

func (*SimulatedBackend) BalanceOf

func (sb *SimulatedBackend) BalanceOf(ctx context.Context, tb testing.TB, addr common.Address) *big.Int

BalanceOf returns the current balance of the address, calling tb.Fatalf on error.

func (*SimulatedBackend) BlockNumber

func (sb *SimulatedBackend) BlockNumber() *big.Int

BlockNumber returns the current block number.

func (*SimulatedBackend) CallFrom

func (sb *SimulatedBackend) CallFrom(account int) *bind.CallOpts

CallFrom returns a CallOpts from the specified account number.

func (*SimulatedBackend) CoverageReport

func (sb *SimulatedBackend) CoverageReport() []byte

CoverageReport returns an LCOV trace file for contracts registered mapped by an solcover.Collector() EVMLogger injected into the SimulatedBackend during construction. The report can be generated at any time that collection is not currently active (i.e. it is not threadsafe with respect to the VM). See solcover.Collector() for more information.

func (*SimulatedBackend) FastForward

func (sb *SimulatedBackend) FastForward(blockNumber *big.Int) bool

FastForward calls sb.Commit() until sb.BlockNumber() >= blockNumber. It returns whether fast-forwarding was required; i.e. false if the requested block number is current or in the past.

NOTE: FastForward is O(curr - requested).

func (*SimulatedBackend) GasSpent

func (sb *SimulatedBackend) GasSpent(ctx context.Context, tb testing.TB, tx *types.Transaction) *big.Int

GasSpent returns the gas spent (i.e. used*cost) by the transaction.

func (*SimulatedBackend) Must

func (sb *SimulatedBackend) Must(tb testing.TB, descFormat string, descArgs ...interface{}) func(*types.Transaction, error) *types.Transaction

Must returns a function that ensures a successful transaction, reporting any error on tb.Fatal, or propagating the transaction.

Intended usage:

sb.Must(t, "ContractFunc()")(foo.ContractFunc(sim.Acc(<acc>), …))

The description format and associated args will be used as a prefix in any reported errors. The returned function MUST be used immediately, and can only be used once.

func (*SimulatedBackend) PrivateKey

func (sb *SimulatedBackend) PrivateKey(account int) *ecdsa.PrivateKey

PrivateKey returns the private key of the specified account number.

func (*SimulatedBackend) SendTransaction

func (sb *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) ServeHTTP

func (sb *SimulatedBackend) ServeHTTP(tb testing.TB) string

ServeHTTP functions identically to RPCStub.ServeHTTP().

func (*SimulatedBackend) WithValueFrom

func (sb *SimulatedBackend) WithValueFrom(account int, value *big.Int) *bind.TransactOpts

WithValueFrom returns a TransactOpts that sends the specified value from the account. If value==0, sb.Acc(account) can be used directly.

type UnimplementedStubMethod

type UnimplementedStubMethod struct {
	Method string
	Stub   reflect.Type
}

UnimplementedStubMethod is an error returned by a stubbed method that exists to avoid panics if called but is otherwise unimplemented.

func (*UnimplementedStubMethod) Error

func (u *UnimplementedStubMethod) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

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