public function testIncomingInvalidatedTransactionUpdatesLedgerEntries()
 {
     $provisional_tx_repository = app('App\\Repositories\\ProvisionalTransactionRepository');
     $notification_repository = app('App\\Repositories\\NotificationRepository');
     $ledger_entry_repository = app('App\\Repositories\\LedgerEntryRepository');
     // setup monitors
     $payment_address_helper = app('PaymentAddressHelper');
     $payment_address_one = $payment_address_helper->createSamplePaymentAddressWithoutInitialBalances(null, ['address' => '1JztLWos5K7LsqW5E78EASgiVBaCe6f7cD']);
     // receive unconfirmed transactions
     $parsed_txs = $this->receiveUnconfirmedTransactions(1);
     // check accounts
     $account_one = AccountHandler::getAccount($payment_address_one, 'default');
     $balance = $ledger_entry_repository->accountBalancesByAsset($account_one, null);
     PHPUnit::assertEquals(0.004, $balance['unconfirmed']['BTC']);
     // now confirm a malleated version of the transaction
     $malleated_tx = $parsed_txs[0];
     $malleated_tx['txid'] = str_repeat('0', 54) . 'MALLEATED1';
     $malleated_txs = [$malleated_tx];
     // now confirm the malleated transaction (with 2 confirmations)
     $this->sendConfirmationEvents(2, $malleated_txs);
     // check for invalidation notification
     $balance = $ledger_entry_repository->accountBalancesByAsset($account_one, null);
     PHPUnit::assertEquals(0.004, $balance['confirmed']['BTC']);
     PHPUnit::assertArrayNotHasKey('BTC', $balance['unconfirmed']);
 }
Esempio n. 2
0
 public function send(PaymentAddress $payment_address, $parsed_tx, $confirmations)
 {
     $is_confirmed = $confirmations >= self::SEND_CONFIRMATIONS_REQUIRED;
     // check for vins
     if (!isset($parsed_tx['bitcoinTx']['vin']) or !$parsed_tx['bitcoinTx']['vin']) {
         EventLog::logError('bitcoinTx.send.noVins', ['txid' => $parsed_tx['txid']]);
         return;
     }
     // get the sending account
     $account = AccountHandler::getAccount($payment_address);
     foreach ($parsed_tx['bitcoinTx']['vin'] as $vin) {
         // update the UTXO record
         $is_spendable = true;
         if ($is_spendable) {
             $spent_txid = isset($vin['txid']) ? $vin['txid'] : null;
             $spent_n = isset($vin['vout']) ? $vin['vout'] : null;
             if ($spent_txid and $spent_n !== null) {
                 $type = $is_confirmed ? TXO::SENT : TXO::SENDING;
                 $spent = true;
                 // spend the utxo (updates an existing utxo)
                 Log::debug("new send TXO: {$spent_txid}:{$spent_n}/" . CurrencyUtil::valueToSatoshis($vin['value']) . " " . TXO::typeIntegerToString($type) . " to " . $payment_address['uuid']);
                 $this->txo_repository->updateOrCreate(['txid' => $spent_txid, 'n' => $spent_n, 'type' => $type, 'spent' => $spent, 'amount' => CurrencyUtil::valueToSatoshis($vin['value'])], $payment_address, $account);
             }
         }
     }
 }
Esempio n. 3
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $payment_address_repo = app('App\\Repositories\\PaymentAddressRepository');
     $account_repository = app('App\\Repositories\\AccountRepository');
     $user_repository = app('App\\Repositories\\UserRepository');
     $payment_address_uuid = $this->input->getArgument('payment-address-uuid');
     $name = $this->input->getOption('name');
     $show_ledger = $this->input->getOption('ledger');
     $show_inactive = $this->input->getOption('inactive');
     $payment_address = $payment_address_repo->findByUuid($payment_address_uuid);
     if (!$payment_address) {
         throw new Exception("Payment address not found", 1);
     }
     $this->info("Showing accounts for payment address {$payment_address['address']} ({$payment_address['uuid']})");
     if (strlen($name)) {
         $account = AccountHandler::getAccount($payment_address, $name);
         if (!$account) {
             $this->error("Account not found for {$name}");
             return;
         }
         $accounts = [$account];
     } else {
         // all accounts, maybe including inactive
         $accounts = $account_repository->findByAddress($payment_address, $show_inactive ? null : 1);
     }
     foreach ($accounts as $account) {
         $this->line($this->showAccount($account, $show_ledger));
     }
     $this->info('done');
 }
