diff --git a/indexer/packages/v4-protos/src/codegen/dydxprotocol/perpetuals/perpetual.ts b/indexer/packages/v4-protos/src/codegen/dydxprotocol/perpetuals/perpetual.ts index bde0abe52e..720980b76d 100644 --- a/indexer/packages/v4-protos/src/codegen/dydxprotocol/perpetuals/perpetual.ts +++ b/indexer/packages/v4-protos/src/codegen/dydxprotocol/perpetuals/perpetual.ts @@ -69,6 +69,9 @@ export interface Perpetual { */ fundingIndex: Uint8Array; + /** Total size of open long contracts, measured in base_quantums. */ + + openInterest: Uint8Array; } /** Perpetual represents a perpetual on the dYdX exchange. */ @@ -81,6 +84,9 @@ export interface PerpetualSDKType { */ funding_index: Uint8Array; + /** Total size of open long contracts, measured in base_quantums. */ + + open_interest: Uint8Array; } /** * PerpetualParams represents the parameters of a perpetual on the dYdX @@ -280,6 +286,19 @@ export interface LiquidityTier { */ impactNotional: Long; + /** + * Lower cap for Open Interest Margin Fracton (OIMF), in quote quantums. + * IMF is not affected when OI <= open_interest_lower_cap. + */ + + openInterestLowerCap: Long; + /** + * Upper cap for Open Interest Margin Fracton (OIMF), in quote quantums. + * IMF scales linearly to 100% as OI approaches open_interest_upper_cap. + * If zero, then the IMF does not scale with OI. + */ + + openInterestUpperCap: Long; } /** LiquidityTier stores margin information. */ @@ -323,12 +342,26 @@ export interface LiquidityTierSDKType { */ impact_notional: Long; + /** + * Lower cap for Open Interest Margin Fracton (OIMF), in quote quantums. + * IMF is not affected when OI <= open_interest_lower_cap. + */ + + open_interest_lower_cap: Long; + /** + * Upper cap for Open Interest Margin Fracton (OIMF), in quote quantums. + * IMF scales linearly to 100% as OI approaches open_interest_upper_cap. + * If zero, then the IMF does not scale with OI. + */ + + open_interest_upper_cap: Long; } function createBasePerpetual(): Perpetual { return { params: undefined, - fundingIndex: new Uint8Array() + fundingIndex: new Uint8Array(), + openInterest: new Uint8Array() }; } @@ -342,6 +375,10 @@ export const Perpetual = { writer.uint32(18).bytes(message.fundingIndex); } + if (message.openInterest.length !== 0) { + writer.uint32(26).bytes(message.openInterest); + } + return writer; }, @@ -362,6 +399,10 @@ export const Perpetual = { message.fundingIndex = reader.bytes(); break; + case 3: + message.openInterest = reader.bytes(); + break; + default: reader.skipType(tag & 7); break; @@ -375,6 +416,7 @@ export const Perpetual = { const message = createBasePerpetual(); message.params = object.params !== undefined && object.params !== null ? PerpetualParams.fromPartial(object.params) : undefined; message.fundingIndex = object.fundingIndex ?? new Uint8Array(); + message.openInterest = object.openInterest ?? new Uint8Array(); return message; } @@ -614,7 +656,9 @@ function createBaseLiquidityTier(): LiquidityTier { initialMarginPpm: 0, maintenanceFractionPpm: 0, basePositionNotional: Long.UZERO, - impactNotional: Long.UZERO + impactNotional: Long.UZERO, + openInterestLowerCap: Long.UZERO, + openInterestUpperCap: Long.UZERO }; } @@ -644,6 +688,14 @@ export const LiquidityTier = { writer.uint32(48).uint64(message.impactNotional); } + if (!message.openInterestLowerCap.isZero()) { + writer.uint32(56).uint64(message.openInterestLowerCap); + } + + if (!message.openInterestUpperCap.isZero()) { + writer.uint32(64).uint64(message.openInterestUpperCap); + } + return writer; }, @@ -680,6 +732,14 @@ export const LiquidityTier = { message.impactNotional = (reader.uint64() as Long); break; + case 7: + message.openInterestLowerCap = (reader.uint64() as Long); + break; + + case 8: + message.openInterestUpperCap = (reader.uint64() as Long); + break; + default: reader.skipType(tag & 7); break; @@ -697,6 +757,8 @@ export const LiquidityTier = { message.maintenanceFractionPpm = object.maintenanceFractionPpm ?? 0; message.basePositionNotional = object.basePositionNotional !== undefined && object.basePositionNotional !== null ? Long.fromValue(object.basePositionNotional) : Long.UZERO; message.impactNotional = object.impactNotional !== undefined && object.impactNotional !== null ? Long.fromValue(object.impactNotional) : Long.UZERO; + message.openInterestLowerCap = object.openInterestLowerCap !== undefined && object.openInterestLowerCap !== null ? Long.fromValue(object.openInterestLowerCap) : Long.UZERO; + message.openInterestUpperCap = object.openInterestUpperCap !== undefined && object.openInterestUpperCap !== null ? Long.fromValue(object.openInterestUpperCap) : Long.UZERO; return message; } diff --git a/proto/dydxprotocol/perpetuals/perpetual.proto b/proto/dydxprotocol/perpetuals/perpetual.proto index c58188f0bb..cb5132ea0f 100644 --- a/proto/dydxprotocol/perpetuals/perpetual.proto +++ b/proto/dydxprotocol/perpetuals/perpetual.proto @@ -17,6 +17,13 @@ message Perpetual { "github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt", (gogoproto.nullable) = false ]; + + // Total size of open long contracts, measured in base_quantums. + bytes open_interest = 3 [ + (gogoproto.customtype) = + "github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt", + (gogoproto.nullable) = false + ]; } enum PerpetualMarketType { @@ -120,4 +127,13 @@ message LiquidityTier { // - Impact ask price = average execution price for a market buy of the // impact notional value. uint64 impact_notional = 6; + + // Lower cap for Open Interest Margin Fracton (OIMF), in quote quantums. + // IMF is not affected when OI <= open_interest_lower_cap. + uint64 open_interest_lower_cap = 7; + + // Upper cap for Open Interest Margin Fracton (OIMF), in quote quantums. + // IMF scales linearly to 100% as OI approaches open_interest_upper_cap. + // If zero, then the IMF does not scale with OI. + uint64 open_interest_upper_cap = 8; } diff --git a/protocol/mocks/PerpetualsKeeper.go b/protocol/mocks/PerpetualsKeeper.go index 921b9a486e..46a4afac45 100644 --- a/protocol/mocks/PerpetualsKeeper.go +++ b/protocol/mocks/PerpetualsKeeper.go @@ -245,23 +245,23 @@ func (_m *PerpetualsKeeper) PerformStatefulPremiumVotesValidation(ctx types.Cont return r0 } -// SetLiquidityTier provides a mock function with given fields: ctx, id, name, initialMarginPpm, maintenanceFractionPpm, impactNotional -func (_m *PerpetualsKeeper) SetLiquidityTier(ctx types.Context, id uint32, name string, initialMarginPpm uint32, maintenanceFractionPpm uint32, impactNotional uint64) (perpetualstypes.LiquidityTier, error) { - ret := _m.Called(ctx, id, name, initialMarginPpm, maintenanceFractionPpm, impactNotional) +// SetLiquidityTier provides a mock function with given fields: ctx, id, name, initialMarginPpm, maintenanceFractionPpm, impactNotional, openInterestLowerCap, openInterestUpperCap +func (_m *PerpetualsKeeper) SetLiquidityTier(ctx types.Context, id uint32, name string, initialMarginPpm uint32, maintenanceFractionPpm uint32, impactNotional uint64, openInterestLowerCap uint64, openInterestUpperCap uint64) (perpetualstypes.LiquidityTier, error) { + ret := _m.Called(ctx, id, name, initialMarginPpm, maintenanceFractionPpm, impactNotional, openInterestLowerCap, openInterestUpperCap) var r0 perpetualstypes.LiquidityTier var r1 error - if rf, ok := ret.Get(0).(func(types.Context, uint32, string, uint32, uint32, uint64) (perpetualstypes.LiquidityTier, error)); ok { - return rf(ctx, id, name, initialMarginPpm, maintenanceFractionPpm, impactNotional) + if rf, ok := ret.Get(0).(func(types.Context, uint32, string, uint32, uint32, uint64, uint64, uint64) (perpetualstypes.LiquidityTier, error)); ok { + return rf(ctx, id, name, initialMarginPpm, maintenanceFractionPpm, impactNotional, openInterestLowerCap, openInterestUpperCap) } - if rf, ok := ret.Get(0).(func(types.Context, uint32, string, uint32, uint32, uint64) perpetualstypes.LiquidityTier); ok { - r0 = rf(ctx, id, name, initialMarginPpm, maintenanceFractionPpm, impactNotional) + if rf, ok := ret.Get(0).(func(types.Context, uint32, string, uint32, uint32, uint64, uint64, uint64) perpetualstypes.LiquidityTier); ok { + r0 = rf(ctx, id, name, initialMarginPpm, maintenanceFractionPpm, impactNotional, openInterestLowerCap, openInterestUpperCap) } else { r0 = ret.Get(0).(perpetualstypes.LiquidityTier) } - if rf, ok := ret.Get(1).(func(types.Context, uint32, string, uint32, uint32, uint64) error); ok { - r1 = rf(ctx, id, name, initialMarginPpm, maintenanceFractionPpm, impactNotional) + if rf, ok := ret.Get(1).(func(types.Context, uint32, string, uint32, uint32, uint64, uint64, uint64) error); ok { + r1 = rf(ctx, id, name, initialMarginPpm, maintenanceFractionPpm, impactNotional, openInterestLowerCap, openInterestUpperCap) } else { r1 = ret.Error(1) } diff --git a/protocol/scripts/genesis/sample_pregenesis.json b/protocol/scripts/genesis/sample_pregenesis.json index 57b6375850..ac04970e33 100644 --- a/protocol/scripts/genesis/sample_pregenesis.json +++ b/protocol/scripts/genesis/sample_pregenesis.json @@ -862,7 +862,9 @@ "impact_notional": 10000000000, "initial_margin_ppm": 50000, "maintenance_fraction_ppm": 600000, - "name": "Large-Cap" + "name": "Large-Cap", + "open_interest_lower_cap": 0, + "open_interest_upper_cap": 0 }, { "base_position_notional": 250000000000, @@ -870,7 +872,9 @@ "impact_notional": 5000000000, "initial_margin_ppm": 100000, "maintenance_fraction_ppm": 500000, - "name": "Mid-Cap" + "name": "Mid-Cap", + "open_interest_lower_cap": 25000000000000, + "open_interest_upper_cap": 50000000000000 }, { "base_position_notional": 100000000000, @@ -878,7 +882,9 @@ "impact_notional": 2500000000, "initial_margin_ppm": 200000, "maintenance_fraction_ppm": 500000, - "name": "Long-Tail" + "name": "Long-Tail", + "open_interest_lower_cap": 10000000000000, + "open_interest_upper_cap": 20000000000000 }, { "base_position_notional": 1000000000, @@ -886,7 +892,9 @@ "impact_notional": 2500000000, "initial_margin_ppm": 1000000, "maintenance_fraction_ppm": 200000, - "name": "Safety" + "name": "Safety", + "open_interest_lower_cap": 0, + "open_interest_upper_cap": 0 } ], "params": { @@ -1816,7 +1824,7 @@ ] } }, - "app_version": "4.0.0-dev0-59-g5530ea29", + "app_version": "4.0.0-dev0-62-g95853be5", "chain_id": "dydx-sample-1", "consensus": { "params": { diff --git a/protocol/testing/genesis.sh b/protocol/testing/genesis.sh index 452ad0ae0e..2c9b065f98 100755 --- a/protocol/testing/genesis.sh +++ b/protocol/testing/genesis.sh @@ -125,6 +125,7 @@ function edit_genesis() { # Update perpetuals module. # Liquidity Tiers. + # TODO(OTE-208): Finalize default values for open interest caps. dasel put -t json -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers' -v "[]" # Liquidity Tier: Large-Cap dasel put -t json -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[]' -v "{}" @@ -134,6 +135,8 @@ function edit_genesis() { dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[0].maintenance_fraction_ppm' -v '600000' # 60% of IM dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[0].base_position_notional' -v '1000000000000' # 1_000_000 USDC dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[0].impact_notional' -v '10000000000' # 10_000 USDC (500 USDC / 5%) + dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[0].open_interest_lower_cap' -v '0' # OIMF doesn't apply to Large-Cap + dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[0].open_interest_upper_cap' -v '0' # OIMF doesn't apply to Large-Cap # Liquidity Tier: Mid-Cap dasel put -t json -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[]' -v "{}" @@ -143,6 +146,8 @@ function edit_genesis() { dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[1].maintenance_fraction_ppm' -v '500000' # 50% of IM dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[1].base_position_notional' -v '250000000000' # 250_000 USDC dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[1].impact_notional' -v '5000000000' # 5_000 USDC (500 USDC / 10%) + dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[1].open_interest_lower_cap' -v '25000000000000' # 25 million USDC + dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[1].open_interest_upper_cap' -v '50000000000000' # 50 million USDC # Liquidity Tier: Long-Tail dasel put -t json -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[]' -v "{}" @@ -152,6 +157,8 @@ function edit_genesis() { dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[2].maintenance_fraction_ppm' -v '500000' # 50% of IM dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[2].base_position_notional' -v '100000000000' # 100_000 USDC dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[2].impact_notional' -v '2500000000' # 2_500 USDC (500 USDC / 20%) + dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[2].open_interest_lower_cap' -v '10000000000000' # 10 million USDC + dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[2].open_interest_upper_cap' -v '20000000000000' # 20 million USDC # Liquidity Tier: Safety dasel put -t json -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[]' -v "{}" @@ -161,6 +168,10 @@ function edit_genesis() { dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[3].maintenance_fraction_ppm' -v '200000' # 20% of IM dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[3].base_position_notional' -v '1000000000' # 1_000 USDC dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[3].impact_notional' -v '2500000000' # 2_500 USDC (2_500 USDC / 100%) + # OIMF doesn't apply to `Safety`, since IMF is already at 100%. + dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[3].open_interest_lower_cap' -v '0' + dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[3].open_interest_upper_cap' -v '0' + # Params. dasel put -t int -f "$GENESIS" '.app_state.perpetuals.params.funding_rate_clamp_factor_ppm' -v '6000000' # 600 % (same as 75% on hourly rate) diff --git a/protocol/testutil/constants/perpetuals.go b/protocol/testutil/constants/perpetuals.go index 3e55df8e80..2069fe0e65 100644 --- a/protocol/testutil/constants/perpetuals.go +++ b/protocol/testutil/constants/perpetuals.go @@ -118,6 +118,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } BtcUsd_0DefaultFunding_0AtomicResolution = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -130,6 +131,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } BtcUsd_NegativeDefaultFunding_10AtomicResolution = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -142,6 +144,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } BtcUsd_0DefaultFunding_10AtomicResolution = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -154,6 +157,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } BtcUsd_0DefaultFunding_10AtomicResolution_20IM_18MM = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -166,6 +170,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } BtcUsd_0_001Percent_DefaultFunding_10AtomicResolution = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -178,6 +183,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } BtcUsd_SmallMarginRequirement = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -190,6 +196,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } BtcUsd_100PercentMarginRequirement = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -202,6 +209,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } BtcUsd_50PercentInitial_40PercentMaintenance = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -214,6 +222,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } BtcUsd_20PercentInitial_10PercentMaintenance = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -226,6 +235,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } BtcUsd_NoMarginRequirement = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -238,6 +248,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } EthUsd_0DefaultFunding_9AtomicResolution = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -250,6 +261,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } EthUsd_NoMarginRequirement = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -262,6 +274,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } EthUsd_20PercentInitial_10PercentMaintenance = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -274,6 +287,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } EthUsd_100PercentMarginRequirement = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -286,6 +300,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } SolUsd_20PercentInitial_10PercentMaintenance = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -298,6 +313,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } IsoUsd_IsolatedMarket = perptypes.Perpetual{ Params: perptypes.PerpetualParams{ @@ -391,6 +407,8 @@ var ( InitialMarginPpm: 200_000, MaintenanceFractionPpm: 500_000, ImpactNotional: 2_500_000_000, + OpenInterestLowerCap: 0, + OpenInterestUpperCap: 0, }, { Id: uint32(1), @@ -398,6 +416,8 @@ var ( InitialMarginPpm: 300_000, MaintenanceFractionPpm: 600_000, ImpactNotional: 1_667_000_000, + OpenInterestLowerCap: 25_000_000_000_000, + OpenInterestUpperCap: 50_000_000_000_000, }, { Id: uint32(2), @@ -405,6 +425,8 @@ var ( InitialMarginPpm: 400_000, MaintenanceFractionPpm: 700_000, ImpactNotional: 1_250_000_000, + OpenInterestLowerCap: 10_000_000_000_000, + OpenInterestUpperCap: 20_000_000_000_000, }, }, Params: PerpetualsGenesisParams, @@ -417,6 +439,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), }, { Params: perptypes.PerpetualParams{ @@ -426,6 +449,7 @@ var ( MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), }, }, } diff --git a/protocol/testutil/keeper/perpetuals.go b/protocol/testutil/keeper/perpetuals.go index 622ffc25d2..851926a66d 100644 --- a/protocol/testutil/keeper/perpetuals.go +++ b/protocol/testutil/keeper/perpetuals.go @@ -192,6 +192,8 @@ func CreateTestLiquidityTiers(t *testing.T, ctx sdk.Context, k *keeper.Keeper) { l.InitialMarginPpm, l.MaintenanceFractionPpm, l.ImpactNotional, + l.OpenInterestLowerCap, + l.OpenInterestUpperCap, ) require.NoError(t, err) diff --git a/protocol/testutil/perpetuals/perpetuals.go b/protocol/testutil/perpetuals/perpetuals.go index 451da22b7b..dbcce92229 100644 --- a/protocol/testutil/perpetuals/perpetuals.go +++ b/protocol/testutil/perpetuals/perpetuals.go @@ -67,6 +67,7 @@ func GeneratePerpetual(optionalModifications ...PerpetualModifierOption) *perpty MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } for _, opt := range optionalModifications { diff --git a/protocol/x/perpetuals/client/cli/query_perpetual_test.go b/protocol/x/perpetuals/client/cli/query_perpetual_test.go index 09d532dce2..a09224f2db 100644 --- a/protocol/x/perpetuals/client/cli/query_perpetual_test.go +++ b/protocol/x/perpetuals/client/cli/query_perpetual_test.go @@ -24,6 +24,19 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" ) +var ( + serializableIntCmpOpts = cmp.Options{ + cmpopts.IgnoreFields(types.Perpetual{}, "FundingIndex"), // existing ignore option + cmp.FilterValues( + func(x, y dtypes.SerializableInt) bool { + // This will apply the custom comparer only to SerializableInt fields + return true // Apply this filter to all SerializableInt values + }, + cmp.Comparer(func(x, y dtypes.SerializableInt) bool { return x.Cmp(y) == 0 }), + ), + } +) + func networkWithLiquidityTierAndPerpetualObjects( t *testing.T, m int, @@ -75,6 +88,7 @@ func networkWithLiquidityTierAndPerpetualObjects( MarketType: marketType, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } nullify.Fill(&perpetual) //nolint:staticcheck state.Perpetuals = append(state.Perpetuals, perpetual) @@ -142,7 +156,7 @@ func TestShowPerpetual(t *testing.T) { // FundingIndex field is ignored since it can vary depending on funding-tick epoch. // TODO(DEC-606): Improve end-to-end testing related to ticking epochs. func checkExpectedPerp(t *testing.T, expected types.Perpetual, received types.Perpetual) { - if diff := cmp.Diff(expected, received, cmpopts.IgnoreFields(types.Perpetual{}, "FundingIndex")); diff != "" { + if diff := cmp.Diff(expected, received, serializableIntCmpOpts...); diff != "" { t.Errorf("resp.Perpetual mismatch (-want +received):\n%s", diff) } } @@ -215,12 +229,13 @@ func TestListPerpetual(t *testing.T) { require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) require.Equal(t, len(objs), int(resp.Pagination.Total)) - cmpOptions := []cmp.Option{ - cmpopts.IgnoreFields(types.Perpetual{}, "FundingIndex"), + cmpOptions := append( + serializableIntCmpOpts, cmpopts.SortSlices(func(x, y types.Perpetual) bool { return x.Params.Id > y.Params.Id }), - } + ) + if diff := cmp.Diff(objs, resp.Perpetual, cmpOptions...); diff != "" { t.Errorf("resp.Perpetual mismatch (-want +received):\n%s", diff) } diff --git a/protocol/x/perpetuals/genesis.go b/protocol/x/perpetuals/genesis.go index fc9e4c9ab6..db9650d916 100644 --- a/protocol/x/perpetuals/genesis.go +++ b/protocol/x/perpetuals/genesis.go @@ -26,6 +26,8 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) elem.InitialMarginPpm, elem.MaintenanceFractionPpm, elem.ImpactNotional, + elem.OpenInterestLowerCap, + elem.OpenInterestUpperCap, ) if err != nil { diff --git a/protocol/x/perpetuals/keeper/msg_server_set_liquidity_tier.go b/protocol/x/perpetuals/keeper/msg_server_set_liquidity_tier.go index e2be739d82..a2e6e75f89 100644 --- a/protocol/x/perpetuals/keeper/msg_server_set_liquidity_tier.go +++ b/protocol/x/perpetuals/keeper/msg_server_set_liquidity_tier.go @@ -30,6 +30,8 @@ func (k msgServer) SetLiquidityTier( msg.LiquidityTier.InitialMarginPpm, msg.LiquidityTier.MaintenanceFractionPpm, msg.LiquidityTier.ImpactNotional, + msg.LiquidityTier.OpenInterestLowerCap, + msg.LiquidityTier.OpenInterestUpperCap, ); err != nil { return nil, err } diff --git a/protocol/x/perpetuals/keeper/msg_server_set_liquidity_tier_test.go b/protocol/x/perpetuals/keeper/msg_server_set_liquidity_tier_test.go index 111aff9d7a..cf60802833 100644 --- a/protocol/x/perpetuals/keeper/msg_server_set_liquidity_tier_test.go +++ b/protocol/x/perpetuals/keeper/msg_server_set_liquidity_tier_test.go @@ -114,6 +114,21 @@ func TestSetLiquidityTier(t *testing.T) { }, expectedErr: "invalid authority", }, + "Failure: invalid open interest caps": { + msg: &types.MsgSetLiquidityTier{ + Authority: lib.GovModuleAddress.String(), + LiquidityTier: types.LiquidityTier{ + Id: testLt.Id, + Name: "medium-cap", + InitialMarginPpm: 567_123, + MaintenanceFractionPpm: 500_001, + ImpactNotional: 1_300_303, + OpenInterestLowerCap: 100, + OpenInterestUpperCap: 50, + }, + }, + expectedErr: "open interest lower cap is larger than upper cap", + }, } for name, tc := range tests { @@ -126,6 +141,8 @@ func TestSetLiquidityTier(t *testing.T) { testLt.InitialMarginPpm, testLt.MaintenanceFractionPpm, testLt.ImpactNotional, + testLt.OpenInterestLowerCap, + testLt.OpenInterestUpperCap, ) require.NoError(t, err) diff --git a/protocol/x/perpetuals/keeper/perpetual.go b/protocol/x/perpetuals/keeper/perpetual.go index eda836e508..f80705a29c 100644 --- a/protocol/x/perpetuals/keeper/perpetual.go +++ b/protocol/x/perpetuals/keeper/perpetual.go @@ -100,6 +100,7 @@ func (k Keeper) CreatePerpetual( MarketType: marketType, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } if err := k.validatePerpetual( @@ -1406,6 +1407,8 @@ func (k Keeper) SetLiquidityTier( initialMarginPpm uint32, maintenanceFractionPpm uint32, impactNotional uint64, + openInterestLowerCap uint64, + openInterestUpperCap uint64, ) ( liquidityTier types.LiquidityTier, err error, @@ -1417,6 +1420,8 @@ func (k Keeper) SetLiquidityTier( InitialMarginPpm: initialMarginPpm, MaintenanceFractionPpm: maintenanceFractionPpm, ImpactNotional: impactNotional, + OpenInterestLowerCap: openInterestLowerCap, + OpenInterestUpperCap: openInterestUpperCap, } // Validate liquidity tier's fields. diff --git a/protocol/x/perpetuals/keeper/perpetual_test.go b/protocol/x/perpetuals/keeper/perpetual_test.go index 95e569892e..fbae544a07 100644 --- a/protocol/x/perpetuals/keeper/perpetual_test.go +++ b/protocol/x/perpetuals/keeper/perpetual_test.go @@ -685,6 +685,8 @@ func TestGetMarginRequirements_Success(t *testing.T) { tc.initialMarginPpm, tc.maintenanceFractionPpm, 1, // dummy impact notional value + 0, // dummy open interest lower cap + 0, // dummy open interest upper cap ) require.NoError(t, err) @@ -2825,6 +2827,8 @@ func TestGetAllLiquidityTiers_Sorted(t *testing.T) { lt.InitialMarginPpm, lt.MaintenanceFractionPpm, lt.ImpactNotional, + lt.OpenInterestLowerCap, + lt.OpenInterestUpperCap, ) require.NoError(t, err) } @@ -2863,6 +2867,8 @@ func TestHasLiquidityTier(t *testing.T) { lt.InitialMarginPpm, lt.MaintenanceFractionPpm, lt.ImpactNotional, + lt.OpenInterestLowerCap, + lt.OpenInterestUpperCap, ) require.NoError(t, err) } @@ -2887,6 +2893,8 @@ func TestCreateLiquidityTier_Success(t *testing.T) { lt.InitialMarginPpm, lt.MaintenanceFractionPpm, lt.ImpactNotional, + lt.OpenInterestLowerCap, + lt.OpenInterestUpperCap, ) require.NoError(t, err) @@ -2911,6 +2919,8 @@ func TestSetLiquidityTier_New_Failure(t *testing.T) { initialMarginPpm uint32 maintenanceFractionPpm uint32 impactNotional uint64 + openInterestLowerCap uint64 + openInterestUpperCap uint64 expectedError error }{ "Initial Margin Ppm exceeds maximum": { @@ -2919,6 +2929,8 @@ func TestSetLiquidityTier_New_Failure(t *testing.T) { initialMarginPpm: lib.OneMillion + 1, maintenanceFractionPpm: 500_000, impactNotional: uint64(lib.OneMillion), + openInterestLowerCap: 0, + openInterestUpperCap: 0, expectedError: errorsmod.Wrap(types.ErrInitialMarginPpmExceedsMax, fmt.Sprint(lib.OneMillion+1)), }, "Maintenance Fraction Ppm exceeds maximum": { @@ -2927,6 +2939,8 @@ func TestSetLiquidityTier_New_Failure(t *testing.T) { initialMarginPpm: 500_000, maintenanceFractionPpm: lib.OneMillion + 1, impactNotional: uint64(lib.OneMillion), + openInterestLowerCap: 0, + openInterestUpperCap: 0, expectedError: errorsmod.Wrap(types.ErrMaintenanceFractionPpmExceedsMax, fmt.Sprint(lib.OneMillion+1)), }, "Impact Notional is zero": { @@ -2935,6 +2949,8 @@ func TestSetLiquidityTier_New_Failure(t *testing.T) { initialMarginPpm: 500_000, maintenanceFractionPpm: lib.OneMillion, impactNotional: uint64(0), + openInterestLowerCap: 0, + openInterestUpperCap: 0, expectedError: types.ErrImpactNotionalIsZero, }, } @@ -2952,6 +2968,8 @@ func TestSetLiquidityTier_New_Failure(t *testing.T) { tc.initialMarginPpm, tc.maintenanceFractionPpm, tc.impactNotional, + tc.openInterestLowerCap, + tc.openInterestUpperCap, ) require.Error(t, err) @@ -2970,6 +2988,8 @@ func TestModifyLiquidityTier_Success(t *testing.T) { lt.InitialMarginPpm, lt.MaintenanceFractionPpm, lt.ImpactNotional, + lt.OpenInterestLowerCap, + lt.OpenInterestUpperCap, ) require.NoError(t, err) } @@ -2981,6 +3001,8 @@ func TestModifyLiquidityTier_Success(t *testing.T) { initialMarginPpm := uint32(i * 2) maintenanceFractionPpm := uint32(i * 2) impactNotional := uint64((i + 1) * 500_000_000) + openInterestLowerCap := uint64(0) + openInterestUpperCap := uint64(0) modifiedLt, err := pc.PerpetualsKeeper.SetLiquidityTier( pc.Ctx, lt.Id, @@ -2988,6 +3010,8 @@ func TestModifyLiquidityTier_Success(t *testing.T) { initialMarginPpm, maintenanceFractionPpm, impactNotional, + openInterestLowerCap, + openInterestUpperCap, ) require.NoError(t, err) obtainedLt, err := pc.PerpetualsKeeper.GetLiquidityTier(pc.Ctx, lt.Id) @@ -3029,6 +3053,8 @@ func TestSetLiquidityTier_Existing_Failure(t *testing.T) { initialMarginPpm uint32 maintenanceFractionPpm uint32 impactNotional uint64 + openInterestLowerCap uint64 + openInterestUpperCap uint64 expectedError error }{ "Initial Margin Ppm exceeds maximum": { @@ -3037,6 +3063,8 @@ func TestSetLiquidityTier_Existing_Failure(t *testing.T) { initialMarginPpm: lib.OneMillion + 1, maintenanceFractionPpm: 500_000, impactNotional: uint64(lib.OneMillion), + openInterestLowerCap: 0, + openInterestUpperCap: 0, expectedError: errorsmod.Wrap(types.ErrInitialMarginPpmExceedsMax, fmt.Sprint(lib.OneMillion+1)), }, "Maintenance Fraction Ppm exceeds maximum": { @@ -3045,6 +3073,8 @@ func TestSetLiquidityTier_Existing_Failure(t *testing.T) { initialMarginPpm: 500_000, maintenanceFractionPpm: lib.OneMillion + 1, impactNotional: uint64(lib.OneMillion), + openInterestLowerCap: 0, + openInterestUpperCap: 0, expectedError: errorsmod.Wrap(types.ErrMaintenanceFractionPpmExceedsMax, fmt.Sprint(lib.OneMillion+1)), }, "Impact Notional is zero": { @@ -3053,8 +3083,25 @@ func TestSetLiquidityTier_Existing_Failure(t *testing.T) { initialMarginPpm: 500_000, maintenanceFractionPpm: lib.OneMillion, impactNotional: uint64(0), + openInterestLowerCap: 0, + openInterestUpperCap: 0, expectedError: types.ErrImpactNotionalIsZero, }, + "Invalid open interest caps": { + id: 1, + name: "Small-Cap", + initialMarginPpm: 500_000, + maintenanceFractionPpm: lib.OneMillion, + impactNotional: uint64(lib.OneMillion), + openInterestLowerCap: 50_000_000_000_000, + openInterestUpperCap: 25_000_000_000_000, + expectedError: errorsmod.Wrapf( + types.ErrOpenInterestLowerCapLargerThanUpperCap, + "open_interest_lower_cap: %d, open_interest_upper_cap: %d", + 50_000_000_000_000, + 25_000_000_000_000, + ), + }, } // Test setup. @@ -3071,6 +3118,8 @@ func TestSetLiquidityTier_Existing_Failure(t *testing.T) { tc.initialMarginPpm, tc.maintenanceFractionPpm, tc.impactNotional, + tc.openInterestLowerCap, + tc.openInterestUpperCap, ) require.Error(t, err) diff --git a/protocol/x/perpetuals/module_test.go b/protocol/x/perpetuals/module_test.go index 7802a105d1..68147a9afc 100644 --- a/protocol/x/perpetuals/module_test.go +++ b/protocol/x/perpetuals/module_test.go @@ -282,7 +282,9 @@ func TestAppModule_InitExportGenesis(t *testing.T) { "name":"Large-Cap", "initial_margin_ppm":50000, "maintenance_fraction_ppm":500000, - "impact_notional":10000000000 + "impact_notional":10000000000, + "open_interest_lower_cap":25000000000000, + "open_interest_upper_cap":50000000000000 } ], "params":{ @@ -314,7 +316,8 @@ func TestAppModule_InitExportGenesis(t *testing.T) { "liquidity_tier":0, "market_type":"PERPETUAL_MARKET_TYPE_CROSS" }, - "funding_index":"0" + "funding_index":"0", + "open_interest":"0" } ], "liquidity_tiers":[ @@ -324,7 +327,9 @@ func TestAppModule_InitExportGenesis(t *testing.T) { "initial_margin_ppm":50000, "maintenance_fraction_ppm":500000, "base_position_notional":"0", - "impact_notional":"10000000000" + "impact_notional":"10000000000", + "open_interest_lower_cap":"25000000000000", + "open_interest_upper_cap":"50000000000000" } ], "params":{ diff --git a/protocol/x/perpetuals/simulation/genesis.go b/protocol/x/perpetuals/simulation/genesis.go index de97e9bbb4..b68e5cbe6f 100644 --- a/protocol/x/perpetuals/simulation/genesis.go +++ b/protocol/x/perpetuals/simulation/genesis.go @@ -203,6 +203,7 @@ func RandomizedGenState(simState *module.SimulationState) { MarketType: marketType, }, FundingIndex: dtypes.ZeroInt(), + OpenInterest: dtypes.ZeroInt(), } } diff --git a/protocol/x/perpetuals/types/errors.go b/protocol/x/perpetuals/types/errors.go index e7e0aed8c8..50f04d5c40 100644 --- a/protocol/x/perpetuals/types/errors.go +++ b/protocol/x/perpetuals/types/errors.go @@ -112,6 +112,11 @@ var ( 23, "Market type is invalid", ) + ErrOpenInterestLowerCapLargerThanUpperCap = errorsmod.Register( + ModuleName, + 24, + "open interest lower cap is larger than upper cap", + ) // Errors for Not Implemented ErrNotImplementedFunding = errorsmod.Register(ModuleName, 1001, "Not Implemented: Perpetuals Funding") diff --git a/protocol/x/perpetuals/types/liquidity_tier.go b/protocol/x/perpetuals/types/liquidity_tier.go index 957e5dfd87..2af218d04c 100644 --- a/protocol/x/perpetuals/types/liquidity_tier.go +++ b/protocol/x/perpetuals/types/liquidity_tier.go @@ -24,6 +24,15 @@ func (liquidityTier LiquidityTier) Validate() error { return ErrImpactNotionalIsZero } + if liquidityTier.OpenInterestLowerCap > liquidityTier.OpenInterestUpperCap { + return errorsmod.Wrapf( + ErrOpenInterestLowerCapLargerThanUpperCap, + "open_interest_lower_cap: %d, open_interest_upper_cap: %d", + liquidityTier.OpenInterestLowerCap, + liquidityTier.OpenInterestUpperCap, + ) + } + return nil } diff --git a/protocol/x/perpetuals/types/perpetual.pb.go b/protocol/x/perpetuals/types/perpetual.pb.go index 6072e4dbea..2325712744 100644 --- a/protocol/x/perpetuals/types/perpetual.pb.go +++ b/protocol/x/perpetuals/types/perpetual.pb.go @@ -62,6 +62,8 @@ type Perpetual struct { // The current index determined by the cumulative all-time // history of the funding mechanism. Starts at zero. FundingIndex github_com_dydxprotocol_v4_chain_protocol_dtypes.SerializableInt `protobuf:"bytes,2,opt,name=funding_index,json=fundingIndex,proto3,customtype=github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt" json:"funding_index"` + // Total size of open long contracts, measured in base_quantums. + OpenInterest github_com_dydxprotocol_v4_chain_protocol_dtypes.SerializableInt `protobuf:"bytes,3,opt,name=open_interest,json=openInterest,proto3,customtype=github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt" json:"open_interest"` } func (m *Perpetual) Reset() { *m = Perpetual{} } @@ -360,6 +362,13 @@ type LiquidityTier struct { // - Impact ask price = average execution price for a market buy of the // impact notional value. ImpactNotional uint64 `protobuf:"varint,6,opt,name=impact_notional,json=impactNotional,proto3" json:"impact_notional,omitempty"` + // Lower cap for Open Interest Margin Fracton (OIMF), in quote quantums. + // IMF is not affected when OI <= open_interest_lower_cap. + OpenInterestLowerCap uint64 `protobuf:"varint,7,opt,name=open_interest_lower_cap,json=openInterestLowerCap,proto3" json:"open_interest_lower_cap,omitempty"` + // Upper cap for Open Interest Margin Fracton (OIMF), in quote quantums. + // IMF scales linearly to 100% as OI approaches open_interest_upper_cap. + // If zero, then the IMF does not scale with OI. + OpenInterestUpperCap uint64 `protobuf:"varint,8,opt,name=open_interest_upper_cap,json=openInterestUpperCap,proto3" json:"open_interest_upper_cap,omitempty"` } func (m *LiquidityTier) Reset() { *m = LiquidityTier{} } @@ -438,6 +447,20 @@ func (m *LiquidityTier) GetImpactNotional() uint64 { return 0 } +func (m *LiquidityTier) GetOpenInterestLowerCap() uint64 { + if m != nil { + return m.OpenInterestLowerCap + } + return 0 +} + +func (m *LiquidityTier) GetOpenInterestUpperCap() uint64 { + if m != nil { + return m.OpenInterestUpperCap + } + return 0 +} + func init() { proto.RegisterEnum("dydxprotocol.perpetuals.PerpetualMarketType", PerpetualMarketType_name, PerpetualMarketType_value) proto.RegisterType((*Perpetual)(nil), "dydxprotocol.perpetuals.Perpetual") @@ -452,51 +475,55 @@ func init() { } var fileDescriptor_ce7204eee10038be = []byte{ - // 696 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x54, 0x4f, 0x4f, 0xdb, 0x3e, - 0x18, 0xae, 0x4b, 0x7f, 0xfd, 0x81, 0xfb, 0x87, 0xd6, 0x20, 0x16, 0x81, 0x54, 0x4a, 0x25, 0x44, - 0xb5, 0xb1, 0x56, 0x62, 0x3b, 0xec, 0xb0, 0xc3, 0x28, 0xb4, 0x5a, 0x35, 0x0a, 0x51, 0x5a, 0x26, - 0x6d, 0xd2, 0x14, 0xb9, 0x89, 0x29, 0x16, 0xb1, 0x93, 0x25, 0xce, 0x44, 0x77, 0xdb, 0x37, 0xe0, - 0x6b, 0xec, 0x6b, 0xec, 0xc4, 0x91, 0xe3, 0xb4, 0x03, 0x9a, 0xe0, 0x7b, 0x4c, 0x53, 0x1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xfa, 0xfd, 0x12, 0x40, 0x35, 0x58, 0xb9, 0x9f, 0xd0, 0xed, 0x1f, 0xec, 0x6d, 0x0f, 0xda, 0xbb, - 0xa5, 0x74, 0xeb, 0xed, 0xf9, 0x55, 0x05, 0x5c, 0x5c, 0x55, 0xc0, 0xcf, 0xab, 0x0a, 0x38, 0xbb, - 0xae, 0xa4, 0x2e, 0xae, 0x2b, 0xa9, 0xef, 0xd7, 0x95, 0xd4, 0xfb, 0x97, 0xff, 0xfe, 0xf9, 0x9f, - 0x4e, 0x3f, 0x75, 0xf2, 0x29, 0x18, 0x66, 0x25, 0xf8, 0xec, 0x77, 0x00, 0x00, 0x00, 0xff, 0xff, - 0xc6, 0x3a, 0xfc, 0x1e, 0x12, 0x05, 0x00, 0x00, + // 758 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x54, 0xcd, 0x6e, 0xf3, 0x44, + 0x14, 0x8d, 0xdd, 0x10, 0xda, 0xc9, 0xcf, 0x97, 0x4c, 0xab, 0x7e, 0x56, 0x2b, 0xa5, 0x69, 0xa4, + 0xaa, 0x11, 0x94, 0x44, 0x2a, 0x20, 0xb1, 0x60, 0x41, 0x93, 0x26, 0xc2, 0x22, 0x69, 0x2d, 0x27, + 0x45, 0x02, 0x09, 0x8d, 0x26, 0xf6, 0x34, 0x1d, 0xd5, 0x1e, 0x0f, 0xf6, 0x18, 0x1a, 0x76, 0xbc, + 0x41, 0x5f, 0x83, 0x25, 0x6f, 0xd1, 0x65, 0x97, 0x88, 0x45, 0x85, 0xda, 0x2d, 0x0f, 0x81, 0x3c, + 0x9e, 0xba, 0x49, 0x7f, 0x04, 0x0b, 0x56, 0x1e, 0xdf, 0x73, 0xce, 0x9d, 0x33, 0xd7, 0xc7, 0x03, + 0xf6, 0xdd, 0xb9, 0x7b, 0xc5, 0xc3, 0x40, 0x04, 0x4e, 0xe0, 0x75, 0x38, 0x09, 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e, 0xec, 0x6f, 0xfa, 0x13, 0x34, 0xf9, 0xce, 0xea, 0xa3, 0xb3, 0x93, 0xb1, 0xd5, 0xef, 0x99, + 0x03, 0xb3, 0x7f, 0x5c, 0xcd, 0xc1, 0x1d, 0xb0, 0xfd, 0x3a, 0xad, 0x67, 0x9f, 0x8e, 0xc7, 0x55, + 0x0d, 0x36, 0x41, 0xfd, 0x75, 0x82, 0x39, 0x3e, 0x1d, 0x1e, 0x4d, 0xfa, 0xc7, 0x55, 0xbd, 0xfb, + 0xed, 0xcd, 0x7d, 0x5d, 0xbb, 0xbd, 0xaf, 0x6b, 0x7f, 0xdd, 0xd7, 0xb5, 0xeb, 0x87, 0x7a, 0xee, + 0xf6, 0xa1, 0x9e, 0xfb, 0xe3, 0xa1, 0x9e, 0xfb, 0xfe, 0xcb, 0xff, 0x7e, 0xd9, 0x5c, 0x2d, 0xde, + 0xe3, 0xf2, 0xe2, 0x99, 0x16, 0x24, 0xf8, 0xe9, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x7d, 0x28, + 0x1e, 0x85, 0xef, 0x05, 0x00, 0x00, } func (m *Perpetual) Marshal() (dAtA []byte, err error) { @@ -519,6 +546,16 @@ func (m *Perpetual) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + { + size := m.OpenInterest.Size() + i -= size + if _, err := m.OpenInterest.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintPerpetual(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a { size := m.FundingIndex.Size() i -= size @@ -711,6 +748,16 @@ func (m *LiquidityTier) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.OpenInterestUpperCap != 0 { + i = encodeVarintPerpetual(dAtA, i, uint64(m.OpenInterestUpperCap)) + i-- + dAtA[i] = 0x40 + } + if m.OpenInterestLowerCap != 0 { + i = encodeVarintPerpetual(dAtA, i, uint64(m.OpenInterestLowerCap)) + i-- + dAtA[i] = 0x38 + } if m.ImpactNotional != 0 { i = encodeVarintPerpetual(dAtA, i, uint64(m.ImpactNotional)) i-- @@ -767,6 +814,8 @@ func (m *Perpetual) Size() (n int) { n += 1 + l + sovPerpetual(uint64(l)) l = m.FundingIndex.Size() n += 1 + l + sovPerpetual(uint64(l)) + l = m.OpenInterest.Size() + n += 1 + l + sovPerpetual(uint64(l)) return n } @@ -863,6 +912,12 @@ func (m *LiquidityTier) Size() (n int) { if m.ImpactNotional != 0 { n += 1 + sovPerpetual(uint64(m.ImpactNotional)) } + if m.OpenInterestLowerCap != 0 { + n += 1 + sovPerpetual(uint64(m.OpenInterestLowerCap)) + } + if m.OpenInterestUpperCap != 0 { + n += 1 + sovPerpetual(uint64(m.OpenInterestUpperCap)) + } return n } @@ -967,6 +1022,39 @@ func (m *Perpetual) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field OpenInterest", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowPerpetual + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthPerpetual + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthPerpetual + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.OpenInterest.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipPerpetual(dAtA[iNdEx:]) @@ -1594,6 +1682,44 @@ func (m *LiquidityTier) Unmarshal(dAtA []byte) error { break } } + case 7: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field OpenInterestLowerCap", wireType) + } + m.OpenInterestLowerCap = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowPerpetual + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.OpenInterestLowerCap |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 8: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field OpenInterestUpperCap", wireType) + } + m.OpenInterestUpperCap = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowPerpetual + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.OpenInterestUpperCap |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipPerpetual(dAtA[iNdEx:]) diff --git a/protocol/x/perpetuals/types/types.go b/protocol/x/perpetuals/types/types.go index 7b7489cf06..48634398a4 100644 --- a/protocol/x/perpetuals/types/types.go +++ b/protocol/x/perpetuals/types/types.go @@ -93,6 +93,8 @@ type PerpetualsKeeper interface { initialMarginPpm uint32, maintenanceFractionPpm uint32, impactNotional uint64, + openInterestLowerCap uint64, + openInterestUpperCap uint64, ) ( liquidityTier LiquidityTier, err error,