Skip to content

Commit

Permalink
[Tier]: update perpetual response (#756)
Browse files Browse the repository at this point in the history
* assets and liability

* update query
  • Loading branch information
amityadav0 authored Aug 29, 2024
1 parent f509128 commit 59a34a6
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 102 deletions.
3 changes: 2 additions & 1 deletion proto/elys/tier/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ message QueryPerpetualRequest {
}

message QueryPerpetualResponse {
string total = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string total_value = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string total_borrows = 2 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

message QueryLiquidTotalRequest {
Expand Down
36 changes: 22 additions & 14 deletions x/tier/keeper/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (k Keeper) RetrieveAllPortfolio(ctx sdk.Context, user sdk.AccAddress) {
totalValue = totalValue.Add(rew)

// Perpetual
perp := k.RetrievePerpetualTotal(ctx, user)
_, _, perp := k.RetrievePerpetualTotal(ctx, user)
totalValue = totalValue.Add(perp)

// Pool assets
Expand Down Expand Up @@ -199,24 +199,20 @@ func (k Keeper) RetrieveRewardsTotal(ctx sdk.Context, user sdk.AccAddress) sdk.D
return totalValue
}

func (k Keeper) RetrievePerpetualTotal(ctx sdk.Context, user sdk.AccAddress) sdk.Dec {
totalValue := sdk.NewDec(0)
func (k Keeper) RetrievePerpetualTotal(ctx sdk.Context, user sdk.AccAddress) (sdk.Dec, sdk.Dec, sdk.Dec) {
totalAssets := sdk.NewDec(0)
totalLiability := sdk.NewDec(0)
netValue := sdk.NewDec(0)
perpetuals, _, err := k.perpetual.GetMTPsForAddress(ctx, user, &query.PageRequest{})
if err == nil {
for _, perpetual := range perpetuals {
asset, found := k.assetProfileKeeper.GetEntryByDenom(ctx, perpetual.GetTradingAsset())
if !found {
continue
}
tokenPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, asset.Denom)
if tokenPrice.Equal(sdk.ZeroDec()) {
tokenPrice = k.CalcAmmPrice(ctx, asset.Denom, asset.Decimals)
}
amount := perpetual.Custody.ToLegacyDec()
totalValue = totalValue.Add((amount.Mul(tokenPrice)))
totalAssets = totalAssets.Add(k.CalculateUSDValue(ctx, perpetual.GetTradingAsset(), perpetual.Custody))
totalLiability = totalLiability.Add(k.CalculateUSDValue(ctx, perpetual.LiabilitiesAsset, perpetual.Liabilities))
totalLiability = totalLiability.Add(k.CalculateUSDValue(ctx, perpetual.CollateralAsset, perpetual.BorrowInterestUnpaidCollateral))
}
netValue = totalAssets.Sub(totalLiability)
}
return totalValue
return totalAssets, totalLiability, netValue
}

func (k Keeper) RetrieveLiquidAssetsTotal(ctx sdk.Context, user sdk.AccAddress) sdk.Dec {
Expand Down Expand Up @@ -317,6 +313,18 @@ func (k Keeper) CalcAmmPrice(ctx sdk.Context, denom string, decimal uint64) sdk.
return spotPrice.Mul(usdcPrice)
}

func (k Keeper) CalculateUSDValue(ctx sdk.Context, denom string, amount sdk.Int) sdk.Dec {
asset, found := k.assetProfileKeeper.GetEntryByDenom(ctx, denom)
if !found {
sdk.ZeroDec()
}
tokenPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, denom)
if tokenPrice.Equal(sdk.ZeroDec()) {
tokenPrice = k.CalcAmmPrice(ctx, asset.Denom, asset.Decimals)
}
return sdk.NewDecFromInt(amount).Mul(tokenPrice)
}

// SetPortfolio set a specific portfolio in the store from its index
func (k Keeper) SetPortfolio(ctx sdk.Context, todayDate string, user string, portfolio types.Portfolio) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(todayDate))
Expand Down
5 changes: 3 additions & 2 deletions x/tier/keeper/query_perpetual.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ func (k Keeper) Perpetual(goCtx context.Context, req *types.QueryPerpetualReques

ctx := sdk.UnwrapSDKContext(goCtx)
sender := sdk.MustAccAddressFromBech32(req.User)
total := k.RetrievePerpetualTotal(ctx, sender)
total, borrow, _ := k.RetrievePerpetualTotal(ctx, sender)

return &types.QueryPerpetualResponse{
Total: total,
TotalValue: total,
TotalBorrows: borrow,
}, nil
}
Loading

0 comments on commit 59a34a6

Please sign in to comment.