Completed
Push — master ( 60e8f7...7e9313 )
by Mikhail
02:24
created

Batch::addSellLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 4
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 Queue $batchQueue;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
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
    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
    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
}