2022-05-07 02:33:22 +03:00
< ? php declare ( strict_types = 1 );
namespace openvk\CLI ;
use Chandler\Database\DatabaseConnection ;
use openvk\Web\Models\Repositories\Users ;
use openvk\Web\Models\Entities\Notifications\CoinsTransferNotification ;
use Symfony\Component\Console\Command\Command ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Input\InputOption ;
use Symfony\Component\Console\Output\OutputInterface ;
use Nette\Utils\ImageException ;
define ( " NANOTON " , 1000000000 );
class FetchToncoinTransactions extends Command
{
private $images ;
protected static $defaultName = " fetch-ton " ;
function __construct ()
{
$this -> transactions = DatabaseConnection :: i () -> getContext () -> table ( " cryptotransactions " );
parent :: __construct ();
}
protected function configure () : void
{
$this -> setDescription ( " Fetches TON transactions to top up the users' balance " )
-> setHelp ( " This command checks for new transactions on TON Wallet and then top up the balance of specified users " );
}
protected function execute ( InputInterface $input , OutputInterface $output ) : int
{
2022-05-07 18:49:14 +03:00
$header = $output -> section ();
2022-05-07 02:33:22 +03:00
$header -> writeln ([
" TONCOIN Fetcher " ,
" ===================== " ,
" " ,
]);
2022-05-07 18:49:14 +03:00
if ( ! OPENVK_ROOT_CONF [ " openvk " ][ " preferences " ][ " ton " ][ " enabled " ]) {
$header -> writeln ( " Sorry, but you handn't enabled the TON support in your config file yet. " );
2022-05-07 02:33:22 +03:00
return Command :: FAILURE ;
}
2022-05-23 15:36:19 +03:00
$testnetSubdomain = OPENVK_ROOT_CONF [ " openvk " ][ " preferences " ][ " ton " ][ " testnet " ] ? " testnet. " : " " ;
2022-05-23 16:13:02 +03:00
$url = " https:// " . $testnetSubdomain . " toncenter.com/api/v2/getTransactions? " ;
2022-05-07 02:33:22 +03:00
$opts = [
" http " => [
" method " => " GET " ,
" header " => " Accept: application/json "
]
];
$selection = $this -> transactions -> select ( 'hash, lt' ) -> order ( " id DESC " ) -> limit ( 1 ) -> fetch ();
2022-05-23 16:13:02 +03:00
$trHash = $selection -> hash ? ? NULL ;
$trLt = $selection -> lt ? ? NULL ;
2022-05-07 02:33:22 +03:00
$data = http_build_query ([
" address " => OPENVK_ROOT_CONF [ " openvk " ][ " preferences " ][ " ton " ][ " address " ],
2022-05-23 16:13:02 +03:00
" limit " => 100 ,
" hash " => $trHash ,
" to_lt " => $trLt
2022-05-07 02:33:22 +03:00
]);
$response = file_get_contents ( $url . $data , false , stream_context_create ( $opts ));
$response = json_decode ( $response , true );
2022-05-09 15:12:14 +03:00
$header -> writeln ( " Gonna up the balance of users " );
2022-05-07 18:49:14 +03:00
foreach ( $response [ " result " ] as $transfer ) {
2022-05-23 16:13:02 +03:00
$outputArray ;
preg_match ( '/' . OPENVK_ROOT_CONF [ " openvk " ][ " preferences " ][ " ton " ][ " regex " ] . '/' , $transfer [ " in_msg " ][ " message " ], $outputArray );
$userId = ctype_digit ( $outputArray [ 1 ]) ? intval ( $outputArray [ 1 ]) : NULL ;
if ( is_null ( $userId )) {
2022-05-09 15:12:14 +03:00
$header -> writeln ( " Well, that's a donation. Thanks! XD " );
2022-05-07 15:42:25 +03:00
} else {
2022-05-23 16:13:02 +03:00
$user = ( new Users ) -> get ( $userId );
2022-05-07 15:42:25 +03:00
if ( ! $user ) {
2022-05-09 15:12:14 +03:00
$header -> writeln ( " Well, that's a donation. Thanks! XD " );
2022-05-07 02:33:22 +03:00
} else {
$value = ( $transfer [ " in_msg " ][ " value " ] / NANOTON ) / OPENVK_ROOT_CONF [ " openvk " ][ " preferences " ][ " ton " ][ " rate " ];
$user -> setCoins ( $user -> getCoins () + $value );
$user -> save ();
2022-06-02 14:07:31 +03:00
( new CoinsTransferNotification ( $user , ( new Users ) -> get ( OPENVK_ROOT_CONF [ " openvk " ][ " preferences " ][ " support " ][ " adminAccount " ]), ( int ) $value , " Via TON cryptocurrency " )) -> emit ();
2022-05-07 18:49:14 +03:00
$header -> writeln ( $value . " coins are added to " . $user -> getId () . " user id " );
2022-05-07 02:33:22 +03:00
$this -> transactions -> insert ([
2022-05-07 15:42:25 +03:00
" id " => NULL ,
2022-05-07 02:33:22 +03:00
" hash " => $transfer [ " transaction_id " ][ " hash " ],
2022-05-07 15:42:25 +03:00
" lt " => $transfer [ " transaction_id " ][ " lt " ]
2022-05-07 02:33:22 +03:00
]);
}
}
}
2022-05-09 15:12:14 +03:00
$header -> writeln ( " Processing finished :3 " );
2022-05-07 02:33:22 +03:00
return Command :: SUCCESS ;
}
}