Skip to content

Commit cfc5dee

Browse files
authored
Perp fixes (#1315)
* fixing open price when adding collateral to an already existing position * fixing model for perpetual counter * fixing open price func
1 parent dcab666 commit cfc5dee

31 files changed

+1985
-2775
lines changed

api/elys/perpetual/genesis.pulsar.go

Lines changed: 182 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/elys/perpetual/pool.pulsar.go

Lines changed: 588 additions & 1299 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/elys/perpetual/query.pulsar.go

Lines changed: 414 additions & 377 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/elys/perpetual/query_grpc.pb.go

Lines changed: 14 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/elys/perpetual/genesis.proto

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ message GenesisState {
1414
Params params = 1 [ (gogoproto.nullable) = false ];
1515
repeated Pool pool_list = 2 [ (gogoproto.nullable) = false ];
1616
repeated MTP mtp_list = 3 [ (gogoproto.nullable) = false ];
17-
repeated string address_whitelist = 4;
17+
repeated string address_whitelist = 4 [ (gogoproto.nullable) = false ];
18+
repeated PerpetualCounter perpetual_counter = 5
19+
[ (gogoproto.nullable) = false ];
1820
}

proto/elys/perpetual/pool.proto

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,6 @@ message PoolAsset {
3636
];
3737
}
3838

39-
message LegacyPool {
40-
uint64 amm_pool_id = 1;
41-
string health = 2 [
42-
(cosmos_proto.scalar) = "cosmos.Dec",
43-
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
44-
(gogoproto.nullable) = false
45-
];
46-
47-
string borrow_interest_rate = 3 [
48-
(cosmos_proto.scalar) = "cosmos.Dec",
49-
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
50-
(gogoproto.nullable) = false
51-
];
52-
repeated PoolAsset pool_assets_long = 4 [ (gogoproto.nullable) = false ];
53-
repeated PoolAsset pool_assets_short = 5 [ (gogoproto.nullable) = false ];
54-
int64 last_height_borrow_interest_rate_computed = 6;
55-
// funding rate, if positive longs pay shorts, if negative shorts pay longs
56-
string funding_rate = 7 [
57-
(cosmos_proto.scalar) = "cosmos.Dec",
58-
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
59-
(gogoproto.nullable) = false
60-
];
61-
repeated cosmos.base.v1beta1.Coin fees_collected = 8
62-
[ (gogoproto.nullable) = false ];
63-
string leverage_max = 9 [
64-
(cosmos_proto.scalar) = "cosmos.Dec",
65-
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
66-
(gogoproto.nullable) = false
67-
];
68-
}
69-
7039
message Pool {
7140
uint64 amm_pool_id = 1;
7241
string base_asset_liabilities_ratio = 2 [
@@ -102,3 +71,9 @@ message Pool {
10271
(gogoproto.nullable) = false
10372
];
10473
}
74+
75+
message PerpetualCounter {
76+
uint64 amm_pool_id = 1;
77+
uint64 counter = 2;
78+
uint64 total_open = 3;
79+
}

proto/elys/perpetual/query.proto

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ service Query {
3434
"{amm_pool_id}/{pagination.key}";
3535
}
3636

37-
// Retuns the total number of open and lifetime mtps.
38-
rpc GetStatus(StatusRequest) returns (StatusResponse) {
39-
option (google.api.http).get = "/elys-network/elys/perpetual/status";
37+
rpc PerpetualCounter(PerpetualCounterRequest)
38+
returns (PerpetualCounterResponse) {
39+
option (google.api.http).get = "/elys-network/elys/perpetual/counter/{id}";
4040
}
4141

4242
// Queries a list of mtp positions for a given address.
@@ -169,11 +169,10 @@ message PositionsByPoolResponse {
169169
cosmos.base.query.v1beta1.PageResponse pagination = 2;
170170
}
171171

172-
message StatusRequest {}
172+
message PerpetualCounterRequest { uint64 id = 1; }
173173

174-
message StatusResponse {
175-
uint64 open_mtp_count = 1;
176-
uint64 lifetime_mtp_count = 2;
174+
message PerpetualCounterResponse {
175+
PerpetualCounter result = 1 [ (gogoproto.nullable) = false ];
177176
}
178177

179178
message PositionsForAddressRequest {

x/perpetual/autocli.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
3131
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "amm_pool_id"}},
3232
},
3333
{
34-
RpcMethod: "GetStatus",
35-
Use: "get-status",
36-
Short: "Query get-status",
34+
RpcMethod: "PerpetualCounter",
35+
Use: "counter [id]",
36+
Short: "Query total open positions for a pool",
3737
},
3838
{
3939
RpcMethod: "GetPositionsForAddress",

x/perpetual/genesis.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
2121
}
2222
}
2323

24-
// Set genesis MTP count
25-
k.SetMTPCount(ctx, (uint64)(len(genState.MtpList)))
26-
// Set genesis open MTP count
27-
k.SetOpenMTPCount(ctx, (uint64)(len(genState.MtpList)))
24+
for _, val := range genState.PerpetualCounter {
25+
k.SetPerpetualCounter(ctx, val)
26+
}
2827

2928
// Set all the whitelisted
3029
for _, elem := range genState.AddressWhitelist {
@@ -46,6 +45,8 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
4645
genesis.PoolList = k.GetAllPools(ctx)
4746
genesis.MtpList = k.GetAllMTPs(ctx)
4847

48+
genesis.PerpetualCounter = k.GetAllPerpetualCounter(ctx)
49+
4950
whitelist := k.GetAllWhitelistedAddress(ctx)
5051
whitelistAddresses := make([]string, len(whitelist))
5152
for i, whitelistAddress := range whitelist {

x/perpetual/keeper/check_max_open_positions.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import (
66
"github.com/elys-network/elys/v6/x/perpetual/types"
77
)
88

9-
func (k Keeper) CheckMaxOpenPositions(ctx sdk.Context) error {
9+
func (k Keeper) CheckMaxOpenPositions(ctx sdk.Context, poolId uint64) error {
1010
// If set to -1, no limit on how many positions can be open
1111
if k.GetMaxOpenPositions(ctx) < 0 {
1212
return nil
1313
}
14-
if k.GetOpenMTPCount(ctx) >= uint64(k.GetMaxOpenPositions(ctx)) {
14+
perpetualCounter := k.GetPerpetualCounter(ctx, poolId)
15+
if perpetualCounter.TotalOpen >= uint64(k.GetMaxOpenPositions(ctx)) {
1516
return errorsmod.Wrap(types.ErrMaxOpenPositions, "cannot open new positions")
1617
}
1718
return nil

0 commit comments

Comments
 (0)