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

[Perpetual] net open interest was added #804

Merged
merged 7 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
NAME?=elys
BINARY?=$(NAME)d
COMMIT:=$(shell git log -1 --format='%H')
VERSION:=$(shell git describe --tags --match 'v*' --abbrev=8 | sed 's/-g/-/' | sed 's/-[0-9]*-/-/')
VERSION:=v999.999.999
fenriz07 marked this conversation as resolved.
Show resolved Hide resolved
GOFLAGS:=""

# if rocksdb env variable is set, add the tag
Expand Down
4 changes: 4 additions & 0 deletions proto/elys/perpetual/pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,9 @@ message Pool {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
/*string net_open_interest = 10 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];*/
}

30 changes: 28 additions & 2 deletions proto/elys/perpetual/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ message QueryGetPoolRequest {
}

message QueryGetPoolResponse {
Pool pool = 1 [(gogoproto.nullable) = false];
PoolResponse pool = 1 [(gogoproto.nullable) = false];
}

message QueryAllPoolRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message QueryAllPoolResponse {
repeated Pool pool = 1 [(gogoproto.nullable) = false];
repeated PoolResponse pool = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

Expand Down Expand Up @@ -196,3 +196,29 @@ message QueryOpenEstimationResponse {
];
string price_impact = 20 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

message PoolResponse {
uint64 amm_pool_id = 1;
string health = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
bool enabled = 3;
bool closed = 4;
string borrow_interest_rate = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
repeated PoolAsset pool_assets_long = 6 [(gogoproto.nullable) = false];
repeated PoolAsset pool_assets_short = 7 [(gogoproto.nullable) = false];
int64 last_height_borrow_interest_rate_computed = 8;
// funding rate, if positive longs pay shorts, if negative shorts pay longs
string funding_rate = 9 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string net_open_interest = 10 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}
23 changes: 23 additions & 0 deletions x/perpetual/keeper/get_net_open_interest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package keeper

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/perpetual/types"
)

func (k Keeper) GetNetOpenInterest(pool types.Pool) math.Int {
assetLiabilitiesLong := sdk.ZeroInt()
assetLiabilitiesShort := sdk.ZeroInt()

for _, asset := range pool.PoolAssetsLong {
assetLiabilitiesLong = assetLiabilitiesLong.Add(asset.Liabilities)
}

for _, asset := range pool.PoolAssetsShort {
assetLiabilitiesShort = assetLiabilitiesShort.Add(asset.Liabilities)
}

netOpenInterest := assetLiabilitiesLong.Sub(assetLiabilitiesShort)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fenriz07 there is one thing that is not right and I realized we are doing it wrong with the existing update funding rate function here https://github.com/elys-network/elys/blob/main/x/perpetual/keeper/update_funding_rate.go as well, we have to make sure that all liabilities amount that we are cumulating within assetLiabilitiesLong and assetLiabilitiesShort are using the same denom USDC if not they must be swapped to USDC the same way we do here https://github.com/elys-network/elys/blob/main/x/perpetual/keeper/keeper.go
can you update both get net open interest and update funding rate functions to reflect that? thanks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this issue mentioned above will be handle as part of a separate PR, everything else looks good

return netOpenInterest
}
21 changes: 21 additions & 0 deletions x/perpetual/keeper/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ func createNPool(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Pool {
return items
}

func createNPoolResponse(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.PoolResponse {
items := make([]types.PoolResponse, n)
for i := range items {
items[i] = types.PoolResponse{
AmmPoolId: uint64(i),
Health: sdk.NewDec(100),
Enabled: true,
Closed: false,
BorrowInterestRate: sdk.MustNewDecFromStr("0.000000000000000001"),
PoolAssetsLong: []types.PoolAsset{},
PoolAssetsShort: []types.PoolAsset{},
LastHeightBorrowInterestRateComputed: 0,
FundingRate: sdk.ZeroDec(),
NetOpenInterest: sdk.ZeroInt(),
}

keeper.SetPool(ctx, types.NewPool(uint64(i)))
}
return items
}

func TestPoolGet(t *testing.T) {
keeper, ctx := keepertest.PerpetualKeeper(t)
items := createNPool(keeper, ctx, 10)
Expand Down
30 changes: 27 additions & 3 deletions x/perpetual/keeper/query_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (k Keeper) Pools(goCtx context.Context, req *types.QueryAllPoolRequest) (*t
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

var pools []types.Pool
var pools []types.PoolResponse
ctx := sdk.UnwrapSDKContext(goCtx)

store := ctx.KVStore(k.storeKey)
Expand All @@ -34,7 +34,18 @@ func (k Keeper) Pools(goCtx context.Context, req *types.QueryAllPoolRequest) (*t
}

if ammPool.PoolParams.UseOracle {
pools = append(pools, pool)
pools = append(pools, types.PoolResponse{
AmmPoolId: pool.AmmPoolId,
Health: pool.Health,
Enabled: pool.Enabled,
Closed: pool.Closed,
BorrowInterestRate: pool.BorrowInterestRate,
PoolAssetsLong: pool.PoolAssetsLong,
PoolAssetsShort: pool.PoolAssetsShort,
LastHeightBorrowInterestRateComputed: pool.LastHeightBorrowInterestRateComputed,
FundingRate: pool.FundingRate,
NetOpenInterest: k.GetNetOpenInterest(pool),
})
}

return nil
Expand All @@ -60,5 +71,18 @@ func (k Keeper) Pool(goCtx context.Context, req *types.QueryGetPoolRequest) (*ty
return nil, status.Error(codes.NotFound, "not found")
}

return &types.QueryGetPoolResponse{Pool: val}, nil
pool := types.PoolResponse{
AmmPoolId: val.AmmPoolId,
Health: val.Health,
Enabled: val.Enabled,
Closed: val.Closed,
BorrowInterestRate: val.BorrowInterestRate,
PoolAssetsLong: val.PoolAssetsLong,
PoolAssetsShort: val.PoolAssetsShort,
LastHeightBorrowInterestRateComputed: val.LastHeightBorrowInterestRateComputed,
FundingRate: val.FundingRate,
NetOpenInterest: k.GetNetOpenInterest(val),
}

return &types.QueryGetPoolResponse{Pool: pool}, nil
}
2 changes: 1 addition & 1 deletion x/perpetual/keeper/query_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestPools_Success(t *testing.T) {
func TestPoolQuerySingle(t *testing.T) {
keeper, ctx := keepertest.PerpetualKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
msgs := createNPool(keeper, ctx, 2)
msgs := createNPoolResponse(keeper, ctx, 2)
tests := []struct {
desc string
request *types.QueryGetPoolRequest
Expand Down
Loading
Loading