/
flags.go
234 lines (211 loc) · 6.99 KB
/
flags.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package flags
import (
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/spf13/cast"
"github.com/spf13/cobra"
)
// List of CLI flags for Server and Client.
const (
// Flag names
FlagUnixSocketAddress = "unix-socket-address"
FlagPriceDaemonEnabled = "price-daemon-enabled"
FlagPriceDaemonLoopDelayMs = "price-daemon-loop-delay-ms"
FlagBridgeDaemonEnabled = "bridge-daemon-enabled"
FlagBridgeDaemonLoopDelayMs = "bridge-daemon-loop-delay-ms"
FlagBridgeDaemonEthRpcEndpoint = "bridge-daemon-eth-rpc-endpoint"
FlagLiquidationDaemonEnabled = "liquidation-daemon-enabled"
FlagLiquidationDaemonLoopDelayMs = "liquidation-daemon-loop-delay-ms"
FlagLiquidationDaemonSubaccountPageLimit = "liquidation-daemon-subaccount-page-limit"
FlagLiquidationDaemonRequestChunkSize = "liquidation-daemon-request-chunk-size"
)
// Shared flags contains configuration flags shared by all daemons.
type SharedFlags struct {
// SocketAddress is the location of the unix socket to communicate with the daemon gRPC service.
SocketAddress string
}
// BridgeFlags contains configuration flags for the Bridge Daemon.
type BridgeFlags struct {
// Enabled toggles the bridge daemon on or off.
Enabled bool
// LoopDelayMs configures the update frequency of the bridge daemon.
LoopDelayMs uint32
// EthRpcEndpoint is the endpoint for the Ethereum node where bridge data is queried.
EthRpcEndpoint string
}
// LiquidationFlags contains configuration flags for the Liquidation Daemon.
type LiquidationFlags struct {
// Enabled toggles the liquidation daemon on or off.
Enabled bool
// LoopDelayMs configures the update frequency of the liquidation daemon.
LoopDelayMs uint32
// SubaccountPageLimit configures the pagination limit for fetching subaccounts.
SubaccountPageLimit uint64
RequestChunkSize uint64
}
// PriceFlags contains configuration flags for the Price Daemon.
type PriceFlags struct {
// Enabled toggles the price daemon on or off.
Enabled bool
// LoopDelayMs configures the update frequency of the price daemon.
LoopDelayMs uint32
}
// DaemonFlags contains the collected configuration flags for all daemons.
type DaemonFlags struct {
Shared SharedFlags
Bridge BridgeFlags
Liquidation LiquidationFlags
Price PriceFlags
}
var defaultDaemonFlags *DaemonFlags
// GetDefaultDaemonFlags returns the default values for the Daemon Flags using a singleton pattern.
func GetDefaultDaemonFlags() DaemonFlags {
if defaultDaemonFlags == nil {
defaultDaemonFlags = &DaemonFlags{
Shared: SharedFlags{
SocketAddress: "/tmp/daemons.sock",
},
Bridge: BridgeFlags{
Enabled: true,
LoopDelayMs: 30_000,
EthRpcEndpoint: "https://eth-sepolia.g.alchemy.com/v2/demo",
},
Liquidation: LiquidationFlags{
Enabled: true,
LoopDelayMs: 1_600,
SubaccountPageLimit: 1_000,
RequestChunkSize: 50,
},
Price: PriceFlags{
Enabled: true,
LoopDelayMs: 3_000,
},
}
}
return *defaultDaemonFlags
}
// AddDaemonFlagsToCmd adds the required flags to instantiate a server and client for
// price updates. These flags should be applied to the `start` command V4 Cosmos application.
// E.g. `dydxprotocold start --price-daemon-enabled=true --unix-socket-address $(unix_socket_address)`
func AddDaemonFlagsToCmd(
cmd *cobra.Command,
) {
//
df := GetDefaultDaemonFlags()
// Shared Flags.
cmd.Flags().String(
FlagUnixSocketAddress,
df.Shared.SocketAddress,
"Socket address for the price daemon to send updates to, if not set "+
"will establish default location to ingest price updates from",
)
// Bridge Daemon.
cmd.Flags().Bool(
FlagBridgeDaemonEnabled,
df.Bridge.Enabled,
"Enable Bridge Daemon. Set to false for non-validator nodes.",
)
cmd.Flags().Uint32(
FlagBridgeDaemonLoopDelayMs,
df.Bridge.LoopDelayMs,
"Delay in milliseconds between running the Bridge Daemon task loop.",
)
cmd.Flags().String(
FlagBridgeDaemonEthRpcEndpoint,
df.Bridge.EthRpcEndpoint,
"Ethereum Node Rpc Endpoint",
)
// Liquidation Daemon.
cmd.Flags().Bool(
FlagLiquidationDaemonEnabled,
df.Liquidation.Enabled,
"Enable Liquidation Daemon. Set to false for non-validator nodes.",
)
cmd.Flags().Uint32(
FlagLiquidationDaemonLoopDelayMs,
df.Liquidation.LoopDelayMs,
"Delay in milliseconds between running the Liquidation Daemon task loop.",
)
cmd.Flags().Uint64(
FlagLiquidationDaemonSubaccountPageLimit,
df.Liquidation.SubaccountPageLimit,
"Limit on the number of subaccounts to fetch per query in the Liquidation Daemon task loop.",
)
cmd.Flags().Uint64(
FlagLiquidationDaemonRequestChunkSize,
df.Liquidation.RequestChunkSize,
"Limit on the number of subaccounts per collateralization check in the Liquidation Daemon task loop.",
)
// Price Daemon.
cmd.Flags().Bool(
FlagPriceDaemonEnabled,
df.Price.Enabled,
"Enable Price Daemon. Set to false for non-validator nodes.",
)
cmd.Flags().Uint32(
FlagPriceDaemonLoopDelayMs,
df.Price.LoopDelayMs,
"Delay in milliseconds between sending price updates to the application.",
)
}
// GetDaemonFlagValuesFromOptions gets all daemon flag values from the `AppOptions` struct.
func GetDaemonFlagValuesFromOptions(
appOpts servertypes.AppOptions,
) DaemonFlags {
// Default value
result := GetDefaultDaemonFlags()
// Shared Flags
if option := appOpts.Get(FlagUnixSocketAddress); option != nil {
if v, err := cast.ToStringE(option); err == nil {
result.Shared.SocketAddress = v
}
}
// Bridge Daemon.
if option := appOpts.Get(FlagBridgeDaemonEnabled); option != nil {
if v, err := cast.ToBoolE(option); err == nil {
result.Bridge.Enabled = v
}
}
if option := appOpts.Get(FlagBridgeDaemonLoopDelayMs); option != nil {
if v, err := cast.ToUint32E(option); err == nil {
result.Bridge.LoopDelayMs = v
}
}
if option := appOpts.Get(FlagBridgeDaemonEthRpcEndpoint); option != nil {
if v, err := cast.ToStringE(option); err == nil {
result.Bridge.EthRpcEndpoint = v
}
}
// Liquidation Daemon.
if option := appOpts.Get(FlagLiquidationDaemonEnabled); option != nil {
if v, err := cast.ToBoolE(option); err == nil {
result.Liquidation.Enabled = v
}
}
if option := appOpts.Get(FlagLiquidationDaemonLoopDelayMs); option != nil {
if v, err := cast.ToUint32E(option); err == nil {
result.Liquidation.LoopDelayMs = v
}
}
if option := appOpts.Get(FlagLiquidationDaemonSubaccountPageLimit); option != nil {
if v, err := cast.ToUint64E(option); err == nil {
result.Liquidation.SubaccountPageLimit = v
}
}
if option := appOpts.Get(FlagLiquidationDaemonRequestChunkSize); option != nil {
if v, err := cast.ToUint64E(option); err == nil {
result.Liquidation.RequestChunkSize = v
}
}
// Price Daemon.
if option := appOpts.Get(FlagPriceDaemonEnabled); option != nil {
if v, err := cast.ToBoolE(option); err == nil {
result.Price.Enabled = v
}
}
if option := appOpts.Get(FlagPriceDaemonLoopDelayMs); option != nil {
if v, err := cast.ToUint32E(option); err == nil {
result.Price.LoopDelayMs = v
}
}
return result
}