forked from bitvavo/python-bitvavo-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprivate.py
More file actions
895 lines (765 loc) · 33.8 KB
/
private.py
File metadata and controls
895 lines (765 loc) · 33.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
"""Private API endpoints that require authentication."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any, ClassVar, TypeVar
import httpx
from returns.result import Failure, Result, Success
from bitvavo_client.adapters.returns_adapter import BitvavoError
from bitvavo_client.core import private_models
from bitvavo_client.core.model_preferences import ModelPreference
from bitvavo_client.endpoints.base import (
_DATAFRAME_LIBRARY_MAP,
BaseAPI,
_create_dataframe_from_data,
)
from bitvavo_client.endpoints.common import create_postfix, default
from bitvavo_client.schemas.private_schemas import DEFAULT_SCHEMAS
if TYPE_CHECKING: # pragma: no cover
from bitvavo_client.core.types import AnyDict
from bitvavo_client.transport.http import HTTPClient
T = TypeVar("T")
class PrivateAPI(BaseAPI):
"""Handles all private Bitvavo API endpoints requiring authentication."""
_endpoint_models: ClassVar[dict[str, Any]] = {
"account": private_models.Account,
"balance": private_models.Balances,
"orders": private_models.Orders,
"order": private_models.Order,
"trade_history": private_models.Trades,
"transaction_history": private_models.TransactionHistory,
"fees": private_models.Fees,
"deposit_history": private_models.DepositHistories,
"deposit": private_models.Deposit,
"withdrawals": private_models.Withdrawals,
"withdraw": private_models.WithdrawResponse,
"cancel_order": private_models.CancelOrderResponse,
}
_default_schemas = DEFAULT_SCHEMAS
def __init__(
self,
http_client: HTTPClient,
*,
preferred_model: ModelPreference | str | None = None,
default_schema: dict | None = None,
) -> None:
"""Initialize private API handler."""
super().__init__(http_client, preferred_model=preferred_model, default_schema=default_schema)
def account(
self,
*,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError | TypeError]:
"""Get account information including fees and capabilities.
Endpoint: GET /v2/account
Rate limit weight: 1
Args:
model: Optional Pydantic model to validate response
Returns:
Result containing account information including fees and capabilities:
{
"fees": {
"tier": 0,
"volume": "0.00",
"maker": "0.0015",
"taker": "0.0025"
},
"capabilities": [
"buy", "sell", "depositCrypto", "depositFiat",
"withdrawCrypto", "withdrawFiat"
]
}
"""
# Check if DataFrame is requested - not supported for this endpoint
effective_model, effective_schema = self._get_effective_model("account", model, schema)
if effective_model in _DATAFRAME_LIBRARY_MAP:
msg = "DataFrame model is not supported due to the shape of data"
return Failure(TypeError(msg))
# Get raw data from API
raw_result = self.http.request("GET", "/account", weight=1)
# Convert to desired format
return self._convert_raw_result(raw_result, "account", model, schema)
def balance(
self,
options: AnyDict | None = None,
*,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError]:
"""Get account balance.
Args:
options: Optional query parameters
model: Optional Pydantic model to validate response
Returns:
Result containing balance information or error
"""
postfix = create_postfix(options)
# Get raw data from API
raw_result = self.http.request("GET", f"/balance{postfix}", weight=5)
# Convert to desired format
return self._convert_raw_result(raw_result, "balance", model, schema)
def place_order(
self,
market: str,
side: str,
order_type: str,
operator_id: int,
body: AnyDict,
*,
response_required: bool = True,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError]:
"""Place a new order.
Args:
market: Market symbol
side: Order side ('buy' or 'sell')
order_type: Order type ('market', 'limit', etc.)
operator_id: Your identifier for the trader or bot within your account
body: Order parameters (amount, price, etc.)
response_required: Whether to return full order details (True) or minimal response (False)
Returns:
Order placement result
"""
effective_model, effective_schema = self._get_effective_model("order", model, schema)
payload = {
"market": market,
"side": side,
"orderType": order_type,
"operatorId": operator_id,
"responseRequired": response_required,
**body,
}
return self.http.request("POST", "/order", body=payload, weight=1)
def get_order(
self,
market: str,
order_id: str,
*,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError]:
"""Get order by ID.
Args:
market: Market symbol
order_id: Order ID
model: Optional Pydantic model to validate response
Returns:
Result containing order information or error
"""
# Get raw data from API
raw_result = self.http.request(
"GET",
f"/{market}/order",
body={"orderId": order_id},
weight=1,
)
# Convert to desired format
return self._convert_raw_result(raw_result, "order", model, schema)
def update_order(
self,
market: str,
operator_id: int,
*,
order_id: str | None = None,
client_order_id: str | None = None,
amount: str | None = None,
amount_quote: str | None = None,
amount_remaining: str | None = None,
price: str | None = None,
trigger_amount: str | None = None,
time_in_force: str | None = None,
self_trade_prevention: str | None = None,
post_only: bool | None = None,
response_required: bool | None = None,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError]:
"""Update an existing limit or trigger order.
Args:
market: Market symbol (e.g., 'BTC-EUR')
operator_id: Your identifier for the trader or bot within your account
order_id: Bitvavo identifier of the order to update (required if client_order_id not provided)
client_order_id: Your identifier of the order to update (required if order_id not provided)
amount: Amount of base currency to update
amount_quote: Amount of quote currency (market orders only)
amount_remaining: Remaining amount of base currency to update
price: Price for limit orders
trigger_amount: Trigger price for stop orders
time_in_force: Time in force ('GTC', 'IOC', 'FOK')
self_trade_prevention: Self-trade prevention
('decrementAndCancel', 'cancelOldest', 'cancelNewest', 'cancelBoth')
post_only: Whether order should only be maker
response_required: Whether to return full response or just status code
model: Optional Pydantic model to validate response
schema: Optional schema for DataFrame conversion
Returns:
Result containing updated order information or error
Note:
- You must set either order_id or client_order_id
- If both are set, client_order_id takes precedence
- Updates are faster than canceling and creating new orders
- Only works with limit or trigger orders
"""
if not order_id and not client_order_id:
error = BitvavoError(
http_status=400,
error_code=203,
reason="Missing or incompatible parameters in your request.",
message="Either order_id or client_order_id must be provided",
raw={"provided": {"order_id": order_id, "client_order_id": client_order_id}},
)
return Failure(error)
effective_model, effective_schema = self._get_effective_model("order", model, schema)
payload = self._build_update_order_payload(
market,
operator_id,
order_id,
client_order_id,
amount,
amount_quote,
amount_remaining,
price,
trigger_amount,
time_in_force,
self_trade_prevention,
post_only=post_only,
response_required=response_required,
)
return self.http.request("PUT", "/order", body=payload, weight=1)
def _build_update_order_payload(
self,
market: str,
operator_id: int,
order_id: str | None,
client_order_id: str | None,
amount: str | None,
amount_quote: str | None,
amount_remaining: str | None,
price: str | None,
trigger_amount: str | None,
time_in_force: str | None,
self_trade_prevention: str | None,
*,
post_only: bool | None,
response_required: bool | None,
) -> dict[str, Any]:
"""Build the payload for update order request."""
payload = {
"market": market,
"operatorId": operator_id,
}
# Add order identifier - clientOrderId takes precedence if both provided
if client_order_id:
payload["clientOrderId"] = client_order_id
elif order_id:
payload["orderId"] = order_id
# Add optional update parameters
payload.update(
{
key: value
for key, value in {
"amount": amount,
"amountQuote": amount_quote,
"amountRemaining": amount_remaining,
"price": price,
"triggerAmount": trigger_amount,
"timeInForce": time_in_force,
"selfTradePrevention": self_trade_prevention,
"postOnly": post_only,
"responseRequired": response_required,
}.items()
if value is not None
}
)
return payload
def cancel_order(
self,
market: str,
operator_id: int,
*,
order_id: str | None = None,
client_order_id: str | None = None,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError]:
"""Cancel an order.
Args:
market: Market symbol
operator_id: Your identifier for the trader or bot within your account
order_id: Bitvavo identifier of the order (required if client_order_id not provided)
client_order_id: Your identifier of the order (required if order_id not provided)
model: Optional Pydantic model to validate response
Returns:
Result containing cancellation result or error
Note:
You must set either order_id or client_order_id. If you set both,
client_order_id takes precedence as per Bitvavo documentation.
"""
if not order_id and not client_order_id:
# Create a validation error using httpx.HTTPError as a fallback
error = httpx.RequestError("Either order_id or client_order_id must be provided")
return Failure(error)
# Build query parameters
params = {
"market": market,
"operatorId": operator_id,
}
if client_order_id:
params["clientOrderId"] = client_order_id
elif order_id:
params["orderId"] = order_id
# Create query string
postfix = create_postfix(params)
effective_model, effective_schema = self._get_effective_model("cancel_order", model, schema)
return self.http.request(
"DELETE",
f"/order{postfix}",
weight=1,
)
def get_orders(
self,
market: str,
options: AnyDict | None = None,
*,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError]:
"""Get orders for a market.
Args:
market: Market symbol (e.g., 'BTC-EUR')
options: Optional query parameters:
- limit (int): Max number of orders (1-1000, default: 500)
- start (int): Unix timestamp in ms to start from
- end (int): Unix timestamp in ms to end at (max: 8640000000000000)
- orderIdFrom (str): UUID to start from
- orderIdTo (str): UUID to end at
model: Optional Pydantic model to validate response
schema: Optional schema for DataFrame conversion
Returns:
Result containing list of orders or error
Rate limit weight: 5
"""
# Constants for validation
MIN_LIMIT = 1
MAX_LIMIT = 1000
MAX_TIMESTAMP = 8640000000000000
# Validate options if provided
if options:
# Validate limit parameter
if "limit" in options:
limit = options["limit"]
if not isinstance(limit, int) or limit < MIN_LIMIT or limit > MAX_LIMIT:
msg = f"Invalid limit '{limit}'. Must be an integer between {MIN_LIMIT} and {MAX_LIMIT}"
raise ValueError(msg)
# Validate end timestamp
if "end" in options:
end = options["end"]
if not isinstance(end, int) or end > MAX_TIMESTAMP:
msg = f"Invalid end timestamp '{end}'. Must be <= {MAX_TIMESTAMP}"
raise ValueError(msg)
# Validate start/end relationship
if "start" in options and "end" in options and options["start"] > options["end"]:
msg = f"Start timestamp ({options['start']}) cannot be greater than end timestamp ({options['end']})"
raise ValueError(msg)
effective_model, effective_schema = self._get_effective_model("orders", model, schema)
options = default(options, {})
options["market"] = market
postfix = create_postfix(options)
# Get raw data first
raw_result = self.http.request("GET", f"/orders{postfix}", weight=5)
# Convert using the shared method
return self._convert_raw_result(raw_result, "orders", effective_model, effective_schema)
def cancel_orders(
self,
operator_id: int,
*,
market: str | None = None,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError]:
"""Cancel all orders for a market or the entire account.
Args:
operator_id: Your identifier for the trader or bot within your account
market: Optional market symbol. If not specified, all open orders are canceled
model: Optional Pydantic model to validate response
Returns:
Result containing array of cancellation results or error
Example Response:
[
{
"orderId": "1be6d0df-d5dc-4b53-a250-3376f3b393e6",
"operatorId": 543462
}
]
"""
effective_model, effective_schema = self._get_effective_model("orders", model, schema)
# Build query parameters
params: dict[str, Any] = {"operatorId": operator_id}
if market is not None:
params["market"] = market
postfix = create_postfix(params)
return self.http.request("DELETE", f"/orders{postfix}", weight=1)
def orders_open(
self,
options: AnyDict | None = None,
*,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError]:
"""Get all open orders.
Args:
options: Optional query parameters. Supports:
- market (str): Filter by specific market (e.g., 'BTC-EUR')
- base (str): Filter by base asset (e.g., 'BTC')
model: Optional Pydantic model to validate response
Returns:
Result containing open orders data or error
Rate limit: 25 points (without market), 1 point (with market)
"""
effective_model, effective_schema = self._get_effective_model("orders", model, schema)
postfix = create_postfix(options)
# Rate limit is 1 point with market parameter, 25 points without
weight = 1 if options and "market" in options else 25
# Get raw data first
raw_result = self.http.request("GET", f"/ordersOpen{postfix}", weight=weight)
# Convert using the shared method
return self._convert_raw_result(raw_result, "orders", effective_model, effective_schema)
def fees(
self,
options: AnyDict | None = None,
*,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError]:
"""Get market-specific trading fees.
Returns your current trading fees from the Category of the specified market
based on the trading volume of your account. This is different from the
account() method which returns general account information with fees wrapped
in a "fees" object plus capabilities.
Endpoint: GET /v2/account/fees
Rate limit weight: 1
Args:
options: Optional query parameters:
- market (str): Market symbol (e.g., 'BTC-EUR'). If not specified,
returns fees for your current tier in Category B.
- quote (str): Quote currency ('EUR' or 'USDC'). If not specified,
returns fees for your current tier in Category B.
model: Optional Pydantic model to validate response
Returns:
Result containing market fee information directly (no wrapper, includes tier):
{
"tier": "0",
"volume": "10000.00",
"taker": "0.0025",
"maker": "0.0015"
}
Note:
This differs from account() which returns:
{
"fees": {"tier": "0", "volume": "...", "maker": "...", "taker": "..."},
"capabilities": [...]
}
"""
# Validate quote parameter if provided
if options and "quote" in options:
quote = options["quote"]
valid_quotes = ["EUR", "USDC"]
if quote not in valid_quotes:
msg = f"Invalid quote currency '{quote}'. Must be one of: {valid_quotes}"
raise ValueError(msg)
effective_model, effective_schema = self._get_effective_model("fees", model, schema)
postfix = create_postfix(options)
# Get raw data first
raw_result = self.http.request("GET", f"/account/fees{postfix}", weight=1)
# Convert using the shared method
return self._convert_raw_result(raw_result, "fees", effective_model, effective_schema)
def deposit(
self,
symbol: str,
*,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError | TypeError]:
"""Get deposit data for making deposits.
Returns wallet or bank account information required to deposit digital or fiat assets.
Endpoint: GET /v2/deposit
Rate limit weight: 1
Args:
symbol: The asset symbol you want to deposit (e.g., 'BTC', 'EUR')
model: Optional Pydantic model to validate response
schema: Optional schema for DataFrame conversion
Returns:
Result containing deposit information:
- For digital assets: {"address": "string", "paymentid": "string"}
- For fiat: {"iban": "string", "bic": "string", "description": "string"}
"""
effective_model, effective_schema = self._get_effective_model("deposit", model, schema)
if effective_model in _DATAFRAME_LIBRARY_MAP:
msg = "DataFrame model is not supported due to the shape of data"
return Failure(TypeError(msg))
params = {"symbol": symbol}
postfix = create_postfix(params)
# Get raw data first
raw_result = self.http.request(
"GET",
f"/deposit{postfix}",
weight=1,
)
# Convert using the shared method
return self._convert_raw_result(raw_result, "deposit", effective_model, effective_schema)
def deposit_history(
self,
options: AnyDict | None = None,
*,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError]:
"""Get deposit history.
Ensures every deposit dict includes a non-empty "address" key (fallback to txId or "unknown").
Args:
options: Optional query parameters
model: Optional Pydantic model to validate response
Returns:
Result containing deposits data or error
"""
effective_model, effective_schema = self._get_effective_model("deposit_history", model, schema)
postfix = create_postfix(options)
# Get raw data first
raw_result = self.http.request("GET", f"/depositHistory{postfix}", weight=5)
# Convert using the shared method
return self._convert_raw_result(raw_result, "deposit_history", effective_model, effective_schema)
def withdrawals(
self,
options: AnyDict | None = None,
*,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError]:
"""Get withdrawal history.
Args:
options: Optional query parameters
model: Optional Pydantic model to validate response
Returns:
Result containing withdrawals data or error
"""
effective_model, effective_schema = self._get_effective_model("withdrawals", model, schema)
postfix = create_postfix(options)
# Get raw data first
raw_result = self.http.request("GET", f"/withdrawalHistory{postfix}", weight=1)
# Convert using the shared method
return self._convert_raw_result(raw_result, "withdrawals", effective_model, effective_schema)
def _convert_transaction_items(
self,
items_data: list[dict],
effective_model: type[T] | Any | None,
effective_schema: dict | None,
) -> Result[Any, BitvavoError]:
"""Convert transaction items to the desired model format."""
if effective_model in _DATAFRAME_LIBRARY_MAP and isinstance(effective_model, ModelPreference):
# Convert items to DataFrame using the specific preference
return _create_dataframe_from_data(
items_data, effective_model, items_key=None, empty_schema=effective_schema
)
if effective_model is Any or effective_model is None:
# Raw data - return items list directly
return Success(items_data)
# Handle Pydantic or other model types
try:
if hasattr(effective_model, "model_validate"):
# Pydantic model - validate items list
parsed_items = effective_model.model_validate(items_data) # type: ignore[misc]
elif effective_schema is None:
# Simple constructor call
parsed_items = effective_model(items_data) # type: ignore[misc]
else:
# Other models with schema
parsed_items = effective_model(items_data, schema=effective_schema) # type: ignore[misc]
return Success(parsed_items)
except (ValueError, TypeError, AttributeError) as exc:
# If conversion fails, return a structured error
error = BitvavoError(
http_status=500,
error_code=-1,
reason="Model conversion failed",
message=str(exc),
raw=items_data if isinstance(items_data, dict) else {"raw": items_data},
)
return Failure(error)
def transaction_history(
self,
options: AnyDict | None = None,
*,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[tuple[Any, dict[str, Any]], BitvavoError | httpx.HTTPError]:
"""Get account transaction history.
Returns all past transactions for your account with pagination support.
Endpoint: GET /v2/account/history
Rate limit weight: 1
Args:
options: Optional query parameters:
- fromDate (int): Unix timestamp in ms to start from (>=0)
- toDate (int): Unix timestamp in ms to end at (<=8640000000000000)
- page (int): Page number for pagination (>=1)
- maxItems (int): Max number of items per page (1-100, default: 100)
- type (str): Transaction type filter:
'sell', 'buy', 'staking', 'fixed_staking', 'deposit', 'withdrawal',
'affiliate', 'distribution', 'internal_transfer', 'withdrawal_cancelled',
'rebate', 'loan', 'external_transferred_funds', 'manually_assigned_bitvavo'
model: Optional Pydantic model to validate response
schema: Optional schema for DataFrame conversion
Returns:
Result containing transaction history as tuple of (items_converted, metadata_dict):
- For DataFrames: Returns tuple of (DataFrame of items, metadata dict)
- For Pydantic models: Returns tuple of (Pydantic model of items, metadata dict)
- For raw responses: Returns tuple of (list of items, metadata dict)
Each transaction contains:
- transactionId: Unique transaction identifier
- executedAt: Execution timestamp (ISO format)
- type: Transaction type
- priceCurrency/priceAmount: Transaction price info (optional for staking)
- sentCurrency/sentAmount: Sent amounts (optional for staking)
- receivedCurrency/receivedAmount: Received amounts
- feesCurrency/feesAmount: Fee information (optional for staking)
- address: Transaction address (nullable)
"""
postfix = create_postfix(options)
# Get raw data first
raw_result = self.http.request("GET", f"/account/history{postfix}", weight=1)
# Always split the response into items and metadata for all model types
match raw_result:
case Success(raw_data):
# Validate response structure
if not isinstance(raw_data, dict) or "items" not in raw_data:
error = BitvavoError(
http_status=500,
error_code=-1,
reason="Response parsing failed",
message="Expected response to have 'items' key for transaction history",
raw=raw_data if isinstance(raw_data, dict) else {"raw": raw_data},
)
return Failure(error)
# Extract items and metadata separately
items_data = raw_data["items"]
metadata = {k: v for k, v in raw_data.items() if k != "items"}
effective_model, effective_schema = self._get_effective_model("transaction_history", model, schema)
# Convert items using helper method
items_result = self._convert_transaction_items(items_data, effective_model, effective_schema)
match items_result:
case Success(converted_items):
return Success((converted_items, metadata))
case Failure(error):
return Failure(error)
case _:
# This case should never be reached, but satisfies type checker
msg = "Unexpected result type from _convert_transaction_items"
raise RuntimeError(msg)
case Failure(error):
return Failure(error)
case _:
# This case should never be reached, but satisfies type checker
msg = "Unexpected result type from HTTP request"
raise RuntimeError(msg)
def trade_history(
self,
market: str,
options: AnyDict | None = None,
*,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError]:
"""Get trade history for your account.
Returns the specified number of past trades for your account, excluding Price Guarantee.
The returned trades are sorted by their timestamp in descending order, from latest to earliest.
Endpoint: GET /v2/trades
Rate limit weight: 5
Args:
market: The market for which to return the past trades (e.g., 'BTC-EUR')
options: Optional query parameters:
- limit (int): Max number of trades (1-1000, default: 500)
- start (int): Unix timestamp in ms to start from
- end (int): Unix timestamp in ms to end at (max: 8640000000000000)
- tradeIdFrom (str): Trade ID to start from
- tradeIdTo (str): Trade ID to end at
- tradeId (str, deprecated): Interpreted as tradeIdTo
model: Optional Pydantic model to validate response
schema: Optional schema for DataFrame conversion
Returns:
Result containing list of trade objects with fields like:
- id: Trade identifier
- orderId: Bitvavo order ID
- clientOrderId: Your order ID
- timestamp: Unix timestamp in ms
- market: Market symbol
- side: 'buy' or 'sell'
- amount: Base currency amount
- price: Price per unit
- taker: Whether you were the taker
- fee: Fee paid (negative for rebates)
- feeCurrency: Currency of the fee
- settled: Whether fee was deducted
Note:
This is a private endpoint that returns YOUR trades, different from the public
trades endpoint which returns public market trades.
"""
# Constants for validation
MIN_LIMIT = 1
MAX_LIMIT = 1000
MAX_TIMESTAMP = 8640000000000000
# Validate options if provided
if options:
# Validate limit parameter
if "limit" in options:
limit = options["limit"]
if not isinstance(limit, int) or limit < MIN_LIMIT or limit > MAX_LIMIT:
msg = f"Invalid limit '{limit}'. Must be an integer between {MIN_LIMIT} and {MAX_LIMIT}"
raise ValueError(msg)
# Validate end timestamp
if "end" in options:
end = options["end"]
if not isinstance(end, int) or end > MAX_TIMESTAMP:
msg = f"Invalid end timestamp '{end}'. Must be <= {MAX_TIMESTAMP}"
raise ValueError(msg)
# Validate start/end relationship
if "start" in options and "end" in options and options["start"] > options["end"]:
msg = f"Start timestamp ({options['start']}) cannot be greater than end timestamp ({options['end']})"
raise ValueError(msg)
# Handle deprecated tradeId parameter
if "tradeId" in options and "tradeIdTo" not in options:
# Move deprecated tradeId to tradeIdTo as per documentation
options = options.copy() # Don't mutate the original
options["tradeIdTo"] = options.pop("tradeId")
effective_model, effective_schema = self._get_effective_model("trade_history", model, schema)
# Add market to options
query_options = default(options, {})
query_options["market"] = market
postfix = create_postfix(query_options)
# Get raw data first
raw_result = self.http.request("GET", f"/trades{postfix}", weight=5)
# Convert using the shared method
return self._convert_raw_result(raw_result, "trade_history", effective_model, effective_schema)
def withdraw(
self,
symbol: str,
amount: str,
address: str,
options: AnyDict | None = None,
*,
model: type[T] | Any | None = None,
schema: dict | None = None,
) -> Result[T, BitvavoError | httpx.HTTPError]:
"""Withdraw assets.
Args:
symbol: Asset symbol
amount: Amount to withdraw
address: Withdrawal address
options: Optional parameters
model: Optional Pydantic model to validate response
Returns:
Result containing withdrawal result or error
"""
body = {"symbol": symbol, "amount": amount, "address": address}
if options:
body.update(options)
raw_result = self.http.request("POST", "/withdrawal", body=body, weight=1)
return self._convert_raw_result(raw_result, "withdraw", model, schema)