/** * Display a listing of the resource. * * @return Response */ public function balances($address_uuid, Guard $auth, Request $request, APIControllerHelper $helper, AccountRepository $account_respository, PaymentAddressRepository $payment_address_repository, LedgerEntryRepository $ledger_entry_repository) { return DB::transaction(function () use($address_uuid, $auth, $request, $helper, $account_respository, $payment_address_repository, $ledger_entry_repository) { $user = $auth->getUser(); if (!$user) { throw new Exception("User not found", 1); } $payment_address = $helper->requireResourceOwnedByUser($address_uuid, $user, $payment_address_repository); $resources = $account_respository->findByAddressAndUserID($payment_address['id'], $user['id'], $this->buildAccountFilter($request, $account_respository)); // get a (valid) type $type = null; if (strlen($type_string = $request->input('type'))) { try { $type = LedgerEntry::typeStringToInteger($type_string); } catch (Exception $e) { return $helper->buildJSONResponse(['message' => 'bad type parameter'], 400); } } // add the balances to each one $accounts_with_balances = []; foreach ($resources as $account) { $account_and_balances = $account->serializeForAPI(); $balances = $ledger_entry_repository->accountBalancesByAsset($account, $type); $account_and_balances['balances'] = $balances; $accounts_with_balances[] = $account_and_balances; } return $helper->buildJSONResponse($accounts_with_balances); }); }
public function zeroAllBalances(PaymentAddress $payment_address, APICall $api_call) { $txid = null; foreach ($this->account_repository->findByAddressAndUserID($payment_address['id'], $payment_address['user_id']) as $account) { $actual_balances_by_type = $this->ledger_entry_repository->accountBalancesByAsset($account, null); foreach ($actual_balances_by_type as $type_string => $actual_balances) { if ($type_string == 'sending') { continue; } foreach ($actual_balances as $asset => $quantity) { $this->ledger_entry_repository->addDebit($quantity, $asset, $account, LedgerEntry::typeStringToInteger($type_string), LedgerEntry::DIRECTION_OTHER, $txid, $api_call); } } } }
protected function addPaymentAddresses($addresses) { if (!$addresses) { return; } foreach ($addresses as $raw_attributes) { $attributes = $raw_attributes; unset($attributes['accountBalances']); unset($attributes['rawAccountBalances']); $payment_address = $this->payment_address_repository->createWithUser($this->getSampleUser(), $this->payment_address_helper->sampleVars($attributes)); // create a default account AccountHandler::createDefaultAccount($payment_address); // assign balances $ledger_entry_repository = app('App\\Repositories\\LedgerEntryRepository'); $account_repository = app('App\\Repositories\\AccountRepository'); $default_account = $account_repository->findByName('default', $payment_address['id']); $btc_balance = 0; if (isset($raw_attributes['accountBalances'])) { foreach ($raw_attributes['accountBalances'] as $asset => $balance) { $ledger_entry_repository->addCredit($balance, $asset, $default_account, LedgerEntry::CONFIRMED, LedgerEntry::DIRECTION_OTHER, 'testtxid'); if ($asset == 'BTC') { $btc_balance += $balance; } } } if (isset($raw_attributes['rawAccountBalances'])) { foreach ($raw_attributes['rawAccountBalances'] as $type_string => $balances) { foreach ($balances as $asset => $balance) { $ledger_entry_repository->addCredit($balance, $asset, $default_account, LedgerEntry::typeStringToInteger($type_string), LedgerEntry::DIRECTION_OTHER, 'testtxid'); if ($asset == 'BTC') { $btc_balance += $balance; } } } } // add UTXOs if ($btc_balance > 0) { $this->payment_address_helper->addUTXOToPaymentAddress($btc_balance, $payment_address); } } }