Skip to content

Commit

Permalink
Use getLogs directly instead of creating a filter and querying it
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshka committed Mar 11, 2024
1 parent 8c7140f commit a5475ab
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 79 deletions.
6 changes: 6 additions & 0 deletions .changeset/great-dragons-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@soundxyz/legacy-sdk': minor
'@soundxyz/sdk': minor
---

legacy sdk minter performance issue fix
45 changes: 11 additions & 34 deletions packages/legacy-sdk/src/client/edition/schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function editionScheduleIds(
this: SoundClientInstance,
{
editionAddress,
fromBlock,
fromBlock = 'earliest',
}: {
editionAddress: Address
fromBlock?: BlockOrBlockTag
Expand Down Expand Up @@ -112,7 +112,7 @@ export async function editionRegisteredMinters(
this: SoundClientInstance,
{
editionAddress,
fromBlock,
fromBlock = 'earliest',
}: {
editionAddress: Address
fromBlock?: BlockOrBlockTag
Expand All @@ -131,7 +131,7 @@ export async function editionRegisteredMinters(
functionName: 'MINTER_ROLE',
})

const rolesUpdatedFilter = await client.createEventFilter({
const roleEvents = await client.getLogs({
event: {
anonymous: false,
inputs: [
Expand All @@ -146,25 +146,16 @@ export async function editionRegisteredMinters(
name: 'RolesUpdated',
type: 'event',
},
fromBlock: fromBlock ?? 'earliest',
fromBlock,
address: editionAddress,
args: {
roles: minterRole,
},
strict: true,
})

const roleEvents = await client.getFilterLogs({
filter: rolesUpdatedFilter,
})

const candidateMinters = Array.from(
roleEvents.reduce((acc, event) => {
if (event.args?.user) acc.add(event.args.user)

return acc
}, new Set<Address>()),
)
// This list may contain duplicates if MINTER_ROLE was granted multiple times
const candidateMinters = [...new Set(roleEvents.map((event) => event.args.user))]

// Check supportsInterface() to verify each address is a minter
const minters = await Promise.all(
Expand All @@ -183,21 +174,16 @@ export async function editionRegisteredMinters(
}
}),
)
// This list may contain duplicates if MINTER_ROLE was granted multiple times
const allMinters = minters.reduce((acc, minter) => {
if (minter) acc.add(minter)
return acc
}, new Set<Address>())

return Array.from(allMinters)
return minters.filter((minter): minter is Address => minter != null)
}

export async function editionMinterMintIds(
this: SoundClientInstance,
{
editionAddress,
minterAddress,
fromBlock,
fromBlock = 'earliest',
}: {
editionAddress: Address
minterAddress: Address
Expand All @@ -207,7 +193,7 @@ export async function editionMinterMintIds(
const client = await this.expectClient()

// Query MintConfigCreated event, for v1 and v2, this signature is the same
const filter = await client.createEventFilter({
const mintScheduleConfigEvents = await client.getLogs({
address: minterAddress,
event: {
anonymous: false,
Expand Down Expand Up @@ -252,23 +238,14 @@ export async function editionMinterMintIds(
name: 'MintConfigCreated',
type: 'event',
},
fromBlock: fromBlock ?? 'earliest',
fromBlock,
args: {
edition: editionAddress,
},
strict: true,
})

const mintScheduleConfigEvents = await client.getFilterLogs({
filter,
})

return Array.from(
mintScheduleConfigEvents.reduce((acc, event) => {
if (event.args?.mintId != null) acc.add(event.args.mintId)
return acc
}, new Set<bigint>()),
)
return [...new Set(mintScheduleConfigEvents.map((event) => event.args.mintId))]
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/legacy-sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type LazyOption<T extends object> = T | (() => T | Promise<T>)

export type ClientProvider = Pick<
PublicClient,
'chain' | 'readContract' | 'getFilterLogs' | 'createEventFilter' | 'estimateContractGas' | 'multicall'
'chain' | 'readContract' | 'getLogs' | 'estimateContractGas' | 'multicall'
>
export type Wallet = Pick<WalletClient, 'account' | 'chain' | 'writeContract' | 'signMessage' | 'sendTransaction'>

Expand Down
5 changes: 1 addition & 4 deletions packages/sdk/src/contract/edition-v1/read/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import { editionMintSchedules, editionMintSchedulesFromIds, editionScheduleIds }
import type { MerkleProvider } from '../../../utils/types'

export function editionV1PublicActions<
Client extends Pick<
PublicClient,
'readContract' | 'multicall' | 'estimateContractGas' | 'createEventFilter' | 'getFilterLogs'
> & {
Client extends Pick<PublicClient, 'readContract' | 'multicall' | 'estimateContractGas' | 'getLogs'> & {
editionV1?: {
sam?: {}
}
Expand Down
51 changes: 11 additions & 40 deletions packages/sdk/src/contract/edition-v1/read/schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ export function isHandledMinterInterfaceId(interfaceId: string): interfaceId is
return HANDLED_MINTER_INTERFACE_IDS_SET.has(interfaceId)
}

export async function editionRegisteredMinters<
Client extends Pick<PublicClient, 'readContract' | 'createEventFilter' | 'getFilterLogs'>,
>(
export async function editionRegisteredMinters<Client extends Pick<PublicClient, 'readContract' | 'getLogs'>>(
client: Client,
{
editionAddress,
Expand All @@ -48,7 +46,7 @@ export async function editionRegisteredMinters<
functionName: 'MINTER_ROLE',
})

const rolesUpdatedFilter = await client.createEventFilter({
const roleEvents = await client.getLogs({
event: {
anonymous: false,
inputs: [
Expand All @@ -71,17 +69,8 @@ export async function editionRegisteredMinters<
strict: true,
})

const roleEvents = await client.getFilterLogs({
filter: rolesUpdatedFilter,
})

const candidateMinters = Array.from(
roleEvents.reduce((acc, event) => {
if (event.args?.user) acc.add(event.args.user)

return acc
}, new Set<Address>()),
)
// This list may contain duplicates if MINTER_ROLE was granted multiple times
const candidateMinters = [...new Set(roleEvents.map((event) => event.args.user))]

// Check supportsInterface() to verify each address is a minter
const minters = await Promise.all(
Expand All @@ -100,16 +89,11 @@ export async function editionRegisteredMinters<
}
}),
)
// This list may contain duplicates if MINTER_ROLE was granted multiple times
const allMinters = minters.reduce((acc, minter) => {
if (minter) acc.add(minter)
return acc
}, new Set<Address>())

return Array.from(allMinters)
return minters.filter((minter): minter is Address => minter != null)
}

export async function editionMinterMintIds<Client extends Pick<PublicClient, 'createEventFilter' | 'getFilterLogs'>>(
export async function editionMinterMintIds<Client extends Pick<PublicClient, 'getLogs'>>(
client: Client,
{
editionAddress,
Expand All @@ -121,8 +105,8 @@ export async function editionMinterMintIds<Client extends Pick<PublicClient, 'cr
fromBlock?: FromBlock
},
) {
// Query MintConfigCreated event, for v1 and v2, this signature is the same
const filter = await client.createEventFilter({
// Query MintConfigCreated event, for v1.0-v1.2, this signature is the same
const mintScheduleConfigEvents = await client.getLogs({
address: minterAddress,
event: {
anonymous: false,
Expand Down Expand Up @@ -174,21 +158,10 @@ export async function editionMinterMintIds<Client extends Pick<PublicClient, 'cr
strict: true,
})

const mintScheduleConfigEvents = await client.getFilterLogs({
filter,
})

return Array.from(
mintScheduleConfigEvents.reduce((acc, event) => {
if (event.args?.mintId != null) acc.add(event.args.mintId)
return acc
}, new Set<bigint>()),
)
return [...new Set(mintScheduleConfigEvents.map((event) => event.args.mintId))]
}

export async function editionScheduleIds<
Client extends Pick<PublicClient, 'createEventFilter' | 'getFilterLogs' | 'readContract'>,
>(
export async function editionScheduleIds<Client extends Pick<PublicClient, 'getLogs' | 'readContract'>>(
client: Client,
{
editionAddress,
Expand Down Expand Up @@ -575,9 +548,7 @@ export async function editionMintSchedulesFromIds<Client extends Pick<PublicClie
}
}

export async function editionMintSchedules<
Client extends Pick<PublicClient, 'readContract' | 'multicall' | 'createEventFilter' | 'getFilterLogs'>,
>(
export async function editionMintSchedules<Client extends Pick<PublicClient, 'readContract' | 'multicall' | 'getLogs'>>(
client: Client,
{
editionAddress,
Expand Down

0 comments on commit a5475ab

Please sign in to comment.