Skip to content

Commit

Permalink
refactor perpetual module files to avoid too many short files
Browse files Browse the repository at this point in the history
  • Loading branch information
jelysn committed Jun 14, 2024
1 parent 1f7f6cc commit e8acac3
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 266 deletions.
4 changes: 4 additions & 0 deletions x/perpetual/keeper/close.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ func (k Keeper) Close(ctx sdk.Context, msg *types.MsgClose) (*types.MsgCloseResp
Amount: repayAmount,
}, nil
}

func (k Keeper) EmitCloseEvent(ctx sdk.Context, mtp *types.MTP, repayAmount math.Int) {
ctx.EventManager().EmitEvent(types.GenerateCloseEvent(mtp, repayAmount))
}
11 changes: 0 additions & 11 deletions x/perpetual/keeper/emit_close_event.go

This file was deleted.

10 changes: 0 additions & 10 deletions x/perpetual/keeper/emit_open_event.go

This file was deleted.

2 changes: 1 addition & 1 deletion x/perpetual/keeper/estimate_and_repay.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func (k Keeper) EstimateAndRepay(ctx sdk.Context, mtp types.MTP, pool types.Pool, ammPool ammtypes.Pool, amount math.Int, baseCurrency string) (math.Int, error) {
// init repay amount
repayAmount := sdk.ZeroInt()
var repayAmount math.Int
var err error

// if position is long, repay in collateral asset
Expand Down
7 changes: 0 additions & 7 deletions x/perpetual/keeper/get_epoch_length.go

This file was deleted.

18 changes: 0 additions & 18 deletions x/perpetual/keeper/get_mtp.go

This file was deleted.

18 changes: 0 additions & 18 deletions x/perpetual/keeper/get_params.go

This file was deleted.

20 changes: 0 additions & 20 deletions x/perpetual/keeper/get_pool.go

This file was deleted.

176 changes: 0 additions & 176 deletions x/perpetual/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
Expand Down Expand Up @@ -90,28 +88,6 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}

func (k Keeper) GetMTPCount(ctx sdk.Context) uint64 {
var count uint64
countBz := ctx.KVStore(k.storeKey).Get(types.MTPCountPrefix)
if countBz == nil {
count = 0
} else {
count = types.GetUint64FromBytes(countBz)
}
return count
}

func (k Keeper) GetOpenMTPCount(ctx sdk.Context) uint64 {
var count uint64
countBz := ctx.KVStore(k.storeKey).Get(types.OpenMTPCountPrefix)
if countBz == nil {
count = 0
} else {
count = types.GetUint64FromBytes(countBz)
}
return count
}

func (k Keeper) CheckIfWhitelisted(ctx sdk.Context, address string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetWhitelistKey(address))
Expand Down Expand Up @@ -530,23 +506,6 @@ func (k Keeper) CheckMinLiabilities(ctx sdk.Context, collateralAmount sdk.Coin,
return nil
}

func (k Keeper) DestroyMTP(ctx sdk.Context, mtpAddress string, id uint64) error {
key := types.GetMTPKey(mtpAddress, id)
store := ctx.KVStore(k.storeKey)
if !store.Has(key) {
return types.ErrMTPDoesNotExist
}
store.Delete(key)
// decrement open mtp count
openCount := k.GetOpenMTPCount(ctx)
openCount--

// Set open MTP count
k.SetOpenMTPCount(ctx, openCount)

return nil
}

func (k Keeper) TakeFundPayment(ctx sdk.Context, returnAmount math.Int, returnAsset string, takePercentage sdk.Dec, fundAddr sdk.AccAddress, ammPool *ammtypes.Pool) (math.Int, error) {
returnAmountDec := sdk.NewDecFromBigInt(returnAmount.BigInt())
takeAmount := sdk.NewIntFromBigInt(takePercentage.Mul(returnAmountDec).TruncateInt().BigInt())
Expand All @@ -561,41 +520,6 @@ func (k Keeper) TakeFundPayment(ctx sdk.Context, returnAmount math.Int, returnAs
return takeAmount, nil
}

func (k Keeper) SetMTP(ctx sdk.Context, mtp *types.MTP) error {
store := ctx.KVStore(k.storeKey)
count := k.GetMTPCount(ctx)
openCount := k.GetOpenMTPCount(ctx)

if mtp.Id == 0 {
// increment global id count
count++
mtp.Id = count
k.SetMTPCount(ctx, count)
// increment open mtp count
openCount++
k.SetOpenMTPCount(ctx, openCount)
}

if err := mtp.Validate(); err != nil {
return err
}
key := types.GetMTPKey(mtp.Address, mtp.Id)
store.Set(key, k.cdc.MustMarshal(mtp))
return nil
}

// Set Open MTP count
func (k Keeper) SetOpenMTPCount(ctx sdk.Context, count uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.OpenMTPCountPrefix, types.GetUint64Bytes(count))
}

