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

[TRA-507] Add new gov message for upgrading markets: UpgradeIsolatedPerpetualToCross #2551

Merged
merged 30 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 23 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Rpc } from "../../helpers";
import * as _m0 from "protobufjs/minimal";
import { MsgSetMarketsHardCap, MsgSetMarketsHardCapResponse, MsgCreateMarketPermissionless, MsgCreateMarketPermissionlessResponse, MsgSetListingVaultDepositParams, MsgSetListingVaultDepositParamsResponse } from "./tx";
import { MsgSetMarketsHardCap, MsgSetMarketsHardCapResponse, MsgCreateMarketPermissionless, MsgCreateMarketPermissionlessResponse, MsgSetListingVaultDepositParams, MsgSetListingVaultDepositParamsResponse, MsgUpgradeIsolatedPerpetualToCross, MsgUpgradeIsolatedPerpetualToCrossResponse } from "./tx";
/** Msg defines the Msg service. */

export interface Msg {
Expand All @@ -12,6 +12,12 @@ export interface Msg {
/** SetListingVaultDepositParams sets PML megavault deposit params */

setListingVaultDepositParams(request: MsgSetListingVaultDepositParams): Promise<MsgSetListingVaultDepositParamsResponse>;
/**
* UpgradeIsolatedPerpetualToCross upgrades a perpetual from isolated to cross
* margin
*/

upgradeIsolatedPerpetualToCross(request: MsgUpgradeIsolatedPerpetualToCross): Promise<MsgUpgradeIsolatedPerpetualToCrossResponse>;
}
export class MsgClientImpl implements Msg {
private readonly rpc: Rpc;
Expand All @@ -21,6 +27,7 @@ export class MsgClientImpl implements Msg {
this.setMarketsHardCap = this.setMarketsHardCap.bind(this);
this.createMarketPermissionless = this.createMarketPermissionless.bind(this);
this.setListingVaultDepositParams = this.setListingVaultDepositParams.bind(this);
this.upgradeIsolatedPerpetualToCross = this.upgradeIsolatedPerpetualToCross.bind(this);
}

setMarketsHardCap(request: MsgSetMarketsHardCap): Promise<MsgSetMarketsHardCapResponse> {
Expand All @@ -41,4 +48,10 @@ export class MsgClientImpl implements Msg {
return promise.then(data => MsgSetListingVaultDepositParamsResponse.decode(new _m0.Reader(data)));
}

upgradeIsolatedPerpetualToCross(request: MsgUpgradeIsolatedPerpetualToCross): Promise<MsgUpgradeIsolatedPerpetualToCrossResponse> {
const data = MsgUpgradeIsolatedPerpetualToCross.encode(request).finish();
const promise = this.rpc.request("dydxprotocol.listing.Msg", "UpgradeIsolatedPerpetualToCross", data);
return promise.then(data => MsgUpgradeIsolatedPerpetualToCrossResponse.decode(new _m0.Reader(data)));
}

}
123 changes: 123 additions & 0 deletions indexer/packages/v4-protos/src/codegen/dydxprotocol/listing/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,40 @@ export interface MsgSetListingVaultDepositParamsResponse {}
*/

export interface MsgSetListingVaultDepositParamsResponseSDKType {}
/**
* MsgUpgradeIsolatedPerpetualToCross is used to upgrade a market from
* isolated margin to cross margin.
*/

export interface MsgUpgradeIsolatedPerpetualToCross {
authority: string;
/** ID of the perpetual to be upgraded to CROSS */

perpetualId: number;
}
/**
* MsgUpgradeIsolatedPerpetualToCross is used to upgrade a market from
* isolated margin to cross margin.
*/

export interface MsgUpgradeIsolatedPerpetualToCrossSDKType {
authority: string;
/** ID of the perpetual to be upgraded to CROSS */

perpetual_id: number;
}
/**
* MsgUpgradeIsolatedPerpetualToCrossResponse defines the
* UpgradeIsolatedPerpetualToCross response type.
*/

export interface MsgUpgradeIsolatedPerpetualToCrossResponse {}
/**
* MsgUpgradeIsolatedPerpetualToCrossResponse defines the
* UpgradeIsolatedPerpetualToCross response type.
*/

export interface MsgUpgradeIsolatedPerpetualToCrossResponseSDKType {}

function createBaseMsgSetMarketsHardCap(): MsgSetMarketsHardCap {
return {
Expand Down Expand Up @@ -366,4 +400,93 @@ export const MsgSetListingVaultDepositParamsResponse = {
return message;
}

};

function createBaseMsgUpgradeIsolatedPerpetualToCross(): MsgUpgradeIsolatedPerpetualToCross {
return {
authority: "",
perpetualId: 0
};
}

export const MsgUpgradeIsolatedPerpetualToCross = {
encode(message: MsgUpgradeIsolatedPerpetualToCross, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.authority !== "") {
writer.uint32(10).string(message.authority);
}

if (message.perpetualId !== 0) {
writer.uint32(16).uint32(message.perpetualId);
}

return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): MsgUpgradeIsolatedPerpetualToCross {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseMsgUpgradeIsolatedPerpetualToCross();

while (reader.pos < end) {
const tag = reader.uint32();

switch (tag >>> 3) {
case 1:
message.authority = reader.string();
break;

case 2:
message.perpetualId = reader.uint32();
break;

default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(object: DeepPartial<MsgUpgradeIsolatedPerpetualToCross>): MsgUpgradeIsolatedPerpetualToCross {
const message = createBaseMsgUpgradeIsolatedPerpetualToCross();
message.authority = object.authority ?? "";
message.perpetualId = object.perpetualId ?? 0;
return message;
}

};

function createBaseMsgUpgradeIsolatedPerpetualToCrossResponse(): MsgUpgradeIsolatedPerpetualToCrossResponse {
return {};
}

export const MsgUpgradeIsolatedPerpetualToCrossResponse = {
encode(_: MsgUpgradeIsolatedPerpetualToCrossResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): MsgUpgradeIsolatedPerpetualToCrossResponse {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseMsgUpgradeIsolatedPerpetualToCrossResponse();

while (reader.pos < end) {
const tag = reader.uint32();

switch (tag >>> 3) {
default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(_: DeepPartial<MsgUpgradeIsolatedPerpetualToCrossResponse>): MsgUpgradeIsolatedPerpetualToCrossResponse {
const message = createBaseMsgUpgradeIsolatedPerpetualToCrossResponse();
return message;
}

};
23 changes: 22 additions & 1 deletion proto/dydxprotocol/listing/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ service Msg {
// SetListingVaultDepositParams sets PML megavault deposit params
rpc SetListingVaultDepositParams(MsgSetListingVaultDepositParams)
returns (MsgSetListingVaultDepositParamsResponse);

// UpgradeIsolatedPerpetualToCross upgrades a perpetual from isolated to cross
// margin
rpc UpgradeIsolatedPerpetualToCross(MsgUpgradeIsolatedPerpetualToCross)
returns (MsgUpgradeIsolatedPerpetualToCrossResponse);
}

// MsgSetMarketsHardCap is used to set a hard cap on the number of markets
Expand Down Expand Up @@ -69,4 +74,20 @@ message MsgSetListingVaultDepositParams {

// MsgSetListingVaultDepositParamsResponse defines the
// MsgSetListingVaultDepositParams response
message MsgSetListingVaultDepositParamsResponse {}
message MsgSetListingVaultDepositParamsResponse {}

// MsgUpgradeIsolatedPerpetualToCross is used to upgrade a market from
// isolated margin to cross margin.
message MsgUpgradeIsolatedPerpetualToCross {
// Authority is the address that controls the module.
option (cosmos.msg.v1.signer) = "authority";

string authority = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];

// ID of the perpetual to be upgraded to CROSS
uint32 perpetual_id = 2;
}

// MsgUpgradeIsolatedPerpetualToCrossResponse defines the
// UpgradeIsolatedPerpetualToCross response type.
message MsgUpgradeIsolatedPerpetualToCrossResponse {}
1 change: 1 addition & 0 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,7 @@ func New(
app.ClobKeeper,
&app.MarketMapKeeper,
app.PerpetualsKeeper,
app.SubaccountsKeeper,
app.VaultKeeper,
)
listingModule := listingmodule.NewAppModule(
Expand Down
14 changes: 8 additions & 6 deletions protocol/app/msgs/all_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,14 @@ var (
"/dydxprotocol.govplus.MsgSlashValidatorResponse": {},

// listing
"/dydxprotocol.listing.MsgSetMarketsHardCap": {},
"/dydxprotocol.listing.MsgSetMarketsHardCapResponse": {},
"/dydxprotocol.listing.MsgCreateMarketPermissionless": {},
"/dydxprotocol.listing.MsgCreateMarketPermissionlessResponse": {},
"/dydxprotocol.listing.MsgSetListingVaultDepositParams": {},
"/dydxprotocol.listing.MsgSetListingVaultDepositParamsResponse": {},
"/dydxprotocol.listing.MsgSetMarketsHardCap": {},
"/dydxprotocol.listing.MsgSetMarketsHardCapResponse": {},
"/dydxprotocol.listing.MsgCreateMarketPermissionless": {},
"/dydxprotocol.listing.MsgCreateMarketPermissionlessResponse": {},
"/dydxprotocol.listing.MsgSetListingVaultDepositParams": {},
"/dydxprotocol.listing.MsgSetListingVaultDepositParamsResponse": {},
"/dydxprotocol.listing.MsgUpgradeIsolatedPerpetualToCross": {},
"/dydxprotocol.listing.MsgUpgradeIsolatedPerpetualToCrossResponse": {},

// perpetuals
"/dydxprotocol.perpetuals.MsgAddPremiumVotes": {},
Expand Down
6 changes: 6 additions & 0 deletions protocol/app/msgs/unsupported_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
gov "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govbeta "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
icacontrollertypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/types"
listingtypes "github.com/dydxprotocol/v4-chain/protocol/x/listing/types"
vaulttypes "github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
)

Expand All @@ -30,6 +31,11 @@ var (
MsgUpdateParams{},
"/ibc.applications.interchain_accounts.controller.v1.MsgUpdateParamsResponse": nil,

// WIP
"/dydxprotocol.listing.MsgUpgradeIsolatedPerpetualToCross": &listingtypes.
MsgUpgradeIsolatedPerpetualToCross{},
"/dydxprotocol.listing.MsgUpgradeIsolatedPerpetualToCrossResponse": nil,

// vault
// MsgSetVaultQuotingParams is deprecated since v6.x and replaced by MsgSetVaultParams.
// nolint:staticcheck
Expand Down
4 changes: 4 additions & 0 deletions protocol/app/msgs/unsupported_msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func TestUnsupportedMsgSamples_Key(t *testing.T) {
"/dydxprotocol.vault.MsgSetVaultQuotingParams",
"/dydxprotocol.vault.MsgUpdateParams",

// WIP
"/dydxprotocol.listing.MsgUpgradeIsolatedPerpetualToCross",
"/dydxprotocol.listing.MsgUpgradeIsolatedPerpetualToCrossResponse",
hwray marked this conversation as resolved.
Show resolved Hide resolved

// ICA Controller messages
"/ibc.applications.interchain_accounts.controller.v1.MsgRegisterInterchainAccount",
"/ibc.applications.interchain_accounts.controller.v1.MsgRegisterInterchainAccountResponse",
Expand Down
20 changes: 0 additions & 20 deletions protocol/mocks/ClobKeeper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions protocol/testutil/keeper/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
clobkeeper "github.com/dydxprotocol/v4-chain/protocol/x/clob/keeper"
perpetualskeeper "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/keeper"
priceskeeper "github.com/dydxprotocol/v4-chain/protocol/x/prices/keeper"
subaccountskeeper "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/keeper"
vaultkeeper "github.com/dydxprotocol/v4-chain/protocol/x/vault/keeper"
marketmapkeeper "github.com/skip-mev/connect/v2/x/marketmap/keeper"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -173,6 +174,7 @@ func ListingKeepers(
perpetualsKeeper,
clobKeeper,
marketMapKeeper,
subaccountsKeeper,
vaultKeeper,
)

Expand All @@ -191,6 +193,7 @@ func createListingKeeper(
perpetualsKeeper *perpetualskeeper.Keeper,
clobKeeper *clobkeeper.Keeper,
marketMapKeeper *marketmapkeeper.Keeper,
subaccountsKeeper *subaccountskeeper.Keeper,
vaultkeeper *vaultkeeper.Keeper,
) (
*keeper.Keeper,
Expand All @@ -211,6 +214,7 @@ func createListingKeeper(
clobKeeper,
marketMapKeeper,
perpetualsKeeper,
subaccountsKeeper,
vaultkeeper,
)

Expand Down
40 changes: 1 addition & 39 deletions protocol/x/clob/keeper/deleveraging.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
assettypes "github.com/dydxprotocol/v4-chain/protocol/x/assets/types"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
perplib "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/lib"
perptypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
)

Expand Down Expand Up @@ -130,43 +129,6 @@ func (k Keeper) MaybeDeleverageSubaccount(
return quantumsDeleveraged, err
}

// GetInsuranceFundBalance returns the current balance of the specific insurance fund based on the
// perpetual (in quote quantums).
// This calls the Bank Keeper’s GetBalance() function for the Module Address of the insurance fund.
func (k Keeper) GetInsuranceFundBalance(ctx sdk.Context, perpetualId uint32) (balance *big.Int) {
usdcAsset, exists := k.assetsKeeper.GetAsset(ctx, assettypes.AssetUsdc.Id)
if !exists {
panic("GetInsuranceFundBalance: Usdc asset not found in state")
}
insuranceFundAddr, err := k.perpetualsKeeper.GetInsuranceFundModuleAddress(ctx, perpetualId)
if err != nil {
return nil
}
insuranceFundBalance := k.bankKeeper.GetBalance(
ctx,
insuranceFundAddr,
usdcAsset.Denom,
)

// Return as big.Int.
return insuranceFundBalance.Amount.BigInt()
}

func (k Keeper) GetCrossInsuranceFundBalance(ctx sdk.Context) (balance *big.Int) {
usdcAsset, exists := k.assetsKeeper.GetAsset(ctx, assettypes.AssetUsdc.Id)
if !exists {
panic("GetCrossInsuranceFundBalance: Usdc asset not found in state")
}
insuranceFundBalance := k.bankKeeper.GetBalance(
ctx,
perptypes.InsuranceFundModuleAddress,
usdcAsset.Denom,
)

// Return as big.Int.
return insuranceFundBalance.Amount.BigInt()
}

// CanDeleverageSubaccount returns true if a subaccount can be deleveraged.
// This function returns two booleans, shouldDeleverageAtBankruptcyPrice and shouldDeleverageAtOraclePrice.
// - shouldDeleverageAtBankruptcyPrice is true if the subaccount has negative TNC.
Expand Down Expand Up @@ -279,7 +241,7 @@ func (k Keeper) IsValidInsuranceFundDelta(ctx sdk.Context, insuranceFundDelta *b

// The insurance fund delta is valid if the insurance fund balance is non-negative after adding
// the delta.
currentInsuranceFundBalance := k.GetInsuranceFundBalance(ctx, perpetualId)
currentInsuranceFundBalance := k.subaccountsKeeper.GetInsuranceFundBalance(ctx, perpetualId)
return new(big.Int).Add(currentInsuranceFundBalance, insuranceFundDelta).Sign() >= 0
}

Expand Down
Loading
Loading