Skip to content

Commit

Permalink
move
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshka committed Dec 14, 2023
1 parent 3f85dc7 commit c8fc60c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-singers-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@soundxyz/sdk': patch
---

Update sdk for SuperMinterV1_1
63 changes: 36 additions & 27 deletions packages/sdk/src/contract/edition-v2/read/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand All @@ -21,38 +21,47 @@ 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 extends Pick<PublicClient, 'multicall'>>(
client: Client,
{ editionAddress }: { editionAddress: Address },
): Promise<SuperMinter> {
const cacheKey = `superMinter-${editionAddress}`

return CacheUtils.getOrSetCache<SuperMinter>(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<SuperMinter>(cacheKey, () => _getSuperMinterForEdition({ client, editionAddress }))
}

async function _getSuperMinterForEdition({
client,
editionAddress,
}: {
client: Pick<PublicClient, 'multicall'>
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')
}
32 changes: 32 additions & 0 deletions packages/sdk/src/utils/cache-singleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export class CacheSingleton {
private static instance: CacheSingleton
private cache: Map<string, any>

private constructor() {
this.cache = new Map<string, any>()
}

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)
}
}
14 changes: 14 additions & 0 deletions packages/sdk/src/utils/cache-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CacheSingleton } from './cache-singleton'

export class CacheUtils {
private static cache = CacheSingleton.getInstance()

public static async getOrSetCache<T>(key: string, fetchData: () => Promise<T>): Promise<T> {
let data = this.cache.get(key)
if (!data) {
data = await fetchData()
this.cache.set(key, data)
}
return data
}
}

0 comments on commit c8fc60c

Please sign in to comment.