Exemplo n.º 1
0
 protected function changeBalance($quantity, $asset, $account = 'default', $type = 'confirmed', $raw_payment_address_id = 'default')
 {
     $payment_address_id = $this->resolvePaymentAddressID($raw_payment_address_id);
     if (isset($this->balances) and isset($this->balances[$payment_address_id])) {
         if (!isset($this->balances[$payment_address_id][$account])) {
             $this->balances[$payment_address_id][$account] = [];
         }
         if (!isset($this->balances[$payment_address_id][$account][$type])) {
             $this->balances[$payment_address_id][$account][$type] = [];
         }
         if (!isset($this->balances[$payment_address_id][$account][$type][$asset])) {
             $this->balances[$payment_address_id][$account][$type][$asset] = 0;
         }
         if ($quantity < 0 and round($this->balances[$payment_address_id][$account][$type][$asset] + $quantity, 8) < 0) {
             $xchain_exception = new XChainException("Insufficient funds: tried to debit account {$type} {$account} {$quantity} {$asset} - but had only {$this->balances[$payment_address_id][$account][$type][$asset]}", 1);
             $xchain_exception->setErrorName('ERR_INSUFFICIENT_FUNDS');
             throw $xchain_exception;
         }
         $this->balances[$payment_address_id][$account][$type][$asset] += $quantity;
     }
 }
Exemplo n.º 2
0
 protected function newAPIRequest($method, $path, $data = [])
 {
     $api_path = '/api/v1' . $path;
     $client = new GuzzleClient();
     if ($data and ($method == 'POST' or $method == 'PATCH')) {
         $body = \GuzzleHttp\Psr7\stream_for(json_encode($data));
         $headers = ['Content-Type' => 'application/json'];
         $request = new \GuzzleHttp\Psr7\Request($method, $this->xchain_url . $api_path, $headers, $body);
     } else {
         if ($method == 'GET') {
             $request = new \GuzzleHttp\Psr7\Request($method, $this->xchain_url . $api_path);
             $request = \GuzzleHttp\Psr7\modify_request($request, ['query' => http_build_query($data, null, '&', PHP_QUERY_RFC3986)]);
         } else {
             $request = new \GuzzleHttp\Psr7\Request($method, $this->xchain_url . $api_path);
         }
     }
     // add auth
     $request = $this->getAuthenticationGenerator()->addSignatureToGuzzle6Request($request, $this->api_token, $this->api_secret_key);
     // send request
     try {
         $response = $client->send($request);
     } catch (RequestException $e) {
         if ($response = $e->getResponse()) {
             // interpret the response and error message
             $code = $response->getStatusCode();
             try {
                 $json = json_decode($response->getBody(), true);
             } catch (Exception $parse_json_exception) {
                 // could not parse json
                 $json = null;
             }
             if ($json and isset($json['message'])) {
                 // throw an XChainException with the errorName
                 if (isset($json['errorName'])) {
                     $xchain_exception = new XChainException($json['message'], $code);
                     $xchain_exception->setErrorName($json['errorName']);
                     throw $xchain_exception;
                 }
                 // generic exception
                 throw new Exception($json['message'], $code);
             }
         }
         // if no response, then just throw the original exception
         throw $e;
     }
     $code = $response->getStatusCode();
     if ($code == 204) {
         // empty content
         return [];
     }
     $json = json_decode($response->getBody(), true);
     if (!is_array($json)) {
         throw new Exception("Unexpected response: " . $response->getBody(), 1);
     }
     return $json;
 }