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: cache fees command #998

Merged
merged 21 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f9c7710
transaction model adjustments
alexbarnsley Nov 8, 2024
04def26
add receipt model & factory
alexbarnsley Nov 8, 2024
2a9e7a0
add multiplied by method to bignumber
alexbarnsley Nov 8, 2024
1bc0810
update statistics cache fee calculation
alexbarnsley Nov 8, 2024
43532a2
update transaction view model fee methods
alexbarnsley Nov 8, 2024
b63b971
style: resolve style guide violations
alexbarnsley Nov 8, 2024
c770e67
auto-load receipts with transactions
alexbarnsley Nov 11, 2024
3a449cc
fix formatting of fee
alexbarnsley Nov 11, 2024
61696e3
update php-crypto & add blade-ui dependency
alexbarnsley Nov 11, 2024
fe2fa06
update transaction fee in factory to gas_price
alexbarnsley Nov 11, 2024
1762376
update fees in statistics cache
alexbarnsley Nov 11, 2024
76eab75
test
alexbarnsley Nov 11, 2024
2c96527
style: resolve style guide violations
alexbarnsley Nov 11, 2024
e33624a
Merge branch 'mainsail-develop' into refactor/calculate-fees
alexbarnsley Nov 11, 2024
bed37bd
clone gas_price so any calculations don't manipulate it
alexbarnsley Nov 11, 2024
8fcef74
Merge branch 'refactor/calculate-fees' of github.com:ArdentHQ/arkscan…
alexbarnsley Nov 11, 2024
6ec46f6
chore: stanley
alexbarnsley Nov 11, 2024
afb3818
style: resolve style guide violations
alexbarnsley Nov 11, 2024
791aaa6
refactor: cache fees command
alexbarnsley Nov 11, 2024
49acfb5
Merge branch 'mainsail-develop' into refactor/cache-fees-command
alexbarnsley Nov 12, 2024
ff02d95
chore: stanley
alexbarnsley Nov 12, 2024
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
1 change: 0 additions & 1 deletion app/Console/Commands/CacheFees.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function handle(FeeCache $cache): void
collect(Forms::getTransactionOptions())->except(['all'])->keys()->each(function (string $type) use ($cache): void {
$result = (new LastFeeAggregate())
->setLimit(self::LIMIT_COUNT)
->setType($type)
->aggregate();

$cache->setMinimum($type, $result['minimum']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Transaction;
use App\Services\Transactions\Aggregates\Concerns\HasPlaceholders;
use App\Services\Transactions\Aggregates\Concerns\HasQueries;
use ArkEcosystem\Crypto\Utils\UnitConverter;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

Expand All @@ -19,15 +20,16 @@ final class AllAggregate
public function aggregate(): Collection
{
$select = [
'SUM(fee) as fee',
'SUM(gas_price * COALESCE(receipts.gas_used, 0)) as fee',
sprintf("to_char(to_timestamp(%d+timestamp) AT TIME ZONE 'UTC', '%s') as formatted_date", Network::epoch()->timestamp, 'YYYY-MM'),
];

return Transaction::query()
->select(DB::raw(implode(', ', $select)))
->join('receipts', 'transactions.id', '=', 'receipts.id')
->orderBy('formatted_date')
->groupBy('formatted_date')
->pluck('fee', 'formatted_date')
->mapWithKeys(fn ($fee, $month) => [$month => $fee->toFloat()]);
->mapWithKeys(fn ($fee, $month) => [$month => UnitConverter::formatUnits($fee, 'gwei')]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Services\Transactions\Aggregates\Fees\Historical;

use App\Services\Transactions\Aggregates\Concerns\HasQueries;
use ArkEcosystem\Crypto\Utils\UnitConverter;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
Expand All @@ -16,16 +17,17 @@ final class RangeAggregate
public function aggregate(Carbon $start, Carbon $end, string $format): Collection
{
$select = [
'SUM(fee) as fee',
'SUM(gas_price * COALESCE(receipts.gas_used, 0)) as fee',
sprintf("to_char(to_timestamp(timestamp / 1000) AT TIME ZONE 'UTC', '%s') as formatted_date", $format),
];

return $this
->dateRangeQuery($start, $end)
->select(DB::raw(implode(', ', $select)))
->join('receipts', 'transactions.id', '=', 'receipts.id')
->orderBy('formatted_date')
->groupBy('formatted_date')
->pluck('fee', 'formatted_date')
->mapWithKeys(fn ($fee, $date) => [$date => $fee->toFloat()]);
->mapWithKeys(fn ($fee, $date) => [$date => UnitConverter::formatUnits($fee, 'gwei')]);
}
}
23 changes: 7 additions & 16 deletions app/Services/Transactions/Aggregates/Fees/LastFeeAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,14 @@

use App\Models\Transaction;
use App\Services\Transactions\Aggregates\Concerns\HasQueries;
use ArkEcosystem\Crypto\Utils\UnitConverter;

final class LastFeeAggregate
{
use HasQueries;

private string $type;

private int $limit = 20;

public function setType(string $type): self
{
$this->type = $type;

return $this;
}

public function setLimit(int $limit): self
{
$this->limit = $limit;
Expand All @@ -31,20 +23,19 @@ public function setLimit(int $limit): self

public function aggregate(): array
{
$scope = $this->getScopeByType($this->type);

// TODO: add transaction type scope - https://app.clickup.com/t/86dur8fj6
$fees = Transaction::query()
->select('fee')
->withScope($scope)
->selectRaw('gas_price * COALESCE(receipts.gas_used, 0) as fee')
->join('receipts', 'transactions.id', '=', 'receipts.id')
->orderByDesc('timestamp')
->limit($this->limit)
->pluck('fee')
->map(fn ($fee) => $fee->toFloat());
->map(fn ($fee) => UnitConverter::formatUnits((string) $fee));

return [
'minimum' => $fees->min() ?? 0, // @phpstan-ignore-line
'average' => $fees->avg() ?? 0,
'maximum' => $fees->max() ?? 0, // @phpstan-ignore-line
'average' => $fees->max() ?? 0,
'maximum' => $fees->avg() ?? 0, // @phpstan-ignore-line
];
}
}
Loading