fix: balance
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Device;
|
||||
use App\Models\WalletAddress;
|
||||
use App\Services\WalletBalanceService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class WalletBalanceServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
#[Test]
|
||||
public function refreshes_tron_trx_and_usdt_only(): void
|
||||
{
|
||||
Http::fake([
|
||||
'*/wallet/getaccount' => Http::response(['balance' => 1_000_000], 200),
|
||||
'*/wallet/triggerconstantcontract' => Http::response([
|
||||
'constant_result' => [str_pad(dechex(3_000_000), 64, '0', STR_PAD_LEFT)],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$device = Device::query()->create([
|
||||
'device_id' => 'dev-bal-1',
|
||||
'channel_id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
|
||||
]);
|
||||
$addr = WalletAddress::query()->create([
|
||||
'device_id' => $device->id,
|
||||
'address' => 'TKKyetwdwuv6fTWVMPsdQUZYwB7yiNwRp6',
|
||||
'chain_type' => 'TRON',
|
||||
'source' => 'imToken',
|
||||
'btc' => 1.23,
|
||||
'eth' => 4.56,
|
||||
'monitor' => 1,
|
||||
]);
|
||||
|
||||
$ok = app(WalletBalanceService::class)->refresh($addr);
|
||||
$this->assertTrue($ok);
|
||||
|
||||
$addr->refresh();
|
||||
$this->assertEqualsWithDelta(1.0, (float) $addr->trx, 0.0000001);
|
||||
$this->assertEqualsWithDelta(3.0, (float) $addr->usdt, 0.0000001);
|
||||
// Non-Tron columns are not overwritten.
|
||||
$this->assertEqualsWithDelta(1.23, (float) $addr->btc, 0.0000001);
|
||||
$this->assertEqualsWithDelta(4.56, (float) $addr->eth, 0.0000001);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function skips_non_tron_chains(): void
|
||||
{
|
||||
$device = Device::query()->create([
|
||||
'device_id' => 'dev-bal-2',
|
||||
'channel_id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
|
||||
]);
|
||||
$addr = WalletAddress::query()->create([
|
||||
'device_id' => $device->id,
|
||||
'address' => '0xab5c66752a9e8167967685f1450532fb96d5d24f',
|
||||
'chain_type' => 'ETH',
|
||||
'source' => 'MetaMask',
|
||||
'eth' => 1.0,
|
||||
'monitor' => 1,
|
||||
]);
|
||||
|
||||
$ok = app(WalletBalanceService::class)->refresh($addr);
|
||||
$this->assertFalse($ok);
|
||||
$addr->refresh();
|
||||
$this->assertEqualsWithDelta(1.0, (float) $addr->eth, 0.0000001);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user