1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace R3bers\BittrexApi\Api; |
6
|
|
|
|
7
|
|
|
use Ds\Queue; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
10
|
|
|
use R3bers\BittrexApi\Exception\TransformResponseException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Batch |
14
|
|
|
* @package R3bers\BittrexApi\Api |
15
|
|
|
*/ |
16
|
|
|
class Batch extends Api |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Queue |
20
|
|
|
*/ |
21
|
|
|
protected $batchQueue; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Api constructor. |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Client $client) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($client); |
29
|
|
|
$this->batchQueue = new Queue(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** https://bittrex.github.io/api/v3#operation--batch-post |
33
|
|
|
* https://bittrex.github.io/api/v3#operation--orders-post |
34
|
|
|
* @param string $market |
35
|
|
|
* @param float $quantity |
36
|
|
|
* @param float $rate |
37
|
|
|
* @param bool $useAwards |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function addBuyLimit(string $market, float $quantity, float $rate, bool $useAwards = false) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$addOrder = [ |
42
|
|
|
"resource" => "order", |
43
|
|
|
"operation" => "post", |
44
|
|
|
'payload' => [ |
45
|
|
|
'marketSymbol' => $market, |
46
|
|
|
'direction' => 'BUY', |
47
|
|
|
'type' => 'LIMIT', |
48
|
|
|
'quantity' => $quantity, |
49
|
|
|
'limit' => $rate, |
50
|
|
|
'timeInForce' => 'GOOD_TIL_CANCELLED', |
51
|
|
|
'useAwards' => $useAwards |
52
|
|
|
|
53
|
|
|
]]; |
54
|
|
|
$this->batchQueue->push($addOrder); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** https://bittrex.github.io/api/v3#operation--batch-post |
58
|
|
|
* https://bittrex.github.io/api/v3#operation--orders-post |
59
|
|
|
* @param string $market |
60
|
|
|
* @param float $quantity |
61
|
|
|
* @param float $rate |
62
|
|
|
* @param bool $useAwards |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function addSellLimit(string $market, float $quantity, float $rate, bool $useAwards = false) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$addOrder = [ |
67
|
|
|
"resource" => "order", |
68
|
|
|
"operation" => "post", |
69
|
|
|
'payload' => [ |
70
|
|
|
'marketSymbol' => $market, |
71
|
|
|
'direction' => 'SELL', |
72
|
|
|
'type' => 'LIMIT', |
73
|
|
|
'quantity' => $quantity, |
74
|
|
|
'limit' => $rate, |
75
|
|
|
'timeInForce' => 'GOOD_TIL_CANCELLED', |
76
|
|
|
'useAwards' => $useAwards |
77
|
|
|
|
78
|
|
|
]]; |
79
|
|
|
$this->batchQueue->push($addOrder); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** https://bittrex.github.io/api/v3#operation--batch-post |
83
|
|
|
* https://bittrex.github.io/api/v3#operation--orders--orderId--delete |
84
|
|
|
* @param string $uuid |
85
|
|
|
*/ |
86
|
|
|
public function addCancel(string $uuid) |
87
|
|
|
{ |
88
|
|
|
$addOrder = [ |
89
|
|
|
"resource" => "order", |
90
|
|
|
"operation" => "delete", |
91
|
|
|
'payload' => [ |
92
|
|
|
'id' => $uuid |
93
|
|
|
]]; |
94
|
|
|
$this->batchQueue->push($addOrder); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** https://bittrex.github.io/api/v3#operation--batch-post |
98
|
|
|
* Create a new batch request. Currently batch requests are limited to placing and cancelling orders. The request model corresponds to the equivalent individual operations. |
99
|
|
|
* Batch operations are executed sequentially in the order they are listed in the request. The response will return one result for each operation in the request in the same order. |
100
|
|
|
* The status and response payload are the same as the responses would be if individual API requests were made for each operation |
101
|
|
|
* @throws TransformResponseException | GuzzleException |
102
|
|
|
*/ |
103
|
|
|
public function executeBatch(): array |
104
|
|
|
{ |
105
|
|
|
$queue = []; |
106
|
|
|
while (!$this->batchQueue->isEmpty()) |
107
|
|
|
$queue[] = $this->batchQueue->pop(); |
108
|
|
|
$options = ['body' => json_encode($queue)]; |
109
|
|
|
return $this->rest('POST', '/batch', $options); |
110
|
|
|
} |
111
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.