Esempio n. 4
0
 public function createSampleTXO($payment_address = null, $overrides = [])
 {
     if ($payment_address === null) {
         $payment_address = app('PaymentAddressHelper')->createSamplePaymentAddress();
     }
     $account = AccountHandler::getAccount($payment_address, 'default');
     // build a real script
     $script = ScriptFactory::scriptPubKey()->payToAddress(AddressFactory::fromString($payment_address['address']));
     $attributes = array_merge(['txid' => $this->nextTXID(), 'n' => 0, 'script' => $script->getBuffer()->getHex(), 'amount' => 54321, 'type' => TXO::CONFIRMED, 'spent' => false, 'green' => false], $overrides);
     $txo_model = $this->txo_repository->create($payment_address, $account, $attributes);
     return $txo_model;
 }
Esempio n. 5
0
 public function addBalancesToPaymentAddressAccount($balances, $payment_address, $with_utxos = true, $account_name = 'default', $txid = 'SAMPLE01')
 {
     if (!$balances) {
         return;
     }
     $account = AccountHandler::getAccount($payment_address, $account_name);
     foreach ($balances as $asset => $quantity) {
         $this->ledger_entry_repository->addCredit($quantity, $asset, $account, LedgerEntry::CONFIRMED, LedgerEntry::DIRECTION_OTHER, $txid);
     }
     if ($with_utxos) {
         $float_btc_balance = isset($balances['BTC']) ? $balances['BTC'] : 0;
         return $this->addUTXOToPaymentAddress($float_btc_balance, $payment_address, $account_name, $txid);
     }
     return [];
 }
Esempio n. 6
0
 public function testPrimingTransactionUpdatesBalancesCorrectly()
 {
     $user = $this->app->make('\\UserHelper')->createSampleUser();
     $payment_address = $this->app->make('\\PaymentAddressHelper')->createSamplePaymentAddress($user);
     $my_address = $payment_address['address'];
     $default_account = AccountHandler::getAccount($payment_address);
     // test balances before
     $ledger_entry_repo = app('App\\Repositories\\LedgerEntryRepository');
     $balances = $ledger_entry_repo->accountBalancesByAsset($default_account, null);
     PHPUnit::assertEquals(1, $balances['confirmed']['BTC']);
     // generate a multi priming transaction
     $parsed_transaction = $this->buildTransaction(0.4, $my_address, [[$my_address, 0.0001], [$my_address, 0.0001], [$my_address, 0.0001], [$my_address, 0.3996]]);
     // echo "\$parsed_transaction: ".json_encode($parsed_transaction, 192)."\n";
     $this->sendTransactionWithConfirmations($parsed_transaction, 0);
     // test balances after
     $ledger_entry_repo = app('App\\Repositories\\LedgerEntryRepository');
     $balances = $ledger_entry_repo->accountBalancesByAsset($default_account, null);
     // echo "[after] \$balances: ".json_encode($balances, 192)."\n";
     PHPUnit::assertEquals(0.9999, $balances['confirmed']['BTC']);
 }
