Fixed problem finding market index by instrument.

This commit is contained in:
Geoff Taylor 2021-11-09 20:15:43 +00:00
parent 01cfae7fa4
commit e2d0853d3f
6 changed files with 16 additions and 30 deletions

View File

@ -51,7 +51,7 @@ for balance in balances:
total_in_wallet = total_in_wallet + balance
value_text = f" worth {balance}"
else:
token_index: typing.Optional[int] = group.find_token_market_index_or_none(balance.token)
token_index: typing.Optional[int] = group.find_instrument_market_index_or_none(balance.token)
if token_index is not None:
cached_token_price: typing.Optional[mango.PriceCache] = cache.price_cache[token_index]
if cached_token_price is not None:

View File

@ -151,7 +151,7 @@ class HealthCalculator:
health: Decimal = quote_report.net_value.value
# print("Health (start)", health)
for priced_report in priced_reports:
market_index = group.find_token_market_index(priced_report.base_token)
market_index = group.find_instrument_market_index(priced_report.base_token)
spot_market: typing.Optional[GroupSlotSpotMarket] = group.spot_markets_by_index[market_index]
if spot_market is None:
raise Exception(f"Could not find market for spot token {priced_report.base_token.symbol}.")

View File

@ -46,7 +46,7 @@ class PerpCollateralCalculator(CollateralCalculator):
total: Decimal = account.shared_quote.net_value.value
collateral_description = [f"{total:,.8f} USDC"]
for basket_token in account.slots:
index = group.find_base_instrument_market_index(basket_token.base_instrument)
index = group.find_instrument_market_index(basket_token.base_instrument)
token_price = group.token_price_from_cache(cache, basket_token.base_instrument)
# Not using perp market asset weights yet - stick with spot.

View File

@ -46,7 +46,7 @@ class SpotCollateralCalculator(CollateralCalculator):
total: Decimal = account.shared_quote.net_value.value
collateral_description = [f"{total:,.8f} USDC"]
for basket_token in account.slots:
index = group.find_base_instrument_market_index(basket_token.base_instrument)
index = group.find_instrument_market_index(basket_token.base_instrument)
token_price = group.token_price_from_cache(cache, basket_token.base_instrument)
spot_market: typing.Optional[GroupSlotSpotMarket] = group.spot_markets_by_index[index]

View File

@ -358,35 +358,21 @@ class Group(AddressableAccount):
raise Exception(f"Could not find perp market {perp_market_address} in group {self.address}")
def find_base_token_market_index(self, base_token: TokenInfo) -> int:
for index, bt in enumerate(self.base_tokens_by_index):
if bt is not None and bt.token == base_token.token:
return index
raise Exception(f"Could not find base token {base_token} in group {self.address}")
def find_base_instrument_market_index(self, instrument: Instrument) -> int:
for index, bt in enumerate(self.base_tokens_by_index):
if bt is not None and bt.token == instrument:
return index
raise Exception(f"Could not find base instrument {instrument} in group {self.address}")
def find_token_market_index_or_none(self, token: Instrument) -> typing.Optional[int]:
for index, bt in enumerate(self.base_tokens_by_index):
if bt is not None and bt.token == token:
def find_instrument_market_index_or_none(self, instrument: Instrument) -> typing.Optional[int]:
for index, slot in enumerate(self.slots_by_index):
if slot is not None and slot.base_instrument == instrument:
return index
return None
def find_token_market_index(self, token: Instrument) -> int:
index = self.find_token_market_index_or_none(token)
def find_instrument_market_index(self, instrument: Instrument) -> int:
index = self.find_instrument_market_index_or_none(instrument)
if index is not None:
return index
raise Exception(f"Could not find token {token} in group {self.address}")
raise Exception(f"Could not find token {instrument} in group {self.address}")
def find_token_info_by_token(self, instrument: Instrument) -> TokenInfo:
def find_token_info_by_instrument(self, instrument: Instrument) -> TokenInfo:
for token_info in self.tokens:
if token_info.token == instrument:
return token_info
@ -408,9 +394,9 @@ class Group(AddressableAccount):
market_cache: MarketCache = self.market_cache_from_cache(cache, token)
return market_cache.perp_market
def market_cache_from_cache(self, cache: Cache, token: Instrument) -> MarketCache:
token_index: int = self.find_token_market_index(token)
return cache.market_cache_for_index(token_index)
def market_cache_from_cache(self, cache: Cache, instrument: Instrument) -> MarketCache:
instrument_index: int = self.find_instrument_market_index(instrument)
return cache.market_cache_for_index(instrument_index)
def __str__(self) -> str:
slot_count = len(self.slots)

View File

@ -94,9 +94,9 @@ class SpotMarketInstructionBuilder(MarketInstructionBuilder):
if self.open_orders_address is None:
return CombinableInstructions.empty()
base_rootbank = self.group.find_token_info_by_token(self.spot_market.base).root_bank
base_rootbank = self.group.find_token_info_by_instrument(self.spot_market.base).root_bank
base_nodebank = base_rootbank.pick_node_bank(self.context)
quote_rootbank = self.group.find_token_info_by_token(self.spot_market.quote).root_bank
quote_rootbank = self.group.find_token_info_by_instrument(self.spot_market.quote).root_bank
quote_nodebank = quote_rootbank.pick_node_bank(self.context)
return build_spot_settle_instructions(self.context, self.wallet, self.account,
self.raw_market, self.group, self.open_orders_address,