-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
101 lines (86 loc) · 2.84 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package indexer
import (
"context"
"net/http"
"time"
"entgo.io/contrib/entgql"
"entgo.io/ent/dialect/sql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/debug"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/cartridge-gg/starknet-indexer/ent"
"github.com/cartridge-gg/starknet-indexer/ent/block"
"github.com/cartridge-gg/starknet-indexer/processor"
"github.com/dontpanicdao/caigo/jsonrpc"
"github.com/dontpanicdao/caigo/types"
"github.com/rs/zerolog/log"
)
type BalanceUpdate struct {
// https://github.com/OpenZeppelin/cairo-contracts/blob/main/src/openzeppelin/token/erc20/library.cairo#L20
// https://github.com/OpenZeppelin/cairo-contracts/blob/main/src/openzeppelin/token/erc721/library.cairo#L30
Event *types.Event
ContractAddress string
ContractType string
}
func New(addr string, drv *sql.Driver, provider *jsonrpc.Client, config Config, opts ...IndexerOption) {
iopts := indexerOptions{
debug: false,
client: http.DefaultClient,
}
for _, opt := range opts {
opt.apply(&iopts)
}
client := ent.NewClient(ent.Driver(drv))
if err := client.Schema.Create(
context.Background(),
); err != nil {
log.Fatal().Err(err).Msg("Running schema migration")
}
srv := handler.NewDefaultServer(NewSchema(client))
srv.Use(entgql.Transactioner{TxOpener: client})
if iopts.debug {
srv.Use(&debug.Tracer{})
}
http.Handle("/",
playground.Handler("Starknet Indexer", "/query"),
)
http.Handle("/query", srv)
ctx := context.Background()
var n uint64
head, err := client.Block.Query().Order(ent.Desc(block.FieldBlockNumber)).First(ctx)
if err != nil && !ent.IsNotFound(err) {
log.Fatal().Err(err).Msg("Getting head block")
} else if head != nil {
n = head.BlockNumber + 1
}
e, err := NewEngine(ctx, client, provider, Config{
Head: n,
Interval: 1 * time.Second,
})
if err != nil {
log.Fatal().Err(err).Msg("Initializing engine.")
}
if err := e.Register(ctx, new(processor.StoreBlock)); err != nil {
log.Fatal().Err(err).Msg("Registering processor.")
}
if err := e.Register(ctx, new(processor.StoreTransaction)); err != nil {
log.Fatal().Err(err).Msg("Registering processor.")
}
if err := e.Register(ctx, new(processor.StoreEvent)); err != nil {
log.Fatal().Err(err).Msg("Registering processor.")
}
if err := e.Register(ctx, new(processor.StoreContract)); err != nil {
log.Fatal().Err(err).Msg("Registering processor.")
}
if err := e.Register(ctx, new(processor.ERC20Contract)); err != nil {
log.Fatal().Err(err).Msg("Registering processor.")
}
if err := e.Register(ctx, new(processor.ERC721Contract)); err != nil {
log.Fatal().Err(err).Msg("Registering processor.")
}
go e.Start(ctx)
log.Info().Str("address", addr).Msg("listening on")
if err := http.ListenAndServe(addr, nil); err != nil {
log.Err(err).Msg("http server terminated")
}
}