2021-08-02 09:14:00 -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 logging
|
|
|
|
|
|
2021-08-27 12:37:23 -07:00
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
2021-08-02 09:14:00 -07:00
|
|
|
|
from .account import Account
|
2021-08-30 16:30:22 -07:00
|
|
|
|
from .cache import Cache
|
|
|
|
|
from .collateralcalculator import CollateralCalculator, SpotCollateralCalculator, PerpCollateralCalculator
|
2021-08-20 16:00:34 -07:00
|
|
|
|
from .group import Group
|
2021-08-02 09:14:00 -07:00
|
|
|
|
from .market import InventorySource, Market
|
2021-08-20 16:00:34 -07:00
|
|
|
|
from .perpmarket import PerpMarket
|
2021-08-02 09:14:00 -07:00
|
|
|
|
from .tokenvalue import TokenValue
|
|
|
|
|
from .watcher import Watcher
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # 🥭 Inventory class
|
|
|
|
|
#
|
|
|
|
|
# This class details inventory of a crypto account for a market.
|
|
|
|
|
#
|
|
|
|
|
class Inventory:
|
2021-08-30 16:30:22 -07:00
|
|
|
|
def __init__(self, inventory_source: InventorySource, liquidity_incentives: TokenValue, available_collateral: TokenValue, base: TokenValue, quote: TokenValue):
|
2021-08-02 09:14:00 -07:00
|
|
|
|
self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)
|
|
|
|
|
self.inventory_source: InventorySource = inventory_source
|
2021-08-30 16:30:22 -07:00
|
|
|
|
self.available_collateral: TokenValue = available_collateral
|
2021-08-27 12:37:23 -07:00
|
|
|
|
self.liquidity_incentives: TokenValue = liquidity_incentives
|
2021-08-02 09:14:00 -07:00
|
|
|
|
self.base: TokenValue = base
|
|
|
|
|
self.quote: TokenValue = quote
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def symbol(self) -> str:
|
|
|
|
|
return f"{self.base.token.symbol}/{self.quote.token.symbol}"
|
|
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2021-08-27 12:37:23 -07:00
|
|
|
|
liquidity_incentives: str = ""
|
|
|
|
|
if self.liquidity_incentives.value > 0:
|
|
|
|
|
liquidity_incentives = f" {self.liquidity_incentives}"
|
2021-08-30 16:30:22 -07:00
|
|
|
|
return f"« 𝙸𝚗𝚟𝚎𝚗𝚝𝚘𝚛𝚢 {self.symbol}{liquidity_incentives} [{self.base} / {self.quote}] ({self.available_collateral} available) »"
|
2021-08-02 09:14:00 -07:00
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"{self}"
|
|
|
|
|
|
|
|
|
|
|
2021-08-20 16:00:34 -07:00
|
|
|
|
class SpotInventoryAccountWatcher:
|
2021-08-30 16:30:22 -07:00
|
|
|
|
def __init__(self, market: Market, account_watcher: Watcher[Account], cache_watcher: Watcher[Cache]):
|
2021-08-02 09:14:00 -07:00
|
|
|
|
self.account_watcher: Watcher[Account] = account_watcher
|
2021-08-30 16:30:22 -07:00
|
|
|
|
self.cache_watcher: Watcher[Cache] = cache_watcher
|
2021-08-02 09:14:00 -07:00
|
|
|
|
account: Account = account_watcher.latest
|
|
|
|
|
base_value = TokenValue.find_by_symbol(account.net_assets, market.base.symbol)
|
|
|
|
|
self.base_index: int = account.net_assets.index(base_value)
|
|
|
|
|
quote_value = TokenValue.find_by_symbol(account.net_assets, market.quote.symbol)
|
|
|
|
|
self.quote_index: int = account.net_assets.index(quote_value)
|
2021-08-30 16:30:22 -07:00
|
|
|
|
self.collateral_calculator: CollateralCalculator = SpotCollateralCalculator()
|
2021-08-02 09:14:00 -07:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def latest(self) -> Inventory:
|
2021-08-27 12:37:23 -07:00
|
|
|
|
account: Account = self.account_watcher.latest
|
2021-08-30 16:30:22 -07:00
|
|
|
|
cache: Cache = self.cache_watcher.latest
|
2021-08-27 12:37:23 -07:00
|
|
|
|
|
|
|
|
|
# Spot markets don't accrue MNGO liquidity incentives
|
|
|
|
|
mngo = account.group.find_token_info_by_symbol("MNGO").token
|
|
|
|
|
mngo_accrued: TokenValue = TokenValue(mngo, Decimal(0))
|
|
|
|
|
|
2021-08-30 16:30:22 -07:00
|
|
|
|
available_collateral: TokenValue = self.collateral_calculator.calculate(account, cache)
|
|
|
|
|
|
2021-08-27 12:37:23 -07:00
|
|
|
|
base_value = account.net_assets[self.base_index]
|
2021-08-02 09:14:00 -07:00
|
|
|
|
if base_value is None:
|
|
|
|
|
raise Exception(
|
2021-08-27 12:37:23 -07:00
|
|
|
|
f"Could not find net assets in account {account.address} at index {self.base_index}.")
|
|
|
|
|
quote_value = account.net_assets[self.quote_index]
|
2021-08-02 09:14:00 -07:00
|
|
|
|
if quote_value is None:
|
|
|
|
|
raise Exception(
|
2021-08-27 12:37:23 -07:00
|
|
|
|
f"Could not find net assets in account {account.address} at index {self.quote_index}.")
|
|
|
|
|
|
2021-08-30 16:30:22 -07:00
|
|
|
|
return Inventory(InventorySource.ACCOUNT, mngo_accrued, available_collateral, base_value, quote_value)
|
2021-08-20 16:00:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PerpInventoryAccountWatcher:
|
2021-08-30 16:30:22 -07:00
|
|
|
|
def __init__(self, market: PerpMarket, account_watcher: Watcher[Account], cache_watcher: Watcher[Cache], group: Group):
|
2021-08-20 16:00:34 -07:00
|
|
|
|
self.market: PerpMarket = market
|
|
|
|
|
self.account_watcher: Watcher[Account] = account_watcher
|
2021-08-30 16:30:22 -07:00
|
|
|
|
self.cache_watcher: Watcher[Cache] = cache_watcher
|
2021-08-20 16:00:34 -07:00
|
|
|
|
self.perp_account_index: int = group.find_perp_market_index(market.address)
|
|
|
|
|
account: Account = account_watcher.latest
|
|
|
|
|
quote_value = TokenValue.find_by_symbol(account.net_assets, market.quote.symbol)
|
|
|
|
|
self.quote_index: int = account.net_assets.index(quote_value)
|
2021-08-30 16:30:22 -07:00
|
|
|
|
self.collateral_calculator: CollateralCalculator = PerpCollateralCalculator()
|
2021-08-20 16:00:34 -07:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def latest(self) -> Inventory:
|
2021-08-30 16:30:22 -07:00
|
|
|
|
account: Account = self.account_watcher.latest
|
|
|
|
|
cache: Cache = self.cache_watcher.latest
|
|
|
|
|
perp_account = account.perp_accounts[self.perp_account_index]
|
2021-08-20 16:00:34 -07:00
|
|
|
|
if perp_account is None:
|
|
|
|
|
raise Exception(
|
2021-08-30 16:30:22 -07:00
|
|
|
|
f"Could not find perp account for {self.market.symbol} in account {account.address} at index {self.perp_account_index}.")
|
|
|
|
|
|
|
|
|
|
available_collateral: TokenValue = self.collateral_calculator.calculate(account, cache)
|
|
|
|
|
|
2021-08-20 16:00:34 -07:00
|
|
|
|
base_lots = perp_account.base_position
|
|
|
|
|
base_value = self.market.lot_size_converter.quantity_lots_to_value(base_lots)
|
|
|
|
|
base_token_value = TokenValue(self.market.base, base_value)
|
2021-08-30 16:30:22 -07:00
|
|
|
|
quote_token_value = account.net_assets[self.quote_index]
|
2021-08-20 16:00:34 -07:00
|
|
|
|
if quote_token_value is None:
|
2021-08-30 16:30:22 -07:00
|
|
|
|
raise Exception(f"Could not find net assets in account {account.address} at index {self.quote_index}.")
|
|
|
|
|
return Inventory(InventorySource.ACCOUNT, perp_account.mngo_accrued, available_collateral, base_token_value, quote_token_value)
|