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: recipient contract badge #1012

Merged
merged 11 commits into from
Nov 21, 2024
36 changes: 36 additions & 0 deletions app/Console/Commands/CacheContractAddresses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Models\Receipt;
use App\Services\Cache\WalletCache;
use Illuminate\Console\Command;

class CacheContractAddresses extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'explorer:cache-contract-addresses';

/**
* The console command description.
*
* @var string|null
*/
protected $description = 'Cache contract addresses';

public function handle(): void
{
$addresses = Receipt::where('deployed_contract_address', '!=', null)
->distinct()
->pluck('deployed_contract_address')
->toArray();

(new WalletCache())->setContractAddresses($addresses);
}
}
3 changes: 3 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Console\Commands\BuildForgingStats;
use App\Console\Commands\CacheAddressStatistics;
use App\Console\Commands\CacheAnnualStatistics;
use App\Console\Commands\CacheContractAddresses;
use App\Console\Commands\CacheCurrenciesData;
use App\Console\Commands\CacheFees;
use App\Console\Commands\CacheGasTrackerData;
Expand Down Expand Up @@ -78,6 +79,8 @@ protected function schedule(Schedule $schedule)

$schedule->command(BuildForgingStats::class)->everyMinute();

$schedule->command(CacheContractAddresses::class)->everyMinute();

// TODO: enable when the monitor is fixed
// $schedule->command(CacheValidatorPerformance::class)->everyMinute();

Expand Down
9 changes: 9 additions & 0 deletions app/DTO/MemoryWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public function address(): ?string
return $this->address;
}

public function isContract(): bool
{
if (in_array($this->address(), config('contracts.addresses'), true)) {
return true;
}

return in_array($this->address(), (new WalletCache())->getContractAddresses(), true);
}

public function publicKey(): ?string
{
return $this->publicKey;
Expand Down
10 changes: 10 additions & 0 deletions app/Services/Cache/WalletCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ public function setMissedBlocks(string $address, int $value): void
$this->put(sprintf('missed_blocks/%s', $address), $value);
}

public function getContractAddresses(): array
{
return $this->get('contract_addresses', []);
}

public function setContractAddresses(array $addresses): void
{
$this->put('contract_addresses', $addresses);
}

public function getCache(): TaggedCache
{
return Cache::tags('wallet');
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions resources/css/_general.css
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,8 @@ input.qr-code-amount[type="number"] {
.code-block-custom-scroll::-webkit-scrollbar-thumb {
@apply rounded-lg bg-theme-primary-200;
}

/** Tooltips */
.tippy-box {
@apply break-words;
}
1 change: 1 addition & 0 deletions resources/lang/en/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'filter' => 'Filter',
'now' => 'Now',
'vote_with' => 'Vote With',
'contract' => 'Contract',

'arkconnect' => [
'mainnet_network' => 'You\'re viewing data from the main network, but your wallet is connected to test network (ARK Testnet). To use ARK Scan, please switch to <a class="link font-semibold" href="https://test.arkscan.io/">test.arkscan.io</a>.',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@props(['transaction'])

<x-general.page-section.container :title="trans('pages.transaction.addressing')">
<x-general.page-section.container
:title="trans('pages.transaction.addressing')"
wrapper-class="flex flex-col flex-1 space-y-3 w-full whitespace-nowrap"
>
<x-transaction.page.section-detail.row
:title="trans('pages.transaction.header.from')"
:transaction="$transaction"
Expand All @@ -14,9 +17,11 @@ class="inline-block"
<x-transaction.page.section-detail.row
:title="trans('pages.transaction.header.to')"
:transaction="$transaction"
value-class="min-w-0"
>
<x-transaction.page.section-detail.address
:address="$transaction->recipient()->address()"
:is-contract="$transaction->recipient()->isContract()"
class="inline-block"
/>
</x-transaction.page.section-detail.row>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
@props([
'address',
'isContract' => false,
])

<span class="inline-flex items-center">
<div class="flex items-center">
<a
href="{{ route('wallet', $address) }}"
class="link"
class="min-w-0 link"
>
<div class="hidden md:inline">
{{ $address }}
<x-truncate-dynamic>{{ $address }}</x-truncate-dynamic>
</div>

<div class="md:hidden">
<x-truncate-middle>
{{ $address }}
</x-truncate-middle>
@unless ($isContract)
<x-truncate-middle>
{{ $address }}
</x-truncate-middle>
@else
@lang('general.contract')
@endif
</div>
</a>

@if ($isContract)
<div class="ml-3 w-5 h-5 md:hidden ark-info-element">
<x-ark-info
:tooltip="$address"
type="info"
/>
</div>
@endif

<x-clipboard
:value="$address"
:tooltip="trans('pages.wallet.address_copied')"
/>
</span>

@if ($isContract)
<div class="hidden items-center md:flex">
<div class="mx-2 border-l border-theme-secondary-300 h-[17px] dark:border-theme-dark-700"></div>

<div class="flex items-center px-1 space-x-1.5 text-xs rounded h-[21px] bg-theme-secondary-200 leading-3.75 text-theme-secondary-700 dark:bg-theme-dark-950 dark:text-theme-dark-200">
<x-ark-icon name="transaction.contract" size="xs" />

<span>@lang('general.contract')</span>
</div>
</div>
@endif
</div>
31 changes: 31 additions & 0 deletions tests/Unit/Console/Commands/CacheContractAddressesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

use App\Console\Commands\CacheContractAddresses;
use App\Models\Receipt;
use App\Services\Cache\WalletCache;

it('should execute the command', function () {
(new CacheContractAddresses())->handle();

$cache = new WalletCache();

expect($cache->getContractAddresses())->toEqual([]);
});

it('should cache contract addresses', function () {
$cache = new WalletCache();

expect($cache->getContractAddresses())->toEqual([]);

(new CacheContractAddresses())->handle();

Receipt::factory()->create([
'deployed_contract_address' => '0x522B3294E6d06aA25Ad0f1B8891242E335D3B459',
]);

(new CacheContractAddresses())->handle();

expect($cache->getContractAddresses())->toEqual(['0x522B3294E6d06aA25Ad0f1B8891242E335D3B459']);
});
Loading