Skip to content

Instantly share code, notes, and snippets.

@mishak87
Created February 6, 2014 13:06
Show Gist options
  • Save mishak87/8843786 to your computer and use it in GitHub Desktop.
Save mishak87/8843786 to your computer and use it in GitHub Desktop.
Preview of async php api for mcxnow.com
<?php
namespace App;
use CryptoBot;
use CryptoBot\McxNow\Currency;
use Nette;
use React;
use React\Promise\When;
use Symfony;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class FundsCommand extends Symfony\Component\Console\Command\Command
{
protected function configure()
{
$this->setName('funds');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var \Nette\DI\Container $container */
$container = $this->getHelper('dic');
/** @var \App\ClientFactory $clientFactory */
$clientFactory = $container->getByType('App\ClientFactory');
$loop = React\EventLoop\Factory::create();
$client = $clientFactory->createClient($loop);
$printInfo = function ($data) use ($loop, $output) {
$book = [];
$btcBalance = 0;
$btcOrderAmount = 0;
$format = function ($value) {
return number_format($value, 8, '.', '');
};
foreach (Currency::traded() as $i => $currency) {
$orderAmount = 0;
foreach ($data[$i]['orders'] as $record) {
if ($record['buy']) {
$btcOrderAmount += $record['amount'];
} else {
$orderAmount += $record['amount'];
}
}
$book[(string) $currency] = [
'balance' => $data[$i]['currency_balance'],
'last_trade_id' => $data[$i]['last_trade_id'],
'orders' => $data[$i]['orders'],
'orders_amount' => $orderAmount,
'orders_btc_amount' => $btcOrderAmount,
];
$btcBalance = $data[$i]['base_balance'];
}
foreach (Currency::traded() as $currency) {
$record = $book[(string) $currency];
$output->writeln((string) $currency . ' ' . $format($record['balance']) . ' (balance) + ' . $format($record['orders_amount']) . ' (' . count($record['orders']) . ' orders) = ' . $format(($record['balance'] + $record['orders_amount'])));
}
$output->writeln('BTC ' . $format($btcBalance) . ' + ' . $format($btcOrderAmount) . ' in orders.');
$output->writeln('Completed in ' . round(Nette\Diagnostics\Debugger::timer('test') * 1000, 4) . 'ms');
};
$main = function () use ($client, $printInfo) {
When::all(array_map($client->info, Currency::traded()))->then($printInfo);
};
// TODO add cookie storage
$client->checkAuthentication()->then(function ($authenticated) use ($client, $output, $main) {
if ($authenticated) {
$output->writeln("<comment>Already authenticated</comment>");
$main();
} else {
$output->writeln("<comment>Authenticating</comment>");
$client->authenticate()->then($main);
}
});
$loop->run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment