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

[OTE-200] OIMF protos and genesis values #1125

Merged
merged 5 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 "{}"
Comment on lines 125 to 131
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 NOTE
This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [1-10]

The script header provides a good overview of its purpose but lacks detailed documentation on how to use it, especially for new contributors or external auditors. It would be beneficial to include a brief usage guide or examples, particularly for the edit_genesis function and other key operations within the script.

Consider adding a commented section at the top of the script with examples of how to run it and descriptions of the key functions and parameters it modifies. This will improve maintainability and ease of use for new developers or contributors.

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 "{}"
Comment on lines 135 to 142
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [128-173]

The changes from lines 128 to 173 introduce default values for open interest caps across different liquidity tiers (Large-Cap, Mid-Cap, Long-Tail, and Safety). It's crucial to ensure these values align with the project's financial models and risk assessments. Additionally, there's a repeated line (line 161) setting the open_interest_lower_cap for the Long-Tail tier, which seems like it should be setting the open_interest_upper_cap instead. This could be a typo that needs correction to avoid potential configuration errors in the genesis state.

- dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[2].open_interest_lower_cap' -v '20000000000000' # 20 million USDC
+ dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[2].open_interest_upper_cap' -v '20000000000000' # 20 million USDC

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
Loading