style: linter
This commit is contained in:
parent
a4071c9ec4
commit
48e104b3a6
|
@ -35,9 +35,7 @@ class AsyncMarket(MarketCore):
|
|||
market_state: MarketState,
|
||||
force_use_request_queue: bool = False,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
market_state=market_state, force_use_request_queue=force_use_request_queue
|
||||
)
|
||||
super().__init__(market_state=market_state, force_use_request_queue=force_use_request_queue)
|
||||
self._conn = conn
|
||||
|
||||
@classmethod
|
||||
|
@ -58,9 +56,7 @@ class AsyncMarket(MarketCore):
|
|||
market_state = await MarketState.async_load(conn, market_address, program_id)
|
||||
return cls(conn, market_state, force_use_request_queue)
|
||||
|
||||
async def find_open_orders_accounts_for_owner(
|
||||
self, owner_address: PublicKey
|
||||
) -> List[AsyncOpenOrdersAccount]:
|
||||
async def find_open_orders_accounts_for_owner(self, owner_address: PublicKey) -> List[AsyncOpenOrdersAccount]:
|
||||
return await AsyncOpenOrdersAccount.find_for_market_and_owner(
|
||||
self._conn, self.state.public_key(), owner_address, self.state.program_id()
|
||||
)
|
||||
|
@ -79,9 +75,7 @@ class AsyncMarket(MarketCore):
|
|||
"""Load orders for owner."""
|
||||
bids = await self.load_bids()
|
||||
asks = await self.load_asks()
|
||||
open_orders_accounts = await self.find_open_orders_accounts_for_owner(
|
||||
owner_address
|
||||
)
|
||||
open_orders_accounts = await self.find_open_orders_accounts_for_owner(owner_address)
|
||||
return self._parse_orders_for_owner(bids, asks, open_orders_accounts)
|
||||
|
||||
async def load_event_queue(self) -> List[t.Event]:
|
||||
|
@ -113,15 +107,11 @@ class AsyncMarket(MarketCore):
|
|||
) -> SendTransactionResp: # TODO: Add open_orders_address_key param and fee_discount_pubkey
|
||||
transaction = Transaction()
|
||||
signers: List[Keypair] = [owner]
|
||||
open_order_accounts = await self.find_open_orders_accounts_for_owner(
|
||||
owner.public_key
|
||||
)
|
||||
open_order_accounts = await self.find_open_orders_accounts_for_owner(owner.public_key)
|
||||
if open_order_accounts:
|
||||
place_order_open_order_account = open_order_accounts[0].address
|
||||
else:
|
||||
mbfre_resp = await self._conn.get_minimum_balance_for_rent_exemption(
|
||||
OPEN_ORDERS_LAYOUT.sizeof()
|
||||
)
|
||||
mbfre_resp = await self._conn.get_minimum_balance_for_rent_exemption(OPEN_ORDERS_LAYOUT.sizeof())
|
||||
place_order_open_order_account = self._after_oo_mbfre_resp(
|
||||
mbfre_resp=mbfre_resp,
|
||||
owner=owner,
|
||||
|
@ -158,15 +148,11 @@ class AsyncMarket(MarketCore):
|
|||
)
|
||||
return await self._conn.send_transaction(txs, owner, opts=opts)
|
||||
|
||||
async def cancel_order(
|
||||
self, owner: Keypair, order: t.Order, opts: TxOpts = TxOpts()
|
||||
) -> SendTransactionResp:
|
||||
async def cancel_order(self, owner: Keypair, order: t.Order, opts: TxOpts = TxOpts()) -> SendTransactionResp:
|
||||
txn = self._build_cancel_order_tx(owner=owner, order=order)
|
||||
return await self._conn.send_transaction(txn, owner, opts=opts)
|
||||
|
||||
async def match_orders(
|
||||
self, fee_payer: Keypair, limit: int, opts: TxOpts = TxOpts()
|
||||
) -> SendTransactionResp:
|
||||
async def match_orders(self, fee_payer: Keypair, limit: int, opts: TxOpts = TxOpts()) -> SendTransactionResp:
|
||||
txn = self._build_match_orders_tx(limit)
|
||||
return await self._conn.send_transaction(txn, fee_payer, opts=opts)
|
||||
|
||||
|
|
|
@ -37,25 +37,20 @@ class MarketCore:
|
|||
|
||||
logger = logging.getLogger("pyserum.market.Market")
|
||||
|
||||
def __init__(
|
||||
self, market_state: MarketState, force_use_request_queue: bool = False
|
||||
) -> None:
|
||||
def __init__(self, market_state: MarketState, force_use_request_queue: bool = False) -> None:
|
||||
self.state = market_state
|
||||
self.force_use_request_queue = force_use_request_queue
|
||||
|
||||
def _use_request_queue(self) -> bool:
|
||||
return (
|
||||
# DEX Version 1
|
||||
self.state.program_id
|
||||
== PublicKey("4ckmDgGdxQoPDLUkDT3vHgSAkzA3QRdNq5ywwY4sUSJn")
|
||||
self.state.program_id == PublicKey("4ckmDgGdxQoPDLUkDT3vHgSAkzA3QRdNq5ywwY4sUSJn")
|
||||
or
|
||||
# DEX Version 1
|
||||
self.state.program_id
|
||||
== PublicKey("BJ3jrUzddfuSrZHXSCxMUUQsjKEyLmuuyZebkcaFp2fg")
|
||||
self.state.program_id == PublicKey("BJ3jrUzddfuSrZHXSCxMUUQsjKEyLmuuyZebkcaFp2fg")
|
||||
or
|
||||
# DEX Version 2
|
||||
self.state.program_id
|
||||
== PublicKey("EUqojwWA2rd19FZrzeBncJsm38Jm1hEhE3zsmX3bRc2o")
|
||||
self.state.program_id == PublicKey("EUqojwWA2rd19FZrzeBncJsm38Jm1hEhE3zsmX3bRc2o")
|
||||
or self.force_use_request_queue
|
||||
)
|
||||
|
||||
|
@ -68,9 +63,7 @@ class MarketCore:
|
|||
def find_best_fee_discount_key(self, owner: PublicKey, cache_duration: int):
|
||||
raise NotImplementedError("find_best_fee_discount_key not implemented")
|
||||
|
||||
def find_quote_token_accounts_for_owner(
|
||||
self, owner_address: PublicKey, include_unwrapped_sol: bool = False
|
||||
):
|
||||
def find_quote_token_accounts_for_owner(self, owner_address: PublicKey, include_unwrapped_sol: bool = False):
|
||||
raise NotImplementedError("find_quote_token_accounts_for_owner not implemented")
|
||||
|
||||
def _parse_bids_or_asks(self, bytes_data: bytes) -> OrderBook:
|
||||
|
@ -83,9 +76,7 @@ class MarketCore:
|
|||
|
||||
all_orders = itertools.chain(bids.orders(), asks.orders())
|
||||
open_orders_addresses = {str(o.address) for o in open_orders_accounts}
|
||||
orders = [
|
||||
o for o in all_orders if str(o.open_order_address) in open_orders_addresses
|
||||
]
|
||||
orders = [o for o in all_orders if str(o.open_order_address) in open_orders_addresses]
|
||||
return orders
|
||||
|
||||
def load_base_token_for_owner(self):
|
||||
|
@ -124,8 +115,7 @@ class MarketCore:
|
|||
side=side,
|
||||
price=price,
|
||||
size=size,
|
||||
fee_cost=event.native_fee_or_rebate
|
||||
* (1 if event.event_flags.maker else -1),
|
||||
fee_cost=event.native_fee_or_rebate * (1 if event.event_flags.maker else -1),
|
||||
)
|
||||
|
||||
def _prepare_new_oo_account(
|
||||
|
@ -160,9 +150,7 @@ class MarketCore:
|
|||
limit_price: float,
|
||||
max_quantity: float,
|
||||
client_id: int,
|
||||
open_order_accounts: Union[
|
||||
List[OpenOrdersAccount], List[AsyncOpenOrdersAccount]
|
||||
],
|
||||
open_order_accounts: Union[List[OpenOrdersAccount], List[AsyncOpenOrdersAccount]],
|
||||
place_order_open_order_account: PublicKey,
|
||||
) -> None:
|
||||
# unwrapped SOL cannot be used for payment
|
||||
|
@ -170,9 +158,9 @@ class MarketCore:
|
|||
raise ValueError("Invalid payer account. Cannot use unwrapped SOL.")
|
||||
|
||||
# TODO: add integration test for SOL wrapping.
|
||||
should_wrap_sol = (
|
||||
side == Side.BUY and self.state.quote_mint() == WRAPPED_SOL_MINT
|
||||
) or (side == Side.SELL and self.state.base_mint() == WRAPPED_SOL_MINT)
|
||||
should_wrap_sol = (side == Side.BUY and self.state.quote_mint() == WRAPPED_SOL_MINT) or (
|
||||
side == Side.SELL and self.state.base_mint() == WRAPPED_SOL_MINT
|
||||
)
|
||||
|
||||
if should_wrap_sol:
|
||||
# wrapped_sol_account = Account()
|
||||
|
@ -236,9 +224,7 @@ class MarketCore:
|
|||
transaction: Transaction,
|
||||
) -> PublicKey:
|
||||
balance_needed = mbfre_resp.value
|
||||
place_order_open_order_account = self._prepare_new_oo_account(
|
||||
owner, balance_needed, signers, transaction
|
||||
)
|
||||
place_order_open_order_account = self._prepare_new_oo_account(owner, balance_needed, signers, transaction)
|
||||
return place_order_open_order_account
|
||||
|
||||
@staticmethod
|
||||
|
@ -246,9 +232,7 @@ class MarketCore:
|
|||
price: float,
|
||||
size: float,
|
||||
side: Side,
|
||||
open_orders_accounts: Union[
|
||||
List[OpenOrdersAccount], List[AsyncOpenOrdersAccount]
|
||||
],
|
||||
open_orders_accounts: Union[List[OpenOrdersAccount], List[AsyncOpenOrdersAccount]],
|
||||
) -> int:
|
||||
lamports = 0
|
||||
if side == Side.BUY:
|
||||
|
@ -326,11 +310,7 @@ class MarketCore:
|
|||
def _build_cancel_order_by_client_id_tx(
|
||||
self, owner: Keypair, open_orders_account: PublicKey, client_id: int
|
||||
) -> Transaction:
|
||||
return Transaction().add(
|
||||
self.make_cancel_order_by_client_id_instruction(
|
||||
owner, open_orders_account, client_id
|
||||
)
|
||||
)
|
||||
return Transaction().add(self.make_cancel_order_by_client_id_instruction(owner, open_orders_account, client_id))
|
||||
|
||||
def make_cancel_order_by_client_id_instruction(
|
||||
self, owner: Keypair, open_orders_account: PublicKey, client_id: int
|
||||
|
@ -360,13 +340,9 @@ class MarketCore:
|
|||
)
|
||||
|
||||
def _build_cancel_order_tx(self, owner: Keypair, order: t.Order) -> Transaction:
|
||||
return Transaction().add(
|
||||
self.make_cancel_order_instruction(owner.public_key, order)
|
||||
)
|
||||
return Transaction().add(self.make_cancel_order_instruction(owner.public_key, order))
|
||||
|
||||
def make_cancel_order_instruction(
|
||||
self, owner: PublicKey, order: t.Order
|
||||
) -> TransactionInstruction:
|
||||
def make_cancel_order_instruction(self, owner: PublicKey, order: t.Order) -> TransactionInstruction:
|
||||
if self._use_request_queue():
|
||||
return instructions.cancel_order(
|
||||
instructions.CancelOrderParams(
|
||||
|
@ -465,12 +441,8 @@ class MarketCore:
|
|||
transaction.add(
|
||||
self.make_settle_funds_instruction(
|
||||
open_orders,
|
||||
base_wallet
|
||||
if self.state.base_mint() != WRAPPED_SOL_MINT
|
||||
else wrapped_sol_account.public_key,
|
||||
quote_wallet
|
||||
if self.state.quote_mint() != WRAPPED_SOL_MINT
|
||||
else wrapped_sol_account.public_key,
|
||||
base_wallet if self.state.base_mint() != WRAPPED_SOL_MINT else wrapped_sol_account.public_key,
|
||||
quote_wallet if self.state.quote_mint() != WRAPPED_SOL_MINT else wrapped_sol_account.public_key,
|
||||
vault_signer,
|
||||
)
|
||||
)
|
||||
|
@ -490,9 +462,7 @@ class MarketCore:
|
|||
return transaction
|
||||
|
||||
def _settle_funds_should_wrap_sol(self) -> bool:
|
||||
return (self.state.quote_mint() == WRAPPED_SOL_MINT) or (
|
||||
self.state.base_mint() == WRAPPED_SOL_MINT
|
||||
)
|
||||
return (self.state.quote_mint() == WRAPPED_SOL_MINT) or (self.state.base_mint() == WRAPPED_SOL_MINT)
|
||||
|
||||
def make_settle_funds_instruction(
|
||||
self,
|
||||
|
|
|
@ -35,9 +35,7 @@ class Market(MarketCore):
|
|||
market_state: MarketState,
|
||||
force_use_request_queue: bool = False,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
market_state=market_state, force_use_request_queue=force_use_request_queue
|
||||
)
|
||||
super().__init__(market_state=market_state, force_use_request_queue=force_use_request_queue)
|
||||
self._conn = conn
|
||||
|
||||
@classmethod
|
||||
|
@ -58,9 +56,7 @@ class Market(MarketCore):
|
|||
market_state = MarketState.load(conn, market_address, program_id)
|
||||
return cls(conn, market_state, force_use_request_queue)
|
||||
|
||||
def find_open_orders_accounts_for_owner(
|
||||
self, owner_address: PublicKey
|
||||
) -> List[OpenOrdersAccount]:
|
||||
def find_open_orders_accounts_for_owner(self, owner_address: PublicKey) -> List[OpenOrdersAccount]:
|
||||
return OpenOrdersAccount.find_for_market_and_owner(
|
||||
self._conn, self.state.public_key(), owner_address, self.state.program_id()
|
||||
)
|
||||
|
@ -115,9 +111,7 @@ class Market(MarketCore):
|
|||
if open_order_accounts:
|
||||
place_order_open_order_account = open_order_accounts[0].address
|
||||
else:
|
||||
mbfre_resp = self._conn.get_minimum_balance_for_rent_exemption(
|
||||
OPEN_ORDERS_LAYOUT.sizeof()
|
||||
)
|
||||
mbfre_resp = self._conn.get_minimum_balance_for_rent_exemption(OPEN_ORDERS_LAYOUT.sizeof())
|
||||
place_order_open_order_account = self._after_oo_mbfre_resp(
|
||||
mbfre_resp=mbfre_resp,
|
||||
owner=owner,
|
||||
|
@ -154,15 +148,11 @@ class Market(MarketCore):
|
|||
)
|
||||
return self._conn.send_transaction(txs, owner, opts=opts)
|
||||
|
||||
def cancel_order(
|
||||
self, owner: Keypair, order: t.Order, opts: TxOpts = TxOpts()
|
||||
) -> SendTransactionResp:
|
||||
def cancel_order(self, owner: Keypair, order: t.Order, opts: TxOpts = TxOpts()) -> SendTransactionResp:
|
||||
txn = self._build_cancel_order_tx(owner=owner, order=order)
|
||||
return self._conn.send_transaction(txn, owner, opts=opts)
|
||||
|
||||
def match_orders(
|
||||
self, fee_payer: Keypair, limit: int, opts: TxOpts = TxOpts()
|
||||
) -> SendTransactionResp:
|
||||
def match_orders(self, fee_payer: Keypair, limit: int, opts: TxOpts = TxOpts()) -> SendTransactionResp:
|
||||
txn = self._build_match_orders_tx(limit)
|
||||
return self._conn.send_transaction(txn, fee_payer, opts=opts)
|
||||
|
||||
|
@ -177,9 +167,7 @@ class Market(MarketCore):
|
|||
# TODO: Handle wrapped sol accounts
|
||||
should_wrap_sol = self._settle_funds_should_wrap_sol()
|
||||
min_bal_for_rent_exemption = (
|
||||
self._conn.get_minimum_balance_for_rent_exemption(165).value
|
||||
if should_wrap_sol
|
||||
else 0
|
||||
self._conn.get_minimum_balance_for_rent_exemption(165).value if should_wrap_sol else 0
|
||||
) # value only matters if should_wrap_sol
|
||||
signers = [owner]
|
||||
transaction = self._build_settle_funds_tx(
|
||||
|
|
|
@ -57,10 +57,7 @@ class _OpenOrdersAccountCore: # pylint: disable=too-many-instance-attributes,to
|
|||
@classmethod
|
||||
def from_bytes(cls: Type[_T], address: PublicKey, buffer: bytes) -> _T:
|
||||
open_order_decoded = OPEN_ORDERS_LAYOUT.parse(buffer)
|
||||
if (
|
||||
not open_order_decoded.account_flags.open_orders
|
||||
or not open_order_decoded.account_flags.initialized
|
||||
):
|
||||
if not open_order_decoded.account_flags.open_orders or not open_order_decoded.account_flags.initialized:
|
||||
raise Exception("Not an open order account or not initialized.")
|
||||
|
||||
return cls(
|
||||
|
@ -73,16 +70,12 @@ class _OpenOrdersAccountCore: # pylint: disable=too-many-instance-attributes,to
|
|||
quote_token_total=open_order_decoded.quote_token_total,
|
||||
free_slot_bits=int.from_bytes(open_order_decoded.free_slot_bits, "little"),
|
||||
is_bid_bits=int.from_bytes(open_order_decoded.is_bid_bits, "little"),
|
||||
orders=[
|
||||
int.from_bytes(order, "little") for order in open_order_decoded.orders
|
||||
],
|
||||
orders=[int.from_bytes(order, "little") for order in open_order_decoded.orders],
|
||||
client_ids=open_order_decoded.client_ids,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _process_get_program_accounts_resp(
|
||||
cls: Type[_T], resp: GetProgramAccountsResp
|
||||
) -> List[_T]:
|
||||
def _process_get_program_accounts_resp(cls: Type[_T], resp: GetProgramAccountsResp) -> List[_T]:
|
||||
accounts = []
|
||||
for keyed_account in resp.value:
|
||||
account_details = keyed_account.account
|
||||
|
@ -96,9 +89,7 @@ class _OpenOrdersAccountCore: # pylint: disable=too-many-instance-attributes,to
|
|||
)
|
||||
)
|
||||
|
||||
return [
|
||||
cls.from_bytes(account.public_key, account.data) for account in accounts
|
||||
]
|
||||
return [cls.from_bytes(account.public_key, account.data) for account in accounts]
|
||||
|
||||
@staticmethod
|
||||
def _build_get_program_accounts_args(
|
||||
|
@ -113,9 +104,7 @@ class _OpenOrdersAccountCore: # pylint: disable=too-many-instance-attributes,to
|
|||
bytes=str(market),
|
||||
),
|
||||
MemcmpOpts(
|
||||
offset=5
|
||||
+ 8
|
||||
+ 32, # 5 bytes of padding, 8 bytes of account flag, 32 bytes of market public key
|
||||
offset=5 + 8 + 32, # 5 bytes of padding, 8 bytes of account flag, 32 bytes of market public key
|
||||
bytes=str(owner),
|
||||
),
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue