Skip to content

Commit

Permalink
refactor: remove unnecessary database queries from wallet data (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
crnkovic authored Oct 10, 2023
1 parent 7a97744 commit 69c8bbf
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 96 deletions.
5 changes: 1 addition & 4 deletions app/Data/Token/TokenData.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ public function __construct(

public static function fromModel(Token $token): self
{
$currency = CurrencyCode::USD;
if (Auth::hasUser()) {
$currency = Auth::user()->currency();
}
$currency = Auth::user()?->currency() ?? CurrencyCode::USD;

return new self(
address: $token->address,
Expand Down
41 changes: 12 additions & 29 deletions app/Data/Wallet/WalletData.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
'totalUsd' => 'number',
'totalBalanceInCurrency' => 'string',
'totalTokens' => 'number',
'nfts' => 'WalletNftData[]',
'nftCount' => 'number',
'collectionCount' => 'number',
'collectionsValue' => 'number | null',
'galleryCount' => 'number',
'timestamps' => '{tokens_fetched_at: number|null, native_balances_fetched_at: number|null}',
])]
Expand All @@ -35,16 +32,9 @@ public function __construct(
public float $totalUsd,
public string $totalBalanceInCurrency,
public int $totalTokens,
/**
* @var WalletNftData[]
*/
public array $nfts,
public int $nftCount,
public int $collectionCount,
public ?float $collectionsValue,
public int $galleryCount,
public int $userId,

public bool $isLocalTestingAddress,
/**
* @var array{tokens_fetched_at: int|null, native_balances_fetched_at: int|null} $timestamps
Expand All @@ -55,28 +45,21 @@ public function __construct(

public static function fromModel(Wallet $wallet): self
{
$domain = $wallet->details?->domain;
$avatarData = WalletAvatarData::fromModel($wallet);
$userCache = UserCache::from($wallet->user);

$user = $wallet->user;

return new self(
$wallet['id'],
$wallet['address'],
$domain,
$avatarData,
$wallet['total_usd'],
$wallet->totalBalanceInCurrency(),
$wallet->totalTokens(),
$userCache->userDetailNfts()->filter(fn ($nft) => ! is_null($nft->collection))->map(fn ($nft) => WalletNftData::fromModel($nft))->all(),
$userCache->nftsCount(),
$userCache->collectionsCount(),
$user->collectionsValue($user->currency()),
$userCache->galleriesCount(),
$user->id,
$wallet->isLocalTestingAddress(),
[
id: $wallet->id,
address: $wallet->address,
domain: $wallet->details?->domain,
avatar: WalletAvatarData::fromModel($wallet),
totalUsd: $wallet->total_usd,
totalBalanceInCurrency: $wallet->totalBalanceInCurrency(),
totalTokens: $wallet->totalTokens(),
collectionCount: $userCache->collectionsCount(),
galleryCount: $userCache->galleriesCount(),
userId: $wallet->user_id,
isLocalTestingAddress: $wallet->isLocalTestingAddress(),
timestamps: [
'tokens_fetched_at' => $wallet->tokensFetchedAt()?->getTimestampMs(),
'native_balances_fetched_at' => $wallet->nativeBalancesFetchedAt()?->getTimestampMs(),
]
Expand Down
31 changes: 0 additions & 31 deletions app/Data/Wallet/WalletNftData.php

This file was deleted.

6 changes: 3 additions & 3 deletions app/Support/Cache/UserCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,23 @@ private static function getShownNftsCountKey(int $userId): string
public function collectionsCount(): int
{
return (int) $this->fromCache(
fn () => $this->user->nfts->unique('collection_id')->count(),
fn () => $this->user->nfts()->distinct('collection_id')->count(),
self::getCollectionsCountKey($this->user->id),
);
}

public function hiddenCollectionsCount(): int
{
return (int) $this->fromCache(
fn () => $this->user->nfts->unique('collection_id')->whereIn('collection_id', $this->user->hiddenCollections->modelKeys())->count(),
fn () => $this->user->nfts()->distinct('collection_id')->whereIn('collection_id', $this->user->hiddenCollections->modelKeys())->count(),
self::getHiddenCollectionsCountKey($this->user->id),
);
}

public function shownCollectionsCount(): int
{
return (int) $this->fromCache(
fn () => $this->user->nfts->unique('collection_id')->whereNotIn('collection_id', $this->user->hiddenCollections->modelKeys())->count(),
fn () => $this->user->nfts()->distinct('collection_id')->whereNotIn('collection_id', $this->user->hiddenCollections->modelKeys())->count(),
self::getShownCollectionsCountKey($this->user->id),
);
}
Expand Down
4 changes: 0 additions & 4 deletions resources/js/Tests/Factories/Wallet/WalletFactory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { faker } from "@faker-js/faker";
import WalletAvatarDataFactory from "./WalletAvatarDataFactory";
import WalletNftDataFactory from "./WalletNftDataFactory";
import ModelFactory from "@/Tests/Factories/ModelFactory";

export default class WalletFactory extends ModelFactory<App.Data.Wallet.WalletData> {
Expand All @@ -12,10 +11,7 @@ export default class WalletFactory extends ModelFactory<App.Data.Wallet.WalletDa
domain: this.optional(faker.internet.domainWord() + ".eth"),
avatar: new WalletAvatarDataFactory().create(),
totalUsd: Number(faker.finance.amount(1, 100000, 2)),
nfts: new WalletNftDataFactory().createMany(faker.datatype.number({ min: 0, max: 3 })),
nftCount: Number(faker.datatype.number({ min: 0, max: 100 })),
collectionCount: Number(faker.datatype.number({ min: 0, max: 100 })),
collectionsValue: this.optional(Number(faker.finance.amount(1, 100000, 2))),
galleryCount: Number(faker.datatype.number({ min: 0, max: 100 })),
timestamps: {
tokens_fetched_at: this.optional(faker.date.past().getTime()),
Expand Down
12 changes: 0 additions & 12 deletions resources/js/Tests/Factories/Wallet/WalletNftDataFactory.ts

This file was deleted.

7 changes: 0 additions & 7 deletions resources/types/generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,17 +422,10 @@ declare namespace App.Data.Wallet {
totalUsd: number;
totalBalanceInCurrency: string;
totalTokens: number;
nfts: WalletNftData[];
nftCount: number;
collectionCount: number;
collectionsValue: number | null;
galleryCount: number;
timestamps: { tokens_fetched_at: number | null; native_balances_fetched_at: number | null };
};
export type WalletNftData = {
id: number;
images: App.Data.ImagesData;
};
}
declare namespace App.Data.Web3 {
export type Web3ContractMetadata = {
Expand Down
6 changes: 0 additions & 6 deletions tests/App/Http/Middleware/HandleInertiaRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@

$middlewareResponse = $middleware->share($request);

expect($middlewareResponse['auth']()->wallet->nftCount)->toBe(4);
expect($middlewareResponse['auth']()->wallet->collectionCount)->toBe(1);
expect($middlewareResponse['auth']()->wallet->galleryCount)->toBe(1);
expect(count($middlewareResponse['auth']()->wallet->nfts))->toBe(4);

Nft::factory(4)->create([
'wallet_id' => $wallet->id,
Expand All @@ -71,10 +69,8 @@

$middlewareResponse = $middleware->share($request);

expect(($middlewareResponse['auth']())->wallet->nftCount)->toBe(4);
expect($middlewareResponse['auth']()->wallet->collectionCount)->toBe(1);
expect($middlewareResponse['auth']()->wallet->galleryCount)->toBe(1);
expect(count($middlewareResponse['auth']()->wallet->nfts))->toBe(4);

$userCache->clearUserDetailNfts();
$userCache->clearNftsCount();
Expand All @@ -83,8 +79,6 @@

$middlewareResponse = $middleware->share($request);

expect($middlewareResponse['auth']()->wallet->nftCount)->toBe(8);
expect($middlewareResponse['auth']()->wallet->collectionCount)->toBe(2);
expect($middlewareResponse['auth']()->wallet->galleryCount)->toBe(2);
expect(count($middlewareResponse['auth']()->wallet->nfts))->toBe(4);
});

0 comments on commit 69c8bbf

Please sign in to comment.