diff --git a/plugin/evm/orderbook/config_service.go b/plugin/evm/orderbook/config_service.go index 4060eb3293..8af98a3bf9 100644 --- a/plugin/evm/orderbook/config_service.go +++ b/plugin/evm/orderbook/config_service.go @@ -18,8 +18,10 @@ type IConfigService interface { getMinSizeRequirement(market Market) *big.Int GetPriceMultiplier(market Market) *big.Int GetActiveMarketsCount() int64 + GetMarketsIncludingSettled() []common.Address GetUnderlyingPrices() []*big.Int GetMidPrices() []*big.Int + GetSettlementPrices() []*big.Int GetCollaterals() []hu.Collateral GetLastPremiumFraction(market Market, trader *common.Address) *big.Int GetCumulativePremiumFraction(market Market) *big.Int @@ -100,6 +102,10 @@ func (cs *ConfigService) GetActiveMarketsCount() int64 { return bibliophile.GetActiveMarketsCount(cs.getStateAtCurrentBlock()) } +func (cs *ConfigService) GetMarketsIncludingSettled() []common.Address { + return bibliophile.GetMarketsIncludingSettled(cs.getStateAtCurrentBlock()) +} + func (cs *ConfigService) GetUnderlyingPrices() []*big.Int { return bibliophile.GetUnderlyingPrices(cs.getStateAtCurrentBlock()) } @@ -108,6 +114,10 @@ func (cs *ConfigService) GetMidPrices() []*big.Int { return bibliophile.GetMidPrices(cs.getStateAtCurrentBlock()) } +func (cs *ConfigService) GetSettlementPrices() []*big.Int { + return bibliophile.GetSettlementPrices(cs.getStateAtCurrentBlock()) +} + func (cs *ConfigService) GetCollaterals() []hu.Collateral { return bibliophile.GetCollaterals(cs.getStateAtCurrentBlock()) } diff --git a/plugin/evm/orderbook/hubbleutils/margin_math.go b/plugin/evm/orderbook/hubbleutils/margin_math.go index 69d180b5f8..0694528625 100644 --- a/plugin/evm/orderbook/hubbleutils/margin_math.go +++ b/plugin/evm/orderbook/hubbleutils/margin_math.go @@ -65,7 +65,7 @@ func GetNotionalPositionAndMargin(hState *HubbleState, userState *UserState, mar return notionalPosition, Add(margin, unrealizedPnl) } -// SUNSET: `hState.ActiveMarkets` in the hState passed contains settled markets too +// SUNSET: `hState.ActiveMarkets` contains active markets and ` hState.SettlementPrices` contains settlement prices func GetTotalNotionalPositionAndUnrealizedPnl(hState *HubbleState, userState *UserState, margin *big.Int, marginMode MarginMode) (*big.Int, *big.Int) { notionalPosition := big.NewInt(0) unrealizedPnl := big.NewInt(0) diff --git a/plugin/evm/orderbook/matching_pipeline.go b/plugin/evm/orderbook/matching_pipeline.go index f8e9769893..6f17c673a5 100644 --- a/plugin/evm/orderbook/matching_pipeline.go +++ b/plugin/evm/orderbook/matching_pipeline.go @@ -95,7 +95,6 @@ func (pipeline *MatchingPipeline) Run(blockNumber *big.Int) bool { // fetch various hubble market params and run the matching engine hState := GetHubbleState(pipeline.configService) - hState.OraclePrices = hu.ArrayToMap(pipeline.configService.GetUnderlyingPrices()) // build trader map liquidablePositions, ordersToCancel, marginMap := pipeline.db.GetNaughtyTraders(hState) diff --git a/plugin/evm/orderbook/mocks.go b/plugin/evm/orderbook/mocks.go index 6a56abde82..0d4a153d18 100644 --- a/plugin/evm/orderbook/mocks.go +++ b/plugin/evm/orderbook/mocks.go @@ -288,6 +288,14 @@ func (cs *MockConfigService) GetMidPrices() []*big.Int { return []*big.Int{} } +func (cs *MockConfigService) GetSettlementPrices() []*big.Int { + return []*big.Int{} +} + +func (cs *MockConfigService) GetMarketsIncludingSettled() []common.Address { + return []common.Address{} +} + func (cs *MockConfigService) GetLastPremiumFraction(market Market, trader *common.Address) *big.Int { return big.NewInt(0) } diff --git a/plugin/evm/orderbook/state.go b/plugin/evm/orderbook/state.go index b77ef9e1a4..1f0c86120b 100644 --- a/plugin/evm/orderbook/state.go +++ b/plugin/evm/orderbook/state.go @@ -12,6 +12,9 @@ func GetHubbleState(configService IConfigService) *hu.HubbleState { } hState := &hu.HubbleState{ Assets: configService.GetCollaterals(), + OraclePrices: hu.ArrayToMap(configService.GetUnderlyingPrices()), + MidPrices: hu.ArrayToMap(configService.GetMidPrices()), + SettlementPrices: hu.ArrayToMap(configService.GetSettlementPrices()), ActiveMarkets: markets, MinAllowableMargin: configService.GetMinAllowableMargin(), MaintenanceMargin: configService.GetMaintenanceMargin(), diff --git a/plugin/evm/orderbook/trading_apis.go b/plugin/evm/orderbook/trading_apis.go index 296fe72131..62863284c6 100644 --- a/plugin/evm/orderbook/trading_apis.go +++ b/plugin/evm/orderbook/trading_apis.go @@ -194,8 +194,7 @@ func (api *TradingAPI) GetMarginAndPositions(ctx context.Context, trader string) return response, fmt.Errorf("trader not found") } - // SUNSET: need to fetch total market count here, not active markets(cuz we're fetching pending funding) - count := api.configService.GetActiveMarketsCount() + count := int64(len(api.configService.GetMarketsIncludingSettled())) markets := make([]Market, count) for i := int64(0); i < count; i++ { markets[i] = Market(i) diff --git a/precompile/contracts/bibliophile/clearing_house.go b/precompile/contracts/bibliophile/clearing_house.go index b1d6db2445..5465c38fc9 100644 --- a/precompile/contracts/bibliophile/clearing_house.go +++ b/precompile/contracts/bibliophile/clearing_house.go @@ -169,6 +169,14 @@ func GetMidPrices(stateDB contract.StateDB) []*big.Int { return underlyingPrices } +func GetSettlementPrices(stateDB contract.StateDB) []*big.Int { + underlyingPrices := make([]*big.Int, 0) + for _, market := range GetMarketsIncludingSettled(stateDB) { + underlyingPrices = append(underlyingPrices, getSettlementPrice(stateDB, market)) + } + return underlyingPrices +} + func GetReduceOnlyAmounts(stateDB contract.StateDB, trader common.Address) []*big.Int { numMarkets := GetActiveMarketsCount(stateDB) sizes := make([]*big.Int, numMarkets)