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)
|
|
|
|
|
2021-08-07 07:07:19 -07:00
|
|
|
import typing
|
2021-06-25 02:33:40 -07:00
|
|
|
|
2021-06-25 07:50:37 -07:00
|
|
|
from .account import Account
|
2021-06-25 02:33:40 -07:00
|
|
|
from .context import Context
|
2021-07-23 06:18:26 -07:00
|
|
|
from .ensuremarketloaded import ensure_market_loaded
|
2021-06-25 02:33:40 -07:00
|
|
|
from .market import Market
|
2022-02-09 11:31:50 -08:00
|
|
|
from .marketoperations import (
|
|
|
|
MarketInstructionBuilder,
|
|
|
|
MarketOperations,
|
|
|
|
NullMarketInstructionBuilder,
|
|
|
|
NullMarketOperations,
|
|
|
|
)
|
2021-11-08 04:43:11 -08:00
|
|
|
from .perpmarketoperations import PerpMarketInstructionBuilder, PerpMarketOperations
|
2021-07-23 06:18:26 -07:00
|
|
|
from .perpmarket import PerpMarket
|
|
|
|
from .serummarket import SerumMarket
|
2021-11-08 04:43:11 -08:00
|
|
|
from .serummarketoperations import SerumMarketInstructionBuilder, SerumMarketOperations
|
2021-07-23 06:18:26 -07:00
|
|
|
from .spotmarket import SpotMarket
|
2021-11-08 04:43:11 -08:00
|
|
|
from .spotmarketoperations import SpotMarketInstructionBuilder, SpotMarketOperations
|
2021-06-25 02:33:40 -07:00
|
|
|
from .wallet import Wallet
|
|
|
|
|
2021-11-08 04:43:11 -08:00
|
|
|
|
|
|
|
# # 🥭 create_market_instruction_builder
|
2021-06-25 02:33:40 -07:00
|
|
|
#
|
2021-11-08 04:43:11 -08:00
|
|
|
# This function deals with the creation of a `MarketInstructionBuilder` object for a given `Market`.
|
|
|
|
#
|
2022-02-09 11:31:50 -08:00
|
|
|
def create_market_instruction_builder(
|
|
|
|
context: Context,
|
|
|
|
wallet: Wallet,
|
|
|
|
account: Account,
|
|
|
|
market: Market,
|
|
|
|
dry_run: bool = False,
|
|
|
|
) -> MarketInstructionBuilder:
|
2021-11-08 04:43:11 -08:00
|
|
|
if dry_run:
|
|
|
|
return NullMarketInstructionBuilder(market.symbol)
|
|
|
|
|
|
|
|
loaded_market: Market = ensure_market_loaded(context, market)
|
|
|
|
if isinstance(loaded_market, SerumMarket):
|
|
|
|
return SerumMarketInstructionBuilder.load(context, wallet, loaded_market)
|
|
|
|
elif isinstance(loaded_market, SpotMarket):
|
2022-02-09 11:31:50 -08:00
|
|
|
return SpotMarketInstructionBuilder.load(
|
|
|
|
context, wallet, loaded_market, loaded_market.group, account
|
|
|
|
)
|
2021-11-08 04:43:11 -08:00
|
|
|
elif isinstance(loaded_market, PerpMarket):
|
2022-02-09 11:31:50 -08:00
|
|
|
return PerpMarketInstructionBuilder.load(
|
|
|
|
context, wallet, loaded_market, loaded_market.group, account
|
|
|
|
)
|
2021-11-08 04:43:11 -08:00
|
|
|
else:
|
2022-02-09 11:31:50 -08:00
|
|
|
raise Exception(
|
|
|
|
f"Could not find market instructions builder for market {market.symbol}"
|
|
|
|
)
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
|
|
|
2021-11-08 04:43:11 -08:00
|
|
|
# # 🥭 create_market_operations
|
|
|
|
#
|
|
|
|
# This function deals with the creation of a `MarketOperations` object for a given `Market`.
|
|
|
|
#
|
2022-02-09 11:31:50 -08:00
|
|
|
def create_market_operations(
|
|
|
|
context: Context,
|
|
|
|
wallet: Wallet,
|
|
|
|
account: typing.Optional[Account],
|
|
|
|
market: Market,
|
|
|
|
dry_run: bool = False,
|
|
|
|
) -> MarketOperations:
|
2021-06-25 02:33:40 -07:00
|
|
|
if dry_run:
|
2022-02-22 06:25:53 -08:00
|
|
|
return NullMarketOperations(market)
|
2021-07-23 06:18:26 -07:00
|
|
|
|
|
|
|
loaded_market: Market = ensure_market_loaded(context, market)
|
|
|
|
if isinstance(loaded_market, SerumMarket):
|
2022-02-09 11:31:50 -08:00
|
|
|
serum_market_instruction_builder: SerumMarketInstructionBuilder = (
|
|
|
|
SerumMarketInstructionBuilder.load(context, wallet, loaded_market)
|
|
|
|
)
|
2022-01-29 06:59:30 -08:00
|
|
|
return SerumMarketOperations(context, wallet, serum_market_instruction_builder)
|
2021-07-23 06:18:26 -07:00
|
|
|
elif isinstance(loaded_market, SpotMarket):
|
2021-08-07 07:07:19 -07:00
|
|
|
if account is None:
|
|
|
|
raise Exception("Account is required for SpotMarket operations.")
|
2022-02-09 11:31:50 -08:00
|
|
|
spot_market_instruction_builder: SpotMarketInstructionBuilder = (
|
|
|
|
SpotMarketInstructionBuilder.load(
|
|
|
|
context, wallet, loaded_market, loaded_market.group, account
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return SpotMarketOperations(
|
|
|
|
context, wallet, account, spot_market_instruction_builder
|
|
|
|
)
|
2021-07-23 06:18:26 -07:00
|
|
|
elif isinstance(loaded_market, PerpMarket):
|
2021-08-07 07:07:19 -07:00
|
|
|
if account is None:
|
|
|
|
raise Exception("Account is required for PerpMarket operations.")
|
2022-02-09 11:31:50 -08:00
|
|
|
perp_market_instruction_builder: PerpMarketInstructionBuilder = (
|
|
|
|
PerpMarketInstructionBuilder.load(
|
|
|
|
context,
|
|
|
|
wallet,
|
|
|
|
loaded_market,
|
|
|
|
loaded_market.underlying_perp_market.group,
|
|
|
|
account,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return PerpMarketOperations(
|
|
|
|
context, wallet, account, perp_market_instruction_builder
|
|
|
|
)
|
2021-06-25 02:33:40 -07:00
|
|
|
else:
|
2022-02-09 11:31:50 -08:00
|
|
|
raise Exception(
|
|
|
|
f"Could not find market operations handler for market {market.symbol}"
|
|
|
|
)
|