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: Alchemy API v3 migration #516

Merged
merged 7 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 19 additions & 21 deletions app/Console/Commands/LiveDumpNfts.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,28 @@ class LiveDumpNfts extends Command
/**
* @var array<mixed>
*/
protected $requiredAttributes = [
'title',
protected array $requiredAttributes = [
'name',
'description',
'tokenId',
'contract' => [
'address',
],
'id' => [
'tokenId',
],
'media' => [
'0' => ['thumbnail', 'gateway', 'raw'],
],
'contractMetadata' => [
'symbol',
'name',
'totalSupply',
'openSea' => ['twitterUsername', 'discordUrl', 'floorPrice', 'collectionName', 'imageUrl', 'externalUrl', 'description'],
'openSeaMetadata' => ['twitterUsername', 'discordUrl', 'floorPrice', 'collectionName', 'imageUrl', 'externalUrl', 'description'],
'deployedBlockNumber',
],
'metadata' => [
'image',
'attributes',
'properties',
'external_url',
'image' => [
'thumbnailUrl', 'cachedUrl', 'originalUrl',
],
'raw' => [
'metadata' => [
'image',
'attributes',
'properties',
'external_url',
],
],
];

Expand Down Expand Up @@ -188,10 +186,10 @@ private function getTopCollections(Chain $chain, int $limit = 25): IlluminateCol
private function removeEncodedAttributes(array $nft): array
{
$potentiallyEncodedAttributes = [
'media.0.thumbnail',
'media.0.gateway',
'media.0.raw',
'metadata.image',
'image.thumbnailUrl',
'image.cachedUrl',
'image.originalUrl',
'raw.metadata.image',
];

foreach ($potentiallyEncodedAttributes as $attribute) {
Expand Down Expand Up @@ -291,7 +289,7 @@ private function getTokenCursorForChunk(int $chunk, Chain $chain, string $contra

$nftChunk = json_decode($file, true);

return Arr::get($nftChunk, 'nextToken');
return Arr::get($nftChunk, 'pageKey');
}

private function getCollectionTraitsAndPersist(Chain $chain, string $address): void
Expand Down
110 changes: 50 additions & 60 deletions app/Http/Client/Alchemy/AlchemyPendingRequest.php

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions tests/App/Console/Commands/LiveDumpNftsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Support\Facades\Mnemonic;
use Illuminate\Console\Command;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
Expand All @@ -19,7 +20,7 @@
if ($request->method() == 'GET') {
$path = $request->toPsrRequest()->getUri()->getPath();
if (Str::contains($path, 'getNFTsForCollection')) {
return Http::response(fixtureData('alchemy.get_nfts_for_collection_last_page'), 200);
return Http::response(fixtureData('alchemy.get_nfts_for_contract'), 200);
}
}

Expand Down Expand Up @@ -101,6 +102,9 @@
$this->artisan('nfts:live-dump --collection-index=0 --chain-id=1');

$liveDumps->each(fn ($liveDump) => expect($this->fakeFileSystem->exists($liveDump))->toBeTrue());

$dump = json_decode($this->fakeFileSystem->get($liveDumps[0]));
expect(Arr::get($dump, 'raw.metadata.image'))->toBe(null);
});

it('should run only in non-production environments', function () {
Expand Down Expand Up @@ -131,10 +135,10 @@
->assertExitCode(Command::INVALID);
});

it('should continue continue from the given chunk', function () {
it('should continue from the given chunk', function () {
$this->fakeFileSystem->put(
'collection-nfts/eth_0x4e1f41613c9084fdb9e34e11fae9412427480e56/nft-chunks/1.json',
json_encode(fixtureData('alchemy.get_nfts_for_collection'))
json_encode(fixtureData('alchemy.get_nfts_for_contract'))
);

$this->artisan('nfts:live-dump --collection-index=0 --chain-id=1 --start-chunk-index=1');
Expand Down
4 changes: 2 additions & 2 deletions tests/App/Console/Commands/LiveDumpWalletsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
Alchemy::fake(function (Request $request) {
if ($request->method() == 'GET') {
$path = $request->toPsrRequest()->getUri()->getPath();
if (Str::endsWith($path, 'getNFTs')) {
return Http::response(fixtureData('alchemy.nfts'), 200);
if (Str::endsWith($path, 'getNFTsForOwner')) {
return Http::response(fixtureData('alchemy.nfts_for_owner_2'), 200);
}
}

Expand Down
54 changes: 27 additions & 27 deletions tests/App/Http/Client/Alchemy/AlchemyPendingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@

it('should handle arrays in NFT descriptions', function () {
Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_array_description'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_for_owner'), 200),
]);

$wallet = Wallet::factory()->create();
Expand All @@ -61,8 +61,8 @@

it('should increment default size for banner image if it is not null in parseNft', function () {
Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_array_description_with_banner'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_for_owner'), 200),
]);

$wallet = Wallet::factory()->create();
Expand All @@ -88,7 +88,7 @@
$nfts->push($nft);

Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*' => Http::response(fixtureData('alchemy.nft_batch_metadata'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*' => Http::response(fixtureData('alchemy.nft_batch_metadata_2'), 200),
]);

$fetchedNfts = Alchemy::nftMetadataBatch($nfts, $network);
Expand All @@ -98,8 +98,8 @@

it('should add a flag in case of responses with errors', function () {
Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_array_with_error'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_for_owner'), 200),
]);

$wallet = Wallet::factory()->create();
Expand All @@ -111,28 +111,28 @@

it('should not add a flag in case of valid responses', function () {
Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_array_valid'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_for_owner'), 200),
]);

$wallet = Wallet::factory()->create();
$network = Network::polygon();

