-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
relay.go
97 lines (85 loc) · 2.76 KB
/
relay.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
package relay
import (
"context"
"fmt"
"regexp"
"github.com/smartcontractkit/chainlink-relay/pkg/loop"
"github.com/smartcontractkit/chainlink-relay/pkg/types"
)
type Network = string
type ChainID = string
var (
EVM Network = "evm"
Cosmos Network = "cosmos"
Solana Network = "solana"
StarkNet Network = "starknet"
SupportedRelays = map[Network]struct{}{
EVM: {},
Cosmos: {},
Solana: {},
StarkNet: {},
}
)
// ID uniquely identifies a relayer by network and chain id
type ID struct {
Network Network
ChainID ChainID
}
func (i *ID) Name() string {
return fmt.Sprintf("%s.%s", i.Network, i.ChainID)
}
func (i *ID) String() string {
return i.Name()
}
func NewID(n Network, c ChainID) ID {
return ID{Network: n, ChainID: c}
}
var idRegex = regexp.MustCompile(
fmt.Sprintf("^((%s)|(%s)|(%s)|(%s))\\.", EVM, Cosmos, Solana, StarkNet),
)
func (i *ID) UnmarshalString(s string) error {
idxs := idRegex.FindStringIndex(s)
if idxs == nil {
return fmt.Errorf("error unmarshaling Identifier. %q does not match expected pattern", s)
}
// ignore the `.` in the match by dropping last rune
network := s[idxs[0] : idxs[1]-1]
chainID := s[idxs[1]:]
newID := &ID{ChainID: ChainID(chainID)}
for n := range SupportedRelays {
if Network(network) == n {
newID.Network = n
break
}
}
if newID.Network == "" {
return fmt.Errorf("error unmarshaling identifier: did not find network in supported list %q", network)
}
i.ChainID = newID.ChainID
i.Network = newID.Network
return nil
}
// ServerAdapter extends [loop.RelayerAdapter] by overriding NewPluginProvider to dispatches calls according to `RelayArgs.ProviderType`.
// This should only be used to adapt relayers not running via GRPC in a LOOPP.
type ServerAdapter struct {
loop.RelayerAdapter
}
// NewServerAdapter returns a new ServerAdapter.
func NewServerAdapter(r types.Relayer, e loop.RelayerExt) *ServerAdapter { //nolint:staticcheck
return &ServerAdapter{RelayerAdapter: loop.RelayerAdapter{Relayer: r, RelayerExt: e}}
}
func (r *ServerAdapter) NewPluginProvider(ctx context.Context, rargs types.RelayArgs, pargs types.PluginArgs) (types.PluginProvider, error) {
switch types.OCR2PluginType(rargs.ProviderType) {
case types.Median:
return r.NewMedianProvider(ctx, rargs, pargs)
case types.Functions:
return r.NewFunctionsProvider(ctx, rargs, pargs)
case types.Mercury:
return r.NewMercuryProvider(ctx, rargs, pargs)
case types.DKG, types.OCR2VRF, types.OCR2Keeper, types.GenericPlugin:
return r.RelayerAdapter.NewPluginProvider(ctx, rargs, pargs)
case types.CCIPCommit, types.CCIPExecution:
return nil, fmt.Errorf("provider type not supported: %s", rargs.ProviderType)
}
return nil, fmt.Errorf("provider type not recognized: %s", rargs.ProviderType)
}