-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize OffsetSubaccountPerpetualPosition subaccount iteration (#906)
* Optimize subaccount iteration * fix test * only get relevant subaccounts for final settlement * start from a random subaccount * fix test * fix test
- Loading branch information
Showing
18 changed files
with
260 additions
and
336 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package clob | ||
|
||
import ( | ||
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types" | ||
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types" | ||
) | ||
|
||
func GetOpenPositionsFromSubaccounts( | ||
subaccounts []satypes.Subaccount, | ||
) []clobtypes.SubaccountOpenPositionInfo { | ||
positionMap := make(map[uint32]*clobtypes.SubaccountOpenPositionInfo) | ||
for _, subaccount := range subaccounts { | ||
for _, position := range subaccount.PerpetualPositions { | ||
info, ok := positionMap[position.PerpetualId] | ||
if !ok { | ||
info = &clobtypes.SubaccountOpenPositionInfo{ | ||
PerpetualId: position.PerpetualId, | ||
SubaccountsWithLongPosition: make([]satypes.SubaccountId, 0), | ||
SubaccountsWithShortPosition: make([]satypes.SubaccountId, 0), | ||
} | ||
positionMap[position.PerpetualId] = info | ||
} | ||
if position.GetIsLong() { | ||
info.SubaccountsWithLongPosition = append( | ||
info.SubaccountsWithLongPosition, | ||
*subaccount.Id, | ||
) | ||
} else { | ||
info.SubaccountsWithShortPosition = append( | ||
info.SubaccountsWithShortPosition, | ||
*subaccount.Id, | ||
) | ||
} | ||
} | ||
} | ||
|
||
positionSlice := make([]clobtypes.SubaccountOpenPositionInfo, 0) | ||
for _, info := range positionMap { | ||
positionSlice = append(positionSlice, *info) | ||
} | ||
return positionSlice | ||
} |
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
Oops, something went wrong.