diff --git a/CLI/FetchToncoinTransactions.php b/CLI/FetchToncoinTransactions.php new file mode 100755 index 00000000..fac83eae --- /dev/null +++ b/CLI/FetchToncoinTransactions.php @@ -0,0 +1,112 @@ +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 + { + $header = $output->section(); + $counter = $output->section(); + + $header->writeln([ + "TONCOIN Fetcher", + "=====================", + "", + ]); + + if(!OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["enabled"]) + { + $header->writeln([ + "Sorry, but you handn't enabled the TON support in your config file yet.", + "", + ]); + return Command::FAILURE; + } + + $testnet_subdomain = OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["testnet"] ? "testnet." : ""; + $url = "https://" . $testnet_subdomain . "toncenter.com/api/v2/getTransactions?"; + + $opts = [ + "http" => [ + "method" => "GET", + "header" => "Accept: application/json" + ] + ]; + + $selection = $this->transactions->select('hash, lt')->order("id DESC")->limit(1)->fetch(); + $tr_hash = $selection->hash ?? NULL; + $tr_lt = $selection->lt ?? NULL; + + $data = http_build_query([ + "address" => OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["address"], + "limit" => 100, + "hash" => $tr_hash, + "to_lt" => $tr_lt + ]); + + $response = file_get_contents($url . $data, false, stream_context_create($opts)); + $response = json_decode($response, true); + + $header->writeln(["Gonna up the balance of users"]); + foreach($response["result"] as $transfer) + { + $output_array; + preg_match('/ovk=([0-9]+)/', $transfer["in_msg"]["message"], $output_array); + $userid = ctype_digit($output_array[1]) ? intval($output_array[1]) : null; + if($userid === null) + { + $header->writeln(["Well, that's a donation. Thanks! XD"]); + } + else + { + $user = (new Users)->get($userid); + if(!$user) + { + $header->writeln(["Well, that's a donation. Thanks! XD"]); + } else { + $value = ($transfer["in_msg"]["value"] / NANOTON) / OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["rate"]; + $user->setCoins($user->getCoins() + $value); + $user->save(); + (new CoinsTransferNotification($user, (new Users)->get(OPENVK_ROOT_CONF["openvk"]["preferences"]["support"]["adminAccount"]), 0, "Via TON cryptocurrency"))->emit(); + $header->writeln([$value . " coins are added to " . $user->getId() . " user id"]); + $this->transactions->insert([ + "id" => null, + "hash" => $transfer["transaction_id"]["hash"], + "lt" => $transfer["transaction_id"]["lt"] + ]); + } + } + } + + $counter->overwrite("Processing finished :3"); + + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/openvkctl b/openvkctl index 6c913aeb..6dd2d3e7 100755 --- a/openvkctl +++ b/openvkctl @@ -9,5 +9,6 @@ $bootstrap->ignite(true); $application = new Application(); $application->add(new CLI\RebuildImagesCommand); +$application->add(new CLI\FetchToncoinTransactions); $application->run(); \ No newline at end of file