Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starknet crawler #45

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
79 changes: 79 additions & 0 deletions blockchain/blockchain_stark.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package {{.BlockchainNameLower}}

import (
"bytes"
"context"
"math/big"
"net/http"
"net/http/cookiejar"

// "github.com/NethermindEth/starknet.go/rpc"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"golang.org/x/net/publicsuffix"
"google.golang.org/protobuf/proto"

seer_common "github.com/moonstream-to/seer/blockchain/common"
"github.com/moonstream-to/seer/indexer"
)

// Based on starknet rpc Provider
func NewClient(url string, timeout int) (*Client, error) {
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return nil, err
}
client := &http.Client{Jar: jar}
rpcClient, clientErr := ethrpc.DialHTTPWithClient(url, client)
if clientErr != nil {
return nil, clientErr
}

return &Client{rpcClient: rpcClient}, nil
}

// Client is a wrapper around the Starknet JSON-RPC client.

type Client struct {
rpcClient *ethrpc.Client
}

// Client common

// ChainType returns the chain type.
func (c *Client) ChainType() string {
return "starknet"
}

func (c *Client) DecodeProtoEntireBlockToJson(rawData *bytes.Buffer) (*seer_common.BlocksBatchJson, error) {
return nil, nil
}

func (c *Client) DecodeProtoEntireBlockToLabels(rawData *bytes.Buffer, blocksCache map[uint64]uint64, abiMap map[string]map[string]map[string]string) ([]indexer.EventLabel, []indexer.TransactionLabel, error) {
return nil, nil, nil
}

func (c *Client) DecodeProtoTransactionsToLabels(transactions []string, blocksCache map[uint64]uint64, abiMap map[string]map[string]map[string]string) ([]indexer.TransactionLabel, error) {
return nil, nil
}

func (c *Client) FetchAsProtoBlocksWithEvents(from, to *big.Int, debug bool, maxRequests int) ([]proto.Message, []indexer.BlockIndex, []indexer.TransactionIndex, []indexer.LogIndex, uint64, error) {
return nil, nil, nil, nil, 0, nil
}

// GetLatestBlockNumber returns the latest block number.
func (c *Client) GetLatestBlockNumber() (*big.Int, error) {
ctx := context.Background()
var bn uint64
if err := c.rpcClient.CallContext(ctx, &bn, "starknet_blockNumber"); err != nil {
return nil, err
}

// Convert the uint64 to a big.Int
blockNumber := new(big.Int).SetUint64(bn)

return blockNumber, nil
}

func (c *Client) ProcessBlocksToBatch(msgs []proto.Message) (proto.Message, error) {
return nil, nil
}
4 changes: 4 additions & 0 deletions blockchain/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/moonstream-to/seer/blockchain/mantle_sepolia"
"github.com/moonstream-to/seer/blockchain/polygon"
"github.com/moonstream-to/seer/blockchain/sepolia"
"github.com/moonstream-to/seer/blockchain/starknet"
"github.com/moonstream-to/seer/blockchain/xai"
"github.com/moonstream-to/seer/blockchain/xai_sepolia"
"github.com/moonstream-to/seer/indexer"
Expand Down Expand Up @@ -62,6 +63,9 @@ func NewClient(chain, url string, timeout int) (BlockchainClient, error) {
} else if chain == "imx_zkevm_sepolia" {
client, err := imx_zkevm_sepolia.NewClient(url, timeout)
return client, err
} else if chain == "starknet" {
client, err := starknet.NewClient(url, timeout)
return client, err
} else {
return nil, errors.New("unsupported chain type")
}
Expand Down
79 changes: 79 additions & 0 deletions blockchain/starknet/starknet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package starknet

import (
"bytes"
"context"
"math/big"
"net/http"
"net/http/cookiejar"

// "github.com/NethermindEth/starknet.go/rpc"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"golang.org/x/net/publicsuffix"
"google.golang.org/protobuf/proto"

seer_common "github.com/moonstream-to/seer/blockchain/common"
"github.com/moonstream-to/seer/indexer"
)

// Based on starknet rpc Provider
func NewClient(url string, timeout int) (*Client, error) {
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return nil, err
}
client := &http.Client{Jar: jar}
rpcClient, clientErr := ethrpc.DialHTTPWithClient(url, client)
if clientErr != nil {
return nil, clientErr
}

return &Client{rpcClient: rpcClient}, nil
}

// Client is a wrapper around the Starknet JSON-RPC client.

type Client struct {
rpcClient *ethrpc.Client
}

// Client common

// ChainType returns the chain type.
func (c *Client) ChainType() string {
return "starknet"
}

func (c *Client) DecodeProtoEntireBlockToJson(rawData *bytes.Buffer) (*seer_common.BlocksBatchJson, error) {
return nil, nil
}

func (c *Client) DecodeProtoEntireBlockToLabels(rawData *bytes.Buffer, blocksCache map[uint64]uint64, abiMap map[string]map[string]map[string]string) ([]indexer.EventLabel, []indexer.TransactionLabel, error) {
return nil, nil, nil
}

func (c *Client) DecodeProtoTransactionsToLabels(transactions []string, blocksCache map[uint64]uint64, abiMap map[string]map[string]map[string]string) ([]indexer.TransactionLabel, error) {
return nil, nil
}

func (c *Client) FetchAsProtoBlocksWithEvents(from, to *big.Int, debug bool, maxRequests int) ([]proto.Message, []indexer.BlockIndex, []indexer.TransactionIndex, []indexer.LogIndex, uint64, error) {
return nil, nil, nil, nil, 0, nil
}

// GetLatestBlockNumber returns the latest block number.
func (c *Client) GetLatestBlockNumber() (*big.Int, error) {
ctx := context.Background()
var bn uint64
if err := c.rpcClient.CallContext(ctx, &bn, "starknet_blockNumber"); err != nil {
return nil, err
}

// Convert the uint64 to a big.Int
blockNumber := new(big.Int).SetUint64(bn)

return blockNumber, nil
}

func (c *Client) ProcessBlocksToBatch(msgs []proto.Message) (proto.Message, error) {
return nil, nil
}
Loading
Loading