public function testCurrentUtilsConversions() { // float value to satoshis PHPUnit::assertEquals(12300000000, CurrencyUtil::valueToSatoshis(123)); PHPUnit::assertEquals(2100000000000000, CurrencyUtil::valueToSatoshis(21000000)); PHPUnit::assertEquals(12311111119, CurrencyUtil::valueToSatoshis(123.111111189)); // satoshis to float value PHPUnit::assertEquals(123, CurrencyUtil::satoshisToValue(12300000000)); PHPUnit::assertEquals(21000000, CurrencyUtil::satoshisToValue(2100000000000000)); PHPUnit::assertEquals(123.11111119, CurrencyUtil::satoshisToValue(12311111119)); // satoshisToFormattedString PHPUnit::assertEquals('123', CurrencyUtil::satoshisToFormattedString(12300000000)); PHPUnit::assertEquals('123.4', CurrencyUtil::satoshisToFormattedString(12340000000)); PHPUnit::assertEquals('1,235.6', CurrencyUtil::satoshisToFormattedString(123560000000)); // valueToFormattedString PHPUnit::assertEquals('1,234.5', CurrencyUtil::valueToFormattedString(1234.5)); }
protected function assertFound($expected_offsets, $sample_entries, $chosen_entries) { $expected_entry_arrays = []; foreach ($expected_offsets as $expected_offset) { $expected_entry_arrays[] = $sample_entries[$expected_offset]->toArray(); } $actual_amounts = []; $chosen_entry_arrays = []; foreach ($chosen_entries as $chosen_entry) { $chosen_entry_arrays[] = $chosen_entry ? $chosen_entry->toArray() : null; $actual_amounts[] = CurrencyUtil::satoshisToFormattedString($chosen_entry['amount']) . ' ' . $chosen_entry['asset']; } PHPUnit::assertEquals($expected_entry_arrays, $chosen_entry_arrays, "Did not find the expected offsets of " . json_encode($expected_offsets) . '. Actual amounts were ' . json_encode($actual_amounts)); }
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; }
/** * Execute the console command. * * @return mixed */ public function fire() { $txo_repository = app('App\\Repositories\\TXORepository'); $payment_address_repo = app('App\\Repositories\\PaymentAddressRepository'); $bitcoin_payer = app('Tokenly\\BitcoinPayer\\BitcoinPayer'); $payment_address_uuid = $this->input->getArgument('payment-address-uuid'); $show_spent = $this->input->getOption('spent'); if ($payment_address_uuid) { $payment_address = $payment_address_repo->findByUuid($payment_address_uuid); if (!$payment_address) { throw new Exception("Payment address not found", 1); } $payment_addresses = [$payment_address]; } else { $payment_addresses = $payment_address_repo->findAll(); } $xchain_utxos_map = []; foreach ($payment_addresses as $payment_address) { // get xchain utxos $db_txos = $txo_repository->findByPaymentAddress($payment_address, null, $show_spent ? null : true); // all or unspent only foreach ($db_txos as $db_txo) { $xchain_utxos_map[$db_txo['txid'] . ':' . $db_txo['n']] = $db_txo; } } // build a table $bool = function ($val) { return $val ? '<info>true</info>' : '<comment>false</comment>'; }; $headers = ['address', 'txid', 'n', 'amount', 'type', 'spent', 'green', 'created']; $rows = []; foreach ($xchain_utxos_map as $identifier => $txo) { $address = $payment_address_repo->findById($txo['payment_address_id']); $pieces = explode('.', CurrencyUtil::satoshisToFormattedString($txo['amount'])); if (count($pieces) == 2) { $amount = $pieces[0] . "." . str_pad($pieces[1], 8, '0', STR_PAD_RIGHT); } else { $amount = $amount . ".00000000"; } $created = $txo['created_at']->setTimezone('America/Chicago')->format("Y-m-d h:i:s A"); $type = TXO::typeIntegerToString($txo['type']); $rows[] = [$address['address'], $txo['txid'], $txo['n'], $amount, $type, $bool($txo['spent']), $bool($txo['green']), $created]; } $this->table($headers, $rows); $this->info('done'); }