Skip to content

Commit

Permalink
galaxy brain int encoding fix
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown unknown committed Sep 19, 2023
1 parent 5ca0fd0 commit 1f1adce
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion x/perp/v2/integration/action/dnr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
27 changes: 18 additions & 9 deletions x/perp/v2/keeper/dnr.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package keeper

import (
"log"
"math/big"

"cosmossdk.io/math"
"github.com/NibiruChain/collections"
"github.com/NibiruChain/nibiru/x/common/asset"

Check failure on line 9 in x/perp/v2/keeper/dnr.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/NibiruChain/nibiru (goimports)
Expand Down Expand Up @@ -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() }
Expand Down
12 changes: 6 additions & 6 deletions x/perp/v2/keeper/dnr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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%

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 1f1adce

Please sign in to comment.