api

package
v0.0.0-...-9345ff8 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2023 License: MIT Imports: 9 Imported by: 0

README

Node RPC API

Go implementation of Starknet node API

Install

go get github.com/dipdup-io/starknet-api-go/pkg/rpc

Usage

You can find example of usage in our repository.

Methods

Library implements following methods:

BlockHashAndNumber(ctx context.Context, opts ...RequestOption) (*Response[BlockHashAndNumber], error)

BlockNumber(ctx context.Context, opts ...RequestOption) (*Response[uint64], error)

Call(ctx context.Context, params CallRequest, block BlockID, opts ...RequestOption) (*Response[[]string], error)

ChainID(ctx context.Context, opts ...RequestOption) (*Response[string], error)

EstimateFee(ctx context.Context, tx Transaction, block BlockID, opts ...RequestOption) (*Response[EstmatedGas], error)

GetBlockTransactionCount(ctx context.Context, block BlockID, opts ...RequestOption) (*Response[uint64], error)

GetBlockWithTxHashes(ctx context.Context, block BlockID, opts ...RequestOption) (*Response[BlockWithTxHashes], error)

GetBlockWithTxs(ctx context.Context, bloxk BlockID, opts ...RequestOption) (*Response[BlockWithTxs], error)

GetClassAt(ctx context.Context, block BlockID, contractAddress string, opts ...RequestOption) (*Response[Class], error)

GetClassHashAt(ctx context.Context, block BlockID, contractAddress string, opts ...RequestOption) (*Response[string], error)

GetClass(ctx context.Context, block BlockID, classHash string, opts ...RequestOption) (*Response[Class], error)

GetEvents(ctx context.Context, filters EventsFilters, opts ...RequestOption) (*Response[EventsResponse], error) 

GetNonce(ctx context.Context, contract string, block BlockID, opts ...RequestOption) (*Response[string], error)

GetStateUpdate(ctx context.Context, block BlockID, opts ...RequestOption) (*Response[StateUpdate], error)

GetStorageAt(ctx context.Context, contract, key string, block BlockID, opts ...RequestOption) (*Response[string], error)

GetTransactionByBlockNumberAndIndex(ctx context.Context, block BlockID, index uint64, opts ...RequestOption) (*Response[Transaction], error)

GetTransactionByHash(ctx context.Context, hash string, opts ...RequestOption) (*Response[Transaction], error)

GetTransactionReceipts(ctx context.Context, hash string, opts ...RequestOption) (*Response[Receipt], error)

PendingTransactions(ctx context.Context, opts ...RequestOption) (*Response[Transaction], error)

Syncing(ctx context.Context, opts ...RequestOption) (*Response[Syncing], error)
Creation

First of all you should import package in your code:

starknetData "github.com/QinPengLin/starknet-go/pkg/data"
rpc "github.com/QinPengLin/starknet-go/pkg/rpc"

Then create API object:

api := rpc.NewAPI("LINK_TO_NODE_RPC")

And call any method:

response, err := api.GetBlockTransactionCount(ctx, starknetData.BlockID{
    Number: 100,
}, rpc.WithTimeout(10))
if err != nil {
    log.Panic(err)
}
Timeout

If you need timeout on your request you can use option WithTimeout and set timeout by passing context:

// by option
response, err := api.GetBlockTransactionCount(ctx, starknetData.BlockID{
    Number: 100,
}, rpc.WithTimeout(10))
if err != nil {
    log.Panic(err)
}

// by context
requestCtx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

response, err := api.GetBlockTransactionCount(requestCtx, starknetData.BlockID{
    Number: 100,
})
if err != nil {
    log.Panic(err)
}

If you pass both variants of timeout the least will be used.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type API

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

API - wrapper of starknet node API.

func NewAPI

func NewAPI(baseURL string, opts ...ApiOption) API

NewAPI - constructor of API

func (API) BlockHashAndNumber

func (api API) BlockHashAndNumber(ctx context.Context, opts ...RequestOption) (*Response[BlockHashAndNumber], error)

BlockHashAndNumber - Get the most recent accepted block hash and number

func (API) BlockNumber

func (api API) BlockNumber(ctx context.Context, opts ...RequestOption) (*Response[uint64], error)

BlockNumber - Get the most recent accepted block number

