-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathserver.go
144 lines (129 loc) · 4.75 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Package relapi provides RESTful API services for the RFQ relayer
package relapi
import (
"context"
"fmt"
"github.com/ipfs/go-log"
"github.com/synapsecns/sanguine/core/ginhelper"
"github.com/synapsecns/sanguine/ethergo/submitter"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/gin-gonic/gin"
"github.com/synapsecns/sanguine/core/metrics"
baseServer "github.com/synapsecns/sanguine/core/server"
"github.com/synapsecns/sanguine/ethergo/listener"
omniClient "github.com/synapsecns/sanguine/services/omnirpc/client"
"github.com/synapsecns/sanguine/services/rfq/contracts/fastbridge"
"github.com/synapsecns/sanguine/services/rfq/relayer/chain"
"github.com/synapsecns/sanguine/services/rfq/relayer/relconfig"
"github.com/synapsecns/sanguine/services/rfq/relayer/reldb"
)
// RelayerAPIServer is a struct that holds the configuration, database connection, gin engine, RPC client, metrics handler, and fast bridge contracts.
// It is used to initialize and run the API server.
type RelayerAPIServer struct {
cfg relconfig.Config
db reldb.Service
engine *gin.Engine
handler metrics.Handler
chains map[uint32]*chain.Chain
submitter submitter.TransactionSubmitter
}
// NewRelayerAPI holds the configuration, database connection, gin engine, RPC client, metrics handler, and fast bridge contracts.
// It is used to initialize and run the API server.
//
//nolint:cyclop
func NewRelayerAPI(
ctx context.Context,
cfg relconfig.Config,
handler metrics.Handler,
omniRPCClient omniClient.RPCClient,
store reldb.Service,
submitter submitter.TransactionSubmitter,
) (*RelayerAPIServer, error) {
if ctx == nil {
return nil, fmt.Errorf("context is nil")
}
if handler == nil {
return nil, fmt.Errorf("handler is nil")
}
if omniRPCClient == nil {
return nil, fmt.Errorf("omniRPCClient is nil")
}
if store == nil {
return nil, fmt.Errorf("store is nil")
}
chains := make(map[uint32]*chain.Chain)
for chainID := range cfg.Chains {
chainClient, err := omniRPCClient.GetChainClient(ctx, chainID)
if err != nil {
return nil, fmt.Errorf("could not create omnirpc client: %w", err)
}
rfqAddr, err := cfg.GetRFQAddress(chainID)
if err != nil {
return nil, fmt.Errorf("could not get rfq address: %w", err)
}
contract, err := fastbridge.NewFastBridgeRef(rfqAddr, chainClient)
if err != nil {
return nil, fmt.Errorf("could not create fast bridge contract: %w", err)
}
startBlock, err := contract.DeployBlock(&bind.CallOpts{Context: ctx})
if err != nil {
return nil, fmt.Errorf("could not get deploy block: %w", err)
}
chainListener, err := listener.NewChainListener(chainClient, store, rfqAddr, uint64(startBlock.Int64()), handler)
if err != nil {
return nil, fmt.Errorf("could not get chain listener: %w", err)
}
chains[uint32(chainID)], err = chain.NewChain(ctx, cfg, chainClient, chainListener, submitter)
if err != nil {
return nil, fmt.Errorf("could not create chain: %w", err)
}
}
return &RelayerAPIServer{
cfg: cfg,
db: store,
handler: handler,
chains: chains,
submitter: submitter,
}, nil
}
const (
getHealthRoute = "/health"
// TODO: replace with non-status specific endpoints.
getQuoteStatusByTxHashRoute = "/status"
getQuoteStatusByTxIDRoute = "/status/by_tx_id"
getRetryRoute = "/retry"
postWithdrawRoute = "/withdraw"
getTxHashByNonceRoute = "/tx_hash/by_nonce"
getRequestByTxID = "/request/by_tx_id"
getRequestByTxHash = "/request/by_tx_hash"
)
var logger = log.Logger("relayer-api")
// Run runs the rest api server.
func (r *RelayerAPIServer) Run(ctx context.Context) error {
engine := ginhelper.New(logger)
// default tracing middleware
engine.Use(r.handler.Gin()...)
h := NewHandler(r.handler, r.db, r.chains, r.cfg, r.submitter)
// Assign GET routes
engine.GET(getHealthRoute, h.GetHealth)
// TODO: replace with non-status specific endpoints
engine.GET(getQuoteStatusByTxHashRoute, h.GetQuoteRequestStatusByTxHash)
// TODO: replace with non-status specific endpoints
engine.GET(getQuoteStatusByTxIDRoute, h.GetQuoteRequestStatusByTxID)
engine.GET(getRetryRoute, h.GetTxRetry)
engine.GET(getRequestByTxID, h.GetQuoteRequestByTxID)
engine.GET(getRequestByTxHash, h.GetQuoteRequestByTxHash)
engine.GET(metrics.MetricsPathDefault, gin.WrapH(r.handler.Handler()))
if r.cfg.EnableAPIWithdrawals {
engine.POST(postWithdrawRoute, h.Withdraw)
engine.GET(getTxHashByNonceRoute, h.GetTxHashByNonce)
}
r.engine = engine
connection := baseServer.Server{}
fmt.Printf("starting api at http://localhost:%s\n", r.cfg.RelayerAPIPort)
err := connection.ListenAndServe(ctx, fmt.Sprintf(":%s", r.cfg.RelayerAPIPort), r.engine)
if err != nil {
return fmt.Errorf("could not start relayer api server: %w", err)
}
return nil
}