feat: quene
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Jobs\AutoTransferAddress;
|
||||
use App\Models\TransferRecord;
|
||||
use App\Models\WalletAddress;
|
||||
use Illuminate\Support\LazyCollection;
|
||||
@@ -20,12 +21,13 @@ class AutoTransferService
|
||||
/**
|
||||
* Cron entry: scan addresses that have a mnemonic and any positive coin balance.
|
||||
*
|
||||
* @return array{inspected: int, triggered: int, ok: int, failed: int, skipped: int}
|
||||
* @return array{inspected: int, queued: int, triggered: int, ok: int, failed: int, skipped: int}
|
||||
*/
|
||||
public function run(): array
|
||||
{
|
||||
$stats = [
|
||||
'inspected' => 0,
|
||||
'queued' => 0,
|
||||
'triggered' => 0,
|
||||
'ok' => 0,
|
||||
'failed' => 0,
|
||||
@@ -37,13 +39,35 @@ class AutoTransferService
|
||||
'at' => now()->toDateTimeString(),
|
||||
], 'transfer');
|
||||
|
||||
$async = config('queue.default') !== 'sync';
|
||||
|
||||
foreach ($this->candidates() as $address) {
|
||||
$stats['inspected']++;
|
||||
if ($async) {
|
||||
AutoTransferAddress::dispatch($address->id, 'cron');
|
||||
$stats['queued']++;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$outcome = $this->evaluate($address, 'cron');
|
||||
$stats['triggered'] += $outcome['triggered'];
|
||||
$stats['ok'] += $outcome['ok'];
|
||||
$stats['failed'] += $outcome['failed'];
|
||||
$stats['skipped'] += $outcome['skipped'];
|
||||
|
||||
if ($outcome['failed'] > 0) {
|
||||
create_log([
|
||||
'event' => 'auto_transfer_run_aborted',
|
||||
'reason' => 'transfer_failed',
|
||||
'wallet_address_id' => $address->id,
|
||||
'address' => $address->address,
|
||||
'chain' => $address->chain_type,
|
||||
'stats' => $stats,
|
||||
'at' => now()->toDateTimeString(),
|
||||
], 'transfer');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
create_log([
|
||||
@@ -179,6 +203,18 @@ class AutoTransferService
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->lastAutoFailed((string) $address->address, $asset)) {
|
||||
create_log($base + [
|
||||
'event' => 'auto_transfer_skip',
|
||||
'skip' => 'previous_auto_transfer_failed',
|
||||
'asset' => $asset,
|
||||
'balance' => (string) $balance,
|
||||
], 'transfer');
|
||||
$stats['skipped']++;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->recentAutoSuccess((string) $address->address, $asset)) {
|
||||
try {
|
||||
$this->balances->refresh($address);
|
||||
@@ -272,6 +308,7 @@ class AutoTransferService
|
||||
'asset' => $asset,
|
||||
'error' => $result['error'] ?? 'unknown',
|
||||
], 'transfer');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -287,21 +324,42 @@ class AutoTransferService
|
||||
return $stats;
|
||||
}
|
||||
|
||||
private function lastAutoFailed(string $fromAddress, string $asset): bool
|
||||
{
|
||||
$latest = $this->latestAutoRecord($fromAddress, $asset);
|
||||
|
||||
return $latest !== null && $latest->status === TransferRecord::STATUS_FAILED;
|
||||
}
|
||||
|
||||
private function recentAutoSuccess(string $fromAddress, string $asset): bool
|
||||
{
|
||||
$latest = $this->latestAutoRecord($fromAddress, $asset);
|
||||
if ($latest === null || $latest->status !== TransferRecord::STATUS_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$at = $latest->created_at;
|
||||
if ($at === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $at->gte(now()->subMinutes(self::COOLDOWN_MINUTES));
|
||||
}
|
||||
|
||||
private function latestAutoRecord(string $fromAddress, string $asset): ?TransferRecord
|
||||
{
|
||||
$fromAddress = trim($fromAddress);
|
||||
$asset = strtoupper(trim($asset));
|
||||
if ($fromAddress === '' || $asset === '') {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
return TransferRecord::query()
|
||||
->where('from_address', $fromAddress)
|
||||
->where('asset', $asset)
|
||||
->where('operator', 'auto')
|
||||
->where('status', TransferRecord::STATUS_SUCCESS)
|
||||
->where('created_at', '>=', now()->subMinutes(self::COOLDOWN_MINUTES))
|
||||
->exists();
|
||||
->orderByDesc('id')
|
||||
->first();
|
||||
}
|
||||
|
||||
private function assetScale(string $asset): int
|
||||
|
||||
Reference in New Issue
Block a user