func (API) Call

func (api API) Call(ctx context.Context, params CallRequest, block data.BlockID, opts ...RequestOption) (*Response[[]string], error)

Call - Calls a function in a contract and returns the return value. Using this call will not create a transaction; hence, will not change the state

func (API) ChainID

func (api API) ChainID(ctx context.Context, opts ...RequestOption) (*Response[string], error)

ChainID - Return the currently configured StarkNet chain id

func (API) EstimateFee

func (api API) EstimateFee(ctx context.Context, tx data.Transaction, block data.BlockID, opts ...RequestOption) (*Response[EstmatedGas], error)

EstimateFee - estimates the resources required by a transaction relative to a given state

func (API) GetBlockTransactionCount

func (api API) GetBlockTransactionCount(ctx context.Context, block data.BlockID, opts ...RequestOption) (*Response[uint64], error)

GetBlockTransactionCount -

func (API) GetBlockWithTxHashes

func (api API) GetBlockWithTxHashes(ctx context.Context, block data.BlockID, opts ...RequestOption) (*Response[BlockWithTxHashes], error)

GetBlockWithTxHashes -

func (API) GetBlockWithTxs

func (api API) GetBlockWithTxs(ctx context.Context, block data.BlockID, opts ...RequestOption) (*Response[BlockWithTxs], error)

GetBlockWithTxs -

func (API) GetClass

func (api API) GetClass(ctx context.Context, block data.BlockID, classHash string, opts ...RequestOption) (*Response[data.Class], error)

GetClass - Get the contract class definition in the given block associated with the given hash

func (API) GetClassAt

func (api API) GetClassAt(ctx context.Context, block data.BlockID, contractAddress string, opts ...RequestOption) (*Response[data.Class], error)

GetClassAt -

func (API) GetClassHashAt

func (api API) GetClassHashAt(ctx context.Context, block data.BlockID, contractAddress string, opts ...RequestOption) (*Response[string], error)

GetClassHashAt -

func (API) GetEvents

func (api API) GetEvents(ctx context.Context, filters EventsFilters, opts ...RequestOption) (*Response[EventsResponse], error)

GetEvents - Returns all event objects matching the conditions in the provided filter

func (API) GetNonce

func (api API) GetNonce(ctx context.Context, contract string, block data.BlockID, opts ...RequestOption) (*Response[string], error)

GetNonce -

func (API) GetStateUpdate

func (api API) GetStateUpdate(ctx context.Context, block data.BlockID, opts ...RequestOption) (*Response[data.StateUpdate], error)

GetStateUpdate -

func (API) GetStorageAt

func (api API) GetStorageAt(ctx context.Context, contract, key string, block data.BlockID, opts ...RequestOption) (*Response[string], error)

GetStorageAt -

func (API) GetTransactionByBlockNumberAndIndex

func (api API) GetTransactionByBlockNumberAndIndex(ctx context.Context, block data.BlockID, index uint64, opts ...RequestOption) (*Response[data.Transaction], error)

GetTransactionByBlockNumberAndIndex - Get the details of the transaction given by the identified block and index in that block. If no transaction is found, null is returned.

func (API) GetTransactionByHash

func (api API) GetTransactionByHash(ctx context.Context, hash string, opts ...RequestOption) (*Response[data.Transaction], error)

GetTransactionByHash -

func (API) GetTransactionReceipts

func (api API) GetTransactionReceipts(ctx context.Context, hash string, opts ...RequestOption) (*Response[Receipt], error)

GetTransactionReceipts -

func (API) PendingTransactions

func (api API) PendingTransactions(ctx context.Context, opts ...RequestOption) (*Response[data.Transaction], error)

PendingTransactions - Returns the transactions in the transaction pool, recognized by this sequencer

func (API) Syncing

func (api API) Syncing(ctx context.Context, opts ...RequestOption) (*Response[Syncing], error)

Syncing - Returns an object about the sync status, or false if the node is not synching

type ApiOption

type ApiOption func(*API)

ApiOption -

func WithRateLimit

func WithRateLimit(requestPerSecond int) ApiOption

WithRateLimit -

type BlockHashAndNumber

type BlockHashAndNumber struct {
	Hash   string `json:"block_hash"`
	Number uint64 `json:"block_number"`
}

