Skip to content

Commit

Permalink
set default values for OrderParams
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Nov 27, 2023
1 parent 2232c2a commit f73da54
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/driftpy/drift_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ def convert_to_spot_precision(self, amount: Union[int, float], market_index) ->
return cast_to_spot_precision(amount, spot_market)

def convert_to_perp_precision(self, amount: Union[int, float]) -> int:
return cast(int, amount * BASE_PRECISION)
return int(amount * BASE_PRECISION)

def convert_to_price_precision(self, amount: Union[int, float]) -> int:
return cast(int, amount * PRICE_PRECISION)
return int(amount * PRICE_PRECISION)

async def fetch_market_lookup_table(self) -> AddressLookupTableAccount:
if self.market_lookup_table_account is not None:
Expand Down Expand Up @@ -802,6 +802,7 @@ async def get_place_spot_order_ix(
order_params: OrderParams,
sub_account_id: int = 0,
):
order_params.set_spot()
user_account_public_key = self.get_user_account_public_key(sub_account_id)

remaining_accounts = self.get_remaining_accounts(
Expand Down Expand Up @@ -888,7 +889,9 @@ async def get_place_perp_order_ix(
order_params: OrderParams,
sub_account_id: int = 0,
):
order_params.set_perp()
user_account_public_key = self.get_user_account_public_key(sub_account_id)
user_stats_public_key = self.get_user_stats_public_key()
remaining_accounts = self.get_remaining_accounts(
readable_perp_market_indexes=[order_params.market_index],
user_accounts=[self.get_user_account(sub_account_id)],
Expand All @@ -900,6 +903,7 @@ async def get_place_perp_order_ix(
accounts={
"state": self.get_state_public_key(),
"user": user_account_public_key,
"userStats": user_stats_public_key,
"authority": self.wallet.public_key,
},
remaining_accounts=remaining_accounts,
Expand Down
2 changes: 1 addition & 1 deletion src/driftpy/math/spot_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ def cast_to_spot_precision(
amount: Union[float, int], spot_market: SpotMarketAccount
) -> int:
precision = 10**spot_market.decimals
return cast(int, amount * precision)
return int(amount * precision)
38 changes: 24 additions & 14 deletions src/driftpy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,22 +327,32 @@ class MarketIdentifier:
@dataclass
class OrderParams:
order_type: OrderType
market_type: MarketType
direction: PositionDirection
user_order_id: int
base_asset_amount: int
price: int
market_index: int
reduce_only: bool
post_only: PostOnlyParams
immediate_or_cancel: bool
max_ts: Optional[int]
trigger_price: Optional[int]
trigger_condition: OrderTriggerCondition
oracle_price_offset: Optional[int]
auction_duration: Optional[int]
auction_start_price: Optional[int]
auction_end_price: Optional[int]
direction: PositionDirection
market_type: MarketType = None
user_order_id: int = 0
price: int = 0
reduce_only: bool = False
post_only: PostOnlyParams = PostOnlyParams.NONE()
immediate_or_cancel: bool = False
max_ts: Optional[int] = None
trigger_price: Optional[int] = None
trigger_condition: OrderTriggerCondition = OrderTriggerCondition.ABOVE()
oracle_price_offset: Optional[int] = None
auction_duration: Optional[int] = None
auction_start_price: Optional[int] = None
auction_end_price: Optional[int] = None

def set_spot(self):
self.market_type = MarketType.SPOT()

def set_perp(self):
self.market_type = MarketType.PERP()

def check_market_type(self):
if self.market_type is None:
raise ValueError("market type not set on order params")


@dataclass
Expand Down

0 comments on commit f73da54

Please sign in to comment.