Skip to content

Commit

Permalink
ensure metadata nav uses volume of 1
Browse files Browse the repository at this point in the history
special handling of unset volume in nav to be one but allow zero for backwards compatibility
  • Loading branch information
iramiller committed Sep 24, 2024
1 parent 6c04a86 commit 2325429
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion x/metadata/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ func ParseNetAssetValueString(netAssetValuesString string) ([]types.NetAssetValu
}
if len(parts) == 2 {
volume, err := strconv.ParseUint(parts[1], 10, 64)
if err != nil {
if err != nil || volume < 1 {
return []types.NetAssetValue{}, fmt.Errorf("invalid volume : %s", parts[1])
}
netAssetValues[i] = types.NewNetAssetValue(coin, volume)
Expand Down
9 changes: 7 additions & 2 deletions x/metadata/keeper/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,12 @@ func (k Keeper) SetNetAssetValue(ctx sdk.Context, scopeID types.MetadataAddress,
return err
}

setNetAssetValueEvent := types.NewEventSetNetAssetValue(scopeID, netAssetValue.Price, source)
// Since this field was added we need to ensure the default value matches the previous behavior of always presuming one is used.
if netAssetValue.Volume < 1 {
netAssetValue.Volume = 1
}

setNetAssetValueEvent := types.NewEventSetNetAssetValue(scopeID, netAssetValue.Price, netAssetValue.Volume, source)
if err := ctx.EventManager().EmitTypedEvent(setNetAssetValueEvent); err != nil {
return err
}
Expand Down Expand Up @@ -757,7 +762,7 @@ func (k Keeper) SetNetAssetValueWithBlockHeight(ctx sdk.Context, scopeID types.M
return err
}

setNetAssetValueEvent := types.NewEventSetNetAssetValue(scopeID, netAssetValue.Price, source)
setNetAssetValueEvent := types.NewEventSetNetAssetValue(scopeID, netAssetValue.Price, 1, source)
if err := ctx.EventManager().EmitTypedEvent(setNetAssetValueEvent); err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions x/metadata/keeper/scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2550,7 +2550,7 @@ func (s *ScopeKeeperTestSuite) TestSetNetAssetValue() {
var expErrs []string
var expEvents sdk.Events
if len(tc.expErr) == 0 {
event := types.NewEventSetNetAssetValue(scopeID, tc.netAssetValue.Price, "test")
event := types.NewEventSetNetAssetValue(scopeID, tc.netAssetValue.Price, 1, "test")
eventU, err := sdk.TypedEventToEvent(event)
s.Require().NoError(err, "TypedEventToEvent(NewEventSetNetAssetValue)")
expEvents = sdk.Events{eventU}
Expand Down Expand Up @@ -2588,6 +2588,7 @@ func (s *ScopeKeeperTestSuite) TestRemoveNetAssetValues() {
Denom: "usd",
Amount: sdkmath.NewInt(1000),
},
Volume: 1,
},
expErr: "",
},
Expand All @@ -2605,7 +2606,7 @@ func (s *ScopeKeeperTestSuite) TestRemoveNetAssetValues() {
})
s.Require().NoError(err, "IterateNetAssetValues err")
s.Require().Len(netAssetValues, 1, "Should have added a NAV")
s.Require().Equal(tc.netAssetValue, netAssetValues[0], "Should have added the test case nave.")
s.Require().Equal(tc.netAssetValue, netAssetValues[0], "Should have added the test case nav.")
s.app.MetadataKeeper.RemoveNetAssetValues(ctx, tc.scopeID)
netAssetValues = []types.NetAssetValue{}
err = s.app.MetadataKeeper.IterateNetAssetValues(ctx, tc.scopeID, func(state types.NetAssetValue) (stop bool) {
Expand Down
5 changes: 4 additions & 1 deletion x/metadata/types/events.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"strconv"

"github.com/hashicorp/go-metrics"

"github.com/cosmos/cosmos-sdk/telemetry"
Expand Down Expand Up @@ -277,10 +279,11 @@ func NewEventOSLocatorDeleted(owner string) *EventOSLocatorDeleted {
}

// NewEventSetNetAssetValue returns a new instance of EventSetNetAssetValue
func NewEventSetNetAssetValue(scopeID MetadataAddress, price sdk.Coin, source string) *EventSetNetAssetValue {
func NewEventSetNetAssetValue(scopeID MetadataAddress, price sdk.Coin, volume uint64, source string) *EventSetNetAssetValue {
return &EventSetNetAssetValue{
ScopeId: scopeID.String(),
Price: price.String(),
Source: source,
Volume: strconv.FormatUint(volume, 10),
}
}

0 comments on commit 2325429

Please sign in to comment.