Skip to content

Commit 86e28d5

Browse files
committed
use constructor function for struct construction
1 parent 714a7de commit 86e28d5

File tree

14 files changed

+194
-89
lines changed

14 files changed

+194
-89
lines changed

x/feeds/client/cli/tx.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,16 @@ $ %s tx feeds signal BTC,1000000 --from mykey
8080
return err
8181
}
8282
signals = append(
83-
signals, types.Signal{
84-
ID: idAndPower[0],
85-
Power: power,
86-
},
83+
signals, types.NewSignal(
84+
idAndPower[0],
85+
power,
86+
),
8787
)
8888
}
8989

90-
msg := types.MsgSubmitSignals{
91-
Delegator: delegator.String(),
92-
Signals: signals,
93-
}
90+
msg := types.NewMsgSubmitSignals(delegator.String(), signals)
9491

95-
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
92+
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
9693
},
9794
}
9895
flags.AddTxFlagsToCmd(cmd)
@@ -174,17 +171,11 @@ $ %s tx feeds update-reference-source-config <YOUR_IPFS_HASH> 1.0.0 --from mykey
174171
}
175172

176173
admin := clientCtx.GetFromAddress()
177-
referenceSourceConfig := types.ReferenceSourceConfig{
178-
IPFSHash: args[0],
179-
Version: args[1],
180-
}
174+
referenceSourceConfig := types.NewReferenceSourceConfig(args[0], args[1])
181175

182-
msg := types.MsgUpdateReferenceSourceConfig{
183-
Admin: admin.String(),
184-
ReferenceSourceConfig: referenceSourceConfig,
185-
}
176+
msg := types.NewMsgUpdateReferenceSourceConfig(admin.String(), referenceSourceConfig)
186177

187-
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
178+
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
188179
},
189180
}
190181
flags.AddTxFlagsToCmd(cmd)

x/feeds/keeper/genesis.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,5 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
2727

2828
// ExportGenesis returns the module's exported genesis
2929
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
30-
return &types.GenesisState{
31-
Params: k.GetParams(ctx),
32-
DelegatorSignals: k.GetAllDelegatorSignals(ctx),
33-
ReferenceSourceConfig: k.GetReferenceSourceConfig(ctx),
34-
}
30+
return types.NewGenesisState(k.GetParams(ctx), k.GetAllDelegatorSignals(ctx), k.GetReferenceSourceConfig(ctx))
3531
}

x/feeds/keeper/grpc_query.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,20 +236,16 @@ func (q queryServer) CurrentFeeds(
236236
)
237237
feedWithDeviations = append(
238238
feedWithDeviations,
239-
types.FeedWithDeviation{
240-
SignalID: feed.SignalID,
241-
Power: feed.Power,
242-
Interval: feed.Interval,
243-
DeviationBasisPoint: deviation,
244-
})
239+
types.NewFeedWithDeviation(feed.SignalID, feed.Power, feed.Interval, deviation),
240+
)
245241
}
246242

247243
return &types.QueryCurrentFeedsResponse{
248-
CurrentFeeds: types.CurrentFeedWithDeviations{
249-
Feeds: feedWithDeviations,
250-
LastUpdateTimestamp: currentFeeds.LastUpdateTimestamp,
251-
LastUpdateBlock: currentFeeds.LastUpdateBlock,
252-
},
244+
CurrentFeeds: types.NewCurrentFeedWithDeviations(
245+
feedWithDeviations,
246+
currentFeeds.LastUpdateTimestamp,
247+
currentFeeds.LastUpdateBlock,
248+
),
253249
}, nil
254250
}
255251

