Skip to content

Commit

Permalink
[OTE-200] OIMF protos (dydxprotocol#1125)
Browse files Browse the repository at this point in the history
* OIMF protos

* add default genesis value, modify methods interface

* fix genesis file

* fix integration test

* lint

Signed-off-by: Eric <eric.warehime@gmail.com>
  • Loading branch information
teddyding authored and Eric-Warehime committed Mar 12, 2024
1 parent 2a062b1 commit 41a3a41
Show file tree
Hide file tree
Showing 20 changed files with 430 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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. */

Expand All @@ -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
Expand Down Expand Up @@ -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. */

Expand Down Expand Up @@ -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()
};
}

Expand All @@ -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;
},

Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
};
}

Expand Down Expand Up @@ -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;
},

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
16 changes: 16 additions & 0 deletions proto/dydxprotocol/perpetuals/perpetual.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
18 changes: 9 additions & 9 deletions protocol/mocks/PerpetualsKeeper.go

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

18 changes: 13 additions & 5 deletions protocol/scripts/genesis/sample_pregenesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -862,31 +862,39 @@
"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,
"id": 1,
"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,
"id": 2,
"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,
"id": 3,
"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": {
Expand Down Expand Up @@ -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": {
Expand Down
11 changes: 11 additions & 0 deletions protocol/testing/genesis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "{}"
Expand All @@ -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 "{}"
Expand All @@ -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 "{}"
Expand All @@ -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 "{}"
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 41a3a41

Please sign in to comment.