BlockHashAndNumber -

type BlockWithTxHashes

type BlockWithTxHashes struct {
	Status           string   `json:"status"`
	BlockHash        string   `json:"block_hash"`
	ParentHash       string   `json:"parent_hash"`
	BlockNumber      uint64   `json:"block_number"`
	NewRoot          string   `json:"new_root"`
	Timestamp        int64    `json:"timestamp"`
	SequencerAddress string   `json:"sequencer_address"`
	Transactions     []string `json:"transactions"`
}

BlockWithTxHashes -

type BlockWithTxs

type BlockWithTxs struct {
	Status           string             `json:"status"`
	BlockHash        string             `json:"block_hash"`
	ParentHash       string             `json:"parent_hash"`
	BlockNumber      uint64             `json:"block_number"`
	NewRoot          string             `json:"new_root"`
	Timestamp        int64              `json:"timestamp"`
	SequencerAddress string             `json:"sequencer_address"`
	Transactions     []data.Transaction `json:"transactions"`
}

BlockWithTxs -

type CallRequest

type CallRequest struct {
	ContractAddress    string   `json:"contract_address"`
	EntrypointSelector string   `json:"entry_point_selector"`
	Calldata           []string `json:"calldata"`
}

CallRequest -

type EstmatedGas

type EstmatedGas struct {
	GasConsumed string `json:"gas_consumed"`
	GasPrice    string `json:"gas_price"`
	OverallFee  string `json:"overall_fee"`
}

EstmatedGas -

type EventsFilters

type EventsFilters struct {
	FromBlock         *data.BlockID `json:"from_block,omitempty"`
	ToBlock           *data.BlockID `json:"to_block,omitempty"`
	Address           string        `json:"address,omitempty"`
	Keys              [][]string    `json:"keys"`
	ContinuationToken string        `json:"continuation_token,omitempty"`
	ChunkSize         int           `json:"chunk_size"`
}

EventsFilters -

type EventsResponse

type EventsResponse struct {
	Events            []data.Event `json:"events"`
	ContinuationToken string       `json:"continuation_token"`
}

EventsResponse -

type Receipt

type Receipt struct {
	Type            string         `json:"type"`
	TransactionHash string         `json:"transaction_hash"`
	ActualFee       string         `json:"actual_fee"`
	Status          string         `json:"status"`
	BlockHash       string         `json:"block_hash"`
	BlockNumber     uint64         `json:"block_number"`
	MessagesSent    []data.Message `json:"messages_sent"`
	Events          []data.Event   `json:"events"`
	ContractAddress string         `json:"contract_address"`
}

Receipt -

type Request

type Request struct {
	Version string `json:"jsonrpc"`
	Method  string `json:"method"`
	Params  []any  `json:"params"`
	ID      uint64 `json:"id"`
	// contains filtered or unexported fields
}

Request -

type RequestOption

type RequestOption func(*Request)

RequestOption -

func WithCustomID

func WithCustomID(id uint64) RequestOption

WithCustomID - set custom request id. Request id increment automatically per request.

func WithJsonRpcVersion

func WithJsonRpcVersion(version string) RequestOption

WithJsonRpcVersion - change json rpc version. Default: 2.0

func WithTimeout

func WithTimeout(timeout uint64) RequestOption

WithTimeout - set custom timeout in seconds. Default: 10 seconds

type Response

type Response[T any] struct {
	Version string      `json:"jsonrpc"`
	Result  T           `json:"result,omitempty"`
	ID      uint64      `json:"id"`
	Error   *data.Error `json:"error,omitempty"`
}

Response -

type Syncing

type Syncing struct {
	Synced            bool   `json:"-"`
	StartingBlockNum  string `json:"starting_block_num"`
	CurrentBlockNum   string `json:"current_block_num"`
	HighestBlockNum   string `json:"highest_block_num"`
	StartingBlockHash string `json:"starting_block_hash"`
	CurrentBlockHash  string `json:"current_block_hash"`
	HighestBlockHash  string `json:"highest_block_hash"`
}

Syncing -

func (*Syncing) UnmarshalJSON

func (s *Syncing) UnmarshalJSON(data []byte) error

UnmarshalJSON -

Jump to

Keyboard shortcuts

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