-
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.
* feat: close estimation query init files * feat: add additional logic + tests * test(perpetual): fix tests
- Loading branch information
1 parent
6e5c3a9
commit e018c0a
Showing
25 changed files
with
2,717 additions
and
850 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cli | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/elys-network/elys/x/perpetual/types" | ||
"github.com/spf13/cast" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var _ = strconv.Itoa(0) | ||
|
||
func CmdCloseEstimation() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "close-estimation [address] [position-id]", | ||
Short: "Query close-estimation", | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
reqAddress := args[0] | ||
|
||
reqPositionId, err := cast.ToUint64E(args[1]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
clientCtx, err := client.GetClientQueryContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
queryClient := types.NewQueryClient(clientCtx) | ||
|
||
params := &types.QueryCloseEstimationRequest{ | ||
Address: reqAddress, | ||
PositionId: reqPositionId, | ||
} | ||
|
||
res, err := queryClient.CloseEstimation(cmd.Context(), params) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return clientCtx.PrintProto(res) | ||
}, | ||
} | ||
|
||
flags.AddQueryFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} |
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package keeper | ||
|
||
import ( | ||
"cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
ammtypes "github.com/elys-network/elys/x/amm/types" | ||
"github.com/elys-network/elys/x/perpetual/types" | ||
) | ||
|
||
func (k Keeper) CalcReturnAmount(ctx sdk.Context, mtp types.MTP, pool types.Pool, ammPool ammtypes.Pool, repayAmount math.Int, amount math.Int, baseCurrency string) (returnAmount math.Int, err error) { | ||
Liabilities := mtp.Liabilities | ||
BorrowInterestUnpaid := mtp.BorrowInterestUnpaidCollateral | ||
|
||
if mtp.BorrowInterestUnpaidCollateral.IsPositive() { | ||
if mtp.Position == types.Position_SHORT { | ||
// swap to trading asset | ||
unpaidCollateralIn := sdk.NewCoin(mtp.CollateralAsset, mtp.BorrowInterestUnpaidCollateral) | ||
C, err := k.CloseEstimationChecker.EstimateSwapGivenOut(ctx, unpaidCollateralIn, mtp.TradingAsset, ammPool) | ||
if err != nil { | ||
return sdk.ZeroInt(), err | ||
} | ||
|
||
BorrowInterestUnpaid = C | ||
} else if mtp.CollateralAsset != baseCurrency { | ||
// swap to base currency | ||
unpaidCollateralIn := sdk.NewCoin(mtp.CollateralAsset, mtp.BorrowInterestUnpaidCollateral) | ||
C, err := k.CloseEstimationChecker.EstimateSwapGivenOut(ctx, unpaidCollateralIn, baseCurrency, ammPool) | ||
if err != nil { | ||
return sdk.ZeroInt(), err | ||
} | ||
|
||
BorrowInterestUnpaid = C | ||
} | ||
} | ||
|
||
// Reminder: | ||
// if long both repay amount and liablities are collateral asset | ||
// if short both repay amount and liablities are trading asset | ||
|
||
have := repayAmount | ||
owe := Liabilities.Add(BorrowInterestUnpaid).Mul(amount.Quo(mtp.Custody)) | ||
|
||
if have.LT(owe) { | ||
// v principle liability; x excess liability | ||
returnAmount = sdk.ZeroInt() | ||
} else { | ||
// can afford both | ||
returnAmount = have.Sub(owe) | ||
} | ||
|
||
// returnAmount is so far in base currency if long or trading asset if short, now should convert it to collateralAsset in order to return | ||
if returnAmount.IsPositive() { | ||
if mtp.Position == types.Position_SHORT { | ||
// swap to collateral asset | ||
amtTokenIn := sdk.NewCoin(mtp.TradingAsset, returnAmount) | ||
C, err := k.CloseEstimationChecker.EstimateSwapGivenOut(ctx, amtTokenIn, mtp.CollateralAsset, ammPool) | ||
if err != nil { | ||
return sdk.ZeroInt(), err | ||
} | ||
|
||
returnAmount = C | ||
} else if mtp.CollateralAsset != baseCurrency { | ||
// swap to collateral asset | ||
amtTokenIn := sdk.NewCoin(baseCurrency, returnAmount) | ||
C, err := k.CloseEstimationChecker.EstimateSwapGivenOut(ctx, amtTokenIn, mtp.CollateralAsset, ammPool) | ||
if err != nil { | ||
return sdk.ZeroInt(), err | ||
} | ||
|
||
returnAmount = C | ||
} | ||
} | ||
|
||
return returnAmount, 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
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
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
Oops, something went wrong.