diff --git a/x/perp/v2/integration/action/dnr.go b/x/perp/v2/integration/action/dnr.go index 464ea66a4..eb2c38035 100644 --- a/x/perp/v2/integration/action/dnr.go +++ b/x/perp/v2/integration/action/dnr.go @@ -203,7 +203,7 @@ type setCustomDiscountAction struct { user sdk.AccAddress } -func (s setCustomDiscountAction) Do(app *app.NibiruApp, ctx sdk.Context) (outCtx sdk.Context, err error, isMandatory bool) { +func (s *setCustomDiscountAction) Do(app *app.NibiruApp, ctx sdk.Context) (outCtx sdk.Context, err error, isMandatory bool) { app.PerpKeeperV2.TraderDiscounts.Insert(ctx, collections.Join(s.user, s.volume), s.fee) return ctx, nil, true } diff --git a/x/perp/v2/keeper/dnr.go b/x/perp/v2/keeper/dnr.go index 9a6103fb1..cb34a6838 100644 --- a/x/perp/v2/keeper/dnr.go +++ b/x/perp/v2/keeper/dnr.go @@ -1,6 +1,9 @@ package keeper import ( + "log" + "math/big" + "cosmossdk.io/math" "github.com/NibiruChain/collections" "github.com/NibiruChain/nibiru/x/common/asset" @@ -48,20 +51,26 @@ func (i intValueEncoder) Name() string { type intKeyEncoder struct{} func (intKeyEncoder) Encode(key math.Int) []byte { - b, err := key.Marshal() - if err != nil { - panic(err) + i := key.BigInt() + if i.Sign() < 0 { + log.Panicf("cannot encode negative math.Int") + } + if i == nil { + log.Panicf("cannot encode nil key") } - return b + + be := i.Bytes() + padded := make([]byte, math.MaxBitLen) + copy(padded[math.MaxBitLen-len(be):], be) + return padded } func (intKeyEncoder) Decode(b []byte) (int, math.Int) { - i := math.Int{} - err := i.Unmarshal(b) - if err != nil { - panic(err) + if len(b) != math.MaxBitLen { + panic("invalid key length") } - return len(b), i + i := new(big.Int).SetBytes(b) + return math.MaxBitLen, math.NewIntFromBigInt(i) } func (intKeyEncoder) Stringify(key math.Int) string { return key.String() } diff --git a/x/perp/v2/keeper/dnr_test.go b/x/perp/v2/keeper/dnr_test.go index 17f258a1e..bfa3f4fc4 100644 --- a/x/perp/v2/keeper/dnr_test.go +++ b/x/perp/v2/keeper/dnr_test.go @@ -106,7 +106,7 @@ func TestDiscount(t *testing.T) { exchangeFee := sdk.MustNewDecFromStr("0.0010") // 0.1%, default fee taken from CreateCustomMarketAction globalFeeDiscount := sdk.MustNewDecFromStr("0.0005") // 0.05% - fauxGlobalFeeDiscount := sdk.MustNewDecFromStr("0.0004") // 0.05% + fauxGlobalFeeDiscount := sdk.MustNewDecFromStr("0.0006") // 0.06% customFeeDiscount := sdk.MustNewDecFromStr("0.0002") // 0.02% fauxCustomFeeDiscount := sdk.MustNewDecFromStr("0.0003") // 0.03% @@ -150,10 +150,11 @@ func TestDiscount(t *testing.T) { SetGlobalDiscount(globalFeeDiscount, sdk.NewInt(100_000)), SetCustomDiscount(alice, fauxCustomFeeDiscount, sdk.NewInt(50_000)), SetCustomDiscount(alice, customFeeDiscount, sdk.NewInt(100_000)), - SetPreviousEpochUserVolume(alice, sdk.NewInt(10_000)), - ).Then( - MarketOrderFeeIs(exchangeFee, alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000), sdk.OneDec(), sdk.ZeroDec()), - ), + SetPreviousEpochUserVolume(alice, sdk.NewInt(10_000)), // lower than 50_000 + ). + Then( + MarketOrderFeeIs(exchangeFee, alice, pairBtcNusd, types.Direction_LONG, sdk.NewInt(10_000), sdk.OneDec(), sdk.ZeroDec()), + ), TC("user has past epoch volume: custom discount applies"). Given( DnREpochIs(2), @@ -195,7 +196,6 @@ func TestDiscount(t *testing.T) { When( SetGlobalDiscount(sdk.MustNewDecFromStr("0.0004"), sdk.NewInt(50_000)), SetGlobalDiscount(globalFeeDiscount, sdk.NewInt(100_000)), - SetCustomDiscount(alice, sdk.MustNewDecFromStr("0.0003"), sdk.NewInt(50_000)), SetPreviousEpochUserVolume(alice, sdk.NewInt(100_000)), ). Then(