You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.8 KiB
PHP
83 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Payment;
|
|
|
|
use App\Models\UsdtWallet;
|
|
|
|
class UsdtWalletService
|
|
{
|
|
public static function parse($raw)
|
|
{
|
|
$lines = preg_split('/\r\n|\r|\n/', trim((string) $raw));
|
|
$wallets = [];
|
|
foreach ($lines as $line) {
|
|
$line = trim($line);
|
|
if ($line === '') {
|
|
continue;
|
|
}
|
|
[$address, $label] = array_pad(array_map('trim', explode('|', $line, 2)), 2, '');
|
|
if (!TronAddress::isValid($address)) {
|
|
throw new \InvalidArgumentException('TRON 收款地址格式或校验和无效:' . $address);
|
|
}
|
|
if (isset($wallets[$address])) {
|
|
throw new \InvalidArgumentException('TRON 收款地址重复:' . $address);
|
|
}
|
|
if (mb_strlen($label) > 100) {
|
|
throw new \InvalidArgumentException('收款地址备注不能超过 100 个字符');
|
|
}
|
|
$wallets[$address] = $label;
|
|
}
|
|
if (count($wallets) > 100) {
|
|
throw new \InvalidArgumentException('收款地址池最多配置 100 个地址');
|
|
}
|
|
return $wallets;
|
|
}
|
|
|
|
public static function sync(array $wallets)
|
|
{
|
|
$now = time();
|
|
$connection = (new UsdtWallet())->getConnection();
|
|
$connection->transaction(function () use ($wallets, $now) {
|
|
if ($wallets === []) {
|
|
UsdtWallet::query()->update(['enabled' => 0, 'updated_at' => $now]);
|
|
return;
|
|
}
|
|
|
|
UsdtWallet::whereNotIn('address', array_keys($wallets))->update([
|
|
'enabled' => 0,
|
|
'updated_at' => $now,
|
|
]);
|
|
$sort = 0;
|
|
foreach ($wallets as $address => $label) {
|
|
$wallet = UsdtWallet::where('address', $address)->lockForUpdate()->first();
|
|
if ($wallet === null) {
|
|
$wallet = new UsdtWallet();
|
|
$wallet->address = $address;
|
|
$wallet->allocation_cursor = 0;
|
|
$wallet->last_assigned_at = 0;
|
|
$wallet->last_scanned_at = 0;
|
|
$wallet->last_error = '';
|
|
$wallet->created_at = $now;
|
|
}
|
|
$wallet->label = $label;
|
|
$wallet->enabled = 1;
|
|
$wallet->sort = $sort++;
|
|
$wallet->updated_at = $now;
|
|
$wallet->save();
|
|
}
|
|
}, 3);
|
|
}
|
|
|
|
public static function text()
|
|
{
|
|
return UsdtWallet::where('enabled', 1)
|
|
->orderBy('sort', 'asc')
|
|
->orderBy('id', 'asc')
|
|
->get()
|
|
->map(function (UsdtWallet $wallet) {
|
|
return (string) $wallet->address . ((string) $wallet->label !== '' ? ' | ' . (string) $wallet->label : '');
|
|
})
|
|
->implode("\n");
|
|
}
|
|
}
|