Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hot fix bug v2 #423

Merged
merged 2 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions x/derivatives/keeper/positions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"encoding/binary"
"errors"
"strconv"
"time"
Expand All @@ -10,12 +11,15 @@ import (
"github.com/UnUniFi/chain/x/derivatives/types"
)

func (k Keeper) GetLastPositionId(ctx sdk.Context) string {
func (k Keeper) GetLastPositionId(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)

bz := store.Get([]byte(types.KeyPrefixLastPositionId))
if bz == nil {
return 0
}

return string(bz)
return binary.BigEndian.Uint64(bz)
}

func (k Keeper) GetLastPosition(ctx sdk.Context) types.Position {
Expand Down Expand Up @@ -43,7 +47,7 @@ func (k Keeper) IncreaseLastPositionId(ctx sdk.Context) {
store.Set([]byte(types.KeyPrefixLastPositionId), types.GetPositionIdBytes(0))
}

lastPositionId := types.GetPositionIdFromString(k.GetLastPositionId(ctx))
lastPositionId := k.GetLastPositionId(ctx)
store.Set([]byte(types.KeyPrefixLastPositionId), types.GetPositionIdBytes(lastPositionId+1))
}

Expand Down Expand Up @@ -142,16 +146,7 @@ func (k Keeper) DeletePosition(ctx sdk.Context, address sdk.AccAddress, id strin
func (k Keeper) OpenPosition(ctx sdk.Context, msg *types.MsgOpenPosition) error {
// todo check sender amount for margin

lastPosition := k.GetLastPosition(ctx)

var positionId string
if lastPosition.Id == "" {
positionId = "0"
} else {
// increment position id
lastPositionId, _ := strconv.Atoi(lastPosition.Id)
positionId = strconv.Itoa(lastPositionId + 1)
}
newPositionId := strconv.FormatUint(k.GetLastPositionId(ctx)+1, 10)

if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, msg.Sender.AccAddress(), types.ModuleName, sdk.NewCoins(msg.Margin)); err != nil {
return err
Expand All @@ -166,9 +161,9 @@ func (k Keeper) OpenPosition(ctx sdk.Context, msg *types.MsgOpenPosition) error
var position *types.Position
switch positionInstance := positionInstance.(type) {
case *types.PerpetualFuturesPositionInstance:
position, err = k.OpenPerpetualFuturesPosition(ctx, positionId, msg.Sender, msg.Margin, msg.Market, *positionInstance)
position, err = k.OpenPerpetualFuturesPosition(ctx, newPositionId, msg.Sender, msg.Margin, msg.Market, *positionInstance)
case *types.PerpetualOptionsPositionInstance:
position, err = k.OpenPerpetualOptionsPosition(ctx, positionId, msg.Sender, msg.Margin, msg.Market, *positionInstance)
position, err = k.OpenPerpetualOptionsPosition(ctx, newPositionId, msg.Sender, msg.Margin, msg.Market, *positionInstance)
default:
panic("")
}
Expand Down
9 changes: 7 additions & 2 deletions x/derivatives/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func GetPositionIdFromString(idStr string) uint64 {
return GetPositionIdFromBytes([]byte(idStr))
}

func GetPositionIdByteFromString(idStr string) []byte {
intPosId, _ := strconv.Atoi(idStr)
return GetPositionIdBytes(uint64(intPosId))
}

func GetBlockTimestampBytes(timestamp int64) (timestampBz []byte) {
timestampBz = make([]byte, 8)
binary.BigEndian.PutUint64(timestampBz, uint64(timestamp))
Expand Down Expand Up @@ -93,15 +98,15 @@ func AssetDepositKeyPrefix(denom string) []byte {
}

func PositionWithIdKeyPrefix(posId string) []byte {
return append([]byte(KeyPrefixPosition), []byte(posId)...)
return append([]byte(KeyPrefixPosition), GetPositionIdByteFromString(posId)...)
}

func AddressPositionKeyPrefix(sender sdk.AccAddress) []byte {
return append([]byte(KeyPrefixUserPosition), address.MustLengthPrefix(sender)...)
}

func AddressPositionWithIdKeyPrefix(sender sdk.AccAddress, posId string) []byte {
return append(AddressPositionKeyPrefix(sender), []byte(posId)...)
return append(AddressPositionKeyPrefix(sender), GetPositionIdByteFromString(posId)...)
}

func DenomNetPositionPerpetualFuturesKeyPrefix(denom string, quoteDenom string) []byte {
Expand Down