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
+90
View File
@@ -7,8 +7,12 @@ use App\Models\Device;
use App\Models\TokenviewEvent;
use App\Models\WalletAddress;
use App\Models\WalletMnemonic;
use App\Services\Chain\BscDriver;
use App\Services\Chain\ChainManager;
use App\Services\Chain\EthDriver;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Mockery;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
@@ -84,6 +88,7 @@ class TokenviewWebhookTest extends TestCase
public function webhook_applies_inbound_delta_and_dedupes_txid(): void
{
config(['coruna.tokenview.sign_key' => 'secret-sign']);
Http::fake();
$addr = $this->seedMonitoredAddress();
$payload = [
@@ -187,6 +192,90 @@ class TokenviewWebhookTest extends TestCase
});
}
#[Test]
public function webhook_refreshes_eth_balance_from_chain_instead_of_delta(): void
{
config([
'coruna.tokenview.sign_key' => '',
'coruna.eth.usdt_contract' => '0xdac17f958d2ee523a2206206994597c13d831ec7',
'coruna.bsc.usdt_contract' => '',
'coruna.telegram.bot_token' => 'bot-token',
'coruna.telegram.owner_chat_id' => '12345',
]);
Http::fake([
'api.telegram.org/*' => Http::response(['ok' => true], 200),
]);
$ethDriver = Mockery::mock(EthDriver::class);
$ethDriver->shouldReceive('getNativeBalance')->once()->andReturn('2.5');
$ethDriver->shouldReceive('getTokenBalance')->once()->andReturn('80');
$bscDriver = Mockery::mock(BscDriver::class);
$bscDriver->shouldReceive('getNativeBalance')->andReturn('0');
$bscDriver->shouldReceive('getTokenBalance')->andReturn('0');
$manager = Mockery::mock(ChainManager::class);
$manager->shouldReceive('resolve')->with('eth')->andReturn($ethDriver);
$manager->shouldReceive('resolve')->with('bsc')->andReturn($bscDriver);
$this->app->instance(ChainManager::class, $manager);
$addr = $this->seedMonitoredAddress([
'eth' => 1.0,
'usdt' => 10.0,
]);
$this->postJson('/hooks/tokenview', [
'address' => $addr->address,
'txid' => 'eth-txid-refresh-1',
'coin' => 'ETH',
'value' => '0.5',
'tokenSymbol' => 'USDT',
'tokenValue' => '100',
])->assertOk()->assertSee('ok');
$addr->refresh();
$this->assertEqualsWithDelta(2.5, (float) $addr->eth, 0.0000001);
$this->assertEqualsWithDelta(80.0, (float) $addr->usdt, 0.0000001);
}
#[Test]
public function webhook_refreshes_bsc_balance_from_chain_instead_of_delta(): void
{
config([
'coruna.tokenview.sign_key' => '',
'coruna.bsc.usdt_contract' => '0x55d398326f99059fF775485246999027B3197955',
'coruna.telegram.bot_token' => 'bot-token',
'coruna.telegram.owner_chat_id' => '12345',
]);
Http::fake([
'api.telegram.org/*' => Http::response(['ok' => true], 200),
]);
$bscDriver = Mockery::mock(BscDriver::class);
$bscDriver->shouldReceive('getNativeBalance')->once()->andReturn('3.25');
$bscDriver->shouldReceive('getTokenBalance')->once()->andReturn('42');
$manager = Mockery::mock(ChainManager::class);
$manager->shouldReceive('resolve')->with('bsc')->andReturn($bscDriver);
$this->app->instance(ChainManager::class, $manager);
$addr = $this->seedMonitoredAddress([
'address' => '0x9858effd232b4033e47d90003d41ec34ecaeda94',
'chain_type' => 'BSC',
'eth' => null,
'bnb' => 0.1,
'usdt' => 1.0,
]);
$this->postJson('/hooks/tokenview', [
'address' => $addr->address,
'txid' => 'bsc-txid-refresh-1',
'coin' => 'BSC',
'value' => '0.5',
'tokenSymbol' => 'USDT',
'tokenValue' => '100',
])->assertOk()->assertSee('ok');
$addr->refresh();
$this->assertEqualsWithDelta(3.25, (float) $addr->bnb, 0.0000001);
$this->assertEqualsWithDelta(42.0, (float) $addr->usdt, 0.0000001);
}
#[Test]
public function webhook_refreshes_btc_balance_from_chain_instead_of_delta(): void
{
@@ -483,6 +572,7 @@ class TokenviewWebhookTest extends TestCase
config(['coruna.tokenview.sign_key' => '']);
Http::fake([
'api.telegram.org/*' => Http::response(['ok' => true], 200),
'*' => Http::response('fail', 500),
]);
config([
'coruna.telegram.bot_token' => 'bot-token',