Skip to content

Commit

Permalink
add default genesis value, modify methods interface
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyding committed Mar 4, 2024
1 parent eb100ae commit f784edb
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 17 deletions.
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.

16 changes: 11 additions & 5 deletions protocol/scripts/genesis/sample_pregenesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -862,31 +862,37 @@
"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": 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": 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 @@ -1815,7 +1821,7 @@
]
}
},
"app_version": "4.0.0-dev0-59-g5530ea29",
"app_version": "4.0.0-dev0-60-geb100ae0",
"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_lower_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_lower_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
8 changes: 8 additions & 0 deletions protocol/testutil/constants/perpetuals.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,20 +339,26 @@ var (
InitialMarginPpm: 200_000,
MaintenanceFractionPpm: 500_000,
ImpactNotional: 2_500_000_000,
OpenInterestLowerCap: 0,
OpenInterestUpperCap: 0,
},
{
Id: uint32(1),
Name: "Mid-Cap",
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),
Name: "Small-Cap",
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,
Expand All @@ -365,6 +371,7 @@ var (
MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS,
},
FundingIndex: dtypes.ZeroInt(),
OpenInterest: dtypes.ZeroInt(),
},
{
Params: perptypes.PerpetualParams{
Expand All @@ -374,6 +381,7 @@ var (
MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS,
},
FundingIndex: dtypes.ZeroInt(),
OpenInterest: dtypes.ZeroInt(),
},
},
}
Expand Down
2 changes: 2 additions & 0 deletions protocol/testutil/keeper/perpetuals.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,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)
Expand Down
1 change: 1 addition & 0 deletions protocol/testutil/perpetuals/perpetuals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions protocol/x/perpetuals/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions protocol/x/perpetuals/keeper/msg_server_set_liquidity_tier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions protocol/x/perpetuals/keeper/msg_server_set_liquidity_tier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -126,6 +141,8 @@ func TestSetLiquidityTier(t *testing.T) {
testLt.InitialMarginPpm,
testLt.MaintenanceFractionPpm,
testLt.ImpactNotional,
testLt.OpenInterestLowerCap,
testLt.OpenInterestUpperCap,
)
require.NoError(t, err)

Expand Down
5 changes: 5 additions & 0 deletions protocol/x/perpetuals/keeper/perpetual.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (k Keeper) CreatePerpetual(
MarketType: marketType,
},
FundingIndex: dtypes.ZeroInt(),
OpenInterest: dtypes.ZeroInt(),
}

if err := k.validatePerpetual(
Expand Down Expand Up @@ -1377,6 +1378,8 @@ func (k Keeper) SetLiquidityTier(
initialMarginPpm uint32,
maintenanceFractionPpm uint32,
impactNotional uint64,
openInterestLowerCap uint64,
openInterestUpperCap uint64,
) (
liquidityTier types.LiquidityTier,
err error,
Expand All @@ -1388,6 +1391,8 @@ func (k Keeper) SetLiquidityTier(
InitialMarginPpm: initialMarginPpm,
MaintenanceFractionPpm: maintenanceFractionPpm,
ImpactNotional: impactNotional,
OpenInterestLowerCap: openInterestLowerCap,
OpenInterestUpperCap: openInterestUpperCap,
}

// Validate liquidity tier's fields.
Expand Down
Loading

0 comments on commit f784edb

Please sign in to comment.