-
Notifications
You must be signed in to change notification settings - Fork 2
/
multichainsync.go
96 lines (83 loc) · 2.38 KB
/
multichainsync.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
package multichain_transaction_syncs
import (
"context"
"sync/atomic"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/ethereum/go-ethereum/log"
"github.com/dapplink-labs/multichain-sync-account/config"
"github.com/dapplink-labs/multichain-sync-account/database"
"github.com/dapplink-labs/multichain-sync-account/rpcclient"
"github.com/dapplink-labs/multichain-sync-account/rpcclient/chain-account/account"
"github.com/dapplink-labs/multichain-sync-account/worker"
)
type MultiChainSync struct {
Synchronizer *worker.BaseSynchronizer
Deposit *worker.Deposit
Withdraw *worker.Withdraw
Internal *worker.Internal
shutdown context.CancelCauseFunc
stopped atomic.Bool
}
func NewMultiChainSync(ctx context.Context, cfg *config.Config, shutdown context.CancelCauseFunc) (*MultiChainSync, error) {
db, err := database.NewDB(ctx, cfg.MasterDB)
if err != nil {
log.Error("init database fail", err)
return nil, err
}
log.Info("New deposit", "ChainAccountRpc", cfg.ChainAccountRpc)
conn, err := grpc.NewClient(cfg.ChainAccountRpc, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Error("Connect to da retriever fail", "err", err)
return nil, err
}
client := account.NewWalletAccountServiceClient(conn)
accountClient, err := rpcclient.NewWalletChainAccountClient(context.Background(), client, "Ethereum")
if err != nil {
log.Error("new wallet account client fail", "err", err)
return nil, err
}
deposit, _ := worker.NewDeposit(cfg, db, accountClient, shutdown)
withdraw, _ := worker.NewWithdraw(cfg, db, accountClient, shutdown)
internal, _ := worker.NewInternal(cfg, db, accountClient, shutdown)
out := &MultiChainSync{
Deposit: deposit,
Withdraw: withdraw,
Internal: internal,
shutdown: shutdown,
}
return out, nil
}
func (mcs *MultiChainSync) Start(ctx context.Context) error {
err := mcs.Deposit.Start()
if err != nil {
return err
}
err = mcs.Withdraw.Start()
if err != nil {
return err
}
err = mcs.Internal.Start()
if err != nil {
return err
}
return nil
}
func (mcs *MultiChainSync) Stop(ctx context.Context) error {
err := mcs.Deposit.Close()
if err != nil {
return err
}
err = mcs.Withdraw.Close()
if err != nil {
return err
}
err = mcs.Internal.Close()
if err != nil {
return err
}
return nil
}
func (mcs *MultiChainSync) Stopped() bool {
return mcs.stopped.Load()
}