Skip to content

Commit

Permalink
split UTXOs command
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensayshi committed Feb 13, 2015
1 parent 31839cf commit 3e8fd5a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Blocktrail\SDK\Console\Commands\ListUTXOCommand;
use Blocktrail\SDK\Console\Commands\ListWalletsCommand;
use Blocktrail\SDK\Console\Commands\PayCommand;
use Blocktrail\SDK\Console\Commands\SplitUTXOsCommand;
use Blocktrail\SDK\Console\Commands\StoreWalletPassphraseCommand;
use Blocktrail\SDK\Console\Commands\UseWalletCommand;
use Symfony\Component\Console\Application as ConsoleApplication;
Expand Down Expand Up @@ -39,6 +40,7 @@ protected function getDefaultCommands() {
$commands[] = new BalanceCommand();
$commands[] = new DiscoveryCommand();
$commands[] = new ListUTXOCommand();
$commands[] = new SplitUTXOsCommand();

return $commands;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/ListUTXOCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function configure() {
// ->setAliases(['utxos'])
->setDescription("List UTXO set for a wallet")
->addOption('page', 'p', InputOption::VALUE_REQUIRED, 'pagination page', 1)
->addOption('per-page', 'pp', InputOption::VALUE_REQUIRED, 'pagination limit', 50);;
->addOption('per-page', 'pp', InputOption::VALUE_REQUIRED, 'pagination limit', 50);

parent::configure();
}
Expand Down
84 changes: 84 additions & 0 deletions src/Console/Commands/SplitUTXOsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Blocktrail\SDK\Console\Commands;

use BitWasp\BitcoinLib\BitcoinLib;
use Blocktrail\SDK\BlocktrailSDK;
use Blocktrail\SDK\Console\Application;
use Blocktrail\SDK\WalletInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;

class SplitUTXOsCommand extends AbstractWalletCommand {

protected function configure() {
$this
->setName('split_utxos')
->setDescription("Split UTXOs")
->addArgument("count", InputArgument::REQUIRED, "the amount of chunks")
->addArgument("value", InputArgument::REQUIRED, "the value of each chunk")
->addOption('value-is-total', null, InputOption::VALUE_NONE, "the value argument should be devided by the count")
->addOption('silent', 's', InputOption::VALUE_NONE, "don't ask for confirmation");

parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output) {
/** @var Output $output */
parent::execute($input, $output);

/** @var QuestionHelper $questionHelper */
$questionHelper = $this->getHelper('question');

$wallet = $this->getWallet($input, $output);

list($confirmed, $unconfirmed) = $wallet->getBalance();

$count = $input->getArgument('count');
$value = $input->getArgument('value');

if (strpos($value, ".") !== false || strpos($value, "," !== false)) {
$value = BlocktrailSDK::toSatoshi($value);
} else {
if (!$questionHelper->ask($input, $output, new ConfirmationQuestion("Did you specify this value in satoshi? [y/N] ", false))) {
$value = BlocktrailSDK::toSatoshi($value);
}
}

if ($input->getOption('value-is-total')) {
$value = (int)floor($value / $count);
}

if ($value * $count > $confirmed) {
$output->writeln("<error>You do not have enough confirmed balance; " . BlocktrailSDK::toBTCString($value * $count) . " BTC > " . BlocktrailSDK::toBTCString($confirmed) . " BTC) </error>");
exit(1);
}

$pay = [];
for ($i = 0; $i < $count; $i++) {
$pay[$wallet->getNewAddress()] = $value;
}

if (!$input->getOption('silent')) {
$output->writeln("Sending payment from [<bold>{$wallet->getIdentifier()}</bold>] to:");
foreach ($pay as $address => $value) {
$output->writeln("[{$address}] {$value} Satoshi = " . BlocktrailSDK::toBTCString($value) . " BTC");
}

if (!$questionHelper->ask($input, $output, new ConfirmationQuestion("Send? [Y/n] ", true))) {
exit(1);
}
}

$txHash = $wallet->pay($pay);

$output->writeln("TX {$txHash}");
}
}

0 comments on commit 3e8fd5a

Please sign in to comment.