Esempio n. 7
0
 public function testUpdateByTXOIdentifiers()
 {
     // add one
     $txo_repository = $this->app->make('App\\Repositories\\TXORepository');
     $txid = $this->TXOHelper()->nextTXID();
     $txid_2 = $this->TXOHelper()->nextTXID();
     $payment_address = app('PaymentAddressHelper')->createSamplePaymentAddressWithoutInitialBalances();
     $default_account = AccountHandler::getAccount($payment_address);
     // create...
     $attributes = ['txid' => $txid, 'n' => 0, 'type' => TXO::CONFIRMED, 'script' => 'testscript', 'spent' => false];
     $txo_model_1 = $txo_repository->updateOrCreate($attributes, $payment_address, $default_account);
     $txo_model_2 = $txo_repository->updateOrCreate(['n' => 1] + $attributes, $payment_address, $default_account);
     $txo_model_3 = $txo_repository->updateOrCreate(['txid' => $txid_2] + $attributes, $payment_address, $default_account);
     $txo_model_4 = $txo_repository->updateOrCreate(['txid' => $txid_2, 'n' => 1] + $attributes, $payment_address, $default_account);
     // update
     $update_vars = ['script' => 'testscriptupdated'];
     $txo_repository->updateByTXOIdentifiers(["{$txid}:1", "{$txid_2}:1"], $update_vars);
     $reloaded_txo_1 = $txo_repository->findByID($txo_model_1['id']);
     $reloaded_txo_2 = $txo_repository->findByID($txo_model_2['id']);
     $reloaded_txo_3 = $txo_repository->findByID($txo_model_3['id']);
     $reloaded_txo_4 = $txo_repository->findByID($txo_model_4['id']);
     PHPUnit::assertEquals('testscript', $reloaded_txo_1['script']);
     PHPUnit::assertEquals('testscriptupdated', $reloaded_txo_2['script']);
     PHPUnit::assertEquals('testscript', $reloaded_txo_3['script']);
     PHPUnit::assertEquals('testscriptupdated', $reloaded_txo_4['script']);
     // update (2)
     $update_vars = ['script' => 'testscriptupdated222'];
     $txo_repository->updateByTXOIdentifiers(["{$txid}:0"], $update_vars);
     $reloaded_txo_1 = $txo_repository->findByID($txo_model_1['id']);
     $reloaded_txo_2 = $txo_repository->findByID($txo_model_2['id']);
     $reloaded_txo_3 = $txo_repository->findByID($txo_model_3['id']);
     $reloaded_txo_4 = $txo_repository->findByID($txo_model_4['id']);
     PHPUnit::assertEquals('testscriptupdated222', $reloaded_txo_1['script']);
     PHPUnit::assertEquals('testscriptupdated', $reloaded_txo_2['script']);
     PHPUnit::assertEquals('testscript', $reloaded_txo_3['script']);
     PHPUnit::assertEquals('testscriptupdated', $reloaded_txo_4['script']);
 }
Esempio n. 8
0
 protected function clearBalances($balance_type, $balances_by_asset, $payment_address, APICall $api_call)
 {
     $ledger = app('App\\Repositories\\LedgerEntryRepository');
     // get the default account
     $account = AccountHandler::getAccount($payment_address, 'default');
     foreach ($balances_by_asset as $asset => $quantity) {
         if ($quantity == 0) {
             continue;
         }
         // debit the {$balance_type} balance
         $msg = "Balancing {$balance_type} ledger with {$quantity} {$asset}";
         $this->info($msg);
         $ledger_entry_type = $balance_type == 'unconfirmed' ? LedgerEntry::UNCONFIRMED : LedgerEntry::SENDING;
         if ($quantity < 0) {
             $ledger->addCredit(0 - $quantity, $asset, $account, $ledger_entry_type, LedgerEntry::DIRECTION_OTHER, null, $api_call);
         } else {
             $ledger->addDebit($quantity, $asset, $account, $ledger_entry_type, LedgerEntry::DIRECTION_OTHER, null, $api_call);
         }
     }
 }
Esempio n. 9
0
 protected function loadPaymentAddressInfo($address)
 {
     if (!isset($this->payment_address_info_cache)) {
         $this->payment_address_info_cache = [];
     }
     if (!isset($this->payment_address_info_cache[$address])) {
         $found_payment_address = $this->payment_address_repository->findByAddress($address)->first();
         if ($found_payment_address) {
             $this->payment_address_info_cache[$address] = [$found_payment_address, AccountHandler::getAccount($found_payment_address)];
         } else {
             $this->payment_address_info_cache[$address] = [null, null];
         }
     }
     return $this->payment_address_info_cache[$address];
 }
