diff --git a/src/shared/db/wallet-credentials/get-wallet-credential.ts b/src/shared/db/wallet-credentials/get-wallet-credential.ts index f1cf5107..e7e77d6b 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 0258feb7..6b04502b 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 10944fa6..f6bdb7b7 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 139f1601..1ddbcdb2 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 92f80472..92de9425 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 7ab01f27..6fc90df7 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 bc3124b8..677671aa 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 51326d56..a2a6a8db 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 17165ed5..2faccc2a 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 c048630d..0665842d 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(