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]: New Liquidation System #766

Merged
merged 51 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
3b677c0
store borrow rate
amityadav0 Sep 2, 2024
8c72c58
add borrow rate
amityadav0 Sep 2, 2024
e56fc36
add funding rate DS and logic
amityadav0 Sep 2, 2024
f980836
trigger
amityadav0 Sep 2, 2024
c3331f5
handle interest
amityadav0 Sep 2, 2024
eab7bbc
rename
amityadav0 Sep 2, 2024
0b6dd6b
Merge branch 'main' into perp-liq
amityadav0 Sep 2, 2024
df28076
fix
amityadav0 Sep 3, 2024
f1e139c
handle funding rates
amityadav0 Sep 3, 2024
1040eaf
remove
amityadav0 Sep 3, 2024
a91765e
exclude time from interest query
amityadav0 Sep 3, 2024
a137f99
add queries
amityadav0 Sep 3, 2024
cec8c54
Merge branch 'main' into perp-liq
amityadav0 Sep 3, 2024
f6f6502
fix
amityadav0 Sep 3, 2024
a0b7ca7
fix
amityadav0 Sep 4, 2024
92f470d
fixes
amityadav0 Sep 4, 2024
fba80f5
add test
amityadav0 Sep 4, 2024
8b49eda
tests
amityadav0 Sep 4, 2024
d6b76dd
more coverage
amityadav0 Sep 4, 2024
5790f23
add
amityadav0 Sep 4, 2024
ab5d02d
add message
amityadav0 Sep 4, 2024
ebb8a5b
add close positions
amityadav0 Sep 4, 2024
4f3a947
handle cli
amityadav0 Sep 4, 2024
06eb9bd
refactor
amityadav0 Sep 4, 2024
112fad5
last funding block
amityadav0 Sep 5, 2024
70afcb7
use leveragelp algo for funding
amityadav0 Sep 5, 2024
f9e8201
add migration script
amityadav0 Sep 5, 2024
807fe4e
merge
amityadav0 Sep 5, 2024
256ae4a
fix
amityadav0 Sep 5, 2024
426821e
fix
amityadav0 Sep 5, 2024
9e36968
custody
amityadav0 Sep 5, 2024
918df8d
Merge branch 'main' into perp-liq
amityadav0 Sep 5, 2024
020d476
update health
amityadav0 Sep 6, 2024
f3d40a1
health
amityadav0 Sep 6, 2024
4c1d88e
Merge branch 'main' into perp-liq
amityadav0 Sep 6, 2024
584c926
merge
amityadav0 Sep 9, 2024
1806051
add custody to history
amityadav0 Sep 9, 2024
b7fa9d2
set distribution value
amityadav0 Sep 9, 2024
e59e0c7
fix
amityadav0 Sep 9, 2024
d9fedb2
add to pay
amityadav0 Sep 10, 2024
7e6a084
Merge branch 'main' into perp-liq
amityadav0 Sep 10, 2024
9a770a7
Merge branch 'main' into perp-liq
amityadav0 Sep 10, 2024
dd68257
update
amityadav0 Sep 10, 2024
fcdd09c
proto
amityadav0 Sep 10, 2024
7b1dbe2
test
amityadav0 Sep 10, 2024
7b823d3
refactor
amityadav0 Sep 10, 2024
ccffde8
fix
amityadav0 Sep 10, 2024
4068188
mig
amityadav0 Sep 10, 2024
a041524
update
amityadav0 Sep 10, 2024
5bc2f7e
skip
amityadav0 Sep 10, 2024
731c3ad
add
amityadav0 Sep 10, 2024
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
471 changes: 347 additions & 124 deletions docs/static/openapi.yml

Large diffs are not rendered by default.

28 changes: 19 additions & 9 deletions proto/elys/perpetual/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import "elys/perpetual/types.proto";

