Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: username ui improvements #1179

Merged
merged 6 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/Console/Commands/CacheKnownWallets.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class CacheKnownWallets extends Command
*
* @var string
*/
protected $signature = 'explorer:cache-known-wallets';
protected $signature = 'explorer:cache-known-wallets {--force}';

/**
* The console command description.
Expand All @@ -34,7 +34,10 @@ public function handle(): void
{
$cache = app(WalletCache::class);

$cache->setKnown(fn () => Http::get(Network::knownWalletsUrl())->json());
$cache->setKnown(
fn () => Http::get(Network::knownWalletsUrl())->json(),
$this->option('force'),
);

$knownWallets = collect(Network::knownWallets());

Expand Down
4 changes: 0 additions & 4 deletions app/Services/Cache/Concerns/ManagesCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ private function put(string $key, $value, $ttl = null)
*/
private function forget(string $key)
{
// @codeCoverageIgnoreStart
// This method is currently not used in the application, keeping it for
// completeness and potential future use.
return $this->getCache()->forget(md5($key));
// @codeCoverageIgnoreEnd
}

private function blockTimeTTL(): int
Expand Down
6 changes: 5 additions & 1 deletion app/Services/Cache/WalletCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ public function getKnown(): array
return $this->get('known', []);
}

public function setKnown(Closure $callback): array
public function setKnown(Closure $callback, bool $force = false): array
{
if ($force) {
$this->forget('known');
}

return $this->remember('known', now()->addDay(), $callback);
}

Expand Down
10 changes: 0 additions & 10 deletions app/ViewModels/ForgingStatsViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ public function validator(): ?WalletViewModel
return new WalletViewModel($this->forgingStats->validator);
}

public function address(): ?string
{
return $this->validator()?->address();
}

public function hasUsername(): bool
{
return $this->validator()?->hasUsername() !== null;
}

public function username(): ?string
{
return $this->validator()?->username();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class="font-semibold"
<x-ark-tables.cell>
@if ($validator)
<x-tables.rows.desktop.encapsulated.address
:model="$block"
:model="$validator"
without-clipboard
/>
@else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
:transaction="$transaction"
>
<x-transaction.page.section-detail.address
:address="$transaction->sender()->address()"
:wallet="$transaction->sender()"
class="inline-block"
/>
</x-transaction.page.section-detail.row>
Expand All @@ -20,7 +20,7 @@ class="inline-block"
value-class="min-w-0"
>
<x-transaction.page.section-detail.address
:address="$transaction->recipient()->address()"
:wallet="$transaction->recipient()"
:is-contract="$transaction->recipient()->isContract()"
class="inline-block"
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
@props([
'address',
'wallet' => null,
'address' => null,
'isContract' => false,
])

@if ($wallet && ! $address)
@php ($address = $wallet->address())
@endif

<div class="flex justify-end items-center sm:justify-start">
<a
href="{{ route('wallet', $address) }}"
class="min-w-0 link"
>
<div class="hidden md:inline">
<x-truncate-dynamic>{{ $address }}</x-truncate-dynamic>
@if ($wallet && $wallet->hasUsername())
{{ $wallet->username() }}
@else
<x-truncate-dynamic>{{ $address }}</x-truncate-dynamic>
@endif
</div>

<div class="md:hidden">
@unless ($isContract)
@if ($wallet && $wallet->hasUsername())
{{ $wallet->username() }}
@elseif (! $isContract)
<x-truncate-middle>
{{ $address }}
</x-truncate-middle>
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/Http/Livewire/Validators/RecentVotesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

declare(strict_types=1);

use App\Console\Commands\CacheKnownWallets;
use App\Enums\SortDirection;
use App\Http\Livewire\Validators\RecentVotes;
use App\Models\Receipt;
use App\Models\Transaction;
use App\Models\Wallet;
use App\ViewModels\WalletViewModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
use Illuminate\View\Compilers\BladeCompiler;
Expand Down Expand Up @@ -679,7 +679,7 @@ function generateTransactions(): array
'timestamp' => Carbon::parse('2023-09-18 07:41:06')->getTimestampMs(),
]);