x/feeds/keeper/keeper_feed.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ func (k Keeper) GetCurrentFeeds(ctx sdk.Context) (sp types.CurrentFeeds) {
2020

2121
// SetCurrentFeeds sets new supported feeds to the store.
2222
func (k Keeper) SetCurrentFeeds(ctx sdk.Context, feeds []types.Feed) {
23-
cf := types.CurrentFeeds{
24-
Feeds: feeds,
25-
LastUpdateTimestamp: ctx.BlockTime().Unix(),
26-
LastUpdateBlock: ctx.BlockHeight(),
27-
}
23+
cf := types.NewCurrentFeeds(feeds, ctx.BlockTime().Unix(), ctx.BlockHeight())
2824

2925
ctx.KVStore(k.storeKey).Set(types.CurrentFeedsStoreKey, k.cdc.MustMarshal(&cf))
3026
emitEventUpdateCurrentFeeds(ctx, cf)
@@ -43,12 +39,14 @@ func (k Keeper) CalculateNewCurrentFeeds(ctx sdk.Context) []types.Feed {
4339
params.MaxInterval,
4440
)
4541
if interval > 0 {
46-
feed := types.Feed{
47-
SignalID: signalTotalPower.ID,
48-
Interval: interval,
49-
Power: signalTotalPower.Power,
50-
}
51-
feeds = append(feeds, feed)
42+
feeds = append(
43+
feeds,
44+
types.NewFeed(
45+
signalTotalPower.ID,
46+
signalTotalPower.Power,
47+
interval,
48+
),
49+
)
5250
}
5351
}
5452

x/feeds/keeper/keeper_price.go

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,7 @@ func (k Keeper) CalculatePrices(ctx sdk.Context) {
8585
return false
8686
}
8787
// collect validator information
88-
validatorInfo := types.ValidatorInfo{
89-
Index: idx,
90-
Address: val.GetOperator(),
91-
Power: val.GetTokens().Uint64(),
92-
Status: status,
93-
}
88+
validatorInfo := types.NewValidatorInfo(idx, val.GetOperator(), val.GetTokens().Uint64(), status)
9489
validatorsByPower = append(validatorsByPower, validatorInfo)
9590
return false
9691
})
@@ -138,13 +133,13 @@ func (k Keeper) CalculatePrices(ctx sdk.Context) {
138133
havePrice := checkHavePrice(feed, valPrice, ctx.BlockTime())
139134
if havePrice {
140135
priceFeedInfos = append(
141-
priceFeedInfos, types.PriceFeedInfo{
142-
PriceStatus: valPrice.PriceStatus,
143-
Price: valPrice.Price,
144-
Power: valInfo.Power,
145-
Timestamp: valPrice.Timestamp,
146-
Index: valInfo.Index,
147-
},
136+
priceFeedInfos, types.NewPriceFeedInfo(
137+
valPrice.PriceStatus,
138+
valInfo.Power,
139+
valPrice.Price,
140+
valPrice.Timestamp,
141+
valInfo.Index,
142+
),
148143
)
149144
}
150145
}
@@ -192,22 +187,22 @@ func (k Keeper) CalculatePrice(
192187

193188
// If more than half of the total have unsupported price status, it returns an unsupported price status.
194189
if unsupportedPower > totalPower/2 {
195-
return types.Price{
196-
PriceStatus: types.PriceStatusUnsupported,
197-
SignalID: feed.SignalID,
198-
Price: 0,
199-
Timestamp: ctx.BlockTime().Unix(),
200-
}, nil
190+
return types.NewPrice(
191+
types.PriceStatusUnsupported,
192+
feed.SignalID,
193+
0,
194+
ctx.BlockTime().Unix(),
195+
), nil
201196
}
202197

203198
// If less than half of total have available price status, it returns an unavailable price status.
204199
if availablePower < totalPower/2 {
205-
return types.Price{
206-
PriceStatus: types.PriceStatusUnavailable,
207-
SignalID: feed.SignalID,
208-
Price: 0,
209-
Timestamp: ctx.BlockTime().Unix(),
210-
}, nil
200+
return types.NewPrice(
201+
types.PriceStatusUnavailable,
202+
feed.SignalID,
203+
0,
204+
ctx.BlockTime().Unix(),
205+
), nil
211206
}
212207

213208
price, err := types.CalculateMedianPriceFeedInfo(
@@ -217,12 +212,12 @@ func (k Keeper) CalculatePrice(
217212
return types.Price{}, err
218213
}
219214

220-
return types.Price{
221-
PriceStatus: types.PriceStatusAvailable,
222-
SignalID: feed.SignalID,
223-
Price: price,
224-
Timestamp: ctx.BlockTime().Unix(),
225-
}, nil
215+
return types.NewPrice(
216+
types.PriceStatusAvailable,
217+
feed.SignalID,
218+
price,
219+
ctx.BlockTime().Unix(),
220+
), nil
226221
}
227222

228223
// checkMissReport checks if a validator has missed a report based on the given parameters.
@@ -296,10 +291,10 @@ func (k Keeper) SetValidatorPriceList(
296291
valAddress sdk.ValAddress,
297292
valPrices []types.ValidatorPrice,
298293
) error {
299-
valPricesList := types.ValidatorPriceList{
300-
Validator: valAddress.String(),
301-
ValidatorPrices: valPrices,
302-
}
294+
valPricesList := types.NewValidatorPriceList(
295+
valAddress,
296+
valPrices,
297+
)
303298

304299
ctx.KVStore(k.storeKey).Set(types.ValidatorPriceListStoreKey(valAddress), k.cdc.MustMarshal(&valPricesList))
305300

x/feeds/keeper/keeper_signal.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ func (k Keeper) CalculateNewSignalTotalPowers(ctx sdk.Context) []types.Signal {
160160

161161
signalTotalPowers := []types.Signal{}
162162
for _, signalID := range keys {
163-
signalTotalPowers = append(signalTotalPowers, types.Signal{
164-
ID: signalID,
165-
Power: signalIDToPower[signalID],
166-
})
163+
signalTotalPowers = append(signalTotalPowers, types.NewSignal(
164+
signalID,
165+
signalIDToPower[signalID],
166+
))
167167
}
168168

169169
return signalTotalPowers
@@ -200,7 +200,7 @@ func (k Keeper) RegisterNewSignals(
200200
signalIDToPowerDiff[prevSignal.ID] -= prevSignal.Power
201201
}
202202

203-
k.SetDelegatorSignals(ctx, types.DelegatorSignals{Delegator: delegator.String(), Signals: signals})
203+
k.SetDelegatorSignals(ctx, types.NewDelegatorSignals(delegator.String(), signals))
204204

205205
for _, signal := range signals {
206206
signalIDToPowerDiff[signal.ID] += signal.Power

x/feeds/keeper/msg_server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ func (ms msgServer) SubmitSignals(
6969
signalTotalPower, err := ms.GetSignalTotalPower(ctx, signalID)
7070
if err != nil {
7171
// initialize a new signal with zero power if the signal ID does not exist
72-
signalTotalPower = types.Signal{
73-
ID: signalID,
74-
Power: 0,
75-
}
72+
signalTotalPower = types.NewSignal(
73+
signalID,
74+
0,
75+
)
7676
}
7777

7878
// update the total power of the signal by adding the power difference

x/feeds/types/delegator_signals.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import (
55
sdk "github.com/cosmos/cosmos-sdk/types"
66
)
77

8+
// NewDelegatorSignals creates a new DelegatorSignals instance.
9+
func NewDelegatorSignals(delegator string, signals []Signal) DelegatorSignals {
10+
return DelegatorSignals{
11+
Delegator: delegator,
12+
Signals: signals,
13+
}
14+
}
15+
816
// Validate validates the delegator signals
917
func (ds *DelegatorSignals) Validate(maxSignalIDCharacters uint64) error {
1018
if _, err := sdk.AccAddressFromBech32(ds.Delegator); err != nil {

x/feeds/types/feed.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,59 @@
11
package types
22

3+
// NewFeed creates a new feed instance
4+
func NewFeed(
5+
signalID string,
6+
power int64,
7+
interval int64,
8+
) Feed {
9+
return Feed{
10+
SignalID: signalID,
11+
Power: power,
12+
Interval: interval,
13+
}
14+
}
15+
16+
// NewFeedWithDeviation creates a new feed instance with deviation
17+
func NewFeedWithDeviation(
18+
signalID string,
19+
power int64,
20+
interval int64,
21+
deviation int64,
22+
) FeedWithDeviation {
23+
return FeedWithDeviation{
24+
SignalID: signalID,
25+
Power: power,
26+
Interval: interval,
27+
DeviationBasisPoint: deviation,
28+
}
29+
}
30+
31+
// NewCurrentFeeds creates a new current feeds instance
32+
func NewCurrentFeeds(
33+
feeds []Feed,
34+
lastUpdateTimestamp int64,
35+
lastUpdateBlock int64,
36+
) CurrentFeeds {
37+
return CurrentFeeds{
38+
Feeds: feeds,
39+
LastUpdateTimestamp: lastUpdateTimestamp,
40+
LastUpdateBlock: lastUpdateBlock,
41+
}
42+
}
43+
44+
// NewCurrentFeedWithDeviations creates a new current feeds with deviations instance
45+
func NewCurrentFeedWithDeviations(
46+
feedWithDeviations []FeedWithDeviation,
47+
lastUpdateTimestamp int64,
48+
lastUpdateBlock int64,
49+
) CurrentFeedWithDeviations {
50+
return CurrentFeedWithDeviations{
51+
Feeds: feedWithDeviations,
52+
LastUpdateTimestamp: lastUpdateTimestamp,
53+
LastUpdateBlock: lastUpdateBlock,
54+
}
55+
}
56+
357
// CalculateInterval calculates feed interval from power
458
func CalculateInterval(power int64, powerStep int64, minInterval int64, maxInterval int64) (interval int64) {
559
if power < powerStep {

x/feeds/types/median.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ type PriceFeedInfo struct {
2323
Index int64 // Index represents the index of the price feed
2424
}
2525

26+
// NewPriceFeedInfo returns a new PriceFeedInfo
27+
func NewPriceFeedInfo(
28+
priceStatus PriceStatus,
29+
power uint64,
30+
price uint64,
31+
timestamp int64,
32+
index int64,
33+
) PriceFeedInfo {
34+
return PriceFeedInfo{
35+
PriceStatus: priceStatus,
36+
Power: power,
37+
Price: price,
38+
Timestamp: timestamp,
39+
Index: index,
40+
}
41+
}
42+
2643
// FilterPriceFeedInfos filters price feed infos based on price status
2744
func FilterPriceFeedInfos(pfInfos []PriceFeedInfo, opt PriceStatus) []PriceFeedInfo {
2845
filtered := []PriceFeedInfo{}

0 commit comments

Comments
 (0)