BtcE::getOrderBook()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Del\Exchange;
4
5
use GuzzleHttp\Client;
6
7
class BtcE extends ExchangeAbstract
8
{
9
    private $nonce = false;
10
11
12
    /**
13
     * @param $uri
14
     * @param array $params
15
     * @return mixed
16
     * @throws \Exception
17
     */
18 7
    public function send($uri, array $params = [])
19
    {
20 7
        if(!$this->nonce) {
21
22 7
            $nonce = explode(' ', microtime());
23 7
            $this->nonce = + $nonce[1].($nonce[0] * 1000000);
0 ignored issues
show
Documentation Bug introduced by
The property $nonce was declared of type boolean, but +$nonce[1] . $nonce[0] * 1000000 is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
24 7
            $this->nonce = substr($this->nonce,5);
0 ignored issues
show
Documentation Bug introduced by
The property $nonce was declared of type boolean, but substr($this->nonce, 5) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
25
26 7
            $this->nonce = explode(' ', microtime())[1];
27
//            $this->nonce = (int) 10000 * microtime(true);
28
        } else {
29
            $this->nonce ++ ;
30
        }
31
32
33 7
        $params['nonce'] = $this->nonce;
34 7
        $params['method'] = $uri;
35
36
        // generate the POST data string
37 7
        $post_data = http_build_query($params, '', '&');
38 7
        $sign = hash_hmac('sha512', $post_data, $this->config->getSecret());
39
40
41
42
        $headers = [
43 7
            'Sign: '.$sign,
44 7
            'Key: '.$this->config->getKey(),
45
        ];
46
47
48 7
        static $ch = null;
49 7
        if (is_null($ch)) {
50 1
            $ch = curl_init();
51 1
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
52 1
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
53
        }
54 7
        curl_setopt($ch, CURLOPT_URL, 'https://wex.nz/tapi/');
55 7
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
56 7
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
57 7
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
58 7
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
59
60
        // run the query
61 7
        $res = curl_exec($ch);
62 7
        if ($res === false) throw new \Exception('Could not get reply: '.curl_error($ch));
63 7
        $dec = json_decode($res, true);
64 7
        if (!$dec) throw new \Exception('Invalid data received, please make sure connection is working and requested API exists');
65 7
        if($dec['success'] == 0) {
66 7
            throw new \Exception($dec['error']);
67
        }
68
        return $dec['return'];
69
70
    }
71
72
    /**
73
     *
74
     */
75 9
    public function setClient()
76
    {
77 9
        $this->client = new Client(['base_uri' => 'https://wex.nz/tapi/']);
78 9
    }
79
80
    /**
81
     * @param $btc
82
     * @param $price
83
     * @return mixed
84
     * @throws \Exception
85
     */
86 1
    public function buyOrder($btc, $price)
87
    {
88 1
        return $this->send('Trade',[
89 1
            'pair' => 'btc_usd',
90 1
            'type' => 'buy',
91 1
            'rate' => $price,
92 1
            'amount' => $btc
93
        ]);
94
    }
95
96
    /**
97
     * @param $btc
98
     * @param $price
99
     * @return mixed
100
     * @throws \Exception
101 1
     */
102
    public function sellOrder($btc, $price)
103 1
    {
104 1
        return $this->send('Trade',[
105 1
            'pair' => 'btc_usd',
106 1
            'type' => 'sell',
107 1
            'rate' => $price,
108
            'amount' => $btc
109
        ]);
110
    }
111
112
    /**
113
     * @return mixed
114
     * @throws \Exception
115 1
     */
116
    public function getInfo()
117 1
    {
118
        return $this->send('getInfo');
119
    }
120
121
    /**
122
     * @return mixed
123 1
     */
124
    public function getTicker()
125 1
    {
126
        return json_decode($this->client->get('https://wex.nz/api/3/ticker/btc_usd')->getBody()->getContents(), true);
127
    }
128
129
130 1
    /**
131
     * @return array
132 1
     */
133 1
    public function getOrderBook($pair = 'btc_usd')
134
    {
135
        return json_decode($this->client->get('https://wex.nz/api/3/depth/' . $pair)->getBody()->getContents(), true);
136
    }
137
138 1
139
    /**
140 1
     * @return mixed
141
     * @throws \Exception
142
     */
143 1
    public function getOrders()
144
    {
145 1
        return $this->send('ActiveOrders',[
146
            'pair' => 'btc_usd'
147
        ]);
148
    }
149
150 1
    /**
151
     * @return mixed
152 1
     * @throws \Exception
153 1
     */
154
    public function getTransactionHistory()
155
    {
156
        return $this->send('TransHistory',[]);
157
    }
158
159
    /**
160
     * @return mixed
161
     * @throws \Exception
162
     */
163
    public function getTradeHistory()
164
    {
165
        return $this->send('TradeHistory',[]);
166
    }
167
168
169
    /**
170
     * @param $order_id
171
     * @return mixed
172
     * @throws \Exception
173
     */
174
    public function cancelOrder($order_id)
175
    {
176
        return $this->send('CancelOrder',[
177
            'order_id' => $order_id
178
        ]);
179
    }
180
181
182
}