Skip to content

Commit

Permalink
chore: merge rc into main (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsANameToo authored Oct 24, 2023
2 parents edd3376 + ec2d695 commit aad0367
Show file tree
Hide file tree
Showing 53 changed files with 2,425 additions and 576 deletions.
53 changes: 53 additions & 0 deletions app/Console/Commands/FetchCollectionActivity.php
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;
}
}
67 changes: 0 additions & 67 deletions app/Console/Commands/FetchNftActivity.php

This file was deleted.

118 changes: 118 additions & 0 deletions app/Console/Commands/SyncActivityPrices.php
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;
}
}
}
6 changes: 6 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Console;

use App\Console\Commands\FetchCoingeckoTokens;
use App\Console\Commands\FetchCollectionActivity;
use App\Console\Commands\FetchCollectionBannerBatch;
use App\Console\Commands\FetchCollectionFloorPrice;
use App\Console\Commands\FetchCollectionMetadata;
Expand Down Expand Up @@ -105,6 +106,11 @@ private function scheduleJobsForCollectionsOrGalleries(Schedule $schedule): void
->withoutOverlapping()
->hourly();

$schedule
->command(FetchCollectionActivity::class)
->withoutOverlapping()
->weeklyOn(Schedule::MONDAY);

$schedule
->command(FetchCollectionFloorPrice::class)
->withoutOverlapping()
Expand Down
2 changes: 1 addition & 1 deletion app/Data/Gallery/GalleryNftData.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function fromModel(Nft $nft, bool $ownedByCurrentUser = false): se
floorPrice: $collection->floor_price,
floorPriceCurrency: $collection->floorPriceToken?->symbol,
floorPriceDecimals: $collection->floorPriceToken?->decimals,
lastActivityFetchedAt: $nft->last_activity_fetched_at,
lastActivityFetchedAt: $collection->activity_updated_at,
lastViewedAt: $nft->last_viewed_at,
ownedByCurrentUser: $ownedByCurrentUser,
);
Expand Down
2 changes: 1 addition & 1 deletion app/Data/Nfts/NftData.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function fromModel(Nft $nft): self
images: NftImagesData::from($nft->images()),
wallet: $nft->wallet_id ? NftWalletData::fromModel($nft->wallet) : null,
lastViewedAt: $nft->last_viewed_at,
lastActivityFetchedAt: $nft->last_activity_fetched_at,
lastActivityFetchedAt: $nft->collection->activity_updated_at,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Carbon\Carbon;
use Spatie\LaravelData\Data;

class Web3NftTransfer extends Data
class CollectionActivity extends Data
{
/**
* @param array<string, mixed> $extraAttributes
Expand All @@ -19,11 +19,22 @@ public function __construct(
public string $sender,
public string $recipient,
public string $txHash,
public NftTransferType $type,
public string $logIndex,
public ?NftTransferType $type,
public Carbon $timestamp,
public ?float $totalNative,
public ?float $totalUsd,
public array $extraAttributes,
) {
}

public function key(): string
{
return implode(':', [
$this->txHash,
$this->logIndex,
$this->tokenId,
$this->type->value,
]);
}
}
14 changes: 14 additions & 0 deletions app/Enums/TokenGuid.php
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';
}
2 changes: 1 addition & 1 deletion app/Http/Client/Alchemy/AlchemyPendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ private function extractTraits(array $nft): array

return collect($props)
->filter(function ($item) {
return ! empty(Arr::get($item, 'trait_type')) && ! empty(Arr::get($item, 'value'));
return ! empty(Arr::get($item, 'trait_type')) && ! empty(Arr::get($item, 'value')) && ! is_array(Arr::get($item, 'value'));
})
->map(function ($item) {
$value = strval($item['value']);
Expand Down
Loading

0 comments on commit aad0367

Please sign in to comment.