-
Notifications
You must be signed in to change notification settings - Fork 115
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
Refactor perpetual upgrade handlers in v5.0.0
#1442
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
sdk "github.com/cosmos/cosmos-sdk/types" | ||
consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types" | ||
|
||
"github.com/dydxprotocol/v4-chain/protocol/dtypes" | ||
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types" | ||
perptypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types" | ||
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types" | ||
|
@@ -19,20 +20,68 @@ import ( | |
"github.com/dydxprotocol/v4-chain/protocol/lib" | ||
) | ||
|
||
// Set all existing perpetuals to cross market type | ||
// perpetuals upgrade handler for v5.0.0 | ||
// - 1. Set all perpetuals to cross market type | ||
// - 2. Initialize perpetual open interest to current OI | ||
func perpetualsUpgrade( | ||
ctx sdk.Context, | ||
perpetualsKeeper perptypes.PerpetualsKeeper, | ||
subaccountsKeeper satypes.SubaccountsKeeper, | ||
) { | ||
// Set all perpetuals to cross market type | ||
perpOIMap := make(map[uint32]*big.Int) | ||
|
||
// Iterate through all subaccounts and perp positions for each subaccount. | ||
// Aggregate open interest for each perpetual market. | ||
subaccounts := subaccountsKeeper.GetAllSubaccount(ctx) | ||
for _, sa := range subaccounts { | ||
for _, perpPosition := range sa.PerpetualPositions { | ||
if perpPosition.Quantums.BigInt().Sign() <= 0 { | ||
// Only record positive positions for total open interest. | ||
// Total negative position size should be equal to total positive position size. | ||
continue | ||
} | ||
if openInterest, exists := perpOIMap[perpPosition.PerpetualId]; exists { | ||
// Already seen this perpetual. Add to open interest. | ||
openInterest.Add( | ||
openInterest, | ||
perpPosition.Quantums.BigInt(), | ||
) | ||
} else { | ||
// Haven't seen this pereptual. Initialize open interest. | ||
perpOIMap[perpPosition.PerpetualId] = new(big.Int).Set( | ||
perpPosition.Quantums.BigInt(), | ||
) | ||
} | ||
} | ||
} | ||
|
||
perpetuals := perpetualsKeeper.GetAllPerpetuals(ctx) | ||
for _, p := range perpetuals { | ||
_, err := perpetualsKeeper.SetPerpetualMarketType( | ||
ctx, p.GetId(), | ||
perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS) | ||
for _, perp := range perpetuals { | ||
// 1. Set all perpetuals to cross market type | ||
perp.Params.MarketType = perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS | ||
|
||
// 2. Initialize perpetual open interest to current OI | ||
perpOpenInterst := big.NewInt(0) | ||
if oi, exists := perpOIMap[perp.GetId()]; exists { | ||
perpOpenInterst.Set(oi) | ||
} | ||
perp.OpenInterest = dtypes.NewIntFromBigInt(perpOpenInterst) | ||
err := perpetualsKeeper.ValidateAndSetPerpetual( | ||
ctx, | ||
perp, | ||
) | ||
if err != nil { | ||
panic(fmt.Sprintf("failed to set perpetual market type for perpetual %d: %s", p.GetId(), err)) | ||
panic(fmt.Sprintf( | ||
"failed to modify open interest for perpetual, openInterest = %v, perpetual = %+v", | ||
perpOpenInterst, | ||
perp, | ||
)) | ||
} | ||
ctx.Logger().Info(fmt.Sprintf( | ||
"Successfully migrated perpetual %d: %+v", | ||
perp.GetId(), | ||
perp, | ||
)) | ||
} | ||
} | ||
|
||
|
@@ -98,8 +147,10 @@ func blockRateLimitConfigUpdate( | |
panic(fmt.Sprintf("failed to update the block rate limit configuration: %s", err)) | ||
} | ||
ctx.Logger().Info( | ||
"Successfully upgraded block rate limit configuration to: %+v\n", | ||
clobKeeper.GetBlockRateLimitConfiguration(ctx), | ||
fmt.Sprintf( | ||
"Successfully upgraded block rate limit configuration to: %+v\n", | ||
clobKeeper.GetBlockRateLimitConfiguration(ctx), | ||
), | ||
) | ||
} | ||
|
||
|
@@ -238,69 +289,6 @@ func voteExtensionsUpgrade( | |
) | ||
} | ||
|
||
// Initialize open interest for perpetuals | ||
func initializePerpOpenInterest( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Combined into |
||
ctx sdk.Context, | ||
perpetualsKeeper perptypes.PerpetualsKeeper, | ||
subaccountsKeeper satypes.SubaccountsKeeper, | ||
) { | ||
perpOIMap := make(map[uint32]*big.Int) | ||
|
||
subaccounts := subaccountsKeeper.GetAllSubaccount(ctx) | ||
|
||
// Iterate through all subaccounts and perp positions for each subaccount. | ||
// Aggregate open interest for each perpetual market. | ||
for _, sa := range subaccounts { | ||
for _, perpPosition := range sa.PerpetualPositions { | ||
if perpPosition.Quantums.BigInt().Sign() <= 0 { | ||
// Only record positive positions for total open interest. | ||
// Total negative position size should be equal to total positive position size. | ||
continue | ||
} | ||
if openInterest, exists := perpOIMap[perpPosition.PerpetualId]; exists { | ||
// Already seen this perpetual. Add to open interest. | ||
openInterest.Add( | ||
openInterest, | ||
perpPosition.Quantums.BigInt(), | ||
) | ||
} else { | ||
// Haven't seen this pereptual. Initialize open interest. | ||
perpOIMap[perpPosition.PerpetualId] = new(big.Int).Set( | ||
perpPosition.Quantums.BigInt(), | ||
) | ||
} | ||
} | ||
} | ||
|
||
allPerps := perpetualsKeeper.GetAllPerpetuals(ctx) | ||
for _, perp := range allPerps { | ||
if perp.OpenInterest.BigInt().Sign() != 0 { | ||
panic(fmt.Sprintf("perpetual %d has non-zero OI (%v) before upgrade", perp.GetId(), perp.OpenInterest)) | ||
} | ||
if openInterest, exists := perpOIMap[perp.GetId()]; exists { | ||
err := perpetualsKeeper.ModifyOpenInterest( | ||
ctx, | ||
perp.GetId(), | ||
openInterest, // by default perpetual.OI = 0, so use the total open interest as delta | ||
) | ||
if err != nil { | ||
panic(fmt.Sprintf( | ||
"failed to modify open interest for perpetual, openInterest = %v, perpetual = %+v", | ||
openInterest, | ||
perp, | ||
)) | ||
} | ||
ctx.Logger().Info(fmt.Sprintf( | ||
"Successfully initialized open interest for perpetual %d = %v", | ||
perp.GetId(), | ||
openInterest, | ||
)) | ||
} else { | ||
ctx.Logger().Info(fmt.Sprintf("Perpetual %d has zero open interest at the time of upgrade", perp.GetId())) | ||
} | ||
} | ||
} | ||
|
||
// Initialize vault module parameters. | ||
func initializeVaultModuleParams( | ||
ctx sdk.Context, | ||
|
@@ -331,11 +319,8 @@ func CreateUpgradeHandler( | |
clobKeeper.MigratePruneableOrders(sdkCtx) | ||
sdkCtx.Logger().Info("Successfully migrated pruneable orders") | ||
|
||
// Set all perpetuals to cross market type | ||
perpetualsUpgrade(sdkCtx, perpetualsKeeper) | ||
|
||
// Initialize open interest for all perpetuals | ||
initializePerpOpenInterest(sdkCtx, perpetualsKeeper, subaccountsKeeper) | ||
// Set all perpetuals to cross market type and initialize open interest | ||
perpetualsUpgrade(sdkCtx, perpetualsKeeper, subaccountsKeeper) | ||
|
||
// Set block rate limit configuration | ||
blockRateLimitConfigUpdate(sdkCtx, clobKeeper) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid two separate calls of
SetPerpetualMarketType
here andModifyOpenInterest
below; just usevalidateAndSetPerpetual
once.