Skip to content

Commit ad4467f

Browse files
Resolve "spot.trade.truncate not working for xStocks"
1 parent 4cedb97 commit ad4467f

File tree

3 files changed

+148
-90
lines changed

3 files changed

+148
-90
lines changed

src/kraken/spot/market.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def get_asset_pairs(
152152
self: Market,
153153
pair: str | list[str] | None = None,
154154
info: str | None = None,
155+
aclass_base: str | None = None,
155156
*,
156157
extra_params: dict | None = None,
157158
) -> dict:
@@ -221,6 +222,8 @@ def get_asset_pairs(
221222
params["pair"] = pair
222223
if defined(info):
223224
params["info"] = info
225+
if defined(aclass_base):
226+
params["aclass_base"] = aclass_base
224227
return self.request( # type: ignore[return-value]
225228
method="GET",
226229
uri="/0/public/AssetPairs",

src/kraken/spot/trade.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ def create_order( # pylint: disable=too-many-branches,too-many-arguments # noqa
108108
Requires the ``Create and modify orders`` permission in the API key
109109
settings.
110110
111+
If the traded asset is a tokenized asset, `extra_params` must contain
112+
the key `"aclass_base"` with value `"tokenized_asset"`.
113+
111114
- https://docs.kraken.com/api/docs/rest-api/add-order
112115
113116
:param ordertype: The kind of the order, one of: ``market``, ``limit``,
@@ -335,7 +338,14 @@ def create_order( # pylint: disable=too-many-branches,too-many-arguments # noqa
335338
"volume": (
336339
volume
337340
if not truncate
338-
else self.truncate(amount=volume, amount_type="volume", pair=pair)
341+
else self.truncate(
342+
amount=volume,
343+
amount_type="volume",
344+
pair=pair,
345+
aclass_base=(
346+
extra_params.get("aclass_base") if extra_params else "currency"
347+
),
348+
)
339349
),
340350
"stptype": stptype,
341351
"starttm": starttm,
@@ -767,26 +777,33 @@ def truncate(
767777
amount: Decimal | float | str,
768778
amount_type: str,
769779
pair: str,
780+
aclass_base: str = "currency",
770781
) -> str:
771782
"""
772-
Kraken only allows volume and price amounts to be specified with a specific number of
773-
decimal places, and these vary depending on the currency pair used.
783+
Kraken only allows volume and price amounts to be specified with a
784+
specific number of decimal places, and these vary depending on the
785+
currency pair used.
774786
775-
This function converts an amount of a specific type and pair to a string that uses
776-
the correct number of decimal places.
787+
This function converts an amount of a specific type and pair to a string
788+
that uses the correct number of decimal places.
777789
778790
This function uses caching. Run ``truncate.clear_cache()`` to clear.
779791
780792
:param amount: The floating point number to represent
781793
:type amount: Decimal | float | str
782-
:param amount_type: What the amount represents. Either ``"price"`` or ``"volume"``
794+
:param amount_type: What the amount represents. Either ``"price"`` or
795+
``"volume"``
783796
:type amount_type: str
784797
:param pair: The currency pair the amount is in reference to.
785798
:type pair: str
786-
:raises ValueError: If the ``amount_type`` is ``price`` and the price is less
787-
than the costmin.
788-
:raises ValueError: If the ``amount_type`` is ``volume`` and the volume is
789-
less than the ordermin.
799+
:param aclass_base: The asset class of the base currency. Default is
800+
``"currency"``. If the traded asset is a tokenized asset, set this
801+
to ``"tokenized_asset"``.
802+
:type aclass_base: str, optional
803+
:raises ValueError: If the ``amount_type`` is ``price`` and the price is
804+
less than the costmin.
805+
:raises ValueError: If the ``amount_type`` is ``volume`` and the volume
806+
is less than the ordermin.
790807
:raises ValueError: If no valid ``amount_type`` was passed.
791808
:return: A string representation of the amount.
792809
:rtype: str
@@ -826,7 +843,10 @@ def truncate(
826843
if amount_type not in {"price", "volume"}:
827844
raise ValueError("Amount type must be 'volume' or 'price'!")
828845

829-
pair_data: dict = self.__market.get_asset_pairs(pair=pair)
846+
pair_data: dict = self.__market.get_asset_pairs(
847+
pair=pair,
848+
aclass_base=aclass_base,
849+
)
830850
data: dict = pair_data[next(iter(pair_data))]
831851

832852
pair_decimals: int = int(data["pair_decimals"])

0 commit comments

Comments
 (0)