Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
atvanguard committed May 13, 2024
1 parent 0ca1f5f commit 5fd81c0
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 4 deletions.
10 changes: 10 additions & 0 deletions plugin/evm/orderbook/config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/evm/orderbook/hubbleutils/margin_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion plugin/evm/orderbook/matching_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions plugin/evm/orderbook/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions plugin/evm/orderbook/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 1 addition & 2 deletions plugin/evm/orderbook/trading_apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions precompile/contracts/bibliophile/clearing_house.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5fd81c0

Please sign in to comment.