Merge pull request #550 from openvk/ton-integration

Интеграция TON
This commit is contained in:
Vladimir Barinov 2022-06-02 13:19:29 +03:00 committed by GitHub
commit 305bd7b02f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 166 additions and 1 deletions

103
CLI/FetchToncoinTransactions.php Executable file
View file

@ -0,0 +1,103 @@
<?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
{
$header = $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;
}
$testnetSubdomain = OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["testnet"] ? "testnet." : "";
$url = "https://" . $testnetSubdomain . "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();
$trHash = $selection->hash ?? NULL;
$trLt = $selection->lt ?? NULL;
$data = http_build_query([
"address" => OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["address"],
"limit" => 100,
"hash" => $trHash,
"to_lt" => $trLt
]);
$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) {
$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)) {
$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"]
]);
}
}
}
$header->writeln("Processing finished :3");
return Command::SUCCESS;
}
}

View file

@ -455,6 +455,18 @@ final class UserPresenter extends OpenVKPresenter
"main", "privacy", "finance", "finance.top-up", "interface"
]) ? $this->queryParam("act")
: "main";
if($this->template->mode == "finance") {
$address = OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["address"];
$text = str_replace("$1", $this->user->identity->getId(), OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["hint"]);
$qrCode = explode("base64,", (new QRCode(new QROptions([
"imageTransparent" => false
])))->render("ton://transfer/$address?text=$text"));
$this->template->qrCodeType = substr($qrCode[0], 5);
$this->template->qrCodeData = $qrCode[1];
}
$this->template->user = $user;
$this->template->themes = Themepacks::i()->getThemeList();
}

View file

@ -383,7 +383,10 @@
{_on_your_account}
<div style="width: 100%; height: 60px; font-weight: 100;" id="balance">{$thisUser->getCoins()}</div>
{_points_count}<br/>
<small><a href="?act=finance.top-up">[{_have_voucher}?]</a></small>
<small><a href="?act=finance.top-up">[{_have_voucher}?]</a></small><br>
<small n:if="OPENVK_ROOT_CONF['openvk']['preferences']['ton']['enabled']">
<a href="javascript:showCoinsTopUpTroughTon()">[{_transfer_trough_ton}]</a>
</small>
</p>
</div>
@ -393,6 +396,28 @@
balance.style.width = Math.ceil(balance.parentNode.getBoundingClientRect().width) + "px";
textFit(balance, { alignVert: true });
function convertCurrency(coin) {
var currencyTON = {php echo OPENVK_ROOT_CONF['openvk']['preferences']['ton']['rate']};
u(".coins_dialog_conv").nodes[0].innerHTML = tr("transfer_ton_currency_per_ton", coin * currencyTON);
}
function showCoinsTopUpTroughTon() {
MessageBox(tr("transfer_trough_ton"), `
<div class="messagebox-content-header">
${tr("transfer_ton_contents")}
</div>
<div style="margin-top: 30px">` +
{tr("transfer_ton_address", OPENVK_ROOT_CONF['openvk']['preferences']['ton']['address'], str_replace("$1", $thisUser->getId(), OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["hint"]))} +
`<div style="text-align: center;">
<img width="225" height="225" src="data:` + {$qrCodeType} + `;base64,` + {$qrCodeData} + `"><br>
<input oninput="convertCurrency(this.value)"> = <span class="coins_dialog_conv">0 TON</span>
</div>
</div>`
, [tr("close")], [
Function.noop
]);
}
</script>
{elseif $isFinanceTU}

View file

@ -0,0 +1,6 @@
CREATE TABLE `cryptotransactions` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`hash` varchar(45) COLLATE utf8mb4_general_nopad_ci NOT NULL,
`lt` bigint(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_nopad_ci;

View file

@ -603,6 +603,11 @@
"transferred_to_you" = "transferred to you";
"transfer_trough_ton" = "Top up with TON";
"transfer_ton_contents" = "You can top up your balance with TON cryptocurrency. It's just enough to scan the QR-code with the Tonkeeper app, or manually send TON according to the requisites. Within a few minutes you will receive a certain amount of votes.";
"transfer_ton_address" = "<b>Wallet address:</b> $1<br/><b>Message content:</b> $2";
"transfer_ton_currency_per_ton" = "$1 TON";
"receiver_address" = "Receiver address";
"coins_count" = "Number of votes";
"message" = "Message";

View file

@ -639,6 +639,11 @@
"transferred_to_you" = "передал вам";
"transfer_trough_ton" = "Пополнить с помощью TON";
"transfer_ton_contents" = "Вы можете пополнить ваш баланс с помощью криптовалюты TON. Достаточно отсканировать QR-code приложением Tonkeeper, или вручную отправить TON по реквизитам. В течении нескольких минут вам придут определенное количество голосов.";
"transfer_ton_address" = "<b>Адрес кошелька:</b> $1<br/><b>Содержание сообщения:</b> $2";
"transfer_ton_currency_per_ton" = "$1 TON";
"receiver_address" = "Адрес получателя";
"coins_count" = "Количество голосов";
"message" = "Сообщение";

View file

@ -53,6 +53,14 @@ openvk:
processingLimit: 3000
emojiProcessingLimit: 1000
commerce: false
ton:
enabled: false
address: "🅿"
testnet: false # Only for testing purposes.
rate: 0.02 # TONs per 1 coin
regex: "ovk=([0-9]+)"
hint: "ovk=$1"
# Please read docs to understand how to turn on automatic checking for new translations
menu:
links:
- name: "@left_menu_donate"

View file

@ -9,5 +9,6 @@ $bootstrap->ignite(true);
$application = new Application();
$application->add(new CLI\RebuildImagesCommand);
$application->add(new CLI\FetchToncoinTransactions);
$application->run();