$collection = Alchemy::getWalletNfts($wallet, $network);
expect($collection->nfts[0]->hasError)->not->toBeTrue();
expect($collection->nfts[1]->hasError)->not->toBeTrue();
});

it('should not filter nfts with errors if the flag is set as true', function () {
Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_array_with_error'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_for_owner'), 200),
]);

$wallet = Wallet::factory()->create();
$network = Network::polygon();

$collection = Alchemy::getWalletNfts($wallet, $network);
expect($collection->nfts)->toHaveCount(1);
expect($collection->nfts)->toHaveCount(4);
});

it('should filter nfts with errors by default', function () {
Expand All @@ -150,18 +150,18 @@
$nfts->push($nft);

Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*' => Http::response(fixtureData('alchemy.nft_batch_metadata_with_error'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*' => Http::response(fixtureData('alchemy.nft_batch_metadata_2'), 200),
]);

$fetchedNfts = Alchemy::nftMetadataBatch($nfts, $network);

expect($fetchedNfts->nfts)->toHaveCount(0);
expect($fetchedNfts->nfts)->toHaveCount(1);
});

it('should return error field with METADATA_OUTDATED if parent metadata object is empty', function () {
Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_array_with_error_and_empty_metadata_object'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_for_owner'), 200),
]);

$wallet = Wallet::factory()->create();
Expand All @@ -175,45 +175,45 @@

it('should return error field with METADATA_OUTDATED if metadata.metadata object is empty', function () {
Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_array_with_error_and_empty_metadata_metadata_object'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_for_owner'), 200),
]);

$wallet = Wallet::factory()->create();
$network = Network::polygon();

$collection = Alchemy::getWalletNfts($wallet, $network);

expect($collection->nfts[0]->hasError)->toBetrue();
expect($collection->nfts[0]->info)->toBe(NftInfo::MetadataOutdated->value);
expect($collection->nfts[2]->hasError)->toBetrue();
expect($collection->nfts[2]->info)->toBe(NftInfo::MetadataOutdated->value);
});

it('should ignore arrays in trait values', function () {
Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_array_collection_array_traits'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_for_owner'), 200),
]);

$wallet = Wallet::factory()->create();
$network = Network::polygon();

$collection = Alchemy::getWalletNfts($wallet, $network);

expect($collection->nfts[0]->traits[1])->toContain('Tails');
expect($collection->nfts[1]->traits[1])->toContain('Kick Kick Snare');
});

it('should handle unexpected trait values', function () {
Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_array_unexpected_traits'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*' => Http::sequence()
->push(fixtureData('alchemy.nfts_for_owner'), 200),
]);

$wallet = Wallet::factory()->create();
$network = Network::polygon();

$collection = Alchemy::getWalletNfts($wallet, $network);

expect(count($collection->nfts[0]->traits))->toBe(1);
expect(count($collection->nfts[3]->traits))->toBe(1);
});

it('should fetch spam contracts', function () {
Expand Down
4 changes: 2 additions & 2 deletions tests/App/Jobs/FetchCollectionBannerBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

it('should fetch nft collection banner', function () {
Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*/getContractMetadataBatch' => Http::response(fixtureData('alchemy.contract-metadata-batch'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*/getContractMetadataBatch' => Http::response(fixtureData('alchemy.contract_metadata_batch'), 200),
]);

$now = Carbon::now();
Expand Down Expand Up @@ -44,7 +44,7 @@

it('should skip updating banner if it is null', function () {
Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*/getContractMetadataBatch' => Http::response(fixtureData('alchemy.contract-metadata-batch'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*/getContractMetadataBatch' => Http::response(fixtureData('alchemy.contract_metadata_batch'), 200),
]);

$network = Network::polygon();
Expand Down
9 changes: 5 additions & 4 deletions tests/App/Jobs/FetchCollectionMetadataJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

it('should update nft collection metadata', function () {
Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*/getContractMetadataBatch' => Http::response(fixtureData('alchemy.contract-metadata-batch'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*/getContractMetadataBatch' => Http::response(fixtureData('alchemy.contract_metadata_batch'), 200),
]);

$now = Carbon::now();
Expand All @@ -21,7 +21,7 @@

/** @var Collection $collection */
$collection = Collection::factory()->create([
'address' => '0xe785e82358879f061bc3dcac6f0444462d4b5330',
'address' => '0xe785E82358879F061BC3dcAC6f0444462D4b5330',
'network_id' => $network->id,
'supply' => 8000,
'minted_block' => 12907765,
Expand All @@ -43,19 +43,20 @@
->and($collection->description)->toBe('World of Women is a collection of 10,000 NFTs.')
->and($updatedAt->timestamp)->toBe($now->timestamp)
->and($collection->image())->toBe('image-url')
->and($collection->openSeaSlug())->toBe('world-of-women-nft')
->and($collection->banner())->toBe('https://i.seadn.io/gae/GHhptRLebBOWOy8kfXpYCVqsqdes-1-6I_jbuRnGTHHW6TD63CtciH75Dotfu2u8v6EmkWt-tjhkFRVLxRUwgMfKqqy5W24AolJayeo?w=500&auto=format');
});

it('should skip updating column if metadata is null', function () {
Alchemy::fake([
'https://polygon-mainnet.g.alchemy.com/nft/v2/*/getContractMetadataBatch' => Http::response(fixtureData('alchemy.contract-metadata-batch'), 200),
'https://polygon-mainnet.g.alchemy.com/nft/v3/*/getContractMetadataBatch' => Http::response(fixtureData('alchemy.contract_metadata_batch'), 200),
]);

$network = Network::polygon()->firstOrFail();

/** @var Collection $collection */
$collection = Collection::factory()->create([
'address' => '0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d',
'address' => '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D',
'network_id' => $network->id,
'supply' => 8000,
'floor_price' => 80000000,
Expand Down
Loading
Loading