Skip to content

Commit

Permalink
Added transactions converting
Browse files Browse the repository at this point in the history
  • Loading branch information
k-karuna committed Dec 23, 2024
1 parent 46937ab commit 29a948c
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 12 deletions.
8 changes: 8 additions & 0 deletions pkg/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indexer
import (
"bytes"
"context"
sqdAdapter "github.com/dipdup-io/starknet-indexer/pkg/indexer/subsquid/adapter"
sqdRcvr "github.com/dipdup-io/starknet-indexer/pkg/indexer/subsquid/receiver"
"runtime"
"sync"
Expand Down Expand Up @@ -55,6 +56,7 @@ type Indexer struct {
idGenerator *generator.IdGenerator
receiver *receiver.Receiver
sqdReceiver *sqdRcvr.Receiver
adapter *sqdAdapter.Adapter
statusChecker *statusChecker
rollbackManager models.Rollback

Expand Down Expand Up @@ -111,6 +113,11 @@ func New(
return nil, err
}
indexer.sqdReceiver = sqdReceiver
indexer.adapter = sqdAdapter.New()

if err = indexer.adapter.AttachTo(indexer.sqdReceiver, sqdRcvr.OutputName, sqdAdapter.InputName); err != nil {
return nil, errors.Wrap(err, "while attaching adapter to receiver")
}
default:
rcvr, err := receiver.NewReceiver(cfg, datasource)
if err != nil {
Expand Down Expand Up @@ -157,6 +164,7 @@ func (indexer *Indexer) Start(ctx context.Context) {
switch indexer.cfg.Datasource {
case "subsquid":
indexer.sqdReceiver.Start(ctx)
indexer.adapter.Start(ctx)
default:
indexer.receiver.Start(ctx)
indexer.G.GoCtx(ctx, indexer.sync)
Expand Down
4 changes: 2 additions & 2 deletions pkg/indexer/subsquid/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const (
StopOutput = "stop"
)

func New() Adapter {
m := Adapter{
func New() *Adapter {
m := &Adapter{
BaseModule: modules.New("sqd adapter"),
}
m.CreateInputWithCapacity(InputName, 128)
Expand Down
127 changes: 127 additions & 0 deletions pkg/indexer/subsquid/adapter/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package adapter

import (
"context"
"fmt"
"github.com/dipdup-io/starknet-go-api/pkg/data"
"github.com/dipdup-io/starknet-go-api/pkg/encoding"
"github.com/dipdup-io/starknet-indexer/internal/storage"
"github.com/dipdup-io/starknet-indexer/pkg/indexer/receiver"
"github.com/dipdup-io/starknet-indexer/pkg/indexer/subsquid/receiver/api"
"time"
)

func (a *Adapter) convert(_ context.Context, block *api.SqdBlockResponse) error {
fmt.Print("converting block number ")
fmt.Print(block.Header.Number)
fmt.Print("\n")

b := receiver.Block{
Height: block.Header.Number,
Status: storage.NewStatus(block.Header.Status),
Hash: data.Felt(block.Header.Hash).Bytes(),
ParentHash: data.Felt(block.Header.ParentHash).Bytes(),
NewRoot: encoding.MustDecodeHex(block.Header.NewRoot),
Time: time.Unix(block.Header.Timestamp, 0).UTC(),
SequencerAddress: encoding.MustDecodeHex(block.Header.SequencerAddress),
Transactions: convertTransactions(block),
Receipts: nil,
}
fmt.Print(b.Hash)

return nil
}

func convertTransactions(block *api.SqdBlockResponse) []receiver.Transaction {
txs := block.Transactions
resultTxs := make([]receiver.Transaction, len(txs))
for i, tx := range txs {
var body any
switch tx.Type {
case data.TransactionTypeInvoke:
body = data.Invoke{
MaxFee: stringToFelt(tx.MaxFee),
Nonce: uint64ToFelt(tx.Nonce),
ContractAddress: stringToFelt(tx.ContractAddress),
EntrypointSelector: stringToFelt(tx.EntryPointSelector),
SenderAddress: stringToFelt(tx.SenderAddress),
Signature: parseStringSlice(tx.Signature),
Calldata: parseStringSlice(tx.Calldata),
}
case data.TransactionTypeDeclare:
body = data.Declare{
MaxFee: stringToFelt(tx.MaxFee),
Nonce: uint64ToFelt(tx.Nonce),
SenderAddress: stringToFelt(tx.SenderAddress),
ContractAddress: stringToFelt(tx.ContractAddress),
Signature: parseStringSlice(tx.Signature),
ClassHash: stringToFelt(tx.ClassHash),
CompiledClassHash: stringToFelt(tx.CompiledClassHash),
}
case data.TransactionTypeDeploy:
body = data.Deploy{
ContractAddressSalt: parseString(tx.ContractAddressSalt),
ConstructorCalldata: parseStringSlice(tx.Calldata),
ClassHash: stringToFelt(tx.ClassHash),
ContractAddress: block.GetDeployContractAddress(tx.TransactionIndex),
}
case data.TransactionTypeDeployAccount:
body = data.DeployAccount{
MaxFee: stringToFelt(tx.MaxFee),
Nonce: uint64ToFelt(tx.Nonce),
ContractAddress: stringToFelt(tx.ContractAddress),
ContractAddressSalt: parseString(tx.ContractAddressSalt),
ClassHash: stringToFelt(tx.ClassHash),
ConstructorCalldata: parseStringSlice(tx.ConstructorCalldata),
Signature: parseStringSlice(tx.Signature),
}
case data.TransactionTypeL1Handler:
body = data.L1Handler{
Nonce: uint64ToFelt(tx.Nonce),
ContractAddress: stringToFelt(tx.ContractAddress),
EntrypointSelector: stringToFelt(tx.EntryPointSelector),
Calldata: parseStringSlice(tx.Calldata),
}
default:
return nil
}

resultTxs[i] = receiver.Transaction{
Type: tx.Type,
Version: data.Felt(tx.Version),
Hash: data.Felt(tx.TransactionHash),
Body: body,
}
}

return resultTxs
}

func uint64ToFelt(value *uint64) data.Felt {
if value == nil {
return ""
}
return data.Felt(fmt.Sprintf("%d", *value))
}

func stringToFelt(value *string) data.Felt {
if value == nil {
return ""
}
return data.Felt(*value)
}

func parseStringSlice(value *[]string) []string {
if value == nil {
return []string{}
}

return *value
}

func parseString(value *string) string {
if value == nil {
return ""
}
return *value
}
10 changes: 7 additions & 3 deletions pkg/indexer/subsquid/adapter/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ func (a *Adapter) listen(ctx context.Context) {
continue
}

a.Log.Info().
Uint64("level", block.Header.Number).
Msg("received block")
if err := a.convert(ctx, block); err != nil {
a.Log.Err(err).
Uint64("height", block.Header.Number).
Msg("convert error")
a.MustOutput(StopOutput).Push(struct{}{})
continue
}
}
}
}
15 changes: 15 additions & 0 deletions pkg/indexer/subsquid/receiver/api/getDeployContractAddress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package api

import "github.com/dipdup-io/starknet-go-api/pkg/data"

func (block *SqdBlockResponse) GetDeployContractAddress(txIndex uint) data.Felt {
for _, trace := range block.Traces {
if trace.TraceType != data.TransactionTypeDeploy || trace.TraceType != data.TransactionTypeDeployAccount ||

Check failure on line 7 in pkg/indexer/subsquid/receiver/api/getDeployContractAddress.go

View workflow job for this annotation

GitHub Actions / Linter

bools: suspect or: trace.TraceType != data.TransactionTypeDeploy || trace.TraceType != data.TransactionTypeDeployAccount (govet)

Check failure on line 7 in pkg/indexer/subsquid/receiver/api/getDeployContractAddress.go

View workflow job for this annotation

GitHub Actions / test

suspect or: trace.TraceType != data.TransactionTypeDeploy || trace.TraceType != data.TransactionTypeDeployAccount
trace.TransactionIndex != txIndex {
continue
}

return data.Felt(trace.ContractAddress)
}
return ""
}
18 changes: 11 additions & 7 deletions pkg/indexer/subsquid/receiver/api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,25 @@ type SqdBlockResponse struct {
}

type BlockHeader struct {
Number uint64 `example:"321" json:"number"`
Hash string `example:"0x44529f2c44d9113e0ba4e53cb6e84f425ec186cda27545827b5a72d5540bfdc" json:"hash"`
Timestamp int64 `example:"1641950335" json:"timestamp"`
Number uint64 `example:"321" json:"number"`
Hash string `example:"0x44529f2c44d9113e0ba4e53cb6e84f425ec186cda27545827b5a72d5540bfdc" json:"hash"`
ParentHash string `example:"0x44529f2c44d9113e0ba4e53cb6e84f425ec186cda27545827b5a72d5540bfdc" json:"parentHash"`
Status string `example:"ACCEPTED_ON_L1" json:"status"`
NewRoot string `example:"0x44529f2c44d9113e0ba4e53cb6e84f425ec186cda27545827b5a72d5540bfdc" json:"newRoot"`
Timestamp int64 `example:"1641950335" json:"timestamp"`
SequencerAddress string `example:"0x44529f2c44d9113e0ba4e53cb6e84f425ec186cda27545827b5a72d5540bfdc" json:"sequencerAddress"`
}

type Transaction struct {
TransactionIndex uint `example:"0" json:"transactionIndex"`
TransactionIndex uint `example:"0" json:"transactionIndex"`
TransactionHash string `example:"0x794fae89c8c4b8f5f77a4996948d2547740f90e54bb4a5cc6119a7c70eca42c" json:"transactionHash"`
ContractAddress *string `example:"0x1cee8364383aea317eefc181dbd8732f1504fd4511aed58f32c369dd546da0d" json:"contractAddress"`
EntryPointSelector *string `example:"0x317eb442b72a9fae758d4fb26830ed0d9f31c8e7da4dbff4e8c59ea6a158e7f" json:"entryPointSelector"`
Calldata *[]string `json:"calldata"`
MaxFee *string `example:"0x0" json:"maxFee"`
Type string `example:"INVOKE" json:"type"`
MaxFee *string `example:"0x0" json:"maxFee"`
Type string `example:"INVOKE" json:"type"`
SenderAddress *string `json:"senderAddress"`
Version string `example:"0x0" json:"version"`
Version string `example:"0x0" json:"version"`
Signature *[]string `json:"signature"`
Nonce *uint64 `json:"nonce"`
ClassHash *string `json:"classHash"`
Expand Down

0 comments on commit 29a948c

Please sign in to comment.