w3types

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package w3types implements common types.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account struct {
	Nonce   uint64
	Balance *big.Int
	Code    []byte
	Storage Storage
	// contains filtered or unexported fields
}

func (*Account) CodeHash

func (acc *Account) CodeHash() common.Hash

CodeHash returns the hash of the account's code.

func (*Account) MarshalJSON

func (acc *Account) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler.

type Caller deprecated

type Caller = RPCCaller

Caller is the interface that groups the basic CreateRequest and HandleResponse methods.

Deprecated: Use the RPCCaller interface instead.

type CallerFactory deprecated

type CallerFactory[T any] interface {
	// Returns given argument points to the variable in which to store the
	// calls result.
	Returns(*T) Caller
}

CallerFactory is the interface that wraps the basic Returns method.

Deprecated: Use the RPCCallerFactory interface instead.

type Func

type Func interface {
	// EncodeArgs ABI-encodes the given args and prepends the Func's 4-byte
	// selector.
	EncodeArgs(args ...any) (input []byte, err error)

	// DecodeArgs ABI-decodes the given input to the given args.
	DecodeArgs(input []byte, args ...any) (err error)

	// DecodeReturns ABI-decodes the given output to the given returns.
	DecodeReturns(output []byte, returns ...any) (err error)
}

Func is the interface that wraps the methods for ABI encoding and decoding.

type Message

type Message struct {
	From          common.Address  // Sender
	To            *common.Address // Recipient
	Nonce         uint64
	GasPrice      *big.Int
	GasFeeCap     *big.Int
	GasTipCap     *big.Int
	Gas           uint64
	Value         *big.Int
	Input         []byte // Input data
	AccessList    types.AccessList
	BlobGasFeeCap *big.Int
	BlobHashes    []common.Hash

	Func Func  // Func to encode
	Args []any // Arguments for Func
}

Message represents a transaction without the signature.

If no input data is given, but Func is not null, the input data is automatically encoded from the given Func and Args arguments by many functions that accept a Message struct as an argument.

func (*Message) MarshalJSON

func (msg *Message) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler.

func (*Message) MustSetTx

func (msg *Message) MustSetTx(tx *types.Transaction, signer types.Signer) *Message

MustSetTx is like [SetTx] but panics if the sender retrieval fails.

func (*Message) Set

func (msg *Message) Set(msg2 *Message) *Message

Set sets msg to the given Message and returns it.

func (*Message) SetCallMsg

func (msg *Message) SetCallMsg(callMsg ethereum.CallMsg) *Message

SetCallMsg sets msg to the ethereum.CallMsg callMsg and returns msg.

func (*Message) SetTx

func (msg *Message) SetTx(tx *types.Transaction, signer types.Signer) (*Message, error)

SetTx sets msg to the types.Transaction tx and returns msg.

func (*Message) UnmarshalJSON

func (msg *Message) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler.

type RPCCaller

type RPCCaller interface {
	// Create a new rpc.BatchElem for doing the RPC call.
	CreateRequest() (elem rpc.BatchElem, err error)

	// Handle the response from the rpc.BatchElem to handle its result.
	HandleResponse(elem rpc.BatchElem) (err error)
}

RPCCaller is the interface that groups the basic CreateRequest and HandleResponse methods.

Example (GetTransactionBySenderAndNonce)
package main

import (
	"fmt"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/rpc"
	"github.com/meltingclock/w3"
	"github.com/meltingclock/w3/w3types"
)

// TxBySenderAndNonceFactory requests the senders transaction hash by the nonce.
func TxBySenderAndNonceFactory(sender common.Address, nonce uint64) w3types.RPCCallerFactory[common.Hash] {
	return &getTransactionBySenderAndNonceFactory{
		sender: sender,
		nonce:  nonce,
	}
}

// getTransactionBySenderAndNonceFactory implements the w3types.RPCCaller and
// w3types.RPCCallerFactory interfaces. It stores the method parameters and
// the the reference to the return value.
type getTransactionBySenderAndNonceFactory struct {
	// params
	sender common.Address
	nonce  uint64

	// returns
	returns *common.Hash
}

// Returns sets the reference to the return value.
//
// Return implements the [w3types.RPCCallerFactory] interface.
func (f *getTransactionBySenderAndNonceFactory) Returns(txHash *common.Hash) w3types.RPCCaller {
	f.returns = txHash
	return f
}

// CreateRequest creates a batch request element for the Otterscan getTransactionBySenderAndNonce method.
//
// CreateRequest implements the [w3types.RPCCaller] interface.
func (f *getTransactionBySenderAndNonceFactory) CreateRequest() (rpc.BatchElem, error) {
	return rpc.BatchElem{
		Method: "ots_getTransactionBySenderAndNonce",
		Args:   []any{f.sender, f.nonce},
		Result: f.returns,
	}, nil
}

// HandleResponse handles the response of the Otterscan getTransactionBySenderAndNonce method.
//
// HandleResponse implements the [w3types.RPCCaller] interface.
func (f *getTransactionBySenderAndNonceFactory) HandleResponse(elem rpc.BatchElem) error {
	if err := elem.Error; err != nil {
		return err
	}
	return nil
}

func main() {
	client := w3.MustDial("https://docs-demo.quiknode.pro")
	defer client.Close()

	addr := w3.A("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")

	var firstTxHash common.Hash
	if err := client.Call(
		TxBySenderAndNonceFactory(addr, 0).Returns(&firstTxHash),
	); err != nil {
		fmt.Printf("Request failed: %v\n", err)
		return
	}

	fmt.Printf("First Tx Hash: %s\n", firstTxHash)
}
Output:

First Tx Hash: 0x6ff0860e202c61189cb2a3a38286bffd694acbc50577df6cb5a7ff40e21ea074

type RPCCallerFactory

type RPCCallerFactory[T any] interface {
	// Returns given argument points to the variable in which to store the
	// calls result.
	Returns(*T) RPCCaller
}

RPCCallerFactory is the interface that wraps the basic Returns method.

type RPCSubscriber

type RPCSubscriber interface {
	// CreateRequest returns the namespace, channel, params for starting a new
	// subscription and an error if the request cannot be created.
	CreateRequest() (namespace string, ch any, params []any, err error)
}

RPCSubscriber is the interface that wraps the basic CreateRequest method.

type State

type State map[common.Address]*Account

func (State) Merge

func (s State) Merge(other State) (merged State)

Merge returns a new state that is the result of merging the called state with the given state. All state in other state will overwrite the state in the called state.

func (State) SetGenesisAlloc

func (s State) SetGenesisAlloc(alloc types.GenesisAlloc) State

SetGenesisAlloc copies the given types.GenesisAlloc to the state and returns it.

type Storage

type Storage map[common.Hash]common.Hash

Jump to

Keyboard shortcuts

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