-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
chain_set.go
79 lines (67 loc) · 2.48 KB
/
chain_set.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
package starknet
import (
"context"
"github.com/pkg/errors"
"go.uber.org/multierr"
"github.com/smartcontractkit/sqlx"
starkchain "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/chain"
"github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/db"
"github.com/smartcontractkit/chainlink/core/chains"
"github.com/smartcontractkit/chainlink/core/chains/starknet/types"
coreconfig "github.com/smartcontractkit/chainlink/core/config"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/services/keystore"
)
type ChainSetOpts struct {
Config coreconfig.GeneralConfig
Logger logger.Logger
DB *sqlx.DB
KeyStore keystore.StarkNet
ORM types.ORM
}
func (o *ChainSetOpts) Validate() (err error) {
required := func(s string) error {
return errors.Errorf("%s is required", s)
}
if o.Config == nil {
err = multierr.Append(err, required("Config"))
}
if o.Logger == nil {
err = multierr.Append(err, required("Logger'"))
}
if o.DB == nil {
err = multierr.Append(err, required("DB"))
}
if o.KeyStore == nil {
err = multierr.Append(err, required("KeyStore"))
}
if o.ORM == nil {
err = multierr.Append(err, required("ORM"))
}
return
}
func (o *ChainSetOpts) ORMAndLogger() (chains.ORM[string, *db.ChainCfg, db.Node], logger.Logger) {
return o.ORM, o.Logger
}
func (o *ChainSetOpts) NewChain(dbchain types.DBChain) (starkchain.Chain, error) {
if !dbchain.Enabled {
return nil, errors.Errorf("cannot create new chain with ID %s, the chain is disabled", dbchain.ID)
}
return NewChain(o.DB, o.KeyStore, dbchain, o.ORM, o.Logger)
}
type ChainSet interface {
starkchain.ChainSet
Add(context.Context, string, *db.ChainCfg) (types.DBChain, error)
Remove(string) error
Configure(ctx context.Context, id string, enabled bool, config *db.ChainCfg) (types.DBChain, error)
Show(id string) (types.DBChain, error)
Index(offset, limit int) ([]types.DBChain, int, error)
GetNodes(ctx context.Context, offset, limit int) (nodes []db.Node, count int, err error)
GetNodesForChain(ctx context.Context, chainID string, offset, limit int) (nodes []db.Node, count int, err error)
CreateNode(ctx context.Context, data db.Node) (db.Node, error)
DeleteNode(ctx context.Context, id int32) error
}
// NewChainSet returns a new chain set for opts.
func NewChainSet(opts ChainSetOpts) (ChainSet, error) {
return chains.NewChainSet[string, *db.ChainCfg, db.Node, starkchain.Chain](&opts, func(s string) string { return s })
}