client

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: Apache-2.0, MIT Imports: 10 Imported by: 0

Documentation

Overview

Package client provides an HTTP RPC client for interacting with the Tempo blockchain.

This package implements a JSON-RPC 2.0 client for sending transactions and querying the Tempo network.

Basic Usage

Create a client and send a transaction:

client := client.New(
	"https://rpc.moderato.tempo.xyz",
	client.WithAuth("username", "password"), // optional basic auth
)

// Send a transaction asynchronously
txHash, err := client.SendRawTransaction(context.Background(), "0x76...")
if err != nil {
	log.Fatal(err)
}
fmt.Printf("Transaction hash: %s\n", txHash)

Synchronous transaction broadcasting:

// Wait for transaction to be included in a block
txHash, err := client.SendRawTransactionSync(context.Background(), "0x76...")
if err != nil {
	log.Fatal(err)
}
fmt.Printf("Transaction confirmed: %s\n", txHash)

Sign a transaction without broadcasting:

// Sign transaction and get the raw hex without broadcasting
signedTx, err := client.SignTransaction(context.Background(), txObject)
if err != nil {
	log.Fatal(err)
}
// Now you can broadcast it yourself or through a different channel
fmt.Printf("Signed transaction: %s\n", signedTx)

Generic RPC requests:

// Call any JSON-RPC method
response, err := client.SendRequest(context.Background(), "eth_blockNumber")
if err != nil {
	log.Fatal(err)
}

if response.Error != nil {
	log.Fatalf("RPC error: %s", response.Error.Message)
}

blockNum := response.Result.(string)
fmt.Printf("Block number: %s\n", blockNum)

Index

Constants

View Source
const (
	// Standard JSON-RPC errors
	ParseError     = -32700
	InvalidRequest = -32600
	MethodNotFound = -32601
	InvalidParams  = -32602
	InternalError  = -32603

	// Custom errors for fee payer server
	InvalidTransactionType = -32000
)

JSON-RPC error codes (standard + custom)

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchRequest

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

BatchRequest represents a batch of JSON-RPC requests. Use NewBatchRequest() to create a new batch and Add() to add requests.

func NewBatchRequest

func NewBatchRequest() *BatchRequest

NewBatchRequest creates a new batch request.

func (*BatchRequest) Add

func (b *BatchRequest) Add(method string, params ...interface{}) *BatchRequest

Add adds a request to the batch. The ID is automatically assigned incrementally.

func (*BatchRequest) Len

func (b *BatchRequest) Len() int

Len returns the number of requests in the batch.

func (*BatchRequest) Requests

func (b *BatchRequest) Requests() []*JSONRPCRequest

Requests returns the list of requests in the batch.

type Client

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

Client is a basic HTTP client for interacting with the Tempo blockchain.

func New

func New(rpcURL string, opts ...Option) *Client

New creates a new Tempo RPC client with the given RPC URL. Optional configuration can be provided via Option functions.

func (*Client) GetBlockNumber

func (c *Client) GetBlockNumber(ctx context.Context) (uint64, error)

GetBlockNumber gets the current block number. This is a useful proxy for making sure that the RPC is responsive.

func (*Client) GetChainID added in v0.2.1

func (c *Client) GetChainID(ctx context.Context) (uint64, error)

GetChainID gets the chain ID from the connected node.

func (*Client) GetNonce added in v0.2.1

func (c *Client) GetNonce(ctx context.Context, address string, nonceKey *big.Int) (uint64, error)

GetNonce gets the nonce for an address and nonce key using the Nonce Manager contract. This is used for Tempo's 2D nonce system which enables parallel transaction submission. The nonceKey is a 192-bit value; use nonceKey > 0 for parallel lanes. Key 0 is the protocol nonce (use GetTransactionCount instead).

func (*Client) GetTransactionCount

func (c *Client) GetTransactionCount(ctx context.Context, address string) (uint64, error)

GetTransactionCount gets the nonce for an address using the default nonce key (0).

