Skip to content

Commit

Permalink
move
Browse files Browse the repository at this point in the history
  • Loading branch information
amityadav0 committed Sep 19, 2024
1 parent 8d12af6 commit a944f6f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
33 changes: 0 additions & 33 deletions x/perpetual/keeper/begin_blocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,4 @@ func (k Keeper) BeginBlocker(ctx sdk.Context) {
}
k.SetPool(ctx, pool)
}

// Handle toPay
err := k.HandleToPay(ctx)
if err != nil {
ctx.Logger().Error(err.Error())
}
}

func (k Keeper) HandleToPay(ctx sdk.Context) error {
toPays := k.GetAllToPayStore(ctx)

if len(toPays) == 0 {
return nil
}
// get funding fee collection address
fundingFeeCollectionAddress := k.GetFundingFeeCollectionAddress(ctx)

for _, toPay := range toPays {
balance := k.bankKeeper.GetBalance(ctx, fundingFeeCollectionAddress, toPay.AssetDenom)
if balance.Amount.LT(toPay.AssetBalance) {
break
} else {
// transfer funding fee amount to mtp address
if err := k.bankKeeper.SendCoins(ctx, fundingFeeCollectionAddress, sdk.MustAccAddressFromBech32(toPay.Address), sdk.NewCoins(sdk.NewCoin(toPay.AssetDenom, toPay.AssetBalance))); err != nil {
return err
}
err := k.DeleteToPay(ctx, sdk.MustAccAddressFromBech32(toPay.Address), toPay.Id)
if err != nil {
return err
}
}
}
return nil
}
33 changes: 33 additions & 0 deletions x/perpetual/keeper/process_mtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import (

func (k Keeper) CheckAndLiquidateUnhealthyPosition(ctx sdk.Context, mtp *types.MTP, pool types.Pool, ammPool ammtypes.Pool, baseCurrency string, baseCurrencyDecimal uint64) error {
var err error
// Handle toPay
err = k.HandleToPay(ctx)
if err != nil {
ctx.Logger().Error(err.Error())
}

// update mtp take profit liabilities
// calculate mtp take profit liabilities, delta x_tp_l = delta y_tp_c * current price (take profit liabilities = take profit custody * current price)
mtp.TakeProfitLiabilities, err = k.CalcMTPTakeProfitLiability(ctx, mtp, baseCurrency)
Expand Down Expand Up @@ -166,3 +172,30 @@ func (k Keeper) CheckAndCloseAtStopLoss(ctx sdk.Context, mtp *types.MTP, pool ty

return nil
}

func (k Keeper) HandleToPay(ctx sdk.Context) error {
toPays := k.GetAllToPayStore(ctx)

if len(toPays) == 0 {
return nil
}
// get funding fee collection address
fundingFeeCollectionAddress := k.GetFundingFeeCollectionAddress(ctx)

for _, toPay := range toPays {
balance := k.bankKeeper.GetBalance(ctx, fundingFeeCollectionAddress, toPay.AssetDenom)
if balance.Amount.LT(toPay.AssetBalance) {
break
} else {
// transfer funding fee amount to mtp address
if err := k.bankKeeper.SendCoins(ctx, fundingFeeCollectionAddress, sdk.MustAccAddressFromBech32(toPay.Address), sdk.NewCoins(sdk.NewCoin(toPay.AssetDenom, toPay.AssetBalance))); err != nil {
return err
}
err := k.DeleteToPay(ctx, sdk.MustAccAddressFromBech32(toPay.Address), toPay.Id)
if err != nil {
return err
}
}
}
return nil
}

0 comments on commit a944f6f

Please sign in to comment.