From 3ce761129317a0e8ee29d1a5343a4a105d619098 Mon Sep 17 00:00:00 2001 From: Prithvish Baidya Date: Thu, 27 Feb 2025 02:10:04 +0530 Subject: [PATCH 1/2] expose cache size env var --- src/shared/db/wallet-credentials/get-wallet-credential.ts | 2 +- src/shared/db/wallets/get-wallet-details.ts | 2 +- src/shared/utils/account.ts | 5 +++-- src/shared/utils/cache/access-token.ts | 3 ++- src/shared/utils/cache/get-sdk.ts | 2 +- src/shared/utils/cache/get-smart-wallet-v5.ts | 3 ++- src/shared/utils/cache/get-wallet.ts | 3 ++- src/shared/utils/cache/get-webhook.ts | 3 ++- src/shared/utils/cache/keypair.ts | 3 ++- src/shared/utils/env.ts | 2 ++ 10 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/shared/db/wallet-credentials/get-wallet-credential.ts b/src/shared/db/wallet-credentials/get-wallet-credential.ts index f1cf5107f..e7e77d6b3 100644 --- a/src/shared/db/wallet-credentials/get-wallet-credential.ts +++ b/src/shared/db/wallet-credentials/get-wallet-credential.ts @@ -29,7 +29,7 @@ export type ParsedWalletCredential = z.infer; export const walletCredentialsCache = new LRUMap< string, ParsedWalletCredential ->(2048); +>(env.ACCOUNT_CAHCE_SIZE); interface GetWalletCredentialParams { id: string; diff --git a/src/shared/db/wallets/get-wallet-details.ts b/src/shared/db/wallets/get-wallet-details.ts index 0258feb79..6b04502b8 100644 --- a/src/shared/db/wallets/get-wallet-details.ts +++ b/src/shared/db/wallets/get-wallet-details.ts @@ -151,7 +151,7 @@ export type SmartBackendWalletType = (typeof SmartBackendWalletTypes)[number]; export type BackendWalletType = (typeof BackendWalletTypes)[number]; export type ParsedWalletDetails = z.infer; -export const walletDetailsCache = new LRUMap(2048); +export const walletDetailsCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); /** * Return the wallet details for the given address. * diff --git a/src/shared/utils/account.ts b/src/shared/utils/account.ts index 10944fa66..f6bdb7b7f 100644 --- a/src/shared/utils/account.ts +++ b/src/shared/utils/account.ts @@ -21,8 +21,9 @@ import { thirdwebClient } from "./sdk"; import { getWalletCredential } from "../db/wallet-credentials/get-wallet-credential"; import { getCircleAccount } from "../../server/utils/wallets/circle"; import { getConfig } from "./cache/get-config"; +import { env } from "./env"; -export const _accountsCache = new LRUMap(2048); +export const _accountsCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); export const getAccount = async (args: { chainId: number; @@ -212,7 +213,7 @@ export const walletDetailsToAccount = async ({ } }; -export const _adminAccountsCache = new LRUMap(2048); +export const _adminAccountsCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); /** * Get the admin account for a smart backend wallet (cached) diff --git a/src/shared/utils/cache/access-token.ts b/src/shared/utils/cache/access-token.ts index 139f16013..1ddbcdb27 100644 --- a/src/shared/utils/cache/access-token.ts +++ b/src/shared/utils/cache/access-token.ts @@ -1,9 +1,10 @@ import type { Tokens } from "@prisma/client"; import LRUMap from "mnemonist/lru-map"; import { getToken } from "../../db/tokens/get-token"; +import { env } from "../env"; // Cache an access token JWT to the token object, or null if not found. -export const accessTokenCache = new LRUMap(2048); +export const accessTokenCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); interface GetAccessTokenParams { jwt: string; diff --git a/src/shared/utils/cache/get-sdk.ts b/src/shared/utils/cache/get-sdk.ts index 92f80472b..92de94253 100644 --- a/src/shared/utils/cache/get-sdk.ts +++ b/src/shared/utils/cache/get-sdk.ts @@ -7,7 +7,7 @@ import { getChain } from "../chain"; import { env } from "../env"; import { getWallet } from "./get-wallet"; -export const sdkCache = new LRUMap(2048); +export const sdkCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); export const networkResponseSchema = Type.Object({ name: Type.String({ diff --git a/src/shared/utils/cache/get-smart-wallet-v5.ts b/src/shared/utils/cache/get-smart-wallet-v5.ts index 7ab01f275..6fc90df7e 100644 --- a/src/shared/utils/cache/get-smart-wallet-v5.ts +++ b/src/shared/utils/cache/get-smart-wallet-v5.ts @@ -3,8 +3,9 @@ import { getContract, readContract, type Address, type Chain } from "thirdweb"; import { smartWallet, type Account } from "thirdweb/wallets"; import { getAccount } from "../account"; import { thirdwebClient } from "../sdk"; +import { env } from "../env"; -export const smartWalletsCache = new LRUMap(2048); +export const smartWalletsCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); interface SmartWalletParams { chain: Chain; diff --git a/src/shared/utils/cache/get-wallet.ts b/src/shared/utils/cache/get-wallet.ts index bc3124b86..677671aae 100644 --- a/src/shared/utils/cache/get-wallet.ts +++ b/src/shared/utils/cache/get-wallet.ts @@ -14,8 +14,9 @@ import { splitAwsKmsArn } from "../../../server/utils/wallets/aws-kms-arn"; import { splitGcpKmsResourcePath } from "../../../server/utils/wallets/gcp-kms-resource-path"; import { getLocalWallet } from "../../../server/utils/wallets/get-local-wallet"; import { getSmartWallet } from "../../../server/utils/wallets/get-smart-wallet"; +import { env } from "../env"; -export const walletsCache = new LRUMap(2048); +export const walletsCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); interface GetWalletParams { pgtx?: PrismaTransaction; diff --git a/src/shared/utils/cache/get-webhook.ts b/src/shared/utils/cache/get-webhook.ts index 51326d569..a2a6a8db2 100644 --- a/src/shared/utils/cache/get-webhook.ts +++ b/src/shared/utils/cache/get-webhook.ts @@ -2,8 +2,9 @@ import type { Webhooks } from "@prisma/client"; import LRUMap from "mnemonist/lru-map"; import { getAllWebhooks } from "../../db/webhooks/get-all-webhooks"; import type { WebhooksEventTypes } from "../../schemas/webhooks"; +import { env } from "../env"; -export const webhookCache = new LRUMap(2048); +export const webhookCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); export const getWebhooksByEventType = async ( eventType: WebhooksEventTypes, diff --git a/src/shared/utils/cache/keypair.ts b/src/shared/utils/cache/keypair.ts index 17165ed5f..2faccc2ae 100644 --- a/src/shared/utils/cache/keypair.ts +++ b/src/shared/utils/cache/keypair.ts @@ -1,9 +1,10 @@ import type { Keypairs } from "@prisma/client"; import LRUMap from "mnemonist/lru-map"; import { getKeypairByHash, getKeypairByPublicKey } from "../../db/keypair/get"; +import { env } from "../env"; // Cache a public key to the Keypair object, or null if not found. -export const keypairCache = new LRUMap(2048); +export const keypairCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); /** * Get a keypair by public key or hash. diff --git a/src/shared/utils/env.ts b/src/shared/utils/env.ts index c048630d1..0665842da 100644 --- a/src/shared/utils/env.ts +++ b/src/shared/utils/env.ts @@ -63,6 +63,7 @@ export const env = createEnv({ .enum(["default", "sandbox", "server_only", "worker_only"]) .default("default"), GLOBAL_RATE_LIMIT_PER_MIN: z.coerce.number().default(400 * 60), + ACCOUNT_CAHCE_SIZE: z.coerce.number().default(2048), DD_TRACER_ACTIVATED: boolEnvSchema(false), // Prometheus @@ -145,6 +146,7 @@ export const env = createEnv({ ENABLE_CUSTOM_HMAC_AUTH: process.env.ENABLE_CUSTOM_HMAC_AUTH, CUSTOM_HMAC_AUTH_CLIENT_ID: process.env.CUSTOM_HMAC_AUTH_CLIENT_ID, CUSTOM_HMAC_AUTH_CLIENT_SECRET: process.env.CUSTOM_HMAC_AUTH_CLIENT_SECRET, + ACCOUNT_CAHCE_SIZE: process.env.ACCOUNT_CAHCE_SIZE, }, onValidationError: (error: ZodError) => { console.error( From dda218814d3b596065d1cbb5fb48550c1186e951 Mon Sep 17 00:00:00 2001 From: Prithvish Baidya Date: Thu, 27 Feb 2025 02:16:19 +0530 Subject: [PATCH 2/2] fix typo --- src/shared/db/wallet-credentials/get-wallet-credential.ts | 2 +- src/shared/db/wallets/get-wallet-details.ts | 2 +- src/shared/utils/account.ts | 4 ++-- src/shared/utils/cache/access-token.ts | 2 +- src/shared/utils/cache/get-sdk.ts | 2 +- src/shared/utils/cache/get-smart-wallet-v5.ts | 2 +- src/shared/utils/cache/get-wallet.ts | 2 +- src/shared/utils/cache/get-webhook.ts | 2 +- src/shared/utils/cache/keypair.ts | 2 +- src/shared/utils/env.ts | 4 ++-- 10 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/shared/db/wallet-credentials/get-wallet-credential.ts b/src/shared/db/wallet-credentials/get-wallet-credential.ts index e7e77d6b3..eacbfb136 100644 --- a/src/shared/db/wallet-credentials/get-wallet-credential.ts +++ b/src/shared/db/wallet-credentials/get-wallet-credential.ts @@ -29,7 +29,7 @@ export type ParsedWalletCredential = z.infer; export const walletCredentialsCache = new LRUMap< string, ParsedWalletCredential ->(env.ACCOUNT_CAHCE_SIZE); +>(env.ACCOUNT_CACHE_SIZE); interface GetWalletCredentialParams { id: string; diff --git a/src/shared/db/wallets/get-wallet-details.ts b/src/shared/db/wallets/get-wallet-details.ts index 6b04502b8..5e28a2219 100644 --- a/src/shared/db/wallets/get-wallet-details.ts +++ b/src/shared/db/wallets/get-wallet-details.ts @@ -151,7 +151,7 @@ export type SmartBackendWalletType = (typeof SmartBackendWalletTypes)[number]; export type BackendWalletType = (typeof BackendWalletTypes)[number]; export type ParsedWalletDetails = z.infer; -export const walletDetailsCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); +export const walletDetailsCache = new LRUMap(env.ACCOUNT_CACHE_SIZE); /** * Return the wallet details for the given address. * diff --git a/src/shared/utils/account.ts b/src/shared/utils/account.ts index f6bdb7b7f..074ce55d3 100644 --- a/src/shared/utils/account.ts +++ b/src/shared/utils/account.ts @@ -23,7 +23,7 @@ import { getCircleAccount } from "../../server/utils/wallets/circle"; import { getConfig } from "./cache/get-config"; import { env } from "./env"; -export const _accountsCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); +export const _accountsCache = new LRUMap(env.ACCOUNT_CACHE_SIZE); export const getAccount = async (args: { chainId: number; @@ -213,7 +213,7 @@ export const walletDetailsToAccount = async ({ } }; -export const _adminAccountsCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); +export const _adminAccountsCache = new LRUMap(env.ACCOUNT_CACHE_SIZE); /** * Get the admin account for a smart backend wallet (cached) diff --git a/src/shared/utils/cache/access-token.ts b/src/shared/utils/cache/access-token.ts index 1ddbcdb27..0379714f6 100644 --- a/src/shared/utils/cache/access-token.ts +++ b/src/shared/utils/cache/access-token.ts @@ -4,7 +4,7 @@ import { getToken } from "../../db/tokens/get-token"; import { env } from "../env"; // Cache an access token JWT to the token object, or null if not found. -export const accessTokenCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); +export const accessTokenCache = new LRUMap(env.ACCOUNT_CACHE_SIZE); interface GetAccessTokenParams { jwt: string; diff --git a/src/shared/utils/cache/get-sdk.ts b/src/shared/utils/cache/get-sdk.ts index 92de94253..9cb18b50b 100644 --- a/src/shared/utils/cache/get-sdk.ts +++ b/src/shared/utils/cache/get-sdk.ts @@ -7,7 +7,7 @@ import { getChain } from "../chain"; import { env } from "../env"; import { getWallet } from "./get-wallet"; -export const sdkCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); +export const sdkCache = new LRUMap(env.ACCOUNT_CACHE_SIZE); export const networkResponseSchema = Type.Object({ name: Type.String({ diff --git a/src/shared/utils/cache/get-smart-wallet-v5.ts b/src/shared/utils/cache/get-smart-wallet-v5.ts index 6fc90df7e..1bedce2f7 100644 --- a/src/shared/utils/cache/get-smart-wallet-v5.ts +++ b/src/shared/utils/cache/get-smart-wallet-v5.ts @@ -5,7 +5,7 @@ import { getAccount } from "../account"; import { thirdwebClient } from "../sdk"; import { env } from "../env"; -export const smartWalletsCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); +export const smartWalletsCache = new LRUMap(env.ACCOUNT_CACHE_SIZE); interface SmartWalletParams { chain: Chain; diff --git a/src/shared/utils/cache/get-wallet.ts b/src/shared/utils/cache/get-wallet.ts index 677671aae..0e0e110dd 100644 --- a/src/shared/utils/cache/get-wallet.ts +++ b/src/shared/utils/cache/get-wallet.ts @@ -16,7 +16,7 @@ import { getLocalWallet } from "../../../server/utils/wallets/get-local-wallet"; import { getSmartWallet } from "../../../server/utils/wallets/get-smart-wallet"; import { env } from "../env"; -export const walletsCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); +export const walletsCache = new LRUMap(env.ACCOUNT_CACHE_SIZE); interface GetWalletParams { pgtx?: PrismaTransaction; diff --git a/src/shared/utils/cache/get-webhook.ts b/src/shared/utils/cache/get-webhook.ts index a2a6a8db2..ae3ac3664 100644 --- a/src/shared/utils/cache/get-webhook.ts +++ b/src/shared/utils/cache/get-webhook.ts @@ -4,7 +4,7 @@ import { getAllWebhooks } from "../../db/webhooks/get-all-webhooks"; import type { WebhooksEventTypes } from "../../schemas/webhooks"; import { env } from "../env"; -export const webhookCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); +export const webhookCache = new LRUMap(env.ACCOUNT_CACHE_SIZE); export const getWebhooksByEventType = async ( eventType: WebhooksEventTypes, diff --git a/src/shared/utils/cache/keypair.ts b/src/shared/utils/cache/keypair.ts index 2faccc2ae..4df53a2ec 100644 --- a/src/shared/utils/cache/keypair.ts +++ b/src/shared/utils/cache/keypair.ts @@ -4,7 +4,7 @@ import { getKeypairByHash, getKeypairByPublicKey } from "../../db/keypair/get"; import { env } from "../env"; // Cache a public key to the Keypair object, or null if not found. -export const keypairCache = new LRUMap(env.ACCOUNT_CAHCE_SIZE); +export const keypairCache = new LRUMap(env.ACCOUNT_CACHE_SIZE); /** * Get a keypair by public key or hash. diff --git a/src/shared/utils/env.ts b/src/shared/utils/env.ts index 0665842da..536b4b0d6 100644 --- a/src/shared/utils/env.ts +++ b/src/shared/utils/env.ts @@ -63,7 +63,7 @@ export const env = createEnv({ .enum(["default", "sandbox", "server_only", "worker_only"]) .default("default"), GLOBAL_RATE_LIMIT_PER_MIN: z.coerce.number().default(400 * 60), - ACCOUNT_CAHCE_SIZE: z.coerce.number().default(2048), + ACCOUNT_CACHE_SIZE: z.coerce.number().default(2048), DD_TRACER_ACTIVATED: boolEnvSchema(false), // Prometheus @@ -146,7 +146,7 @@ export const env = createEnv({ ENABLE_CUSTOM_HMAC_AUTH: process.env.ENABLE_CUSTOM_HMAC_AUTH, CUSTOM_HMAC_AUTH_CLIENT_ID: process.env.CUSTOM_HMAC_AUTH_CLIENT_ID, CUSTOM_HMAC_AUTH_CLIENT_SECRET: process.env.CUSTOM_HMAC_AUTH_CLIENT_SECRET, - ACCOUNT_CAHCE_SIZE: process.env.ACCOUNT_CAHCE_SIZE, + ACCOUNT_CACHE_SIZE: process.env.ACCOUNT_CAHCE_SIZE, }, onValidationError: (error: ZodError) => { console.error(