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
|
2021-08-19 03:00:39 -07:00
|
|
|
from solana.publickey import PublicKey
|
2021-06-25 02:33:40 -07:00
|
|
|
|
2021-06-25 07:50:37 -07:00
|
|
|
from .account import Account
|
2021-07-12 09:18:56 -07:00
|
|
|
from .combinableinstructions import CombinableInstructions
|
2021-08-19 03:00:39 -07:00
|
|
|
from .constants import SYSTEM_PROGRAM_ADDRESS
|
2021-06-25 02:33:40 -07:00
|
|
|
from .context import Context
|
2021-11-08 04:43:11 -08:00
|
|
|
from .group import Group
|
|
|
|
from .instructions import build_cancel_perp_order_instructions, build_mango_consume_events_instructions, build_place_perp_order_instructions, build_redeem_accrued_mango_instructions
|
|
|
|
from .marketoperations import MarketInstructionBuilder, MarketOperations
|
2021-10-26 10:45:04 -07:00
|
|
|
from .orders import Order, OrderBook
|
2021-07-23 06:18:26 -07:00
|
|
|
from .perpmarket import PerpMarket
|
2022-01-10 06:38:05 -08:00
|
|
|
from .publickey import encode_public_key_for_sorting
|
2021-11-13 11:23:11 -08:00
|
|
|
from .tokenbank import TokenBank
|
2021-06-25 02:33:40 -07:00
|
|
|
from .wallet import Wallet
|
|
|
|
|
|
|
|
|
2021-11-08 04:43:11 -08:00
|
|
|
# # 🥭 PerpMarketInstructionBuilder
|
|
|
|
#
|
|
|
|
# This file deals with building instructions for Perp markets.
|
|
|
|
#
|
|
|
|
# As a matter of policy for all InstructionBuidlers, construction and build_* methods should all work with
|
|
|
|
# existing data, requiring no fetches from Solana or other sources. All necessary data should all be loaded
|
|
|
|
# on initial setup in the `load()` method.
|
|
|
|
#
|
|
|
|
class PerpMarketInstructionBuilder(MarketInstructionBuilder):
|
2021-11-09 05:23:36 -08:00
|
|
|
def __init__(self, context: Context, wallet: Wallet, group: Group, account: Account,
|
|
|
|
perp_market: PerpMarket) -> None:
|
2021-11-08 04:43:11 -08:00
|
|
|
super().__init__()
|
|
|
|
self.context: Context = context
|
|
|
|
self.wallet: Wallet = wallet
|
|
|
|
self.group: Group = group
|
|
|
|
self.account: Account = account
|
|
|
|
self.perp_market: PerpMarket = perp_market
|
2021-11-13 11:23:11 -08:00
|
|
|
self.mngo_token_bank: TokenBank = self.group.liquidity_incentive_token_bank
|
2021-11-08 04:43:11 -08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load(context: Context, wallet: Wallet, group: Group, account: Account, perp_market: PerpMarket) -> "PerpMarketInstructionBuilder":
|
|
|
|
return PerpMarketInstructionBuilder(context, wallet, group, account, perp_market)
|
|
|
|
|
|
|
|
def build_cancel_order_instructions(self, order: Order, ok_if_missing: bool = False) -> CombinableInstructions:
|
|
|
|
if self.perp_market.underlying_perp_market is None:
|
|
|
|
raise Exception(f"PerpMarket {self.perp_market.symbol} has not been loaded.")
|
|
|
|
return build_cancel_perp_order_instructions(
|
|
|
|
self.context, self.wallet, self.account, self.perp_market.underlying_perp_market, order, ok_if_missing)
|
|
|
|
|
|
|
|
def build_place_order_instructions(self, order: Order) -> CombinableInstructions:
|
|
|
|
if self.perp_market.underlying_perp_market is None:
|
|
|
|
raise Exception(f"PerpMarket {self.perp_market.symbol} has not been loaded.")
|
|
|
|
return build_place_perp_order_instructions(
|
|
|
|
self.context, self.wallet, self.perp_market.underlying_perp_market.group, self.account, self.perp_market.underlying_perp_market, order.price, order.quantity, order.client_id, order.side, order.order_type)
|
|
|
|
|
|
|
|
def build_settle_instructions(self) -> CombinableInstructions:
|
|
|
|
return CombinableInstructions.empty()
|
|
|
|
|
|
|
|
def build_crank_instructions(self, account_addresses: typing.Sequence[PublicKey], limit: Decimal = Decimal(32)) -> CombinableInstructions:
|
|
|
|
if self.perp_market.underlying_perp_market is None:
|
|
|
|
raise Exception(f"PerpMarket {self.perp_market.symbol} has not been loaded.")
|
2022-01-10 06:38:05 -08:00
|
|
|
|
|
|
|
addresses_to_crank: typing.Sequence[PublicKey] = [*account_addresses, self.account.address]
|
|
|
|
distinct_addresses_to_crank: typing.List[PublicKey] = []
|
|
|
|
for address in addresses_to_crank:
|
|
|
|
if address not in distinct_addresses_to_crank:
|
|
|
|
distinct_addresses_to_crank += [address]
|
|
|
|
|
|
|
|
limited_addresses_to_crank = distinct_addresses_to_crank[0:min(
|
|
|
|
int(limit), len(distinct_addresses_to_crank))]
|
|
|
|
|
|
|
|
limited_addresses_to_crank.sort(key=encode_public_key_for_sorting)
|
|
|
|
|
|
|
|
return build_mango_consume_events_instructions(self.context, self.group, self.perp_market.underlying_perp_market, limited_addresses_to_crank, limit)
|
2021-11-08 04:43:11 -08:00
|
|
|
|
|
|
|
def build_redeem_instructions(self) -> CombinableInstructions:
|
2021-11-13 11:23:11 -08:00
|
|
|
return build_redeem_accrued_mango_instructions(self.context, self.wallet, self.perp_market, self.group, self.account, self.mngo_token_bank)
|
2021-11-08 04:43:11 -08:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2021-12-13 04:06:42 -08:00
|
|
|
return """« PerpMarketInstructionBuilder »"""
|
2021-11-08 04:43:11 -08:00
|
|
|
|
|
|
|
|
2021-06-25 02:33:40 -07:00
|
|
|
# # 🥭 PerpMarketOperations
|
|
|
|
#
|
|
|
|
# This file deals with placing orders for Perps.
|
|
|
|
#
|
|
|
|
class PerpMarketOperations(MarketOperations):
|
|
|
|
def __init__(self, market_name: str, context: Context, wallet: Wallet,
|
2021-07-12 10:26:35 -07:00
|
|
|
market_instruction_builder: PerpMarketInstructionBuilder,
|
2021-11-09 05:23:36 -08:00
|
|
|
account: Account, perp_market: PerpMarket) -> None:
|
2021-09-30 10:53:33 -07:00
|
|
|
super().__init__(perp_market)
|
2021-06-25 02:33:40 -07:00
|
|
|
self.market_name: str = market_name
|
|
|
|
self.context: Context = context
|
|
|
|
self.wallet: Wallet = wallet
|
2021-07-12 10:26:35 -07:00
|
|
|
self.market_instruction_builder: PerpMarketInstructionBuilder = market_instruction_builder
|
|
|
|
self.account: Account = account
|
2021-07-23 06:18:26 -07:00
|
|
|
self.perp_market: PerpMarket = perp_market
|
2021-07-23 02:20:44 -07:00
|
|
|
|
2021-08-17 11:42:11 -07:00
|
|
|
def cancel_order(self, order: Order, ok_if_missing: bool = False) -> typing.Sequence[str]:
|
2021-12-13 03:15:24 -08:00
|
|
|
self._logger.info(f"Cancelling {self.market_name} order {order}.")
|
2021-07-12 09:18:56 -07:00
|
|
|
signers: CombinableInstructions = CombinableInstructions.from_wallet(self.wallet)
|
2021-08-17 11:42:11 -07:00
|
|
|
cancel: CombinableInstructions = self.market_instruction_builder.build_cancel_order_instructions(
|
|
|
|
order, ok_if_missing=ok_if_missing)
|
2021-07-23 06:18:26 -07:00
|
|
|
accounts_to_crank = self.perp_market.accounts_to_crank(self.context, self.account.address)
|
2021-07-23 02:20:44 -07:00
|
|
|
crank = self.market_instruction_builder.build_crank_instructions(accounts_to_crank)
|
2021-08-16 06:43:44 -07:00
|
|
|
settle = self.market_instruction_builder.build_settle_instructions()
|
|
|
|
return (signers + cancel + crank + settle).execute(self.context)
|
2021-06-25 02:33:40 -07:00
|
|
|
|
2021-07-28 07:29:24 -07:00
|
|
|
def place_order(self, order: Order) -> Order:
|
2021-09-07 11:07:22 -07:00
|
|
|
client_id: int = self.context.generate_client_id()
|
2021-07-12 09:18:56 -07:00
|
|
|
signers: CombinableInstructions = CombinableInstructions.from_wallet(self.wallet)
|
2021-07-28 07:29:24 -07:00
|
|
|
order_with_client_id: Order = order.with_client_id(client_id)
|
2021-12-13 03:15:24 -08:00
|
|
|
self._logger.info(f"Placing {self.market_name} order {order_with_client_id}.")
|
2021-07-28 07:29:24 -07:00
|
|
|
place: CombinableInstructions = self.market_instruction_builder.build_place_order_instructions(
|
|
|
|
order_with_client_id)
|
2021-07-23 06:18:26 -07:00
|
|
|
accounts_to_crank = self.perp_market.accounts_to_crank(self.context, self.account.address)
|
2021-07-23 02:20:44 -07:00
|
|
|
crank = self.market_instruction_builder.build_crank_instructions(accounts_to_crank)
|
2021-08-16 06:43:44 -07:00
|
|
|
settle = self.market_instruction_builder.build_settle_instructions()
|
|
|
|
(signers + place + crank + settle).execute(self.context)
|
2021-07-28 07:29:24 -07:00
|
|
|
return order_with_client_id
|
2021-06-25 02:33:40 -07:00
|
|
|
|
2021-07-19 07:04:53 -07:00
|
|
|
def settle(self) -> typing.Sequence[str]:
|
2021-07-22 07:18:37 -07:00
|
|
|
signers: CombinableInstructions = CombinableInstructions.from_wallet(self.wallet)
|
|
|
|
settle = self.market_instruction_builder.build_settle_instructions()
|
|
|
|
return (signers + settle).execute(self.context)
|
2021-07-19 07:04:53 -07:00
|
|
|
|
|
|
|
def crank(self, limit: Decimal = Decimal(32)) -> typing.Sequence[str]:
|
2021-07-22 07:18:37 -07:00
|
|
|
signers: CombinableInstructions = CombinableInstructions.from_wallet(self.wallet)
|
2021-08-16 06:43:44 -07:00
|
|
|
accounts_to_crank = self.perp_market.accounts_to_crank(self.context, None)
|
2021-07-23 02:20:44 -07:00
|
|
|
crank = self.market_instruction_builder.build_crank_instructions(accounts_to_crank, limit)
|
2021-07-22 07:18:37 -07:00
|
|
|
return (signers + crank).execute(self.context)
|
2021-07-19 07:04:53 -07:00
|
|
|
|
2021-08-19 03:00:39 -07:00
|
|
|
def create_openorders(self) -> PublicKey:
|
|
|
|
return SYSTEM_PROGRAM_ADDRESS
|
|
|
|
|
|
|
|
def ensure_openorders(self) -> PublicKey:
|
|
|
|
return SYSTEM_PROGRAM_ADDRESS
|
|
|
|
|
2021-10-26 10:45:04 -07:00
|
|
|
def load_orderbook(self) -> OrderBook:
|
|
|
|
return self.perp_market.fetch_orderbook(self.context)
|
2021-06-25 02:33:40 -07:00
|
|
|
|
2021-06-25 07:50:37 -07:00
|
|
|
def load_my_orders(self) -> typing.Sequence[Order]:
|
2021-10-26 10:45:04 -07:00
|
|
|
orderbook: OrderBook = self.load_orderbook()
|
|
|
|
return list([o for o in [*orderbook.bids, *orderbook.asks] if o.owner == self.account.address])
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2021-12-13 04:06:42 -08:00
|
|
|
return f"""« PerpMarketOperations [{self.market_name}] »"""
|