/** * 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); }); }
protected function assembleAccountBalancesWithTXID($results, $in_satoshis = false) { $sums = array_fill_keys(LedgerEntry::allTypeStrings(), []); foreach ($results as $result) { $txid = $result['txid']; if (!$txid) { $txid = 'none'; } if ($in_satoshis) { $sums[LedgerEntry::typeIntegerToString($result['type'])][$txid][$result['asset']] = $result['total_amount']; } else { $sums[LedgerEntry::typeIntegerToString($result['type'])][$txid][$result['asset']] = CurrencyUtil::satoshisToValue($result['total_amount']); } } return $sums; }
protected function showAccount(Account $account, $show_ledger = false) { $ledger = app('App\\Repositories\\LedgerEntryRepository'); $all_account_balances = $ledger->accountBalancesByAsset($account, null); $sep = str_repeat('-', 60) . "\n"; $out = ''; $out .= "{$sep}{$account['name']} ({$account['id']}, {$account['uuid']})\n{$sep}"; if ($show_ledger) { $out .= "\n"; $rows = []; $all_entries = $ledger->findByAccount($account); foreach ($all_entries as $entry) { $row = []; $row['date'] = $entry['created_at']->setTimezone('America/Chicago')->format('Y-m-d H:i:s T'); $row['amount'] = CurrencyUtil::satoshisToFormattedString($entry['amount']); $row['asset'] = $entry['asset']; $row['type'] = LedgerEntry::typeIntegerToString($entry['type']); $row['txid'] = $entry['txid']; $rows[] = $row; } $renderer = new ArrayToTextTable($rows); $renderer->showHeaders(true); $out .= $renderer->render(true) . "\n"; } // $out .= "BALANCES\n"; $out .= "\n"; foreach (LedgerEntry::allTypeStrings() as $type_string) { $out .= "{$type_string}:\n"; if (isset($all_account_balances[$type_string]) and $all_account_balances[$type_string]) { foreach ($all_account_balances[$type_string] as $asset => $balance) { $out .= " {$asset}: " . CurrencyUtil::valueToFormattedString($balance) . "\n"; } } else { $out .= " [empty]\n"; } } $out .= "\n{$sep}\n"; return $out; }
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 clearDatabasesForScenario() { \App\Models\Block::truncate(); \App\Models\Transaction::truncate(); \App\Models\Notification::truncate(); \App\Models\MonitoredAddress::truncate(); \App\Models\PaymentAddress::truncate(); \App\Models\Send::truncate(); \App\Models\User::truncate(); \App\Models\Account::truncate(); \App\Models\APICall::truncate(); \App\Models\LedgerEntry::truncate(); return; }