|
1 | 1 | package keeper
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + |
4 | 6 | sdk "github.com/cosmos/cosmos-sdk/types"
|
5 | 7 | stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
6 | 8 |
|
@@ -63,6 +65,35 @@ func (k Keeper) DeletePrice(ctx sdk.Context, signalID string) {
|
63 | 65 | ctx.KVStore(k.storeKey).Delete(types.PriceStoreKey(signalID))
|
64 | 66 | }
|
65 | 67 |
|
| 68 | +// CalculatePrices calculates final prices for all supported feeds. |
| 69 | +func (k Keeper) CalculatePrices(ctx sdk.Context) { |
| 70 | + feeds := k.GetSupportedFeedsByPower(ctx) |
| 71 | + for _, feed := range feeds { |
| 72 | + price, err := k.CalculatePrice(ctx, feed) |
| 73 | + if err != nil { |
| 74 | + ctx.EventManager().EmitEvent( |
| 75 | + sdk.NewEvent( |
| 76 | + types.EventTypeCalculatePriceFailed, |
| 77 | + sdk.NewAttribute(types.AttributeKeySignalID, feed.SignalID), |
| 78 | + sdk.NewAttribute(types.AttributeKeyErrorMessage, err.Error()), |
| 79 | + ), |
| 80 | + ) |
| 81 | + continue |
| 82 | + } |
| 83 | + |
| 84 | + k.SetPrice(ctx, price) |
| 85 | + |
| 86 | + ctx.EventManager().EmitEvent( |
| 87 | + sdk.NewEvent( |
| 88 | + types.EventTypeUpdatePrice, |
| 89 | + sdk.NewAttribute(types.AttributeKeySignalID, price.SignalID), |
| 90 | + sdk.NewAttribute(types.AttributeKeyPrice, fmt.Sprintf("%d", price.Price)), |
| 91 | + sdk.NewAttribute(types.AttributeKeyTimestamp, fmt.Sprintf("%d", price.Timestamp)), |
| 92 | + ), |
| 93 | + ) |
| 94 | + } |
| 95 | +} |
| 96 | + |
66 | 97 | // CalculatePrice calculates final price from price-validator and punish validators those did not report.
|
67 | 98 | func (k Keeper) CalculatePrice(ctx sdk.Context, feed types.Feed) (types.Price, error) {
|
68 | 99 | var priceFeedInfos []types.PriceFeedInfo
|
|
0 commit comments