Skip to content

Commit

Permalink
[Perpetual]: Fix open interest (#879)
Browse files Browse the repository at this point in the history
* fix open interest

* coment
  • Loading branch information
amityadav0 authored Oct 24, 2024
1 parent 411c60b commit 933d2c6
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions x/perpetual/keeper/get_net_open_interest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,22 @@ import (
)

// GetNetOpenInterest calculates the net open interest for a given pool.
// Note: Net open interest should always be in terms of trading asset
func (k Keeper) GetNetOpenInterest(ctx sdk.Context, pool types.Pool) math.Int {
uusdc, found := k.assetProfileKeeper.GetEntry(ctx, "uusdc")
if !found {
return sdk.ZeroInt()
// account custody from long position
totalCustodyLong := sdk.ZeroInt()
for _, asset := range pool.PoolAssetsLong {
totalCustodyLong = totalCustodyLong.Add(asset.Custody)
}

var err error

// Calculate liabilities for long and short assets using the separate helper function
assetLiabilitiesLong, err := k.CalcTotalLiabilities(ctx, pool.PoolAssetsLong, pool.AmmPoolId, uusdc.Denom)
if err != nil {
return sdk.ZeroInt()
}

assetLiabilitiesShort, err := k.CalcTotalLiabilities(ctx, pool.PoolAssetsShort, pool.AmmPoolId, uusdc.Denom)
if err != nil {
return sdk.ZeroInt()
// account liabilities from short position
totalLiabilityShort := sdk.ZeroInt()
for _, asset := range pool.PoolAssetsShort {
totalLiabilityShort = totalLiabilityShort.Add(asset.Liabilities)
}

// Net Open Interest = Long Liabilities - Short Liabilities
netOpenInterest := assetLiabilitiesLong.Sub(assetLiabilitiesShort)
// Net Open Interest = Long custody - Short Liabilities
netOpenInterest := totalCustodyLong.Sub(totalLiabilityShort)

return netOpenInterest
}

0 comments on commit 933d2c6

Please sign in to comment.