// Msg defines the Msg service.
service Msg {
rpc Open (MsgOpen ) returns (MsgOpenResponse );
rpc BrokerOpen (MsgBrokerOpen ) returns (MsgOpenResponse );
rpc Close (MsgClose ) returns (MsgCloseResponse );
rpc BrokerClose (MsgBrokerClose ) returns (MsgCloseResponse );
rpc UpdateParams (MsgUpdateParams ) returns (MsgUpdateParamsResponse );
rpc Whitelist (MsgWhitelist ) returns (MsgWhitelistResponse );
rpc Dewhitelist (MsgDewhitelist ) returns (MsgDewhitelistResponse );
rpc AddCollateral (MsgAddCollateral ) returns (MsgAddCollateralResponse );
rpc BrokerAddCollateral (MsgBrokerAddCollateral) returns (MsgAddCollateralResponse);
rpc Open (MsgOpen ) returns (MsgOpenResponse );
rpc BrokerOpen (MsgBrokerOpen ) returns (MsgOpenResponse );
rpc Close (MsgClose ) returns (MsgCloseResponse );
rpc BrokerClose (MsgBrokerClose ) returns (MsgCloseResponse );
rpc UpdateParams (MsgUpdateParams ) returns (MsgUpdateParamsResponse );
rpc Whitelist (MsgWhitelist ) returns (MsgWhitelistResponse );
rpc Dewhitelist (MsgDewhitelist ) returns (MsgDewhitelistResponse );
rpc AddCollateral (MsgAddCollateral ) returns (MsgAddCollateralResponse );
rpc BrokerAddCollateral (MsgBrokerAddCollateral) returns (MsgAddCollateralResponse );
rpc ClosePositions (MsgClosePositions ) returns (MsgClosePositionsResponse);
}
message MsgOpen {
string creator = 1;
Expand Down Expand Up @@ -101,3 +102,12 @@ message MsgBrokerAddCollateral {
int32 id = 3;
string owner = 4;
}

message MsgClosePositions {
string creator = 1;
repeated PositionRequest liquidate = 2;
repeated PositionRequest stop_loss = 3;
}

message MsgClosePositionsResponse {}

132 changes: 132 additions & 0 deletions proto/elys/perpetual/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,95 @@ enum Position {
SHORT = 2;
}

message LegacyMTP {
string address = 1;
string collateral_asset = 2;
string trading_asset = 3;
string liabilities_asset = 4;
string custody_asset = 5;
string collateral = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string liabilities = 7 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string borrow_interest_paid_collateral = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string borrow_interest_paid_custody = 9 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string borrow_interest_unpaid_collateral = 10 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string custody = 11 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string take_profit_liabilities = 12 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string take_profit_custody = 13 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string leverage = 14 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string mtp_health = 15 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
Position position = 16;
uint64 id = 17;
uint64 amm_pool_id = 18;
string consolidate_leverage = 19 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string sum_collateral = 20 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string take_profit_price = 21 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string take_profit_borrow_rate = 22 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// funding fee paid
string funding_fee_paid_collateral = 23 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string funding_fee_paid_custody = 24 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
// funding fee received
string funding_fee_received_collateral = 25 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string funding_fee_received_custody = 26 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string open_price = 27 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

message MTP {
string address = 1;
string collateral_asset = 2;
Expand Down Expand Up @@ -99,8 +188,51 @@ message MTP {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
uint64 last_interest_calc_time = 28;
uint64 last_interest_calc_block = 29;
uint64 last_funding_calc_time = 30;
uint64 last_funding_calc_block = 31;
}

message InterestBlock {
string interest_rate = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
int64 block_height = 2;
}

message FundingRateBlock {
string funding_rate = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
int64 block_height = 2;
string funding_amount_short = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string funding_amount_long = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}

message ToPay {
string asset_balance = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string asset_denom = 2;
string address = 3;
uint64 id = 4;
}

message WhiteList {
repeated string validator_list = 1;
}

message PositionRequest {
string address = 1;
uint64 id = 2;
}
8 changes: 4 additions & 4 deletions x/leveragelp/keeper/query_get_position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ func (suite KeeperTestSuite) TestQueryGetPosition() {

expected := types.PositionAndInterest{
Position: &types.QueryPosition{
Position: position,
UpdatedLeverage: updated_leverage,
Position: position,
UpdatedLeverage: updated_leverage,
PositionUsdValue: sdk.NewDec(5000).Quo(sdk.NewDec(1000000)),
},
InterestRateHour: sdk.MustNewDecFromStr("0.000017123287671233"),
InterestRateHour: sdk.MustNewDecFromStr("0.000017123287671233"),
InterestRateHourUsd: sdk.ZeroDec(),
}
pos_for_address_res, _ := k.QueryPositionsForAddress(suite.ctx, &types.PositionsForAddressRequest{Address: addr.String(), Pagination: nil} )
pos_for_address_res, _ := k.QueryPositionsForAddress(suite.ctx, &types.PositionsForAddressRequest{Address: addr.String(), Pagination: nil})

suite.Require().Equal(expected.Position, pos_for_address_res.Positions[0].Position)
suite.Require().True(expected.InterestRateHour.Equal(pos_for_address_res.Positions[0].InterestRateHour))
Expand Down
2 changes: 1 addition & 1 deletion x/leveragelp/keeper/query_position.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (k Keeper) Position(goCtx context.Context, req *types.PositionRequest) (*ty
if err != nil {
return nil, err
}
updatedLeveragePosition, err := k.GetLeverageLpUpdatedLeverage(ctx, []*types.Position{&position})
updatedLeveragePosition, err := k.GetLeverageLpUpdatedLeverage(ctx, []*types.Position{&position})

if err != nil {
return nil, err
Expand Down
3 changes: 0 additions & 3 deletions x/leveragelp/types/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var _ LeverageLpHooks = MultiLeverageLpHooks{}
// combine multiple leverageLp hooks, all hook functions are run in array sequence.
type MultiLeverageLpHooks []LeverageLpHooks


func NewMultiLeverageLpHooks(hooks ...LeverageLpHooks) MultiLeverageLpHooks {
return hooks
}
Expand All @@ -35,7 +34,6 @@ func (h MultiLeverageLpHooks) AfterLeverageLpPositionOpen(ctx sdk.Context, sende
return nil
}


func (h MultiLeverageLpHooks) AfterLeverageLpPositionClose(ctx sdk.Context, sender sdk.AccAddress) error {
for i := range h {
err := h[i].AfterLeverageLpPositionClose(ctx, sender)
Expand All @@ -46,7 +44,6 @@ func (h MultiLeverageLpHooks) AfterLeverageLpPositionClose(ctx sdk.Context, send
return nil
}


func (h MultiLeverageLpHooks) AfterLeverageLpPositionOpenConsolidate(ctx sdk.Context, sender sdk.AccAddress) error {
for i := range h {
err := h[i].AfterLeverageLpPositionOpenConsolidate(ctx, sender)
Expand Down
1 change: 1 addition & 0 deletions x/perpetual/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func GetTxCmd() *cobra.Command {
cmd.AddCommand(CmdWhitelist())
cmd.AddCommand(CmdDewhitelist())
cmd.AddCommand(CmdAddCollateral())
cmd.AddCommand(CmdClosePositions())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
78 changes: 78 additions & 0 deletions x/perpetual/client/cli/tx_close_positions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cli

import (
"encoding/json"
"io/ioutil"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/elys-network/elys/x/perpetual/types"
"github.com/spf13/cobra"
)

var _ = strconv.Itoa(0)

func CmdClosePositions() *cobra.Command {
cmd := &cobra.Command{
Use: "close-positions [liquidate] [stoploss]",
Short: "Broadcast message close-positions",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

liquidate, err := readPositionRequestJSON(args[0])
if err != nil {
return err
}
// Convert to slice of pointers
var liqudiatePtrs []*types.PositionRequest
for i := range liquidate {
liqudiatePtrs = append(liqudiatePtrs, &liquidate[i])
}

stopLoss, err := readPositionRequestJSON(args[1])
if err != nil {
return err
}
// Convert to slice of pointers
var stoplossPtrs []*types.PositionRequest
for i := range stopLoss {
stoplossPtrs = append(stoplossPtrs, &stopLoss[i])
}

msg := types.NewMsgClosePositions(
clientCtx.GetFromAddress().String(),
liqudiatePtrs,
stoplossPtrs,
)
if err = msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

func readPositionRequestJSON(filename string) ([]types.PositionRequest, error) {
var positions []types.PositionRequest
bz, err := ioutil.ReadFile(filename)
if err != nil {
return []types.PositionRequest{}, err
}
err = json.Unmarshal(bz, &positions)
if err != nil {
return []types.PositionRequest{}, err
}

return positions, nil
}
4 changes: 2 additions & 2 deletions x/perpetual/keeper/add_collateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (k Keeper) AddCollateralToMtp(ctx sdk.Context, msg *types.MsgAddCollateral)
return nil, err
}

h, err := k.UpdateMTPHealth(ctx, mtp, ammPool, baseCurrency) // set mtp in func or return h?
h, err := k.GetMTPHealth(ctx, mtp, ammPool, baseCurrency) // set mtp in func or return h?
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func (k Keeper) AddCollateralToMtp(ctx sdk.Context, msg *types.MsgAddCollateral)
}

// Update the MTP health.
lr, err := k.OpenLongChecker.UpdateMTPHealth(ctx, mtp, ammPool, baseCurrency)
lr, err := k.OpenLongChecker.GetMTPHealth(ctx, mtp, ammPool, baseCurrency)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading