Skip to content

Commit a5475ab

Browse files
committed
Use getLogs directly instead of creating a filter and querying it
1 parent 8c7140f commit a5475ab

File tree

5 files changed

+30
-79
lines changed

5 files changed

+30
-79
lines changed

.changeset/great-dragons-cough.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@soundxyz/legacy-sdk': minor
3+
'@soundxyz/sdk': minor
4+
---
5+
6+
legacy sdk minter performance issue fix

packages/legacy-sdk/src/client/edition/schedules.ts

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export async function editionScheduleIds(
7979
this: SoundClientInstance,
8080
{
8181
editionAddress,
82-
fromBlock,
82+
fromBlock = 'earliest',
8383
}: {
8484
editionAddress: Address
8585
fromBlock?: BlockOrBlockTag
@@ -112,7 +112,7 @@ export async function editionRegisteredMinters(
112112
this: SoundClientInstance,
113113
{
114114
editionAddress,
115-
fromBlock,
115+
fromBlock = 'earliest',
116116
}: {
117117
editionAddress: Address
118118
fromBlock?: BlockOrBlockTag
@@ -131,7 +131,7 @@ export async function editionRegisteredMinters(
131131
functionName: 'MINTER_ROLE',
132132
})
133133

134-
const rolesUpdatedFilter = await client.createEventFilter({
134+
const roleEvents = await client.getLogs({
135135
event: {
136136
anonymous: false,
137137
inputs: [
@@ -146,25 +146,16 @@ export async function editionRegisteredMinters(
146146
name: 'RolesUpdated',
147147
type: 'event',
148148
},
149-
fromBlock: fromBlock ?? 'earliest',
149+
fromBlock,
150150
address: editionAddress,
151151
args: {
152152
roles: minterRole,
153153
},
154154
strict: true,
155155
})
156156

157-
const roleEvents = await client.getFilterLogs({
158-
filter: rolesUpdatedFilter,
159-
})
160-
161-
const candidateMinters = Array.from(
162-
roleEvents.reduce((acc, event) => {
163-
if (event.args?.user) acc.add(event.args.user)
164-
165-
return acc
166-
}, new Set<Address>()),
167-
)
157+
// This list may contain duplicates if MINTER_ROLE was granted multiple times
158+
const candidateMinters = [...new Set(roleEvents.map((event) => event.args.user))]
168159

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

192-
return Array.from(allMinters)
178+
return minters.filter((minter): minter is Address => minter != null)
193179
}
194180

195181
export async function editionMinterMintIds(
196182
this: SoundClientInstance,
197183
{
198184
editionAddress,
199185
minterAddress,
200-
fromBlock,
186+
fromBlock = 'earliest',
201187
}: {
202188
editionAddress: Address
203189
minterAddress: Address
@@ -207,7 +193,7 @@ export async function editionMinterMintIds(
207193
const client = await this.expectClient()
208194

209195
// Query MintConfigCreated event, for v1 and v2, this signature is the same
210-
const filter = await client.createEventFilter({
196+
const mintScheduleConfigEvents = await client.getLogs({
211197
address: minterAddress,
212198
event: {
213199
anonymous: false,
@@ -252,23 +238,14 @@ export async function editionMinterMintIds(
252238
name: 'MintConfigCreated',
253239
type: 'event',
254240
},
255-
fromBlock: fromBlock ?? 'earliest',
241+
fromBlock,
256242
args: {
257243
edition: editionAddress,
258244
},
259245
strict: true,
260246
})
261247

262-
const mintScheduleConfigEvents = await client.getFilterLogs({
263-
filter,
264-
})
265-
266-
return Array.from(
267-
mintScheduleConfigEvents.reduce((acc, event) => {
268-
if (event.args?.mintId != null) acc.add(event.args.mintId)
269-
return acc
270-
}, new Set<bigint>()),
271-
)
248+
return [...new Set(mintScheduleConfigEvents.map((event) => event.args.mintId))]
272249
}
273250

274251
/**

packages/legacy-sdk/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type LazyOption<T extends object> = T | (() => T | Promise<T>)
1818

1919
export type ClientProvider = Pick<
2020
PublicClient,
21-
'chain' | 'readContract' | 'getFilterLogs' | 'createEventFilter' | 'estimateContractGas' | 'multicall'
21+
'chain' | 'readContract' | 'getLogs' | 'estimateContractGas' | 'multicall'
2222
>
2323
export type Wallet = Pick<WalletClient, 'account' | 'chain' | 'writeContract' | 'signMessage' | 'sendTransaction'>
2424

packages/sdk/src/contract/edition-v1/read/actions.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ import { editionMintSchedules, editionMintSchedulesFromIds, editionScheduleIds }
2121
import type { MerkleProvider } from '../../../utils/types'
2222

2323
export function editionV1PublicActions<
24-
Client extends Pick<
25-
PublicClient,
26-
'readContract' | 'multicall' | 'estimateContractGas' | 'createEventFilter' | 'getFilterLogs'
27-
> & {
24+
Client extends Pick<PublicClient, 'readContract' | 'multicall' | 'estimateContractGas' | 'getLogs'> & {
2825
editionV1?: {
2926
sam?: {}
3027
}

packages/sdk/src/contract/edition-v1/read/schedules.ts

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ export function isHandledMinterInterfaceId(interfaceId: string): interfaceId is
2727
return HANDLED_MINTER_INTERFACE_IDS_SET.has(interfaceId)
2828
}
2929

30-
export async function editionRegisteredMinters<
31-
Client extends Pick<PublicClient, 'readContract' | 'createEventFilter' | 'getFilterLogs'>,
32-
>(
30+
export async function editionRegisteredMinters<Client extends Pick<PublicClient, 'readContract' | 'getLogs'>>(
3331
client: Client,
3432
{
3533
editionAddress,
@@ -48,7 +46,7 @@ export async function editionRegisteredMinters<
4846
functionName: 'MINTER_ROLE',
4947
})
5048

51-
const rolesUpdatedFilter = await client.createEventFilter({
49+
const roleEvents = await client.getLogs({
5250
event: {
5351
anonymous: false,
5452
inputs: [
@@ -71,17 +69,8 @@ export async function editionRegisteredMinters<
7169
strict: true,
7270
})
7371

74-
const roleEvents = await client.getFilterLogs({
75-
filter: rolesUpdatedFilter,
76-
})
77-
78-
const candidateMinters = Array.from(
79-
roleEvents.reduce((acc, event) => {
80-
if (event.args?.user) acc.add(event.args.user)
81-
82-
return acc
83-
}, new Set<Address>()),
84-
)
72+
// This list may contain duplicates if MINTER_ROLE was granted multiple times
73+
const candidateMinters = [...new Set(roleEvents.map((event) => event.args.user))]
8574

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

109-
return Array.from(allMinters)
93+
return minters.filter((minter): minter is Address => minter != null)
11094
}
11195

112-
export async function editionMinterMintIds<Client extends Pick<PublicClient, 'createEventFilter' | 'getFilterLogs'>>(
96+
export async function editionMinterMintIds<Client extends Pick<PublicClient, 'getLogs'>>(
11397
client: Client,
11498
{
11599
editionAddress,
@@ -121,8 +105,8 @@ export async function editionMinterMintIds<Client extends Pick<PublicClient, 'cr
121105
fromBlock?: FromBlock
122106
},
123107
) {
124-
// Query MintConfigCreated event, for v1 and v2, this signature is the same
125-
const filter = await client.createEventFilter({
108+
// Query MintConfigCreated event, for v1.0-v1.2, this signature is the same
109+
const mintScheduleConfigEvents = await client.getLogs({
126110
address: minterAddress,
127111
event: {
128112
anonymous: false,
@@ -174,21 +158,10 @@ export async function editionMinterMintIds<Client extends Pick<PublicClient, 'cr
174158
strict: true,
175159
})
176160

177-
const mintScheduleConfigEvents = await client.getFilterLogs({
178-
filter,
179-
})
180-
181-
return Array.from(
182-
mintScheduleConfigEvents.reduce((acc, event) => {
183-
if (event.args?.mintId != null) acc.add(event.args.mintId)
184-
return acc
185-
}, new Set<bigint>()),
186-
)
161+
return [...new Set(mintScheduleConfigEvents.map((event) => event.args.mintId))]
187162
}
188163

189-
export async function editionScheduleIds<
190-
Client extends Pick<PublicClient, 'createEventFilter' | 'getFilterLogs' | 'readContract'>,
191-
>(
164+
export async function editionScheduleIds<Client extends Pick<PublicClient, 'getLogs' | 'readContract'>>(
192165
client: Client,
193166
{
194167
editionAddress,
@@ -575,9 +548,7 @@ export async function editionMintSchedulesFromIds<Client extends Pick<PublicClie
575548
}
576549
}
577550

578-
export async function editionMintSchedules<
579-
Client extends Pick<PublicClient, 'readContract' | 'multicall' | 'createEventFilter' | 'getFilterLogs'>,
580-
>(
551+
export async function editionMintSchedules<Client extends Pick<PublicClient, 'readContract' | 'multicall' | 'getLogs'>>(
581552
client: Client,
582553
{
583554
editionAddress,

0 commit comments

Comments
 (0)