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-244] Add upgrade handler for OIMF caps #1232

Merged
merged 1 commit into from
Mar 25, 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
52 changes: 52 additions & 0 deletions protocol/app/upgrades/v5.0.0/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,55 @@ func blockRateLimitConfigUpdate(
)
}

// Initialize soft and upper caps for OIMF
func initializeOIMFCaps(
ctx sdk.Context,
perpetualsKeeper perptypes.PerpetualsKeeper,
) {
allLiquidityTiers := perpetualsKeeper.GetAllLiquidityTiers(ctx)
for _, tier := range allLiquidityTiers {
if tier.Id == 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Based on values here

// For large cap, no OIMF caps
tier.OpenInterestLowerCap = 0
tier.OpenInterestUpperCap = 0
} else if tier.Id == 1 {
// Mid cap
tier.OpenInterestLowerCap = 25_000_000_000_000 // 25 million USDC
tier.OpenInterestUpperCap = 50_000_000_000_000 // 50 million USDC
} else if tier.Id == 2 {
// Long tail
tier.OpenInterestLowerCap = 10_000_000_000_000 // 10 million USDC
tier.OpenInterestUpperCap = 20_000_000_000_000 // 20 million USDC
} else {
// Safety
tier.OpenInterestLowerCap = 500_000_000_000 // 0.5 million USDC
tier.OpenInterestUpperCap = 1_000_000_000_000 // 1 million USDC
}

lt, err := perpetualsKeeper.SetLiquidityTier(
ctx,
tier.Id,
tier.Name,
tier.InitialMarginPpm,
tier.MaintenanceFractionPpm,
tier.ImpactNotional,
tier.OpenInterestLowerCap,
tier.OpenInterestUpperCap,
)
if err != nil {
panic(fmt.Sprintf("failed to set liquidity tier: %+v,\n err: %s", tier.Id, err))
}
// TODO(OTE-248): Optional - emit indexer events that for updated liquidity tier
ctx.Logger().Info(
fmt.Sprintf(
"Successfully set liqiquidity tier with `OpenInterestLower/UpperCap`: %+v\n",
lt,
),
)
// TODO(OTE-249): Add upgrade test that checks if the OIMF caps are set correctly
}
}

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
Expand All @@ -114,6 +163,9 @@ func CreateUpgradeHandler(
// Set block rate limit configuration
blockRateLimitConfigUpdate(sdkCtx, clobKeeper)

// Initialize liquidity tier with lower and upper OI caps.
initializeOIMFCaps(sdkCtx, perpetualsKeeper)

// TODO(TRA-93): Initialize `x/vault` module.

return mm.RunMigrations(ctx, configurator, vm)
Expand Down
20 changes: 20 additions & 0 deletions protocol/mocks/PerpetualsKeeper.go

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

4 changes: 2 additions & 2 deletions protocol/scripts/genesis/sample_pregenesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,8 @@
"initial_margin_ppm": 1000000,
"maintenance_fraction_ppm": 200000,
"name": "Safety",
"open_interest_lower_cap": 0,
"open_interest_upper_cap": 0
"open_interest_lower_cap": 500000000000,
"open_interest_upper_cap": 1000000000000
}
],
"params": {
Expand Down
7 changes: 3 additions & 4 deletions protocol/testing/genesis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,9 @@ 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'

# For `Safety` IMF is already at 100%; still we set OIMF for completeness.
dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[3].open_interest_lower_cap' -v '500000000000' # 0.5 million USDC
dasel put -t int -f "$GENESIS" '.app_state.perpetuals.liquidity_tiers.[3].open_interest_upper_cap' -v '1000000000000' # 1 million USDC

# 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
1 change: 1 addition & 0 deletions protocol/x/perpetuals/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type PerpetualsKeeper interface {
GetAllPerpetuals(
ctx sdk.Context,
) []Perpetual
GetAllLiquidityTiers(ctx sdk.Context) (list []LiquidityTier)
}

type OpenInterestDelta struct {
Expand Down
Loading