public function transfer($address_uuid, Guard $auth, AccountTransferRequest $request, APIControllerHelper $helper, PaymentAddressRepository $payment_address_repository, APICallRepository $api_call_repository)
 {
     $user = $auth->getUser();
     if (!$user) {
         throw new Exception("User not found", 1);
     }
     $payment_address = $helper->requireResourceOwnedByUser($address_uuid, $user, $payment_address_repository);
     $params = $helper->getAttributesFromRequest($request);
     $api_call = $api_call_repository->create(['user_id' => $user['id'], 'details' => ['method' => 'api/v1/accounts/transfer/' . $address_uuid, 'args' => $params]]);
     try {
         if (isset($params['close']) and $params['close']) {
             AccountHandler::close($payment_address, $params['from'], $params['to'], $api_call);
         } else {
             if (isset($params['quantity']) and isset($params['asset'])) {
                 AccountHandler::transfer($payment_address, $params['from'], $params['to'], $params['quantity'], $params['asset'], isset($params['txid']) ? $params['txid'] : null, $api_call);
             } else {
                 // transfer all
                 AccountHandler::transferAllByTIXD($payment_address, $params['from'], $params['to'], $params['txid'], $api_call);
             }
         }
         // done
         return $helper->buildJSONResponse([], 204);
     } catch (AccountException $e) {
         return $helper->buildJSONResponse(['message' => $e->getMessage(), 'errorName' => $e->getErrorName()], $e->getStatusCode());
     } catch (HttpException $e) {
         return $helper->buildJSONResponse(['message' => $e->getMessage()], $e->getStatusCode());
     }
 }
示例#2
0
 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function show($uuid, Guard $auth, APIControllerHelper $helper, AccountRepository $account_respository)
 {
     $user = $auth->getUser();
     if (!$user) {
         throw new Exception("User not found", 1);
     }
     $resource = $helper->requireResourceOwnedByUser($uuid, $user, $account_respository);
     return $helper->transformResourceForOutput($resource);
 }
示例#3
0
 /**
  * Prime the address with UTXOs of a certain size
  *   only if needed
  * 
  *
  * @return Response
  */
 public function primeAddress($address_uuid, Guard $auth, Request $request, APIControllerHelper $helper, PaymentAddressRepository $payment_address_repository, TXORepository $txo_repository, TXOChooser $txo_chooser, PaymentAddressSender $payment_address_sender)
 {
     try {
         $user = $auth->getUser();
         if (!$user) {
             throw new Exception("User not found", 1);
         }
         $payment_address = $helper->requireResourceOwnedByUser($address_uuid, $user, $payment_address_repository);
         $size = floatval($request->input('size'));
         if ($size <= 0) {
             throw new Exception("Invalid size", 400);
         }
         $size_satoshis = CurrencyUtil::valueToSatoshis($size);
         $desired_prime_count = floatval($request->input('count'));
         if ($desired_prime_count <= 0) {
             throw new Exception("Invalid count", 400);
         }
         $fee = $request->input('fee');
         if ($fee !== null) {
             $fee = floatval($fee);
             if ($fee <= 0) {
                 throw new Exception("Invalid fee", 400);
             }
         }
         // get the UTXOs
         //   [TXO::UNCONFIRMED, TXO::CONFIRMED]
         $txos = $this->filterGreenOrConfirmedUTXOs($txo_repository->findByPaymentAddress($payment_address, null, true));
         // count the number that match the size
         $current_primed_count = 0;
         foreach ($txos as $txo) {
             if ($txo['amount'] == $size_satoshis) {
                 ++$current_primed_count;
             }
         }
         $txid = null;
         $new_primed_count = $current_primed_count;
         if ($desired_prime_count > $current_primed_count) {
             // create a new priming transaction...
             $desired_new_primes_count_to_create = $desired_prime_count - $current_primed_count;
             $actual_new_primes_count_to_create = $this->findMaximumNewPrimeCountTXOs($txo_chooser, $payment_address, $desired_new_primes_count_to_create, $size, $fee);
             if ($actual_new_primes_count_to_create > 0) {
                 $txid = $this->sendPrimingTransaction($payment_address_sender, $payment_address, $size, $actual_new_primes_count_to_create, $fee);
                 $new_primed_count = $current_primed_count + $actual_new_primes_count_to_create;
             }
         }
         $output = ['oldPrimedCount' => $current_primed_count, 'newPrimedCount' => $new_primed_count, 'txid' => $txid, 'primed' => $txid ? true : false];
         return $helper->buildJSONResponse($output);
     } catch (Exception $e) {
         if ($e->getCode() >= 400 and $e->getCode() < 500) {
             throw new HttpResponseException(new JsonResponse(['errors' => [$e->getMessage()]], 400));
         }
         throw $e;
     }
 }