Skip to content

Commit

Permalink
[Perpetual]: Funding Rate Display (#880)
Browse files Browse the repository at this point in the history
* add to query

* fix

* update

* chore(perpetual): gen proto

---------

Co-authored-by: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com>
  • Loading branch information
amityadav0 and cosmic-vagabond authored Oct 25, 2024
1 parent 0f23bc1 commit 951ed74
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 135 deletions.
10 changes: 10 additions & 0 deletions proto/elys/perpetual/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ message PoolResponse {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
// Rate at which long pays/receive from short
// if negative longs pay, if positive long receives
string long_rate = 9 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string short_rate = 10 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

message QueryCloseEstimationRequest {
Expand Down
34 changes: 34 additions & 0 deletions x/perpetual/keeper/get_net_open_interest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,37 @@ func (k Keeper) GetNetOpenInterest(ctx sdk.Context, pool types.Pool) math.Int {

return netOpenInterest
}

func (k Keeper) GetFundingPaymentRates(ctx sdk.Context, pool types.Pool) (long sdk.Dec, short sdk.Dec) {
fundingRateLong, fundingRateShort := k.ComputeFundingRate(ctx, pool)

// account custody from long position
totalCustodyLong := sdk.ZeroInt()
for _, asset := range pool.PoolAssetsLong {
totalCustodyLong = totalCustodyLong.Add(asset.Custody)
}

// account custody from short position
totalLiabilitiesShort := sdk.ZeroInt()
for _, asset := range pool.PoolAssetsShort {
totalLiabilitiesShort = totalLiabilitiesShort.Add(asset.Liabilities)
}

if fundingRateLong.IsZero() {
// short will pay
// long will receive
unpopular_rate := sdk.ZeroDec()
if !totalCustodyLong.IsZero() {
unpopular_rate = fundingRateShort.Mul(totalLiabilitiesShort.ToLegacyDec()).Quo(totalCustodyLong.ToLegacyDec())
}
return unpopular_rate.Neg(), fundingRateShort
} else {
// long will pay
// short will receive
unpopular_rate := sdk.ZeroDec()
if !totalLiabilitiesShort.IsZero() {
unpopular_rate = fundingRateLong.Mul(totalCustodyLong.ToLegacyDec()).Quo(totalLiabilitiesShort.ToLegacyDec())
}
return fundingRateLong, unpopular_rate.Neg()
}
}
7 changes: 7 additions & 0 deletions x/perpetual/keeper/query_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (k Keeper) Pools(goCtx context.Context, req *types.QueryAllPoolRequest) (*t
}

if ammPool.PoolParams.UseOracle {
longRate, shortRate := k.GetFundingPaymentRates(ctx, pool)
pools = append(pools, types.PoolResponse{
AmmPoolId: pool.AmmPoolId,
Health: pool.Health,
Expand All @@ -43,6 +44,8 @@ func (k Keeper) Pools(goCtx context.Context, req *types.QueryAllPoolRequest) (*t
LastHeightBorrowInterestRateComputed: pool.LastHeightBorrowInterestRateComputed,
FundingRate: pool.FundingRate,
NetOpenInterest: k.GetNetOpenInterest(ctx, pool),
LongRate: longRate,
ShortRate: shortRate,
})
}

Expand All @@ -69,6 +72,8 @@ func (k Keeper) Pool(goCtx context.Context, req *types.QueryGetPoolRequest) (*ty
return nil, status.Error(codes.NotFound, "not found")
}

longRate, shortRate := k.GetFundingPaymentRates(ctx, val)

pool := types.PoolResponse{
AmmPoolId: val.AmmPoolId,
Health: val.Health,
Expand All @@ -78,6 +83,8 @@ func (k Keeper) Pool(goCtx context.Context, req *types.QueryGetPoolRequest) (*ty
LastHeightBorrowInterestRateComputed: val.LastHeightBorrowInterestRateComputed,
FundingRate: val.FundingRate,
NetOpenInterest: k.GetNetOpenInterest(ctx, val),
LongRate: longRate,
ShortRate: shortRate,
}

return &types.QueryGetPoolResponse{Pool: pool}, nil
Expand Down
Loading

0 comments on commit 951ed74

Please sign in to comment.