-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dev 1847 create stop loss triggering logic (#787)
* feat:perpetual add stop loss price * feat: add test * feat: add cli command * fix: test * fix * mgration script * store borrow rate * add borrow rate * add funding rate DS and logic * trigger * handle interest * rename * fix * handle funding rates * remove * exclude time from interest query * add queries * fix * fix * fixes * add test * tests * more coverage * add * add message * add close positions * handle cli * refactor * last funding block * use leveragelp algo for funding * add migration script * fix * fix * custody * update health * health * add custody to history * set distribution value * fix * feat: stop loss trigger * add to pay * update * proto * feat: add stop loss trigger * update * test * refactor * mig * update * skip * add * fix * add test --------- Co-authored-by: Amit <[email protected]> Co-authored-by: Cosmic Vagabond <[email protected]>
- Loading branch information
1 parent
b5a0597
commit f71027b
Showing
4 changed files
with
197 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,3 +117,42 @@ func (k Keeper) CheckAndLiquidateUnhealthyPosition(ctx sdk.Context, mtp *types.M | |
|
||
return nil | ||
} | ||
|
||
func (k Keeper) CheckAndCloseAtStopLoss(ctx sdk.Context, mtp *types.MTP, pool types.Pool, ammPool ammtypes.Pool, baseCurrency string, baseCurrencyDecimal uint64) error { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
if msg, ok := r.(string); ok { | ||
ctx.Logger().Error(msg) | ||
} | ||
} | ||
}() | ||
|
||
lpTokenPrice, err := ammPool.LpTokenPrice(ctx, k.oracleKeeper) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
cosmic-vagabond
Author
Contributor
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
underStopLossPrice := !mtp.StopLossPrice.IsNil() && lpTokenPrice.LTE(mtp.StopLossPrice) | ||
if !underStopLossPrice { | ||
return fmt.Errorf("mtp stop loss price is not <= lp token price") | ||
} | ||
|
||
var repayAmount math.Int | ||
switch mtp.Position { | ||
case types.Position_LONG: | ||
repayAmount, err = k.ForceCloseLong(ctx, mtp, &pool, true, baseCurrency) | ||
case types.Position_SHORT: | ||
repayAmount, err = k.ForceCloseShort(ctx, mtp, &pool, true, baseCurrency) | ||
default: | ||
return errors.Wrap(types.ErrInvalidPosition, fmt.Sprintf("invalid position type: %s", mtp.Position)) | ||
} | ||
|
||
if err == nil { | ||
// Emit event if position was closed | ||
k.EmitForceClose(ctx, mtp, repayAmount, "") | ||
} else { | ||
return errors.Wrap(err, "error executing force close") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shouldn't this be triggered when custody price is below stop loss price ? we are not leveraging lp here, Please confirm with Wari