Skip to content

Commit

Permalink
remove rolling average, fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
nullpointer0x00 committed Aug 29, 2023
1 parent 9227bfb commit 649122f
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 105 deletions.
2 changes: 1 addition & 1 deletion x/marker/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ func (s *IntegrationTestSuite) TestMarkerQueryCommands() {
name: "marker net asset value query",
cmd: markercli.NetAssetValuesCmd(),
args: []string{"testcoin"},
expectedOutput: "net_asset_values:\n- price_per_token:\n amount: \"100\"\n denom: usd\n updated_block_height: \"0\"\n volume: \"100\"",
expectedOutput: "net_asset_values:\n- price:\n amount: \"100\"\n denom: usd\n updated_block_height: \"0\"\n volume: \"100\"",
},
}
for _, tc := range testCases {
Expand Down
34 changes: 0 additions & 34 deletions x/marker/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ func (k Keeper) AddSetNetAssetValues(ctx sdk.Context, marker types.MarkerAccount
}

// SetNetAssetValue adds/updates a net asset value to marker
// If multiple net asset values are set during same block height that rolling average will be stored
func (k Keeper) SetNetAssetValue(ctx sdk.Context, marker types.MarkerAccountI, netAssetValue types.NetAssetValue, source string) error {
netAssetValue.UpdatedBlockHeight = uint64(ctx.BlockHeight())
if err := netAssetValue.Validate(); err != nil {
Expand All @@ -256,22 +255,6 @@ func (k Keeper) SetNetAssetValue(ctx sdk.Context, marker types.MarkerAccountI, n

key := types.NetAssetValueKey(marker.GetAddress(), netAssetValue.Price.Denom)
store := ctx.KVStore(k.storeKey)

value := store.Get(key)
if value != nil {
var prevNav types.NetAssetValue
err := k.cdc.Unmarshal(value, &prevNav)
if err != nil {
return err
}
if prevNav.UpdatedBlockHeight == netAssetValue.UpdatedBlockHeight {
netAssetValue, err = k.CalculateRollingAverage(prevNav, netAssetValue)
if err != nil {
return err
}
}
}

if netAssetValue.Volume > marker.GetSupply().Amount.Uint64() {
return fmt.Errorf("volume(%v) cannot exceed marker %q supply(%v) ", netAssetValue.Volume, marker.GetDenom(), marker.GetSupply())
}
Expand All @@ -285,23 +268,6 @@ func (k Keeper) SetNetAssetValue(ctx sdk.Context, marker types.MarkerAccountI, n
return nil
}

// CalculateRollingAverage returns an updated net asset value with an average price per token and summed volume
func (k Keeper) CalculateRollingAverage(prevNav types.NetAssetValue, netAssetValue types.NetAssetValue) (types.NetAssetValue, error) {
if prevNav.Price.Denom != netAssetValue.Price.Denom {
return types.NetAssetValue{}, fmt.Errorf("net asset value denom do not match %v:%v", prevNav.Price.Denom, netAssetValue.Price.Denom)
}
totalVolume := prevNav.Volume + netAssetValue.Volume
if totalVolume == 0 {
return netAssetValue, nil
}
prevTotalPrice := prevNav.Price.Amount.Mul(sdk.NewInt(int64(prevNav.Volume)))
currentTotalPrice := netAssetValue.Price.Amount.Mul(sdk.NewInt(int64(netAssetValue.Volume)))
average := prevTotalPrice.Add(currentTotalPrice).Quo(sdk.NewInt(int64(totalVolume)))
netAssetValue.Price = sdk.NewCoin(prevNav.Price.Denom, average)
netAssetValue.Volume = totalVolume
return netAssetValue, nil
}

// IterateNetAssetValues iterates net asset values for marker
func (k Keeper) IterateNetAssetValues(ctx sdk.Context, markerAddr sdk.AccAddress, handler func(state types.NetAssetValue) (stop bool)) error {
store := ctx.KVStore(k.storeKey)
Expand Down
70 changes: 0 additions & 70 deletions x/marker/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1455,73 +1455,3 @@ func TestBypassAddrsLocked(t *testing.T) {
act00 := kAddrs[0][0]
assert.Equal(t, orig00, act00, "first byte of first address returned by GetReqAttrBypassAddrs")
}

func TestCalculateRollingAverage(t *testing.T) {
mk := markerkeeper.NewKeeper(nil, nil, paramtypes.NewSubspace(nil, nil, nil, nil, "test"), nil, &dummyBankKeeper{}, nil, nil, nil, nil, nil, nil)
testCases := []struct {
name string
prevNav types.NetAssetValue
newNav types.NetAssetValue
expNav types.NetAssetValue
expErr string
}{
{
name: "volumes are zero",
prevNav: types.NetAssetValue{},
newNav: types.NetAssetValue{},
expNav: types.NetAssetValue{},
},
{
name: "denoms do not match",
prevNav: types.NetAssetValue{
Price: sdk.NewInt64Coin("denoma", 1),
},
newNav: types.NetAssetValue{
Price: sdk.NewInt64Coin("denomb", 1),
},
expErr: "net asset value denom do not match denoma:denomb",
},
{
name: "succesfully compute rolling average and new volume",
prevNav: types.NetAssetValue{
Price: sdk.NewInt64Coin("usd", 100),
Volume: 2,
},
newNav: types.NetAssetValue{
Price: sdk.NewInt64Coin("usd", 50),
Volume: 2,
},
expNav: types.NetAssetValue{
Price: sdk.NewInt64Coin("usd", 75),
Volume: 4,
},
},
{
name: "succesfully compute rolling average with rounding and new volume",
prevNav: types.NetAssetValue{
Price: sdk.NewInt64Coin("usd", 99),
Volume: 2,
},
newNav: types.NetAssetValue{
Price: sdk.NewInt64Coin("usd", 50),
Volume: 2,
},
expNav: types.NetAssetValue{
Price: sdk.NewInt64Coin("usd", 74),
Volume: 4,
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := mk.CalculateRollingAverage(tc.prevNav, tc.newNav)
if len(tc.expErr) > 0 {
assert.EqualError(t, err, tc.expErr)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expNav, result)
}
})
}
}

0 comments on commit 649122f

Please sign in to comment.