func (*Client) RPCURL added in v0.2.1

func (c *Client) RPCURL() string

RPCURL returns the RPC URL configured for this client.

func (*Client) SendBatch

func (c *Client) SendBatch(ctx context.Context, batch *BatchRequest) ([]*JSONRPCResponse, error)

SendBatch sends a batch of JSON-RPC requests to the Tempo network. This is more efficient than sending multiple individual requests. All requests are sent in a single HTTP request to reduce network overhead.

Example:

batch := client.NewBatchRequest()
batch.Add("eth_blockNumber").
     Add("eth_getBalance", "0x...", "latest")
responses, err := client.SendBatch(ctx, batch)

func (*Client) SendRawTransaction

func (c *Client) SendRawTransaction(ctx context.Context, serializedTx string) (string, error)

SendRawTransaction broadcasts a raw transaction to the Tempo network. The transaction should be a hex-encoded TempoTransaction starting with prefix "0x76".

func (*Client) SendRawTransactionSync

func (c *Client) SendRawTransactionSync(ctx context.Context, serializedTx string) (string, error)

SendRawTransactionSync broadcasts a raw transaction synchronously to the Tempo network. This waits for the transaction to be included in a block before returning.

func (*Client) SendRawTransactionWithMethod

func (c *Client) SendRawTransactionWithMethod(ctx context.Context, method, serializedTx string) (string, error)

SendRawTransactionWithMethod broadcasts a raw transaction using the specified method.

func (*Client) SendRequest

func (c *Client) SendRequest(ctx context.Context, method string, params ...interface{}) (*JSONRPCResponse, error)

SendRequest sends a generic JSON-RPC request to the Tempo network.

func (*Client) SignTransaction

func (c *Client) SignTransaction(ctx context.Context, tx interface{}) (string, error)

SignTransaction signs a transaction without broadcasting it. Returns the signed transaction as a hex string that can be broadcast later. This is useful when you want to sign a transaction but broadcast it through a different channel.

type JSONRPCError

type JSONRPCError struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

JSONRPCError represents a JSON-RPC 2.0 error.

func (*JSONRPCError) Error

func (e *JSONRPCError) Error() string

Error implements the error interface for JSONRPCError.

type JSONRPCRequest

type JSONRPCRequest struct {
	JSONRPC string        `json:"jsonrpc"`
	ID      interface{}   `json:"id"`
	Method  string        `json:"method"`
	Params  []interface{} `json:"params"`
}

JSONRPCRequest represents a JSON-RPC 2.0 request.

func NewJSONRPCRequest

func NewJSONRPCRequest(id interface{}, method string, params ...interface{}) *JSONRPCRequest

NewJSONRPCRequest creates a new JSON-RPC request.

type JSONRPCResponse

type JSONRPCResponse struct {
	JSONRPC string        `json:"jsonrpc"`
	ID      interface{}   `json:"id"`
	Result  interface{}   `json:"result,omitempty"`
	Error   *JSONRPCError `json:"error,omitempty"`
}

JSONRPCResponse represents a JSON-RPC 2.0 response.

func NewJSONRPCErrorResponse

func NewJSONRPCErrorResponse(id interface{}, code int, message string, data interface{}) *JSONRPCResponse

NewJSONRPCErrorResponse creates an error JSON-RPC response.

func NewJSONRPCResponse

func NewJSONRPCResponse(id interface{}, result interface{}) *JSONRPCResponse

NewJSONRPCResponse creates a successful JSON-RPC response.

func (*JSONRPCResponse) CheckError

func (r *JSONRPCResponse) CheckError() error

CheckError returns an error if the response contains an RPC error.

type Option

type Option func(*Client)

Option is a functional option for configuring the Client.

func WithAuth

func WithAuth(username, password string) Option

WithAuth configures basic authentication for the client.

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

WithHTTPClient configures a custom HTTP client.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout configures the HTTP timeout for the client.

Jump to

Keyboard shortcuts

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