2021-06-25 02:33:40 -07:00
|
|
|
|
# # ⚠ Warning
|
|
|
|
|
#
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
|
|
|
|
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
|
# NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
|
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
|
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
#
|
|
|
|
|
# [🥭 Mango Markets](https://mango.markets/) support is available at:
|
|
|
|
|
# [Docs](https://docs.mango.markets/)
|
|
|
|
|
# [Discord](https://discord.gg/67jySBhxrg)
|
|
|
|
|
# [Twitter](https://twitter.com/mangomarkets)
|
|
|
|
|
# [Github](https://github.com/blockworks-foundation)
|
|
|
|
|
# [Email](mailto:hello@blockworks.foundation)
|
|
|
|
|
|
|
|
|
|
import typing
|
|
|
|
|
|
|
|
|
|
from decimal import Decimal
|
|
|
|
|
from solana.publickey import PublicKey
|
|
|
|
|
from solana.rpc.types import MemcmpOpts
|
|
|
|
|
|
|
|
|
|
from .accountinfo import AccountInfo
|
|
|
|
|
from .addressableaccount import AddressableAccount
|
2021-11-11 03:56:29 -08:00
|
|
|
|
from .cache import Cache, RootBankCache
|
2021-06-25 02:33:40 -07:00
|
|
|
|
from .context import Context
|
|
|
|
|
from .encoding import encode_key
|
2021-06-25 07:50:37 -07:00
|
|
|
|
from .group import Group
|
2021-11-08 03:39:09 -08:00
|
|
|
|
from .instrumentvalue import InstrumentValue
|
2021-06-25 02:33:40 -07:00
|
|
|
|
from .layouts import layouts
|
|
|
|
|
from .metadata import Metadata
|
2021-10-04 10:27:07 -07:00
|
|
|
|
from .openorders import OpenOrders
|
2021-08-17 11:42:11 -07:00
|
|
|
|
from .orders import Side
|
2021-07-29 08:19:52 -07:00
|
|
|
|
from .perpaccount import PerpAccount
|
2021-08-30 16:30:22 -07:00
|
|
|
|
from .perpopenorders import PerpOpenOrders
|
|
|
|
|
from .placedorder import PlacedOrder
|
2021-11-09 11:54:50 -08:00
|
|
|
|
from .token import Instrument, Token
|
2021-11-13 11:23:11 -08:00
|
|
|
|
from .tokenbank import TokenBank
|
2021-06-25 02:33:40 -07:00
|
|
|
|
from .version import Version
|
|
|
|
|
|
2021-08-03 04:40:00 -07:00
|
|
|
|
|
2021-11-08 03:39:09 -08:00
|
|
|
|
# # 🥭 AccountSlot class
|
2021-08-03 10:32:12 -07:00
|
|
|
|
#
|
2021-11-08 03:39:09 -08:00
|
|
|
|
# `AccountSlot` gathers slot items together instead of separate arrays.
|
2021-08-03 10:32:12 -07:00
|
|
|
|
#
|
2021-11-08 03:39:09 -08:00
|
|
|
|
class AccountSlot:
|
2021-11-13 11:23:11 -08:00
|
|
|
|
def __init__(self, index: int, base_instrument: Instrument, base_token_bank: typing.Optional[TokenBank], quote_token_bank: TokenBank, raw_deposit: Decimal, deposit: InstrumentValue, raw_borrow: Decimal, borrow: InstrumentValue, spot_open_orders: typing.Optional[PublicKey], perp_account: typing.Optional[PerpAccount]) -> None:
|
2021-11-09 13:08:06 -08:00
|
|
|
|
self.index: int = index
|
2021-11-09 11:54:50 -08:00
|
|
|
|
self.base_instrument: Instrument = base_instrument
|
2021-11-13 11:23:11 -08:00
|
|
|
|
self.base_token_bank: typing.Optional[TokenBank] = base_token_bank
|
|
|
|
|
self.quote_token_bank: TokenBank = quote_token_bank
|
2021-11-04 08:36:14 -07:00
|
|
|
|
self.raw_deposit: Decimal = raw_deposit
|
2021-11-08 03:39:09 -08:00
|
|
|
|
self.deposit: InstrumentValue = deposit
|
2021-11-04 08:36:14 -07:00
|
|
|
|
self.raw_borrow: Decimal = raw_borrow
|
2021-11-08 03:39:09 -08:00
|
|
|
|
self.borrow: InstrumentValue = borrow
|
|
|
|
|
self.spot_open_orders: typing.Optional[PublicKey] = spot_open_orders
|
|
|
|
|
self.perp_account: typing.Optional[PerpAccount] = perp_account
|
2021-08-03 04:40:00 -07:00
|
|
|
|
|
|
|
|
|
@property
|
2021-11-08 03:39:09 -08:00
|
|
|
|
def net_value(self) -> InstrumentValue:
|
2021-08-04 05:08:34 -07:00
|
|
|
|
return self.deposit - self.borrow
|
|
|
|
|
|
2021-11-04 08:36:14 -07:00
|
|
|
|
@property
|
|
|
|
|
def raw_net_value(self) -> Decimal:
|
|
|
|
|
return self.raw_deposit - self.raw_borrow
|
|
|
|
|
|
2021-08-03 04:40:00 -07:00
|
|
|
|
def __str__(self) -> str:
|
|
|
|
|
perp_account: str = "None"
|
|
|
|
|
if self.perp_account is not None:
|
|
|
|
|
perp_account = f"{self.perp_account}".replace("\n", "\n ")
|
2021-11-09 11:54:50 -08:00
|
|
|
|
return f"""« 𝙰𝚌𝚌𝚘𝚞𝚗𝚝𝚂𝚕𝚘𝚝 {self.base_instrument.symbol}
|
2021-08-03 04:40:00 -07:00
|
|
|
|
Net Value: {self.net_value}
|
2021-11-04 08:36:14 -07:00
|
|
|
|
Deposited: {self.deposit} (raw value: {self.raw_deposit})
|
|
|
|
|
Borrowed: {self.borrow} (raw value {self.raw_borrow})
|
2021-08-03 04:40:00 -07:00
|
|
|
|
Spot OpenOrders: {self.spot_open_orders or "None"}
|
|
|
|
|
Perp Account:
|
|
|
|
|
{perp_account}
|
|
|
|
|
»"""
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"{self}"
|
|
|
|
|
|
2021-08-03 10:32:12 -07:00
|
|
|
|
|
2021-06-25 07:50:37 -07:00
|
|
|
|
# # 🥭 Account class
|
2021-06-25 02:33:40 -07:00
|
|
|
|
#
|
2021-06-25 07:50:37 -07:00
|
|
|
|
# `Account` holds information about the account for a particular user/wallet for a particualr `Group`.
|
2021-06-25 02:33:40 -07:00
|
|
|
|
#
|
2021-06-25 07:50:37 -07:00
|
|
|
|
class Account(AddressableAccount):
|
2021-06-25 02:33:40 -07:00
|
|
|
|
def __init__(self, account_info: AccountInfo, version: Version,
|
2021-10-04 10:27:07 -07:00
|
|
|
|
meta_data: Metadata, group_name: str, group_address: PublicKey, owner: PublicKey,
|
2021-11-08 03:39:09 -08:00
|
|
|
|
info: str, shared_quote: AccountSlot,
|
2021-08-04 09:50:38 -07:00
|
|
|
|
in_margin_basket: typing.Sequence[bool],
|
2021-11-08 03:39:09 -08:00
|
|
|
|
slot_indices: typing.Sequence[bool],
|
2021-11-10 09:46:54 -08:00
|
|
|
|
base_slots: typing.Sequence[AccountSlot],
|
2021-11-09 05:23:36 -08:00
|
|
|
|
msrm_amount: Decimal, being_liquidated: bool, is_bankrupt: bool) -> None:
|
2021-06-25 02:33:40 -07:00
|
|
|
|
super().__init__(account_info)
|
|
|
|
|
self.version: Version = version
|
|
|
|
|
|
|
|
|
|
self.meta_data: Metadata = meta_data
|
2021-10-04 10:27:07 -07:00
|
|
|
|
self.group_name: str = group_name
|
|
|
|
|
self.group_address: PublicKey = group_address
|
2021-06-25 02:33:40 -07:00
|
|
|
|
self.owner: PublicKey = owner
|
2021-08-13 04:15:17 -07:00
|
|
|
|
self.info: str = info
|
2021-11-08 03:39:09 -08:00
|
|
|
|
self.shared_quote: AccountSlot = shared_quote
|
2021-08-04 09:50:38 -07:00
|
|
|
|
self.in_margin_basket: typing.Sequence[bool] = in_margin_basket
|
2021-11-08 03:39:09 -08:00
|
|
|
|
self.slot_indices: typing.Sequence[bool] = slot_indices
|
2021-11-10 09:46:54 -08:00
|
|
|
|
self.base_slots: typing.Sequence[AccountSlot] = base_slots
|
2021-07-13 04:43:50 -07:00
|
|
|
|
self.msrm_amount: Decimal = msrm_amount
|
|
|
|
|
self.being_liquidated: bool = being_liquidated
|
2021-07-01 13:24:08 -07:00
|
|
|
|
self.is_bankrupt: bool = is_bankrupt
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
2021-11-08 03:39:09 -08:00
|
|
|
|
@property
|
|
|
|
|
def shared_quote_token(self) -> Token:
|
2021-11-13 11:23:11 -08:00
|
|
|
|
token_bank = self.shared_quote.base_token_bank
|
|
|
|
|
if token_bank is None:
|
2021-11-09 11:54:50 -08:00
|
|
|
|
raise Exception(f"Shared quote does not have a token: {self.shared_quote}")
|
2021-11-13 11:23:11 -08:00
|
|
|
|
return Token.ensure(token_bank.token)
|
2021-11-08 03:39:09 -08:00
|
|
|
|
|
2021-11-10 09:46:54 -08:00
|
|
|
|
@property
|
|
|
|
|
def slots(self) -> typing.Sequence[AccountSlot]:
|
|
|
|
|
return [*[slot for slot in self.base_slots], self.shared_quote]
|
|
|
|
|
|
2021-11-08 03:39:09 -08:00
|
|
|
|
@property
|
|
|
|
|
def slots_by_index(self) -> typing.Sequence[typing.Optional[AccountSlot]]:
|
|
|
|
|
mapped_items: typing.List[typing.Optional[AccountSlot]] = []
|
|
|
|
|
slot_counter = 0
|
|
|
|
|
for available in self.slot_indices:
|
|
|
|
|
if available:
|
2021-11-10 09:46:54 -08:00
|
|
|
|
mapped_items += [self.base_slots[slot_counter]]
|
2021-11-08 03:39:09 -08:00
|
|
|
|
slot_counter += 1
|
|
|
|
|
else:
|
|
|
|
|
mapped_items += [None]
|
|
|
|
|
mapped_items += [self.shared_quote]
|
|
|
|
|
|
|
|
|
|
return mapped_items
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def deposits(self) -> typing.Sequence[InstrumentValue]:
|
|
|
|
|
return [slot.deposit for slot in self.slots]
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def deposits_by_index(self) -> typing.Sequence[typing.Optional[InstrumentValue]]:
|
|
|
|
|
return [slot.deposit if slot is not None else None for slot in self.slots_by_index]
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def borrows(self) -> typing.Sequence[InstrumentValue]:
|
|
|
|
|
return [slot.borrow for slot in self.slots]
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def borrows_by_index(self) -> typing.Sequence[typing.Optional[InstrumentValue]]:
|
|
|
|
|
return [slot.borrow if slot is not None else None for slot in self.slots_by_index]
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def net_values(self) -> typing.Sequence[InstrumentValue]:
|
|
|
|
|
return [slot.net_value for slot in self.slots]
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def net_values_by_index(self) -> typing.Sequence[typing.Optional[InstrumentValue]]:
|
|
|
|
|
return [slot.net_value if slot is not None else None for slot in self.slots_by_index]
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def spot_open_orders(self) -> typing.Sequence[PublicKey]:
|
2021-11-10 09:46:54 -08:00
|
|
|
|
return [slot.spot_open_orders for slot in self.base_slots if slot.spot_open_orders is not None]
|
2021-11-08 03:39:09 -08:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def spot_open_orders_by_index(self) -> typing.Sequence[typing.Optional[PublicKey]]:
|
|
|
|
|
return [slot.spot_open_orders if slot is not None else None for slot in self.slots_by_index]
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def perp_accounts(self) -> typing.Sequence[PerpAccount]:
|
2021-11-10 09:46:54 -08:00
|
|
|
|
return [slot.perp_account for slot in self.base_slots if slot.perp_account is not None]
|
2021-11-08 03:39:09 -08:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def perp_accounts_by_index(self) -> typing.Sequence[typing.Optional[PerpAccount]]:
|
|
|
|
|
return [slot.perp_account if slot is not None else None for slot in self.slots_by_index]
|
|
|
|
|
|
2021-06-25 02:33:40 -07:00
|
|
|
|
@staticmethod
|
2021-11-11 03:56:29 -08:00
|
|
|
|
def from_layout(layout: typing.Any, account_info: AccountInfo, version: Version, group: Group, cache: Cache) -> "Account":
|
2021-06-25 02:33:40 -07:00
|
|
|
|
meta_data = Metadata.from_layout(layout.meta_data)
|
|
|
|
|
owner: PublicKey = layout.owner
|
2021-08-13 04:15:17 -07:00
|
|
|
|
info: str = layout.info
|
2021-11-08 03:39:09 -08:00
|
|
|
|
mngo_token = group.liquidity_incentive_token
|
2021-08-04 05:08:34 -07:00
|
|
|
|
in_margin_basket: typing.Sequence[bool] = list([bool(in_basket) for in_basket in layout.in_margin_basket])
|
2021-08-04 09:50:38 -07:00
|
|
|
|
active_in_basket: typing.List[bool] = []
|
2021-11-08 03:39:09 -08:00
|
|
|
|
slots: typing.List[AccountSlot] = []
|
|
|
|
|
placed_orders_all_markets: typing.List[typing.List[PlacedOrder]] = [[]
|
|
|
|
|
for _ in range(len(group.slot_indices) - 1)]
|
2021-08-17 11:42:11 -07:00
|
|
|
|
for index, order_market in enumerate(layout.order_market):
|
|
|
|
|
if order_market != 0xFF:
|
|
|
|
|
side = Side.from_value(layout.order_side[index])
|
|
|
|
|
id = layout.order_ids[index]
|
|
|
|
|
client_id = layout.client_order_ids[index]
|
|
|
|
|
placed_order = PlacedOrder(id, client_id, side)
|
|
|
|
|
placed_orders_all_markets[int(order_market)] += [placed_order]
|
|
|
|
|
|
2021-11-13 11:23:11 -08:00
|
|
|
|
quote_token_bank: TokenBank = group.shared_quote
|
2021-11-08 03:39:09 -08:00
|
|
|
|
quote_token: Token = group.shared_quote_token
|
2021-10-02 04:28:16 -07:00
|
|
|
|
|
2021-11-09 11:54:50 -08:00
|
|
|
|
for index in range(len(group.slots_by_index)):
|
|
|
|
|
group_slot = group.slots_by_index[index]
|
|
|
|
|
if group_slot is not None:
|
|
|
|
|
instrument = group_slot.base_instrument
|
2021-11-13 11:23:11 -08:00
|
|
|
|
token_bank = group_slot.base_token_bank
|
2021-11-09 11:54:50 -08:00
|
|
|
|
raw_deposit: Decimal = Decimal(0)
|
|
|
|
|
intrinsic_deposit: Decimal = Decimal(0)
|
|
|
|
|
raw_borrow: Decimal = Decimal(0)
|
|
|
|
|
intrinsic_borrow: Decimal = Decimal(0)
|
2021-11-13 11:23:11 -08:00
|
|
|
|
if token_bank is not None:
|
2021-11-09 11:54:50 -08:00
|
|
|
|
raw_deposit = layout.deposits[index]
|
2021-11-13 11:23:11 -08:00
|
|
|
|
root_bank_cache: typing.Optional[RootBankCache] = token_bank.root_bank_cache_from_cache(
|
2021-11-11 03:56:29 -08:00
|
|
|
|
cache, index)
|
|
|
|
|
if root_bank_cache is None:
|
2021-11-13 11:23:11 -08:00
|
|
|
|
raise Exception(f"No root bank cache found for token {token_bank} at index {index}")
|
2021-11-11 03:56:29 -08:00
|
|
|
|
intrinsic_deposit = root_bank_cache.deposit_index * raw_deposit
|
2021-11-09 11:54:50 -08:00
|
|
|
|
raw_borrow = layout.borrows[index]
|
2021-11-11 03:56:29 -08:00
|
|
|
|
intrinsic_borrow = root_bank_cache.borrow_index * raw_borrow
|
2021-11-09 11:54:50 -08:00
|
|
|
|
|
|
|
|
|
deposit = InstrumentValue(instrument, instrument.shift_to_decimals(intrinsic_deposit))
|
|
|
|
|
borrow = InstrumentValue(instrument, instrument.shift_to_decimals(intrinsic_borrow))
|
|
|
|
|
|
2021-08-17 11:42:11 -07:00
|
|
|
|
perp_open_orders = PerpOpenOrders(placed_orders_all_markets[index])
|
2021-11-09 11:54:50 -08:00
|
|
|
|
|
2021-08-17 11:42:11 -07:00
|
|
|
|
perp_account = PerpAccount.from_layout(
|
2021-10-02 04:28:16 -07:00
|
|
|
|
layout.perp_accounts[index],
|
2021-11-09 11:54:50 -08:00
|
|
|
|
instrument,
|
2021-11-08 03:39:09 -08:00
|
|
|
|
quote_token,
|
2021-10-02 04:28:16 -07:00
|
|
|
|
perp_open_orders,
|
2021-11-08 03:39:09 -08:00
|
|
|
|
group_slot.perp_lot_size_converter,
|
|
|
|
|
mngo_token)
|
2021-08-03 04:40:00 -07:00
|
|
|
|
spot_open_orders = layout.spot_open_orders[index]
|
2021-11-13 11:23:11 -08:00
|
|
|
|
account_slot: AccountSlot = AccountSlot(index, instrument, token_bank, quote_token_bank,
|
2021-11-09 11:54:50 -08:00
|
|
|
|
raw_deposit, deposit, raw_borrow, borrow,
|
|
|
|
|
spot_open_orders, perp_account)
|
|
|
|
|
|
2021-11-08 03:39:09 -08:00
|
|
|
|
slots += [account_slot]
|
2021-08-04 09:50:38 -07:00
|
|
|
|
active_in_basket += [True]
|
|
|
|
|
else:
|
|
|
|
|
active_in_basket += [False]
|
2021-07-01 13:24:08 -07:00
|
|
|
|
|
2021-11-11 03:56:29 -08:00
|
|
|
|
quote_index: int = len(layout.deposits) - 1
|
|
|
|
|
raw_quote_deposit: Decimal = layout.deposits[quote_index]
|
2021-11-13 11:23:11 -08:00
|
|
|
|
quote_root_bank_cache: typing.Optional[RootBankCache] = quote_token_bank.root_bank_cache_from_cache(
|
2021-11-11 03:56:29 -08:00
|
|
|
|
cache, quote_index)
|
|
|
|
|
if quote_root_bank_cache is None:
|
2021-11-13 11:23:11 -08:00
|
|
|
|
raise Exception(f"No root bank cache found for quote token {quote_token_bank} at index {index}")
|
2021-11-11 03:56:29 -08:00
|
|
|
|
intrinsic_quote_deposit = quote_root_bank_cache.deposit_index * raw_quote_deposit
|
2021-11-08 03:39:09 -08:00
|
|
|
|
quote_deposit = InstrumentValue(quote_token, quote_token.shift_to_decimals(intrinsic_quote_deposit))
|
2021-11-11 03:56:29 -08:00
|
|
|
|
raw_quote_borrow: Decimal = layout.borrows[quote_index]
|
|
|
|
|
intrinsic_quote_borrow = quote_root_bank_cache.borrow_index * raw_quote_borrow
|
2021-11-08 03:39:09 -08:00
|
|
|
|
quote_borrow = InstrumentValue(quote_token, quote_token.shift_to_decimals(intrinsic_quote_borrow))
|
2021-11-13 11:23:11 -08:00
|
|
|
|
quote: AccountSlot = AccountSlot(len(layout.deposits) - 1, quote_token_bank.token, quote_token_bank,
|
|
|
|
|
quote_token_bank, raw_quote_deposit, quote_deposit, raw_quote_borrow,
|
2021-11-09 13:08:06 -08:00
|
|
|
|
quote_borrow, None, None)
|
2021-08-04 05:08:34 -07:00
|
|
|
|
|
2021-07-13 04:43:50 -07:00
|
|
|
|
msrm_amount: Decimal = layout.msrm_amount
|
2021-07-29 08:19:52 -07:00
|
|
|
|
being_liquidated: bool = bool(layout.being_liquidated)
|
|
|
|
|
is_bankrupt: bool = bool(layout.is_bankrupt)
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
2021-11-08 03:39:09 -08:00
|
|
|
|
return Account(account_info, version, meta_data, group.name, group.address, owner, info, quote, in_margin_basket, active_in_basket, slots, msrm_amount, being_liquidated, is_bankrupt)
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
2021-11-11 03:56:29 -08:00
|
|
|
|
def parse(account_info: AccountInfo, group: Group, cache: Cache) -> "Account":
|
2021-06-25 02:33:40 -07:00
|
|
|
|
data = account_info.data
|
|
|
|
|
if len(data) != layouts.MANGO_ACCOUNT.sizeof():
|
|
|
|
|
raise Exception(
|
2021-07-12 02:57:05 -07:00
|
|
|
|
f"Account data length ({len(data)}) does not match expected size ({layouts.MANGO_ACCOUNT.sizeof()})")
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
|
|
|
|
layout = layouts.MANGO_ACCOUNT.parse(data)
|
2021-11-11 03:56:29 -08:00
|
|
|
|
return Account.from_layout(layout, account_info, Version.V3, group, cache)
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
2021-07-01 13:24:08 -07:00
|
|
|
|
def load(context: Context, address: PublicKey, group: Group) -> "Account":
|
2021-06-25 02:33:40 -07:00
|
|
|
|
account_info = AccountInfo.load(context, address)
|
|
|
|
|
if account_info is None:
|
2021-06-25 07:50:37 -07:00
|
|
|
|
raise Exception(f"Account account not found at address '{address}'")
|
2021-11-11 03:56:29 -08:00
|
|
|
|
cache: Cache = group.fetch_cache(context)
|
|
|
|
|
return Account.parse(account_info, group, cache)
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
2021-10-26 10:45:04 -07:00
|
|
|
|
@staticmethod
|
|
|
|
|
def load_all(context: Context, group: Group) -> typing.Sequence["Account"]:
|
|
|
|
|
# mango_group is just after the METADATA, which is the first entry.
|
|
|
|
|
group_offset = layouts.METADATA.sizeof()
|
|
|
|
|
# owner is just after mango_group in the layout, and it's a PublicKey which is 32 bytes.
|
|
|
|
|
filters = [
|
|
|
|
|
MemcmpOpts(
|
|
|
|
|
offset=group_offset,
|
|
|
|
|
bytes=encode_key(group.address)
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
results = context.client.get_program_accounts(
|
|
|
|
|
context.mango_program_address, memcmp_opts=filters, data_size=layouts.MANGO_ACCOUNT.sizeof())
|
2021-11-11 03:56:29 -08:00
|
|
|
|
cache: Cache = group.fetch_cache(context)
|
|
|
|
|
accounts: typing.List[Account] = []
|
2021-10-26 10:45:04 -07:00
|
|
|
|
for account_data in results:
|
|
|
|
|
address = PublicKey(account_data["pubkey"])
|
|
|
|
|
account_info = AccountInfo._from_response_values(account_data["account"], address)
|
2021-11-11 03:56:29 -08:00
|
|
|
|
account = Account.parse(account_info, group, cache)
|
2021-10-26 10:45:04 -07:00
|
|
|
|
accounts += [account]
|
|
|
|
|
return accounts
|
|
|
|
|
|
2021-06-25 02:33:40 -07:00
|
|
|
|
@staticmethod
|
2021-06-25 07:50:37 -07:00
|
|
|
|
def load_all_for_owner(context: Context, owner: PublicKey, group: Group) -> typing.Sequence["Account"]:
|
2021-06-25 02:33:40 -07:00
|
|
|
|
# mango_group is just after the METADATA, which is the first entry.
|
|
|
|
|
group_offset = layouts.METADATA.sizeof()
|
|
|
|
|
# owner is just after mango_group in the layout, and it's a PublicKey which is 32 bytes.
|
|
|
|
|
owner_offset = group_offset + 32
|
|
|
|
|
filters = [
|
|
|
|
|
MemcmpOpts(
|
|
|
|
|
offset=group_offset,
|
|
|
|
|
bytes=encode_key(group.address)
|
|
|
|
|
),
|
|
|
|
|
MemcmpOpts(
|
|
|
|
|
offset=owner_offset,
|
|
|
|
|
bytes=encode_key(owner)
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
|
2021-10-26 10:45:04 -07:00
|
|
|
|
results = context.client.get_program_accounts(
|
|
|
|
|
context.mango_program_address, memcmp_opts=filters, data_size=layouts.MANGO_ACCOUNT.sizeof())
|
2021-11-11 03:56:29 -08:00
|
|
|
|
cache: Cache = group.fetch_cache(context)
|
|
|
|
|
accounts: typing.List[Account] = []
|
2021-08-07 07:07:19 -07:00
|
|
|
|
for account_data in results:
|
2021-06-25 02:33:40 -07:00
|
|
|
|
address = PublicKey(account_data["pubkey"])
|
|
|
|
|
account_info = AccountInfo._from_response_values(account_data["account"], address)
|
2021-11-11 03:56:29 -08:00
|
|
|
|
account = Account.parse(account_info, group, cache)
|
2021-06-25 02:33:40 -07:00
|
|
|
|
accounts += [account]
|
|
|
|
|
return accounts
|
|
|
|
|
|
2021-07-23 03:42:22 -07:00
|
|
|
|
@staticmethod
|
2021-09-02 09:19:17 -07:00
|
|
|
|
def load_for_owner_by_address(context: Context, owner: PublicKey, group: Group, account_address: typing.Optional[PublicKey]) -> "Account":
|
|
|
|
|
if account_address is not None:
|
|
|
|
|
return Account.load(context, account_address, group)
|
|
|
|
|
|
2021-07-31 06:43:39 -07:00
|
|
|
|
accounts: typing.Sequence[Account] = Account.load_all_for_owner(context, owner, group)
|
2021-09-02 09:19:17 -07:00
|
|
|
|
if len(accounts) > 1:
|
|
|
|
|
raise Exception(f"More than 1 Mango account for owner '{owner}' and which to choose not specified.")
|
|
|
|
|
|
|
|
|
|
return accounts[0]
|
2021-07-23 03:42:22 -07:00
|
|
|
|
|
2021-11-10 09:46:54 -08:00
|
|
|
|
def slot_by_instrument_or_none(self, instrument: Instrument) -> typing.Optional[AccountSlot]:
|
|
|
|
|
for slot in self.slots:
|
|
|
|
|
if slot.base_instrument == instrument:
|
|
|
|
|
return slot
|
|
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def slot_by_instrument(self, instrument: Instrument) -> AccountSlot:
|
|
|
|
|
slot: typing.Optional[AccountSlot] = self.slot_by_instrument_or_none(instrument)
|
|
|
|
|
if slot is not None:
|
|
|
|
|
return slot
|
|
|
|
|
|
|
|
|
|
raise Exception(f"Could not find token {instrument} in account {self.address}")
|
|
|
|
|
|
2021-10-04 10:27:07 -07:00
|
|
|
|
def load_all_spot_open_orders(self, context: Context) -> typing.Dict[str, OpenOrders]:
|
2021-11-08 03:39:09 -08:00
|
|
|
|
spot_open_orders_account_infos = AccountInfo.load_multiple(context, self.spot_open_orders)
|
2021-10-04 10:27:07 -07:00
|
|
|
|
spot_open_orders_account_infos_by_address = {
|
|
|
|
|
str(account_info.address): account_info for account_info in spot_open_orders_account_infos}
|
|
|
|
|
spot_open_orders: typing.Dict[str, OpenOrders] = {}
|
2021-11-10 09:46:54 -08:00
|
|
|
|
for slot in self.base_slots:
|
2021-11-08 03:39:09 -08:00
|
|
|
|
if slot.spot_open_orders is not None:
|
|
|
|
|
account_info = spot_open_orders_account_infos_by_address[str(slot.spot_open_orders)]
|
2021-11-09 11:54:50 -08:00
|
|
|
|
oo = OpenOrders.parse(account_info, slot.base_instrument.decimals,
|
|
|
|
|
self.shared_quote.base_instrument.decimals)
|
2021-11-08 03:39:09 -08:00
|
|
|
|
spot_open_orders[str(slot.spot_open_orders)] = oo
|
2021-10-04 10:27:07 -07:00
|
|
|
|
return spot_open_orders
|
|
|
|
|
|
2021-08-04 05:08:34 -07:00
|
|
|
|
def update_spot_open_orders_for_market(self, spot_market_index: int, spot_open_orders: PublicKey) -> None:
|
2021-11-08 03:39:09 -08:00
|
|
|
|
item_to_update = self.slots_by_index[spot_market_index]
|
2021-08-04 05:08:34 -07:00
|
|
|
|
if item_to_update is None:
|
|
|
|
|
raise Exception(f"Could not find AccountBasketItem in Account {self.address} at index {spot_market_index}.")
|
|
|
|
|
item_to_update.spot_open_orders = spot_open_orders
|
2021-08-03 04:40:00 -07:00
|
|
|
|
|
2021-08-01 10:03:46 -07:00
|
|
|
|
def __str__(self) -> str:
|
2021-08-13 04:15:17 -07:00
|
|
|
|
info = f"'{self.info}'" if self.info else "(𝑢𝑛-𝑛𝑎𝑚𝑒𝑑)"
|
2021-11-08 03:39:09 -08:00
|
|
|
|
shared_quote: str = f"{self.shared_quote}".replace("\n", "\n ")
|
2021-11-10 09:46:54 -08:00
|
|
|
|
slot_count = len(self.base_slots)
|
|
|
|
|
slots = "\n ".join([f"{item}".replace("\n", "\n ") for item in self.base_slots])
|
2021-11-08 03:39:09 -08:00
|
|
|
|
|
2021-11-10 09:46:54 -08:00
|
|
|
|
symbols: typing.Sequence[str] = [slot.base_instrument.symbol for slot in self.base_slots]
|
2021-11-08 03:39:09 -08:00
|
|
|
|
in_margin_basket = ", ".join(symbols) or "None"
|
2021-08-13 04:15:17 -07:00
|
|
|
|
return f"""« 𝙰𝚌𝚌𝚘𝚞𝚗𝚝 {info}, {self.version} [{self.address}]
|
2021-06-25 02:33:40 -07:00
|
|
|
|
{self.meta_data}
|
|
|
|
|
Owner: {self.owner}
|
2021-10-04 10:27:07 -07:00
|
|
|
|
Group: « 𝙶𝚛𝚘𝚞𝚙 '{self.group_name}' [{self.group_address}] »
|
2021-08-03 04:40:00 -07:00
|
|
|
|
MSRM: {self.msrm_amount}
|
2021-07-29 08:19:52 -07:00
|
|
|
|
Bankrupt? {self.is_bankrupt}
|
|
|
|
|
Being Liquidated? {self.being_liquidated}
|
2021-08-07 10:42:39 -07:00
|
|
|
|
Shared Quote Token:
|
2021-11-08 03:39:09 -08:00
|
|
|
|
{shared_quote}
|
2021-08-03 04:40:00 -07:00
|
|
|
|
In Basket: {in_margin_basket}
|
2021-11-08 03:39:09 -08:00
|
|
|
|
Basket [{slot_count} in basket]:
|
|
|
|
|
{slots}
|
2021-06-25 02:33:40 -07:00
|
|
|
|
»"""
|
2021-07-12 02:57:05 -07:00
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"{self}"
|