Esempio n. 10
0
 public function testMoveAccounts()
 {
     // receiving a transaction adds TXOs
     $txo_repository = $this->app->make('App\\Repositories\\TXORepository');
     // setup monitors
     $payment_address_helper = app('PaymentAddressHelper');
     $receiving_address_one = $payment_address_helper->createSamplePaymentAddressWithoutInitialBalances(null, ['address' => '1JztLWos5K7LsqW5E78EASgiVBaCe6f7cD']);
     // create a second account
     $default_account = AccountHandler::getAccount($receiving_address_one);
     $account_1 = AccountHandler::createAccount($receiving_address_one, 'account1');
     // receive unconfirmed transactions
     $parsed_txs = $this->receiveUnconfirmedTransactions(1);
     $loaded_txos = $txo_repository->findAll();
     // check that it is in the default account
     $all_txos = $txo_repository->findAll();
     $loaded_txo = $all_txos[0];
     PHPUnit::assertEquals($default_account['id'], $loaded_txo['account_id']);
     // move the utxo to account 1
     TXOHandler::moveAccounts([$loaded_txo], $default_account, $account_1);
     $all_txos = $txo_repository->findAll();
     PHPUnit::assertCount(1, $all_txos);
     $loaded_txo = $all_txos[0];
     PHPUnit::assertEquals($account_1['id'], $loaded_txo['account_id']);
     // check findByAccount
     $loaded_txos = $txo_repository->findByAccount($default_account, true);
     PHPUnit::assertCount(0, $loaded_txos);
     $loaded_txos = $txo_repository->findByAccount($account_1, true);
     PHPUnit::assertCount(1, $loaded_txos);
     $loaded_txos = $txo_repository->findByAccount($account_1, false);
     PHPUnit::assertCount(0, $loaded_txos);
 }
