-
Notifications
You must be signed in to change notification settings - Fork 104
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
[CLOB-1050] update daemon liquidation info to include negative tnc su… #879
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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
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
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
117 changes: 117 additions & 0 deletions
117
protocol/daemons/server/types/liquidations/daemon_liquidation_info.go
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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package types | ||
|
||
import ( | ||
"sync" | ||
|
||
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types" | ||
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types" | ||
) | ||
|
||
// DaemonLiquidationInfo maintains the list of subaccount ids to be liquidated | ||
// in the next block. Methods are goroutine safe. | ||
type DaemonLiquidationInfo struct { | ||
sync.Mutex // lock | ||
blockHeight uint32 // block height of the last update | ||
liquidatableSubaccountIds []satypes.SubaccountId // liquidatable subaccount ids | ||
negativeTncSubaccountIds []satypes.SubaccountId // negative total net collateral subaccount ids | ||
subaccountsWithPositions map[uint32]*clobtypes.SubaccountOpenPositionInfo | ||
} | ||
|
||
// NewDaemonLiquidationInfo creates a new `NewDaemonLiquidationInfo` struct. | ||
func NewDaemonLiquidationInfo() *DaemonLiquidationInfo { | ||
return &DaemonLiquidationInfo{ | ||
liquidatableSubaccountIds: make([]satypes.SubaccountId, 0), | ||
negativeTncSubaccountIds: make([]satypes.SubaccountId, 0), | ||
subaccountsWithPositions: make(map[uint32]*clobtypes.SubaccountOpenPositionInfo), | ||
} | ||
} | ||
|
||
// UpdateBlockHeight updates the struct with the given block height. | ||
func (ls *DaemonLiquidationInfo) UpdateBlockHeight(blockHeight uint32) { | ||
ls.Lock() | ||
defer ls.Unlock() | ||
ls.blockHeight = blockHeight | ||
} | ||
|
||
// GetBlockHeight returns the block height of the last update. | ||
func (ls *DaemonLiquidationInfo) GetBlockHeight() uint32 { | ||
ls.Lock() | ||
defer ls.Unlock() | ||
return ls.blockHeight | ||
} | ||
|
||
// UpdateLiquidatableSubaccountIds updates the struct with the given a list of potentially | ||
// liquidatable subaccount ids. | ||
func (ls *DaemonLiquidationInfo) UpdateLiquidatableSubaccountIds(updates []satypes.SubaccountId) { | ||
ls.Lock() | ||
defer ls.Unlock() | ||
ls.liquidatableSubaccountIds = make([]satypes.SubaccountId, len(updates)) | ||
copy(ls.liquidatableSubaccountIds, updates) | ||
} | ||
|
||
// GetLiquidatableSubaccountIds returns the list of potentially liquidatable subaccount ids | ||
// reported by the liquidation daemon. | ||
func (ls *DaemonLiquidationInfo) GetLiquidatableSubaccountIds() []satypes.SubaccountId { | ||
ls.Lock() | ||
defer ls.Unlock() | ||
results := make([]satypes.SubaccountId, len(ls.liquidatableSubaccountIds)) | ||
copy(results, ls.liquidatableSubaccountIds) | ||
return results | ||
} | ||
|
||
// UpdateNegativeTncSubaccountIds updates the struct with the given a list of subaccount ids | ||
// with negative total net collateral. | ||
func (ls *DaemonLiquidationInfo) UpdateNegativeTncSubaccountIds(updates []satypes.SubaccountId) { | ||
ls.Lock() | ||
defer ls.Unlock() | ||
ls.negativeTncSubaccountIds = make([]satypes.SubaccountId, len(updates)) | ||
copy(ls.negativeTncSubaccountIds, updates) | ||
} | ||
|
||
// GetNegativeTncSubaccountIds returns the list of subaccount ids with negative total net collateral | ||
// reported by the liquidation daemon. | ||
func (ls *DaemonLiquidationInfo) GetNegativeTncSubaccountIds() []satypes.SubaccountId { | ||
ls.Lock() | ||
defer ls.Unlock() | ||
results := make([]satypes.SubaccountId, len(ls.negativeTncSubaccountIds)) | ||
copy(results, ls.negativeTncSubaccountIds) | ||
return results | ||
} | ||
|
||
// UpdateSubaccountsWithPositions updates the struct with the given a list of subaccount ids with open positions. | ||
func (ls *DaemonLiquidationInfo) UpdateSubaccountsWithPositions( | ||
subaccountsWithPositions map[uint32]*clobtypes.SubaccountOpenPositionInfo, | ||
) { | ||
ls.Lock() | ||
defer ls.Unlock() | ||
ls.subaccountsWithPositions = make(map[uint32]*clobtypes.SubaccountOpenPositionInfo) | ||
for perpetualId, info := range subaccountsWithPositions { | ||
clone := &clobtypes.SubaccountOpenPositionInfo{ | ||
PerpetualId: perpetualId, | ||
SubaccountsWithLongPosition: make([]satypes.SubaccountId, len(info.SubaccountsWithLongPosition)), | ||
SubaccountsWithShortPosition: make([]satypes.SubaccountId, len(info.SubaccountsWithShortPosition)), | ||
} | ||
copy(clone.SubaccountsWithLongPosition, info.SubaccountsWithLongPosition) | ||
copy(clone.SubaccountsWithShortPosition, info.SubaccountsWithShortPosition) | ||
ls.subaccountsWithPositions[perpetualId] = clone | ||
} | ||
} | ||
|
||
// GetSubaccountsWithPositions returns the list of subaccount ids with open positions. | ||
func (ls *DaemonLiquidationInfo) GetSubaccountsWithPositions() map[uint32]*clobtypes.SubaccountOpenPositionInfo { | ||
ls.Lock() | ||
defer ls.Unlock() | ||
|
||
result := make(map[uint32]*clobtypes.SubaccountOpenPositionInfo) | ||
for perpetualId, info := range ls.subaccountsWithPositions { | ||
clone := &clobtypes.SubaccountOpenPositionInfo{ | ||
PerpetualId: perpetualId, | ||
SubaccountsWithLongPosition: make([]satypes.SubaccountId, len(info.SubaccountsWithLongPosition)), | ||
SubaccountsWithShortPosition: make([]satypes.SubaccountId, len(info.SubaccountsWithShortPosition)), | ||
} | ||
copy(clone.SubaccountsWithLongPosition, info.SubaccountsWithLongPosition) | ||
copy(clone.SubaccountsWithShortPosition, info.SubaccountsWithShortPosition) | ||
result[perpetualId] = clone | ||
} | ||
return result | ||
} |
Oops, something went wrong.
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.
Are all of the liquidatable / negative TNC subaccounts read from the same block height (which is the same as this
blockHeight
field)?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.
yes - I will be updating the liquidation daemon to make height based queries so everything is read from the same height