diff --git a/packages/sdk/src/contract/edition-v2/read/helpers.ts b/packages/sdk/src/contract/edition-v2/read/helpers.ts index e4d723a8..d92d1f2a 100644 --- a/packages/sdk/src/contract/edition-v2/read/helpers.ts +++ b/packages/sdk/src/contract/edition-v2/read/helpers.ts @@ -3,8 +3,8 @@ import { nowUnixTimestamp } from '../../../utils/helpers' import { SOUND_EDITION_V2_ABI } from '../abi/sound-edition-v2' import { SUPER_MINTER_V1, SUPER_MINTER_V1_ADDRESS } from '../abi/super-minter-v1' import { SUPER_MINTER_V1_1, SUPER_MINTER_V1_1_ADDRESS } from '../abi/super-minter-v1_1' -import { CacheUtils } from '../cache/cache-utils' import type { GetEditionContractInfoReturnType } from './info' +import { CacheUtils } from '../../../utils/cache-utils' export function getTierCurrentMaxMintable( tierInfo: Pick< @@ -21,6 +21,7 @@ export function getTierCurrentMaxMintable( export const MINTER_ROLE = 2n export type SuperMinter = typeof SUPER_MINTER_V1 | typeof SUPER_MINTER_V1_1 +export type SuperMinterAddress = SuperMinter['address'] export async function getSuperMinterForEdition>( client: Client, @@ -28,31 +29,39 @@ export async function getSuperMinterForEdition { const cacheKey = `superMinter-${editionAddress}` - return CacheUtils.getOrSetCache(cacheKey, async () => { - const [hasSuperMinterV1, hasSuperMinterV1_1] = await client.multicall({ - contracts: [ - { - abi: SOUND_EDITION_V2_ABI, - address: editionAddress, - functionName: 'hasAnyRole', - args: [SUPER_MINTER_V1_ADDRESS, MINTER_ROLE], - }, - { - abi: SOUND_EDITION_V2_ABI, - address: editionAddress, - functionName: 'hasAnyRole', - args: [SUPER_MINTER_V1_1_ADDRESS, MINTER_ROLE], - }, - ], - allowFailure: false, - }) - - if (hasSuperMinterV1) { - return SUPER_MINTER_V1 - } else if (hasSuperMinterV1_1) { - return SUPER_MINTER_V1_1 - } - - throw new Error('No super minter found for edition') + return CacheUtils.getOrSetCache(cacheKey, () => _getSuperMinterForEdition({ client, editionAddress })) +} + +async function _getSuperMinterForEdition({ + client, + editionAddress, +}: { + client: Pick + editionAddress: Address +}) { + const [hasSuperMinterV1, hasSuperMinterV1_1] = await client.multicall({ + contracts: [ + { + abi: SOUND_EDITION_V2_ABI, + address: editionAddress, + functionName: 'hasAnyRole', + args: [SUPER_MINTER_V1_ADDRESS, MINTER_ROLE], + }, + { + abi: SOUND_EDITION_V2_ABI, + address: editionAddress, + functionName: 'hasAnyRole', + args: [SUPER_MINTER_V1_1_ADDRESS, MINTER_ROLE], + }, + ], + allowFailure: false, }) + + if (hasSuperMinterV1) { + return SUPER_MINTER_V1 + } else if (hasSuperMinterV1_1) { + return SUPER_MINTER_V1_1 + } + + throw new Error('No super minter found for edition') } diff --git a/packages/sdk/src/utils/cache-singleton.ts b/packages/sdk/src/utils/cache-singleton.ts new file mode 100644 index 00000000..32d9b33a --- /dev/null +++ b/packages/sdk/src/utils/cache-singleton.ts @@ -0,0 +1,32 @@ +export class CacheSingleton { + private static instance: CacheSingleton + private cache: Map + + private constructor() { + this.cache = new Map() + } + + public static getInstance(): CacheSingleton { + if (!CacheSingleton.instance) { + CacheSingleton.instance = new CacheSingleton() + } + return CacheSingleton.instance + } + + public set(key: string, value: any): void { + this.cache.set(key, value) + } + + public get(key: string): any { + return this.cache.get(key) + } + + // Optionally, you can add methods to clear the cache or check if a key exists + public clear(): void { + this.cache.clear() + } + + public has(key: string): boolean { + return this.cache.has(key) + } +} diff --git a/packages/sdk/src/utils/cache-utils.ts b/packages/sdk/src/utils/cache-utils.ts new file mode 100644 index 00000000..2133ddfb --- /dev/null +++ b/packages/sdk/src/utils/cache-utils.ts @@ -0,0 +1,14 @@ +import { CacheSingleton } from './cache-singleton' + +export class CacheUtils { + private static cache = CacheSingleton.getInstance() + + public static async getOrSetCache(key: string, fetchData: () => Promise): Promise { + let data = this.cache.get(key) + if (!data) { + data = await fetchData() + this.cache.set(key, data) + } + return data + } +}