Skip to content

Commit

Permalink
[Perpetual]: Update checksameassetposition function (#576)
Browse files Browse the repository at this point in the history
* [Bug]: Fix calculate discount and add more tests (#570)

* add calculate discount test and bug fix

* get asset by denom

* ci: use post-upgrade-snapshot-generator (#571)

* ci: use post-upgrade-snapshot-generator

* ci: update delete snapshot CI to use post upgrade snapshot gen binary

* update checksame position

* update tests

* add comment

---------

Co-authored-by: Cosmic Vagabond <[email protected]>
  • Loading branch information
amityadav0 and cosmic-vagabond committed Jun 19, 2024
1 parent 55912a9 commit b7bb704
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
10 changes: 7 additions & 3 deletions x/perpetual/keeper/check_same_asset_position.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/elys-network/elys/x/perpetual/types"
)

func (k Keeper) CheckSameAssetPosition(ctx sdk.Context, msg *types.MsgOpen) *types.MTP {
mtps := k.GetAllMTPs(ctx)
mtps, _, err := k.GetMTPsForAddress(ctx, sdk.MustAccAddressFromBech32(msg.Creator), &query.PageRequest{})
if err != nil {
return nil
}
for _, mtp := range mtps {
if mtp.Address == msg.Creator && mtp.Position == msg.Position && mtp.CollateralAsset == msg.Collateral.Denom && mtp.CustodyAsset == msg.TradingAsset {
return &mtp
if mtp.Position == msg.Position && mtp.CollateralAsset == msg.Collateral.Denom && mtp.CustodyAsset == msg.TradingAsset {
return mtp
}
}

Expand Down
5 changes: 3 additions & 2 deletions x/perpetual/keeper/check_same_asset_position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ func TestCheckSameAssetPosition_NewPosition(t *testing.T) {
ctx := app.BaseApp.NewContext(true, tmproto.Header{})

k := app.PerpetualKeeper
addr := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(1000000))

mtp := types.NewMTP("creator", ptypes.BaseCurrency, ptypes.ATOM, ptypes.BaseCurrency, ptypes.ATOM, types.Position_LONG, sdk.NewDec(5), sdk.MustNewDecFromStr(types.TakeProfitPriceDefault), 1)
mtp := types.NewMTP(addr[0].String(), ptypes.BaseCurrency, ptypes.ATOM, ptypes.BaseCurrency, ptypes.ATOM, types.Position_LONG, sdk.NewDec(5), sdk.MustNewDecFromStr(types.TakeProfitPriceDefault), 1)
err := k.SetMTP(ctx, mtp)
assert.Nil(t, err)

msg := &types.MsgOpen{
Creator: "creator",
Creator: addr[0].String(),
Position: types.Position_SHORT,
Leverage: sdk.NewDec(1),
TradingAsset: ptypes.ATOM,
Expand Down
1 change: 1 addition & 0 deletions x/perpetual/keeper/estimate_and_repay.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (k Keeper) EstimateAndRepay(ctx sdk.Context, mtp types.MTP, pool types.Pool
return sdk.ZeroInt(), types.ErrInvalidPosition
}

// Note: Long settlement is done in trading asset. And short settlement in usdc in Repay function
if err := k.Repay(ctx, &mtp, &pool, ammPool, repayAmount, false, amount, baseCurrency); err != nil {
return sdk.ZeroInt(), err
}
Expand Down

0 comments on commit b7bb704

Please sign in to comment.