-
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.
- Loading branch information
Showing
331 changed files
with
7,321 additions
and
5,020 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
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 |
---|---|---|
|
@@ -6,6 +6,8 @@ on: | |
- "**.css" | ||
- "**.js" | ||
- "**.ts" | ||
- "**.yaml" | ||
- "**.json" | ||
- "**.php" | ||
- "**.svg" | ||
- "**.tsx" | ||
|
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,108 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Jobs\FetchCollectionFloorPrice; | ||
use App\Jobs\FetchCollectionOpenseaSlug; | ||
use Carbon\Carbon; | ||
use Closure; | ||
use Illuminate\Support\Facades\Config; | ||
|
||
trait DependsOnOpenseaRateLimit | ||
{ | ||
/** | ||
* @var array<string> | ||
*/ | ||
private array $jobsThatMayUseOpensea = [ | ||
FetchCollectionFloorPrice::class, | ||
]; | ||
|
||
/** | ||
* @var array<string> | ||
*/ | ||
private array $jobsThatUseOpensea = [ | ||
FetchCollectionOpenseaSlug::class, | ||
]; | ||
|
||
/** | ||
* Used to delay jobs by a certain amount of seconds to prevent overlapping | ||
* | ||
* @var array<string, int> | ||
*/ | ||
private array $jobsDelayThreshold = [ | ||
FetchCollectionFloorPrice::class => 0, | ||
FetchCollectionOpenseaSlug::class => 1, | ||
]; | ||
|
||
private function getDispatchAt(string $job, int $index): Carbon | ||
{ | ||
$maxRequests = config('services.opensea.rate.max_requests'); | ||
|
||
$perSeconds = config('services.opensea.rate.per_seconds'); | ||
|
||
$delayInSeconds = (int) floor($index / $maxRequests) * $perSeconds * $this->getRateLimitFactor(); | ||
|
||
return Carbon::now() | ||
// Round to the next minute so the delay is always calculated from | ||
// the beginning of the minute which prevents overlapping considering | ||
// jobs may be dispatched at different times | ||
// e.g. 12:00:34 -> 12:01:00, 12:00:59 -> 12:01:00 | ||
->addMinute()->startOfMinute() | ||
// Delay the job by the amount of seconds + the delay treshold | ||
// e.g. 12:01:00 + 1 -> 12:01:01, 12:01:00 + 2 -> 12:01:02 | ||
->addSeconds($delayInSeconds + $this->getDelayTreshold($job)); | ||
} | ||
|
||
private function getDelayTreshold(string $job): int | ||
{ | ||
return $this->jobsDelayThreshold[$job] ?? 0; | ||
} | ||
|
||
private function getLimitPerMinutes(int $minutes): int | ||
{ | ||
$maxRequests = config('services.opensea.rate.max_requests'); | ||
|
||
$perSeconds = config('services.opensea.rate.per_seconds'); | ||
|
||
$requestsPerHour = $maxRequests * $minutes * 60 / $perSeconds; | ||
|
||
// limit to the requests per hour to leave some room for other tasks | ||
return (int) floor($requestsPerHour / $this->getRateLimitFactor()); | ||
} | ||
|
||
/** | ||
* This value is used to ensure we only run as many jobs as we can with opensea, | ||
* it depends of the number of jobs that use opensea consider the rare but | ||
* possible case where jobs are running at the same time. | ||
*/ | ||
private function getRateLimitFactor(): int | ||
{ | ||
$totalJobsThatUseOpensea | ||
= collect($this->jobsThatMayUseOpensea)->filter(fn ($job) => $this->usesOpensea($job))->count() | ||
+ count($this->jobsThatUseOpensea); | ||
|
||
return $totalJobsThatUseOpensea; | ||
} | ||
|
||
private function usesOpensea(string $job): bool | ||
{ | ||
return Config::get('dashbrd.web3_providers.'.$job) === 'opensea' || in_array($job, $this->jobsThatUseOpensea); | ||
} | ||
|
||
private function dispatchDelayed(Closure $callback, int $index, string $job): void | ||
{ | ||
if (! $this->usesOpensea($job)) { | ||
|
||
$callback(); | ||
|
||
return; | ||
} | ||
|
||
// Note: I cant use delay directly on the job because it throws | ||
// the "too many attempts" error after some time so im delaying | ||
// with a queued closure instead | ||
dispatch($callback)->delay($this->getDispatchAt($job, $index)); | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class PruneMetaImages extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'prune-meta-images'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Removes unused meta images'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): int | ||
{ | ||
$validImages = array_map(fn ($item) => $item->image_name, DB::select(get_query('gallery.get_meta_images'))); | ||
// Convert to associative array so we can use `isset` | ||
$validImages = array_combine($validImages, $validImages); | ||
|
||
$directory = storage_path(sprintf('meta/galleries/')); | ||
|
||
if (is_dir($directory)) { | ||
// Open directory | ||
if ($handle = opendir($directory)) { | ||
// Loop through each file in the directory | ||
while (false !== ($file = readdir($handle))) { | ||
// Check if file is a PNG and not in validImages | ||
if (pathinfo($file, PATHINFO_EXTENSION) === 'png' && ! isset($validImages[$file])) { | ||
// Delete the file | ||
unlink($directory.$file); | ||
} | ||
} | ||
|
||
// Close directory handle | ||
closedir($handle); | ||
} | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Jobs\UpdateNftDescription; | ||
use App\Models\Network; | ||
use Illuminate\Console\Command; | ||
|
||
class UpdateDescriptions extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'nfts:update-descriptions {--start=} {--network=}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Dispatch a job to update descriptions for NFTs'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): int | ||
{ | ||
$network = Network::find((int) $this->option('network')); | ||
|
||
if (! $network) { | ||
$this->warn('Network does not exist.'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
UpdateNftDescription::dispatch((int) ($this->option('start') ?? 1), $network); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
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.