Skip to content

Commit df35f5f

Browse files
committed
clean up
1 parent 3d8a471 commit df35f5f

File tree

12 files changed

+98
-88
lines changed

12 files changed

+98
-88
lines changed

grogu/cmd/keys.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ func keysAddCmd(c *grogucontext.Context) *cobra.Command {
7373
fmt.Printf("Mnemonic: %s\n", mnemonic)
7474
}
7575

76-
if err != nil {
77-
return err
78-
}
7976
account, err := cmd.Flags().GetUint32(flagAccount)
8077
if err != nil {
8178
return err
@@ -99,9 +96,11 @@ func keysAddCmd(c *grogucontext.Context) *cobra.Command {
9996
return nil
10097
},
10198
}
99+
102100
cmd.Flags().Bool(flagRecover, false, "Provide seed phrase to recover existing key instead of creating")
103101
cmd.Flags().Uint32(flagAccount, 0, "Account number for HD derivation")
104102
cmd.Flags().Uint32(flagIndex, 0, "Address index number for HD derivation")
103+
105104
return cmd
106105
}
107106

@@ -138,6 +137,7 @@ func keysDeleteCmd(c *grogucontext.Context) *cobra.Command {
138137
return nil
139138
},
140139
}
140+
141141
return cmd
142142
}
143143

@@ -186,6 +186,7 @@ func keysListCmd(c *grogucontext.Context) *cobra.Command {
186186
return nil
187187
},
188188
}
189+
189190
cmd.Flags().BoolP(flagAddress, "a", false, "Output the address only")
190191
_ = viper.BindPFlag(flagAddress, cmd.Flags().Lookup(flagAddress))
191192

@@ -216,5 +217,6 @@ func keysShowCmd(c *grogucontext.Context) *cobra.Command {
216217
return nil
217218
},
218219
}
220+
219221
return cmd
220222
}

grogu/cmd/run.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
grogucontext "github.com/bandprotocol/chain/v2/grogu/context"
1818
"github.com/bandprotocol/chain/v2/grogu/feed"
1919
"github.com/bandprotocol/chain/v2/grogu/priceservice"
20+
"github.com/bandprotocol/chain/v2/pkg/logger"
2021
"github.com/bandprotocol/chain/v2/x/feeds/types"
2122
)
2223

@@ -31,8 +32,8 @@ const (
3132
flagDistributionPercentageRange = "distribution-range"
3233
)
3334