(new CacheKnownWallets())->handle();
Artisan::call('explorer:cache-known-wallets');

generateReceipts();

Expand Down Expand Up @@ -771,7 +771,7 @@ function generateTransactions(): array
'timestamp' => Carbon::parse('2023-09-18 07:41:06')->getTimestampMs(),
]);

(new CacheKnownWallets())->handle();
Artisan::call('explorer:cache-known-wallets');

generateReceipts();

Expand Down
45 changes: 30 additions & 15 deletions tests/Unit/Console/Commands/CacheKnownWalletsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

declare(strict_types=1);

use App\Console\Commands\CacheKnownWallets;
use App\Models\Transaction;
use App\Models\Wallet;
use App\Services\Cache\WalletCache;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;

it('should cache the known wallet name if defined', function () {
$knownWalletsUrl = 'https://knownwallets.com/known-wallets.json';

Config::set('arkscan.networks.development.knownWallets', $knownWalletsUrl);
Config::set('arkscan.networks.development.knownWallets', 'https://knownwallets.com/known-wallets.json');

Http::fake(Http::response([
[
Expand All @@ -31,16 +29,14 @@
'attributes->username' => 'regular',
]);

(new CacheKnownWallets())->handle();
Artisan::call('explorer:cache-known-wallets');

expect(Cache::tags('wallet')->get(md5("name_by_address/$knownWallet->address")))->toBe('Hot Wallet');
expect(Cache::tags('wallet')->get(md5("name_by_address/$regularWallet->address")))->toBe('regular');
});

it('should cache the known wallet name if doesnt have validator name', function () {
$knownWalletsUrl = 'https://knownwallets.com/known-wallets.json';

Config::set('arkscan.networks.development.knownWallets', $knownWalletsUrl);
Config::set('arkscan.networks.development.knownWallets', 'https://knownwallets.com/known-wallets.json');

Http::fake(Http::response([
[
Expand All @@ -55,15 +51,13 @@
'attributes->username' => null,
]);

(new CacheKnownWallets())->handle();
Artisan::call('explorer:cache-known-wallets');

expect(Cache::tags('wallet')->get(md5("name_by_address/$wallet->address")))->toBe('Hot Wallet');
});

it('should forget wallets with resigned usernames', function () {
$knownWalletsUrl = 'https://knownwallets.com/known-wallets.json';

Config::set('arkscan.networks.development.knownWallets', $knownWalletsUrl);
Config::set('arkscan.networks.development.knownWallets', 'https://knownwallets.com/known-wallets.json');

Http::fake(Http::response([
[],
Expand All @@ -78,7 +72,7 @@
],
]);

(new CacheKnownWallets())->handle();
Artisan::call('explorer:cache-known-wallets');

expect($cache->getWalletNameByAddress($wallet->address))->toBe('joeblogs');

Expand All @@ -88,15 +82,36 @@
],
])->save();

(new CacheKnownWallets())->handle();
Artisan::call('explorer:cache-known-wallets');

expect($cache->getWalletNameByAddress($wallet->address))->toBe('joeblogs');

Transaction::factory()->usernameResignation()->create([
'sender_address' => $wallet->address,
]);

(new CacheKnownWallets())->handle();
Artisan::call('explorer:cache-known-wallets');

expect($cache->getWalletNameByAddress($wallet->address))->toBeNull();
});

it('should force update of known wallets', function () {
Config::set('arkscan.networks.development.knownWallets', 'https://knownwallets.com/known-wallets.json');

$cache = new WalletCache();
$cache->setKnown(fn () => [
'0xC5a19e23E99bdFb7aae4301A009763AdC01c1b5B',
]);

Http::fake(Http::response([], 200));

Artisan::call('explorer:cache-known-wallets');

expect($cache->getKnown())->toBe([
'0xC5a19e23E99bdFb7aae4301A009763AdC01c1b5B',
]);

Artisan::call('explorer:cache-known-wallets', ['--force' => true]);

expect($cache->getKnown())->toBe([]);
});