-
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
53 changed files
with
2,425 additions
and
576 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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Jobs\FetchCollectionActivity as Job; | ||
use App\Models\Collection; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
class FetchCollectionActivity extends Command | ||
{ | ||
use InteractsWithCollections; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'collections:fetch-activity {--collection-id=}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Fetch collection activity for all collections'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): int | ||
{ | ||
if (! config('dashbrd.features.activities')) { | ||
return Command::SUCCESS; | ||
} | ||
|
||
// Modify the query to only fetch activities for collections that we index NFTs for... | ||
$queryCallback = function (Builder $query) { | ||
/** @var Builder<Collection> */ | ||
return $query->where('is_fetching_activity', false) | ||
->whereNotNull('supply') | ||
->where('supply', '<=', config('dashbrd.collections_max_cap')); | ||
}; | ||
|
||
$this->forEachCollection(function ($collection) { | ||
Job::dispatch($collection, forced: true); | ||
}, $queryCallback); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Enums\Chains; | ||
use App\Enums\TokenGuid; | ||
use App\Models\Network; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class SyncActivityPrices extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'activities:sync-prices'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Command to update NFT activity records of a collection with calculated values'; | ||
|
||
/** | ||
* Command to update NFT activity records of a collection with calculated values | ||
* | ||
* This command updates the 'nft_activity' table by calculating and setting the 'total_native' and 'total_usd' columns based on the | ||
* values stored in the 'extra_attributes' column from the 'token_price_history' table. The | ||
* command performs the following actions: | ||
* | ||
* 1. For each record in 'nft_activity': | ||
* - Extract the 'totalNative' value from the 'extra_attributes' JSON column and set it as 'total_native'. | ||
* - Calculate 'total_usd' by multiplying 'total_native' with the 'ethereum' token price from 'token_price_history' | ||
* for the time that activity happened. | ||
* | ||
* 2. Update is limited to records in 'nft_activity' associated with collections having a 'network_id' of 1 (Polygon in our app). | ||
* | ||
* 3. The command uses a SQL transaction to ensure data consistency. It commits the changes after successful execution. | ||
* | ||
* Caution: Before running this command, make sure to run `tokens:live-dump-price-history`. | ||
*/ | ||
public function handle(): int | ||
{ | ||
try { | ||
DB::beginTransaction(); | ||
$this->info('Updating NFT activity table...'); | ||
|
||
$network = Network::firstWhere('chain_id', Chains::Polygon); | ||
$ethereumGuid = TokenGuid::Ethereum->value; | ||
$polygonGuid = TokenGuid::Polygon->value; | ||
|
||
$updateSql = " | ||
UPDATE nft_activity | ||
SET | ||
total_usd = (extra_attributes->'recipientPaid'->>'totalNative')::numeric * | ||
( | ||
SELECT price | ||
FROM token_price_history | ||
WHERE | ||
token_guid = '{$polygonGuid}' | ||
AND currency = 'usd' | ||
AND timestamp <= nft_activity.timestamp | ||
ORDER BY timestamp DESC | ||
LIMIT 1 | ||
), | ||
total_native = ( | ||
SELECT ( | ||
(extra_attributes->'recipientPaid'->>'totalNative')::numeric * | ||
( | ||
SELECT price | ||
FROM token_price_history | ||
WHERE | ||
token_guid = '{$polygonGuid}' | ||
AND currency = 'usd' | ||
AND timestamp <= nft_activity.timestamp | ||
ORDER BY timestamp DESC | ||
LIMIT 1 | ||
) | ||
) / ( | ||
SELECT price | ||
FROM token_price_history | ||
WHERE | ||
token_guid = '{$ethereumGuid}' | ||
AND timestamp <= nft_activity.timestamp | ||
AND currency = 'usd' | ||
ORDER BY timestamp DESC | ||
LIMIT 1 | ||
) | ||
) | ||
WHERE | ||
nft_activity.collection_id IN ( | ||
SELECT collections.id | ||
FROM collections | ||
WHERE collections.network_id = '$network->id' | ||
) | ||
AND nft_activity.type = 'LABEL_SALE'; | ||
"; | ||
|
||
DB::statement($updateSql); | ||
|
||
DB::commit(); | ||
|
||
$this->info('NFT activity table updated successfully.'); | ||
|
||
return Command::SUCCESS; | ||
} catch (\Exception $e) { | ||
DB::rollBack(); | ||
$this->error('An error occurred while updating the NFT activity table: '.$e->getMessage()); | ||
|
||
return Command::FAILURE; | ||
} | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Enums; | ||
|
||
use Spatie\TypeScriptTransformer\Attributes\TypeScript; | ||
|
||
#[TypeScript] | ||
enum TokenGuid: string | ||
{ | ||
case Ethereum = 'ethereum'; | ||
case Polygon = 'matic-network'; | ||
} |
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.