34-
func runImpl(c *grogucontext.Context, l *grogucontext.Logger) error {
35-
l.Info(":rocket: Starting WebSocket subscriber")
35+
func runImpl(c *grogucontext.Context) error {
36+
c.Logger.Info(":rocket: Starting WebSocket subscriber")
3637
err := c.Client.Start()
3738
if err != nil {
3839
return err
@@ -43,14 +44,14 @@ func runImpl(c *grogucontext.Context, l *grogucontext.Logger) error {
4344
c.FreeKeys <- i
4445
}
4546

46-
l.Info(":rocket: Starting Prices submitter")
47-
go feed.StartSubmitPrices(c, l)
47+
c.Logger.Info(":rocket: Starting Prices submitter")
48+
go feed.StartSubmitPrices(c)
4849

49-
l.Info(":rocket: Starting Prices querier")
50-
go feed.StartQuerySignalIDs(c, l)
50+
c.Logger.Info(":rocket: Starting Prices querier")
51+
go feed.StartQuerySignalIDs(c)
5152

52-
l.Info(":rocket: Starting Feed checker")
53-
feed.StartCheckFeeds(c, l)
53+
c.Logger.Info(":rocket: Starting Feed checker")
54+
feed.StartCheckFeeds(c)
5455

5556
return nil
5657
}
@@ -89,12 +90,12 @@ func RunCmd(c *grogucontext.Context) *cobra.Command {
8990
if err != nil {
9091
return err
9192
}
92-
l := grogucontext.NewLogger(allowLevel)
93+
c.Logger = logger.New(allowLevel)
9394
c.PriceService, err = priceservice.PriceServiceFromUrl(config.PriceService)
9495
if err != nil {
9596
return err
9697
}
97-
l.Info(":star: Creating HTTP client with node URI: %s", config.NodeURI)
98+
c.Logger.Info(":star: Creating HTTP client with node URI: %s", config.NodeURI)
9899
c.Client, err = httpclient.New(config.NodeURI, "/websocket")
99100
if err != nil {
100101
return err
@@ -121,9 +122,10 @@ func RunCmd(c *grogucontext.Context) *cobra.Command {
121122
c.InProgressSignalIDs = &sync.Map{}
122123
c.PendingSignalIDs = make(chan map[string]time.Time, 100)
123124
c.PendingPrices = make(chan []types.SubmitPrice, 30)
124-
return runImpl(c, l)
125+
return runImpl(c)
125126
},
126127
}
128+
127129
cmd.Flags().String(flags.FlagChainID, "", "chain ID of BandChain network")
128130
cmd.Flags().String(flags.FlagNode, "tcp://localhost:26657", "RPC url to BandChain node")
129131
cmd.Flags().String(flagValidator, "", "validator address")
@@ -148,5 +150,6 @@ func RunCmd(c *grogucontext.Context) *cobra.Command {
148150
_ = viper.BindPFlag(flagMaxTry, cmd.Flags().Lookup(flagMaxTry))
149151
_ = viper.BindPFlag(flagDistributionStartPercentage, cmd.Flags().Lookup(flagDistributionStartPercentage))
150152
_ = viper.BindPFlag(flagDistributionPercentageRange, cmd.Flags().Lookup(flagDistributionPercentageRange))
153+
151154
return cmd
152155
}

grogu/context/context.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
sdk "github.com/cosmos/cosmos-sdk/types"
1010

1111
"github.com/bandprotocol/chain/v2/grogu/priceservice"
12+
"github.com/bandprotocol/chain/v2/pkg/logger"
1213
"github.com/bandprotocol/chain/v2/x/feeds/types"
1314
)
1415

@@ -39,6 +40,7 @@ type Context struct {
3940
RPCPollInterval time.Duration
4041
Config Config
4142
Keyring keyring.Keyring
43+
Logger *logger.Logger
4244

4345
PendingSignalIDs chan map[string]time.Time
4446
PendingPrices chan []types.SubmitPrice

grogu/feed/check.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/bandprotocol/chain/v2/x/feeds/types"
1818
)
1919

20-
func checkFeeds(c *grogucontext.Context, l *grogucontext.Logger) {
20+
func checkFeeds(c *grogucontext.Context) {
2121
// Fetch parameters, supported feeds, validator prices, and prices
2222
params, feeds, validatorPrices, prices, err := fetchData(c)
2323
if err != nil {
@@ -66,7 +66,7 @@ func checkFeeds(c *grogucontext.Context, l *grogucontext.Logger) {
6666
}
6767

6868
if len(requestedSignalIDs) != 0 {
69-
l.Info("found signal ids to send: %v", maps.Keys(requestedSignalIDs))
69+
c.Logger.Info("found signal ids to send: %v", maps.Keys(requestedSignalIDs))
7070
c.PendingSignalIDs <- requestedSignalIDs
7171
}
7272
}
@@ -207,9 +207,9 @@ func deviationInThousandth(originalValue, newValue uint64) int64 {
207207
return int64(deviation)
208208
}
209209

210-
func StartCheckFeeds(c *grogucontext.Context, l *grogucontext.Logger) {
210+
func StartCheckFeeds(c *grogucontext.Context) {
211211
for {
212-
checkFeeds(c, l)
212+
checkFeeds(c)
213213
time.Sleep(time.Second)
214214
}
215215
}

grogu/feed/query.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import (
1313
"github.com/bandprotocol/chain/v2/x/feeds/types"
1414
)
1515

16-
func StartQuerySignalIDs(c *grogucontext.Context, l *grogucontext.Logger) {
16+
func StartQuerySignalIDs(c *grogucontext.Context) {
1717
for {
1818
// block for signal ids from channel PendingSignalIDs and retrieve them in batch
1919
signalIDsWithTimeLimit := BlockAndRetrieveBatchedPendingSignalIDs(c.PendingSignalIDs)
2020
// query prices for signal ids
21-
QuerySignalIDs(c, l, signalIDsWithTimeLimit)
21+
QuerySignalIDs(c, signalIDsWithTimeLimit)
2222
}
2323
}
2424

@@ -41,13 +41,13 @@ GetAllSignalIDs:
4141
return signalIDsWithTimeLimit
4242
}
4343

44-
func QuerySignalIDs(c *grogucontext.Context, l *grogucontext.Logger, signalIDsWithTimeLimit map[string]time.Time) {
44+
func QuerySignalIDs(c *grogucontext.Context, signalIDsWithTimeLimit map[string]time.Time) {
4545
signalIDs := maps.Keys(signalIDsWithTimeLimit)
4646

47-
l.Info("Try to get prices for signal ids: %+v", signalIDs)
47+
c.Logger.Info("Try to get prices for signal ids: %+v", signalIDs)
4848
prices, err := c.PriceService.Query(signalIDs)
4949
if err != nil {
50-
l.Error(":exploding_head: Failed to get prices from price-service with error: %s", c, err.Error())
50+
c.Logger.Error(":exploding_head: Failed to get prices from price-service with error: %s", err.Error())
5151
}
5252

5353
maxSafePrice := math.MaxUint64 / uint64(math.Pow10(9))
@@ -66,7 +66,11 @@ func QuerySignalIDs(c *grogucontext.Context, l *grogucontext.Logger, signalIDsWi
6666
case bothanproto.PriceStatus_PRICE_STATUS_AVAILABLE:
6767
price, err := strconv.ParseFloat(strings.TrimSpace(priceData.Price), 64)
6868
if err != nil || price > float64(maxSafePrice) || price < 0 {
69-
l.Error(":exploding_head: Failed to parse price from singal id:", c, priceData.SignalId, err)
69+
c.Logger.Error(
70+
":exploding_head: Failed to parse price from signal id: %s with err: %s",
71+
priceData.SignalId,
72+
err.Error(),
73+
)
7074
priceData.PriceStatus = bothanproto.PriceStatus_PRICE_STATUS_UNAVAILABLE
7175
priceData.Price = ""
7276
} else {
@@ -97,10 +101,10 @@ func QuerySignalIDs(c *grogucontext.Context, l *grogucontext.Logger, signalIDsWi
97101
}
98102

99103
if len(submitPrices) == 0 {
100-
l.Debug(":exploding_head: query signal got no prices with signal ids: %+v", signalIDs)
104+
c.Logger.Debug(":exploding_head: query signal got no prices with signal ids: %+v", signalIDs)
101105
return
102106
}
103-
l.Info("got prices for signal ids: %+v", maps.Keys(signalIDPriceMap))
107+
c.Logger.Info("got prices for signal ids: %+v", maps.Keys(signalIDPriceMap))
104108
c.PendingPrices <- submitPrices
105109
}
106110

grogu/feed/query_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
grogucontext "github.com/bandprotocol/chain/v2/grogu/context"
1616
"github.com/bandprotocol/chain/v2/grogu/feed"
17+
"github.com/bandprotocol/chain/v2/pkg/logger"
1718
"github.com/bandprotocol/chain/v2/x/feeds/types"
1819
)
1920

@@ -46,8 +47,9 @@ func (mps *MockPriceService) Query(signalIDs []string) ([]*bothanproto.PriceData
4647

4748
func TestQuerySignalIDs(t *testing.T) {
4849
// Create a mock context and logger for testing.
49-
mockContext := grogucontext.Context{}
50-
mockLogger := grogucontext.NewLogger(log.AllowAll())
50+
mockContext := grogucontext.Context{
51+
Logger: logger.New(log.AllowAll()),
52+
}
5153

5254
// Mock the pending signalIDs channel.
5355
mockContext.PendingSignalIDs = make(chan map[string]time.Time, 10)
@@ -73,7 +75,6 @@ func TestQuerySignalIDs(t *testing.T) {
7375
// Call the function being tested.
7476
feed.QuerySignalIDs(
7577
&mockContext,
76-
mockLogger,
7778
feed.BlockAndRetrieveBatchedPendingSignalIDs(mockContext.PendingSignalIDs),
7879
)
7980

@@ -113,7 +114,6 @@ func TestQuerySignalIDs(t *testing.T) {
113114
// Call the function being tested.
114115
feed.QuerySignalIDs(
115116
&mockContext,
116-
mockLogger,
117117
feed.BlockAndRetrieveBatchedPendingSignalIDs(mockContext.PendingSignalIDs),
118118
)
119119

@@ -137,7 +137,6 @@ func TestQuerySignalIDs(t *testing.T) {
137137
// Call the function being tested.
138138
feed.QuerySignalIDs(
139139
&mockContext,
140-
mockLogger,
141140
feed.BlockAndRetrieveBatchedPendingSignalIDs(mockContext.PendingSignalIDs),
142141
)
143142

grogu/feed/submit.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ import (
1919
"github.com/bandprotocol/chain/v2/x/feeds/types"
2020
)
2121

22-
func StartSubmitPrices(c *grogucontext.Context, l *grogucontext.Logger) {
22+
func StartSubmitPrices(c *grogucontext.Context) {
2323
for {
2424
// Return key and update pending metric when done with SubmitReport whether successfully or not.
2525
keyIndex := <-c.FreeKeys
26-
go SubmitPrices(c, l, keyIndex)
26+
go SubmitPrices(c, keyIndex)
2727
}
2828
}
2929

30-
func SubmitPrices(c *grogucontext.Context, l *grogucontext.Logger, keyIndex int64) {
30+
func SubmitPrices(c *grogucontext.Context, keyIndex int64) {
3131
// Return key and update pending metric when done with SubmitReport whether successfully or not.
3232
defer func() {
3333
c.FreeKeys <- keyIndex
@@ -70,19 +70,19 @@ GetAllPrices:
7070

7171
for sendAttempt := uint64(1); sendAttempt <= c.MaxTry; sendAttempt++ {
7272
var txHash string
73-
l.Info(":e-mail: Sending report transaction attempt: (%d/%d)", sendAttempt, c.MaxTry)
73+
c.Logger.Info(":e-mail: Sending report transaction attempt: (%d/%d)", sendAttempt, c.MaxTry)
7474
for broadcastTry := uint64(1); broadcastTry <= c.MaxTry; broadcastTry++ {
75-
l.Info(":writing_hand: Try to sign and broadcast report transaction(%d/%d)", broadcastTry, c.MaxTry)
75+
c.Logger.Info(":writing_hand: Try to sign and broadcast report transaction(%d/%d)", broadcastTry, c.MaxTry)
7676
res, err := signAndBroadcast(c, key, msgs, gasAdjustment)
7777
if err != nil {
7878
// Use info level because this error can happen and retry process can solve this error.
79-
l.Info(":warning: %s", err.Error())
79+
c.Logger.Info(":warning: %s", err.Error())
8080
time.Sleep(c.RPCPollInterval)
8181
continue
8282
}
8383
if res.Codespace == sdkerrors.RootCodespace && res.Code == sdkerrors.ErrOutOfGas.ABCICode() {
8484
gasAdjustment += 0.1
85-
l.Info(
85+
c.Logger.Info(
8686
":fuel_pump: Tx(%s) is out of gas and will be rebroadcasted with gas adjustment(%f)",
8787
txHash,
8888
gasAdjustment,
@@ -94,7 +94,7 @@ GetAllPrices:
9494
break
9595
}
9696
if txHash == "" {
97-
l.Error(":exploding_head: Cannot try to broadcast more than %d try", c, c.MaxTry)
97+
c.Logger.Error(":exploding_head: Cannot try to broadcast more than %d try", c.MaxTry)
9898
return
9999
}
100100
txFound := false
@@ -103,36 +103,35 @@ GetAllPrices:
103103
time.Sleep(c.RPCPollInterval)
104104
txRes, err := authtx.QueryTx(clientCtx, txHash)
105105
if err != nil {
106-
l.Debug(":warning: Failed to query tx with error: %s", err.Error())
106+
c.Logger.Debug(":warning: Failed to query tx with error: %s", err.Error())
107107
continue
108108
}
109109

110110
if txRes.Code == 0 {
111-
l.Info(":smiling_face_with_sunglasses: Successfully broadcast tx with hash: %s", txHash)
111+
c.Logger.Info(":smiling_face_with_sunglasses: Successfully broadcast tx with hash: %s", txHash)
112112
return
113113
}
114114
if txRes.Codespace == sdkerrors.RootCodespace &&
115115
txRes.Code == sdkerrors.ErrOutOfGas.ABCICode() {
116116
// Increase gas adjustment and try to broadcast again
117117
gasAdjustment += 0.1
118-
l.Info(":fuel_pump: Tx(%s) is out of gas and will be rebroadcasted with gas adjustment(%f)", txHash, gasAdjustment)
118+
c.Logger.Info(":fuel_pump: Tx(%s) is out of gas and will be rebroadcasted with gas adjustment(%f)", txHash, gasAdjustment)
119119
txFound = true
120120
break FindTx
121121
} else {
122-
l.Error(":exploding_head: Tx returned nonzero code %d with log %s, tx hash: %s", c, txRes.Code, txRes.RawLog, txRes.TxHash)
122+
c.Logger.Error(":exploding_head: Tx returned nonzero code %d with log %s, tx hash: %s", txRes.Code, txRes.RawLog, txRes.TxHash)
123123
return
124124
}
125125
}
126126
if !txFound {
127-
l.Error(
127+
c.Logger.Error(
128128
":question_mark: Cannot get transaction response from hash: %s transaction might be included in the next few blocks or check your node's health.",
129-
c,
130129
txHash,
131130
)
132131
return
133132
}
134133
}
135-
l.Error(":anxious_face_with_sweat: Cannot send price with adjusted gas: %d", c, gasAdjustment)
134+
c.Logger.Error(":anxious_face_with_sweat: Cannot send price with adjusted gas: %f", gasAdjustment)
136135
}
137136

138137
func signAndBroadcast(

0 commit comments

Comments
 (0)