Esempio n. 11
0
 /**
  * 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');
     $should_reconcile = $this->input->getOption('reconcile');
     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();
     }
     foreach ($payment_addresses as $payment_address) {
         Log::debug("reconciling TXOs for {$payment_address['address']} ({$payment_address['uuid']})");
         // get xchain utxos
         $xchain_utxos_map = [];
         $db_txos = $txo_repository->findByPaymentAddress($payment_address, null, true);
         // unspent only
         foreach ($db_txos as $db_txo) {
             if ($db_txo['type'] != TXO::CONFIRMED) {
                 continue;
             }
             $filtered_utxo = ['txid' => $db_txo['txid'], 'n' => $db_txo['n'], 'script' => $db_txo['script'], 'amount' => $db_txo['amount']];
             $xchain_utxos_map[$filtered_utxo['txid'] . ':' . $filtered_utxo['n']] = $filtered_utxo;
         }
         // get daemon utxos
         $daemon_utxos_map = [];
         $all_utxos = $bitcoin_payer->getAllUTXOs($payment_address['address']);
         if ($all_utxos) {
             foreach ($all_utxos as $utxo) {
                 if (!isset($utxo['confirmations']) or $utxo['confirmations'] == 0) {
                     continue;
                 }
                 $filtered_utxo = ['txid' => $utxo['txid'], 'n' => $utxo['vout'], 'script' => $utxo['script'], 'amount' => CurrencyUtil::valueToSatoshis($utxo['amount'])];
                 $daemon_utxos_map[$utxo['txid'] . ':' . $utxo['vout']] = $filtered_utxo;
             }
         }
         // compare
         $differences = $this->buildDifferences($xchain_utxos_map, $daemon_utxos_map);
         if ($differences['any']) {
             $this->comment("Differences found for {$payment_address['address']} ({$payment_address['uuid']})");
             $this->line(json_encode($differences, 192));
             $this->line('');
             if ($should_reconcile) {
                 $account = AccountHandler::getAccount($payment_address);
                 foreach ($differences['differences'] as $difference) {
                     // delete the xchain txo if it is different in any way
                     if (isset($difference['xchain']) and $difference['xchain']) {
                         $utxo = $difference['xchain'];
                         $this->line('Removing xchain UTXO: ' . json_encode($utxo, 192));
                         // delete
                         $txo_model = $txo_repository->findByTXIDAndOffset($utxo['txid'], $utxo['n']);
                         if ($txo_model['payment_address_id'] != $payment_address['id']) {
                             throw new Exception("Mismatched payment address id.  Expected {$payment_address['id']}.  Found {$txo_model['payment_address_id']}", 1);
                         }
                     }
                     if (isset($difference['daemon'])) {
                         $utxo = $difference['daemon'];
                         $this->line('Adding daemon UTXO: ' . json_encode($utxo, 192));
                         // add the daemon's utxo
                         $txo_repository->create($payment_address, $account, ['txid' => $utxo['txid'], 'n' => $utxo['n'], 'script' => $utxo['script'], 'amount' => $utxo['amount']]);
                     }
                 }
             }
         } else {
             $this->comment("No differences found for {$payment_address['address']} ({$payment_address['uuid']})");
         }
     }
     $this->info('done');
 }
Esempio n. 12
0
 protected function executeSend(APIControllerHelper $helper, Request $request, PaymentAddressRepository $payment_address_respository, SendRepository $send_respository, PaymentAddressSender $address_sender, Guard $auth, APICallRepository $api_call_repository, $id)
 {
     $user = $auth->getUser();
     if (!$user) {
         throw new Exception("User not found", 1);
     }
     // get the address
     $payment_address = $payment_address_respository->findByUuid($id);
     if (!$payment_address) {
         return new JsonResponse(['message' => 'address not found'], 404);
     }
     // make sure this address belongs to this user
     if ($payment_address['user_id'] != $user['id']) {
         return new JsonResponse(['message' => 'Not authorized to send from this address'], 403);
     }
     // attributes
     $request_attributes = $request->only(array_keys($request->rules()));
     // determine if this is a multisend
     $is_multisend = (isset($request_attributes['destinations']) and $request_attributes['destinations']);
     $is_regular_send = !$is_multisend;
     // normalize destinations
     $destinations = $is_multisend ? $this->normalizeDestinations($request_attributes['destinations']) : '';
     $destination = $is_regular_send ? $request_attributes['destination'] : '';
     // determine variables
     $quantity_sat = CurrencyUtil::valueToSatoshis($is_multisend ? $this->sumMultisendQuantity($destinations) : $request_attributes['quantity']);
     $asset = $is_regular_send ? $request_attributes['asset'] : 'BTC';
     $is_sweep = isset($request_attributes['sweep']) ? !!$request_attributes['sweep'] : false;
     $float_fee = isset($request_attributes['fee']) ? $request_attributes['fee'] : PaymentAddressSender::DEFAULT_FEE;
     $dust_size = isset($request_attributes['dust_size']) ? $request_attributes['dust_size'] : PaymentAddressSender::DEFAULT_REGULAR_DUST_SIZE;
     $request_id = isset($request_attributes['requestId']) ? $request_attributes['requestId'] : Uuid::uuid4()->toString();
     // create attibutes
     $create_attributes = [];
     $create_attributes['user_id'] = $user['id'];
     $create_attributes['payment_address_id'] = $payment_address['id'];
     $create_attributes['destination'] = $destination;
     $create_attributes['quantity_sat'] = $quantity_sat;
     $create_attributes['asset'] = $asset;
     $create_attributes['is_sweep'] = $is_sweep;
     $create_attributes['fee'] = $float_fee;
     $create_attributes['dust_size'] = $dust_size;
     // for multisends
     $create_attributes['destinations'] = $destinations;
     // the transaction must be committed before the lock is release and not after
     //   therefore we must release the lock after this closure completes
     $lock_must_be_released = false;
     $lock_must_be_released_with_delay = false;
     // create a send and lock it immediately
     $send_result = $send_respository->executeWithNewLockedSendByRequestID($request_id, $create_attributes, function ($locked_send) use($request_attributes, $create_attributes, $payment_address, $user, $helper, $send_respository, $address_sender, $api_call_repository, $request_id, $is_multisend, $is_regular_send, $quantity_sat, $asset, $destination, $destinations, $is_sweep, $float_fee, $dust_size, &$lock_must_be_released, &$lock_must_be_released_with_delay) {
         $api_call = $api_call_repository->create(['user_id' => $user['id'], 'details' => ['method' => 'api/v1/sends/' . $payment_address['uuid'], 'args' => $request_attributes]]);
         // if a send already exists by this request_id, just return it
         if (isset($locked_send['txid']) && strlen($locked_send['txid'])) {
             EventLog::log('send.alreadyFound', $locked_send);
             return $helper->transformResourceForOutput($locked_send);
         }
         $float_quantity = CurrencyUtil::satoshisToValue($quantity_sat);
         // send
         EventLog::log('send.requested', array_merge($request_attributes, $create_attributes));
         if ($is_sweep) {
             try {
                 // get lock
                 $lock_acquired = AccountHandler::acquirePaymentAddressLock($payment_address);
                 if ($lock_acquired) {
                     $lock_must_be_released = true;
                 }
                 list($txid, $float_balance_sent) = $address_sender->sweepAllAssets($payment_address, $request_attributes['destination'], $float_fee);
                 $quantity_sat_sent = CurrencyUtil::valueToSatoshis($float_balance_sent);
                 // clear all balances from all accounts
                 AccountHandler::zeroAllBalances($payment_address, $api_call);
                 // release the account lock with a slight delay
                 if ($lock_acquired) {
                     $lock_must_be_released_with_delay = true;
                 }
             } catch (PaymentException $e) {
                 EventLog::logError('error.sweep', $e);
                 return new JsonResponse(['message' => $e->getMessage()], 500);
             } catch (Exception $e) {
                 EventLog::logError('error.sweep', $e);
                 return new JsonResponse(['message' => 'Unable to complete this request'], 500);
             }
         } else {
             try {
                 // get the account
                 $account_name = (isset($request_attributes['account']) and strlen($request_attributes['account'])) ? $request_attributes['account'] : 'default';
                 $account = AccountHandler::getAccount($payment_address, $account_name);
                 if (!$account) {
                     EventLog::logError('error.send.accountMissing', ['address_id' => $payment_address['id'], 'account' => $account_name]);
                     return new JsonResponse(['message' => "This account did not exist."], 404);
                 }
                 // Log::debug("\$account=".json_encode($account, 192));
                 // get lock
                 $lock_acquired = AccountHandler::acquirePaymentAddressLock($payment_address);
                 if ($lock_acquired) {
                     $lock_must_be_released = true;
                 }
                 // whether to spend unconfirmed balances
                 $allow_unconfirmed = isset($request_attributes['unconfirmed']) ? $request_attributes['unconfirmed'] : false;
                 // Log::debug("\$allow_unconfirmed=".json_encode($allow_unconfirmed, 192));
                 // validate that the funds are available
                 if ($allow_unconfirmed) {
                     $has_enough_funds = AccountHandler::accountHasSufficientFunds($account, $float_quantity, $asset, $float_fee, $dust_size);
                 } else {
                     $has_enough_funds = AccountHandler::accountHasSufficientConfirmedFunds($account, $float_quantity, $asset, $float_fee, $dust_size);
                 }
                 if (!$has_enough_funds) {
                     EventLog::logError('error.send.insufficient', ['address_id' => $payment_address['id'], 'account' => $account_name, 'quantity' => $float_quantity, 'asset' => $asset]);
                     return new JsonResponse(['message' => "This account does not have sufficient" . ($allow_unconfirmed ? '' : ' confirmed') . " funds available."], 400);
                 }
                 // send the funds
                 EventLog::log('send.begin', ['request_id' => $request_id, 'address_id' => $payment_address['id'], 'account' => $account_name, 'quantity' => $float_quantity, 'asset' => $asset, 'destination' => $is_multisend ? $destinations : $destination]);
                 $txid = $address_sender->sendByRequestID($request_id, $payment_address, $is_multisend ? $destinations : $destination, $float_quantity, $asset, $float_fee, $dust_size);
                 EventLog::log('send.complete', ['txid' => $txid, 'request_id' => $request_id, 'address_id' => $payment_address['id'], 'account' => $account_name, 'quantity' => $float_quantity, 'asset' => $asset, 'destination' => $is_multisend ? $destinations : $destination]);
                 // tag funds as sent with the txid
                 if ($allow_unconfirmed) {
                     AccountHandler::markAccountFundsAsSending($account, $float_quantity, $asset, $float_fee, $dust_size, $txid);
                 } else {
                     AccountHandler::markConfirmedAccountFundsAsSending($account, $float_quantity, $asset, $float_fee, $dust_size, $txid);
                     // Log::debug("After marking confirmed funds as sent, all accounts for ${account['name']}: ".json_encode(app('App\Repositories\LedgerEntryRepository')->accountBalancesByAsset($account, null), 192));
                     // Log::debug("After marking confirmed funds as sent, all accounts for default: ".json_encode(app('App\Repositories\LedgerEntryRepository')->accountBalancesByAsset(AccountHandler::getAccount($payment_address), null), 192));
                 }
                 // release the account lock
                 if ($lock_acquired) {
                     $lock_must_be_released_with_delay = true;
                 }
             } catch (AccountException $e) {
                 EventLog::logError('error.pay', $e);
                 return new JsonResponse(['message' => $e->getMessage(), 'errorName' => $e->getErrorName()], $e->getStatusCode());
             } catch (PaymentException $e) {
                 EventLog::logError('error.pay', $e);
                 return new JsonResponse(['message' => $e->getMessage()], 500);
             } catch (Exception $e) {
                 EventLog::logError('error.pay', $e);
                 return new JsonResponse(['message' => 'Unable to complete this request'], 500);
             }
         }
         $attributes = [];
         $attributes['sent'] = time();
         $attributes['txid'] = $txid;
         EventLog::log('send.complete', $attributes);
         // update and send response
         $send_respository->update($locked_send, $attributes);
         return $helper->buildJSONResponse($locked_send->serializeForAPI());
     }, self::SEND_LOCK_TIMEOUT);
     // make sure to release the lock
     if ($lock_must_be_released_with_delay) {
         $this->releasePaymentAddressLockWithDelay($payment_address);
     } else {
         if ($lock_must_be_released) {
             AccountHandler::releasePaymentAddressLock($payment_address);
         }
     }
     return $send_result;
 }
 protected function syncConfirmedBalances($payment_address, $balance_differences, APICall $api_call)
 {
     $ledger = app('App\\Repositories\\LedgerEntryRepository');
     foreach ($balance_differences as $asset => $qty_by_type) {
         $xchain_quantity = $qty_by_type['xchain'];
         $daemon_quantity = $qty_by_type['daemon'];
         $default_account = AccountHandler::getAccount($payment_address);
         if ($xchain_quantity > $daemon_quantity) {
             // debit
             $quantity = $xchain_quantity - $daemon_quantity;
             $msg = "Debiting {$quantity} {$asset} from account {$default_account['name']}";
             $this->info($msg);
             $ledger->addDebit($quantity, $asset, $default_account, LedgerEntry::CONFIRMED, LedgerEntry::DIRECTION_OTHER, null, $api_call);
         } else {
             // credit
             $quantity = $daemon_quantity - $xchain_quantity;
             $msg = "Crediting {$quantity} {$asset} to account {$default_account['name']}";
             $this->info($msg);
             $ledger->addCredit($quantity, $asset, $default_account, LedgerEntry::CONFIRMED, LedgerEntry::DIRECTION_OTHER, null, $api_call);
         }
     }
 }