Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[waitting #403]Hot temporary fix v2 #429

Merged
merged 9 commits into from
Mar 15, 2023
54 changes: 50 additions & 4 deletions docs/client/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6034,9 +6034,35 @@ paths:
fees_24hours:
type: string
long_positions:
type: string
type: object
properties:
denom:
type: string
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.


NOTE: The amount field is an Int which implements the custom
method

signatures required by gogoproto.
short_positions:
type: string
type: object
properties:
denom:
type: string
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.


NOTE: The amount field is an Int which implements the custom
method

signatures required by gogoproto.
default:
description: An unexpected error response.
schema:
Expand Down Expand Up @@ -13575,9 +13601,29 @@ definitions:
fees_24hours:
type: string
long_positions:
type: string
type: object
properties:
denom:
type: string
amount:
type: string
description: |-
Coin defines a token with a denomination and an amount.

NOTE: The amount field is an Int which implements the custom method
signatures required by gogoproto.
short_positions:
type: string
type: object
properties:
denom:
type: string
amount:
type: string
description: |-
Coin defines a token with a denomination and an amount.

NOTE: The amount field is an Int which implements the custom method
signatures required by gogoproto.
ununifi.derivatives.QueryPerpetualOptionsMarketResponse:
type: object
ununifi.derivatives.QueryPerpetualOptionsResponse:
Expand Down
4 changes: 2 additions & 2 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2134,8 +2134,8 @@ QueryParamsResponse is response type for the Query/Params RPC method.
| `metrics_quote_ticker` | [string](#string) | | |
| `volume_24hours` | [string](#string) | | |
| `fees_24hours` | [string](#string) | | |
| `long_positions` | [string](#string) | | |
| `short_positions` | [string](#string) | | |
| `long_positions` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | |
| `short_positions` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | |



Expand Down
8 changes: 4 additions & 4 deletions proto/derivatives/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ message QueryPerpetualFuturesResponse {
(gogoproto.moretags) = "yaml:\"fees_24hours\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec"
];
string long_positions = 5 [
cosmos.base.v1beta1.Coin long_positions = 5 [
(gogoproto.moretags) = "yaml:\"long_positions\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec"
(gogoproto.nullable) = false
];
string short_positions = 6 [
cosmos.base.v1beta1.Coin short_positions = 6 [
(gogoproto.moretags) = "yaml:\"short_positions\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec"
(gogoproto.nullable) = false
];
}

Expand Down
7 changes: 6 additions & 1 deletion x/derivatives/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package derivatives

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/UnUniFi/chain/x/derivatives/keeper"
Expand Down Expand Up @@ -32,7 +34,10 @@ func CheckPosition(ctx sdk.Context, k keeper.Keeper) {
for _, position := range positions {
currentBaseUsdRate, currentQuoteUsdRate, err := k.GetPairUsdPriceFromMarket(ctx, position.Market)
if err != nil {
panic(err)
// todo: user logger
fmt.Println("failed to get pair usd price from market")
fmt.Println(err)
continue
}
if position.NeedLiquidation(params.PerpetualFutures.MarginMaintenanceRate, currentBaseUsdRate, currentQuoteUsdRate) {
msg := types.MsgReportLiquidation{
Expand Down
23 changes: 17 additions & 6 deletions x/derivatives/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ func (k Keeper) PerpetualFutures(c context.Context, req *types.QueryPerpetualFut
}
}

longUsd := positions.EvaluateLongPositions(getPriceFunc(ctx))
shortUsd := positions.EvaluateShortPositions(getPriceFunc(ctx))
longUUsd := positions.EvaluateLongPositions(getPriceFunc(ctx))
shortUUsd := positions.EvaluateShortPositions(getPriceFunc(ctx))
longUsd := types.MicroToNormalDenom(longUUsd)
shortUsd := types.MicroToNormalDenom(shortUUsd)
// TODO: implement the handler logic
ctx.BlockHeight()
metricsQuoteTicker := "USD"
Expand All @@ -61,8 +63,8 @@ func (k Keeper) PerpetualFutures(c context.Context, req *types.QueryPerpetualFut
MetricsQuoteTicker: metricsQuoteTicker,
Volume_24Hours: &volume24Hours,
Fees_24Hours: &fees24Hours,
LongPositions: &longUsd,
ShortPositions: &shortUsd,
LongPositions: sdk.NewCoin("usd", longUsd),
ShortPositions: sdk.NewCoin("usd", shortUsd),
}, nil
}

Expand Down Expand Up @@ -210,11 +212,20 @@ func (k Keeper) MakeQueriedPositions(ctx sdk.Context, positions types.Positions)
}

profit := perpetualFuturesPosition.ProfitAndLossInMetrics(currentBaseUsdRate, currentQuoteUsdRate)
// fixme do not use sdk.Coin directly
positiveOrNegativeProfitCoin := sdk.Coin{
Denom: "uusd",
Amount: types.MicroToNormalDenom(profit),
}
positiveOrNegativeEffectiveMargin := sdk.Coin{
Denom: "uusd",
Amount: types.MicroToNormalDenom(perpetualFuturesPosition.EffectiveMarginInMetrics(currentBaseUsdRate, currentQuoteUsdRate)),
}
queriedPosition := types.QueriedPosition{
Position: position,
ValuationProfit: sdk.NewCoin("uusd", types.MicroToNormalDenom(profit)),
ValuationProfit: positiveOrNegativeProfitCoin,
MarginMaintenanceRate: perpetualFuturesPosition.MarginMaintenanceRate(currentBaseUsdRate, currentQuoteUsdRate),
EffectiveMargin: sdk.NewCoin("uusd", types.MicroToNormalDenom(perpetualFuturesPosition.EffectiveMarginInMetrics(currentBaseUsdRate, currentQuoteUsdRate))),
EffectiveMargin: positiveOrNegativeEffectiveMargin,
}
queriedPositions = append(queriedPositions, queriedPosition)
}
Expand Down
6 changes: 5 additions & 1 deletion x/derivatives/types/positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,14 @@ func (m PerpetualFuturesPosition) EvaluatePosition(currentBaseUsdRate sdk.Dec) s
return currentBaseUsdRate.Mul(m.PositionInstance.Size_)
}

func MicroToNormalDenom(amount sdk.Dec) math.Int {
func MicroToNormalDenom(amount sdk.Dec) sdk.Int {
return amount.Mul(sdk.MustNewDecFromStr("1000000")).TruncateInt()
}

func MicroToNormalDec(amount sdk.Dec) sdk.Dec {
return amount.Mul(sdk.MustNewDecFromStr("1000000"))
}

func (m PerpetualFuturesPosition) CalcReturningAmountAtClose(baseUSDRate, quoteUSDRate sdk.Dec) (returningAmount math.Int, lossToLP math.Int) {
principal := m.RemainingMargin.Amount
pnlAmount := m.ProfitAndLossInMetrics(baseUSDRate, quoteUSDRate)
Expand Down
Loading