-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: store collection volume changes (#576)
- Loading branch information
Showing
21 changed files
with
691 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Enums\Period; | ||
use App\Jobs\FetchAverageCollectionVolume as FetchAverageCollectionVolumeJob; | ||
use Exception; | ||
use Illuminate\Console\Command; | ||
|
||
class FetchAverageCollectionVolume extends Command | ||
{ | ||
use InteractsWithCollections; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'collections:fetch-average-volumes {--collection-id=} {--period=}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Fetch the initial average volume stats for collections'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): int | ||
{ | ||
$period = $this->option('period'); | ||
|
||
if ($period !== null && ! in_array($period, ['1d', '7d', '30d'])) { | ||
$this->error('Invalid period value. Supported: 1d, 7d, 30d'); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
$this->forEachCollection(function ($collection) { | ||
collect($this->periods())->each(fn ($period) => FetchAverageCollectionVolumeJob::dispatch($collection, $period)); | ||
}); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
/** | ||
* @return Period[] | ||
*/ | ||
private function periods(): array | ||
{ | ||
if ($this->option('period') === null) { | ||
return [ | ||
Period::DAY, | ||
Period::WEEK, | ||
Period::MONTH, | ||
]; | ||
} | ||
|
||
return [match ($this->option('period')) { | ||
'1d' => Period::DAY, | ||
'7d' => Period::WEEK, | ||
'30d' => Period::MONTH, | ||
default => throw new Exception('Unsupported period value'), | ||
}]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Enums\Period; | ||
use App\Jobs\Traits\RecoversFromProviderErrors; | ||
use App\Models\Collection; | ||
use App\Support\Facades\Mnemonic; | ||
use App\Support\Queues; | ||
use DateTime; | ||
use Exception; | ||
use Illuminate\Bus\Batchable; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class FetchAverageCollectionVolume implements ShouldQueue | ||
{ | ||
use Batchable, Dispatchable, InteractsWithQueue, Queueable, RecoversFromProviderErrors, SerializesModels; | ||
|
||
/** | ||
* Create a new job instance. | ||
*/ | ||
public function __construct( | ||
public Collection $collection, | ||
public Period $period, | ||
) { | ||
$this->onQueue(Queues::SCHEDULED_COLLECTIONS); | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
$volume = Mnemonic::getAverageCollectionVolume( | ||
chain: $this->collection->network->chain(), | ||
contractAddress: $this->collection->address, | ||
period: $this->period, | ||
); | ||
|
||
$this->collection->update([ | ||
$this->field() => $volume ?? 0, | ||
]); | ||
|
||
ResetCollectionRanking::dispatch(); | ||
} | ||
|
||
private function field(): string | ||
{ | ||
return match ($this->period) { | ||
Period::DAY => 'avg_volume_1d', | ||
Period::WEEK => 'avg_volume_7d', | ||
Period::MONTH => 'avg_volume_30d', | ||
default => throw new Exception('Unsupported period value'), | ||
}; | ||
} | ||
|
||
public function uniqueId(): string | ||
{ | ||
return static::class.':'.$this->collection->id; | ||
} | ||
|
||
public function retryUntil(): DateTime | ||
{ | ||
return now()->addMinutes(10); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
class TradingVolume extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $guarded = []; | ||
|
||
/** | ||
* @return BelongsTo<Collection, TradingVolume> | ||
*/ | ||
public function collection(): BelongsTo | ||
{ | ||
return $this->belongsTo(Collection::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.