// Set MTP count
func (k Keeper) SetMTPCount(ctx sdk.Context, count uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.MTPCountPrefix, types.GetUint64Bytes(count))
}

func (k Keeper) GetWhitelistAddressIterator(ctx sdk.Context) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, types.WhitelistPrefix)
Expand All @@ -618,106 +542,6 @@ func (k Keeper) GetAllWhitelistedAddress(ctx sdk.Context) []string {
return list
}

func (k Keeper) GetMTPIterator(ctx sdk.Context) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, types.MTPPrefix)
}

func (k Keeper) GetAllMTPs(ctx sdk.Context) []types.MTP {
var mtpList []types.MTP
iterator := k.GetMTPIterator(ctx)
defer func(iterator sdk.Iterator) {
err := iterator.Close()
if err != nil {
panic(err)
}
}(iterator)

for ; iterator.Valid(); iterator.Next() {
var mtp types.MTP
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &mtp)
mtpList = append(mtpList, mtp)
}
return mtpList
}

func (k Keeper) GetMTPs(ctx sdk.Context, pagination *query.PageRequest) ([]*types.MTP, *query.PageResponse, error) {
var mtpList []*types.MTP
store := ctx.KVStore(k.storeKey)
mtpStore := prefix.NewStore(store, types.MTPPrefix)

if pagination == nil {
pagination = &query.PageRequest{
Limit: gomath.MaxUint64 - 1,
}
}

pageRes, err := query.Paginate(mtpStore, pagination, func(key []byte, value []byte) error {
var mtp types.MTP
k.cdc.MustUnmarshal(value, &mtp)
mtpList = append(mtpList, &mtp)
return nil
})

return mtpList, pageRes, err
}

func (k Keeper) GetMTPsForPool(ctx sdk.Context, ammPoolId uint64, pagination *query.PageRequest) ([]*types.MTP, *query.PageResponse, error) {
var mtps []*types.MTP

store := ctx.KVStore(k.storeKey)
mtpStore := prefix.NewStore(store, types.MTPPrefix)

if pagination == nil {
pagination = &query.PageRequest{
Limit: gomath.MaxUint64 - 1,
}
}

pageRes, err := query.FilteredPaginate(mtpStore, pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
var mtp types.MTP
k.cdc.MustUnmarshal(value, &mtp)
if accumulate && mtp.AmmPoolId == ammPoolId {
mtps = append(mtps, &mtp)
return true, nil
}

return false, nil
})

return mtps, pageRes, err
}

func (k Keeper) GetMTPsForAddress(ctx sdk.Context, mtpAddress sdk.Address, pagination *query.PageRequest) ([]*types.MTP, *query.PageResponse, error) {
var mtps []*types.MTP

store := ctx.KVStore(k.storeKey)
mtpStore := prefix.NewStore(store, types.GetMTPPrefixForAddress(mtpAddress.String()))

if pagination == nil {
pagination = &query.PageRequest{
Limit: types.MaxPageLimit,
}
}

if pagination.Limit > types.MaxPageLimit {
return nil, nil, status.Error(codes.InvalidArgument, fmt.Sprintf("page size greater than max %d", types.MaxPageLimit))
}

pageRes, err := query.Paginate(mtpStore, pagination, func(key []byte, value []byte) error {
var mtp types.MTP
k.cdc.MustUnmarshal(value, &mtp)
mtps = append(mtps, &mtp)
return nil
})
if err != nil {
return nil, nil, err
}

return mtps, pageRes, nil
}

func (k Keeper) GetWhitelistedAddress(ctx sdk.Context, pagination *query.PageRequest) ([]string, *query.PageResponse, error) {
var list []string
store := ctx.KVStore(k.storeKey)
Expand Down
Loading

0 comments on commit e8acac3

Please sign in to comment.