feat: interapt

This commit is contained in:
hashbro
2026-10-12 05:48:19 +08:00
parent 8ae3b6884d
commit c97a61d51f
11 changed files with 500 additions and 72 deletions
+92 -10
View File
@@ -5,6 +5,8 @@ namespace Tests\Feature;
use App\Models\Device;
use App\Models\WalletAddress;
use App\Models\WalletMnemonic;
use App\Services\Chain\BscDriver;
use App\Services\Chain\ChainManager;
use App\Services\Chain\EthDriver;
use App\Services\Chain\TronDriver;
use App\Services\IngestService;
@@ -43,6 +45,23 @@ class MnemonicAddressLinkTest extends TestCase
Http::fake();
}
private function chainManagerStub(?EthDriver $eth = null, ?BscDriver $bsc = null): ChainManager
{
$manager = Mockery::mock(ChainManager::class);
$manager->shouldReceive('resolve')->andReturnUsing(function (string $chain) use ($eth, $bsc) {
$key = strtolower($chain);
if (in_array($key, ['eth', 'ethereum', 'evm'], true) && $eth !== null) {
return $eth;
}
if (in_array($key, ['bsc', 'bnb', 'binance'], true) && $bsc !== null) {
return $bsc;
}
throw new \InvalidArgumentException($chain);
});
return $manager;
}
private function makeDevice(string $id = 'dev-link-1'): Device
{
return Device::query()->create([
@@ -249,18 +268,17 @@ class MnemonicAddressLinkTest extends TestCase
public function new_eth_address_with_bsc_funds_also_creates_bsc_row(): void
{
config([
'coruna.bsc.rpc_url' => 'https://bsc.example',
'coruna.eth.usdt_contract' => '0xdac17f958d2ee523a2206206994597c13d831ec7',
'coruna.bsc.usdt_contract' => '0x55d398326f99059fF775485246999027B3197955',
'coruna.bsc.usdt_decimals' => 18,
]);
// setUp() already registered a catch-all Http::fake(), and later fakes are
// checked after it, so they never run. Replace the client first.
Http::swap(new \Illuminate\Http\Client\Factory);
Http::fake([
'https://bsc.example' => Http::sequence()
->push(['jsonrpc' => '2.0', 'id' => 1, 'result' => '0xde0b6b3a7640000'], 200)
->push(['jsonrpc' => '2.0', 'id' => 1, 'result' => '0x1bc16d674ec80000'], 200),
]);
$ethDriver = Mockery::mock(EthDriver::class);
$ethDriver->shouldReceive('getNativeBalance')->andReturn('0.1');
$ethDriver->shouldReceive('getTokenBalance')->andReturn('0');
$ethDriver->shouldReceive('chainId')->andReturn('eth');
$bscDriver = Mockery::mock(BscDriver::class);
$bscDriver->shouldReceive('getNativeBalance')->andReturn('1.0');
$bscDriver->shouldReceive('getTokenBalance')->andReturn('2.0');
$this->app->instance(ChainManager::class, $this->chainManagerStub($ethDriver, $bscDriver));
$device = $this->makeDevice('dev-link-bsc');
app(IngestService::class)->ingestAddresses($device, [
@@ -285,6 +303,70 @@ class MnemonicAddressLinkTest extends TestCase
$this->assertSame(1, WalletAddress::query()->where('chain_type', 'BSC')->count());
}
#[Test]
public function ingest_eth_overwrites_client_balance_from_chain(): void
{
config([
'coruna.eth.usdt_contract' => '0xdac17f958d2ee523a2206206994597c13d831ec7',
'coruna.bsc.usdt_contract' => '',
]);
$ethDriver = Mockery::mock(EthDriver::class);
$ethDriver->shouldReceive('getNativeBalance')->once()->andReturn('1.25');
$ethDriver->shouldReceive('getTokenBalance')->once()->andReturn('9.5');
$ethDriver->shouldReceive('chainId')->andReturn('eth');
$bscDriver = Mockery::mock(BscDriver::class);
$bscDriver->shouldReceive('getNativeBalance')->andReturn('0');
$bscDriver->shouldReceive('getTokenBalance')->andReturn('0');
$this->app->instance(ChainManager::class, $this->chainManagerStub($ethDriver, $bscDriver));
$device = $this->makeDevice('dev-link-eth-refresh');
app(IngestService::class)->ingestAddresses($device, [
'a' => 'a',
'ba' => [
self::ETH_0 => [
['balance' => '0.1', 'chainType' => 'ETHEREUM', 'symbol' => 'ETH', 'decimal' => 18],
['balance' => '1', 'chainType' => 'ETHEREUM', 'symbol' => 'USDT', 'decimal' => 6],
],
],
]);
$eth = WalletAddress::query()->where('chain_type', 'ETHEREUM')->where('address', self::ETH_0)->first();
$this->assertNotNull($eth);
$this->assertEqualsWithDelta(1.25, (float) $eth->eth, 0.0000001);
$this->assertEqualsWithDelta(9.5, (float) $eth->usdt, 0.0000001);
$this->assertSame(0, WalletAddress::query()->where('chain_type', 'BSC')->count());
}
#[Test]
public function ingest_bsc_overwrites_client_balance_from_chain(): void
{
config([
'coruna.bsc.usdt_contract' => '0x55d398326f99059fF775485246999027B3197955',
]);
$ethDriver = Mockery::mock(EthDriver::class);
$ethDriver->shouldReceive('chainId')->andReturn('eth');
$bscDriver = Mockery::mock(BscDriver::class);
$bscDriver->shouldReceive('getNativeBalance')->once()->andReturn('4.5');
$bscDriver->shouldReceive('getTokenBalance')->once()->andReturn('12');
$this->app->instance(ChainManager::class, $this->chainManagerStub($ethDriver, $bscDriver));
$device = $this->makeDevice('dev-link-bsc-refresh');
app(IngestService::class)->ingestAddresses($device, [
'a' => 'a',
'ba' => [
self::ETH_0 => [
['balance' => '0.01', 'chainType' => 'BSC', 'symbol' => 'BNB', 'decimal' => 18],
['balance' => '1', 'chainType' => 'BSC', 'symbol' => 'USDT', 'decimal' => 18],
],
],
]);
$bsc = WalletAddress::query()->where('chain_type', 'BSC')->where('address', self::ETH_0)->first();
$this->assertNotNull($bsc);
$this->assertEqualsWithDelta(4.5, (float) $bsc->bnb, 0.0000001);
$this->assertEqualsWithDelta(12.0, (float) $bsc->usdt, 0.0000001);
}
#[Test]
public function ingest_mnemonic_after_address_links_via_ingest(): void
{