Skip to content

Commit 73a714d

Browse files
authored
refactor(ponder): remove redundant event arg casts (#21)
Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
1 parent af5e65a commit 73a714d

5 files changed

Lines changed: 35 additions & 69 deletions

File tree

apps/ponder/src/event-utils.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ export function eventBlock(event: { block: { number: bigint } }) {
44
}
55
}
66

7-
export function metadataFromEntries(
8-
keys: readonly string[] | undefined,
9-
values: readonly string[] | undefined
10-
): Record<string, string> | null {
11-
if (!keys?.length) return null
7+
export function metadataFromEntries(keys: readonly string[], values: readonly string[]): Record<string, string> | null {
8+
if (keys.length === 0) return null
129

1310
const metadata: Record<string, string> = {}
1411
for (let i = 0; i < keys.length; i++) {

apps/ponder/src/fwss.ts

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@ import { decodePiece } from './cid-utils.ts'
44
import { eventBlock, metadataFromEntries, metadataHasEmptyFlag } from './event-utils.ts'
55

66
ponder.on('FWSS:DataSetCreated', async ({ event, context }) => {
7-
const { dataSetId, providerId, payer, metadataKeys, metadataValues } = event.args as {
8-
dataSetId: bigint
9-
providerId: bigint
10-
payer: `0x${string}`
11-
metadataKeys?: readonly string[]
12-
metadataValues?: readonly string[]
13-
}
14-
7+
const { dataSetId, providerId, payer, metadataKeys, metadataValues } = event.args
158
const metadata = metadataFromEntries(metadataKeys, metadataValues)
169
const source = metadata?.source ?? null
1710
const block = eventBlock(event)
@@ -46,19 +39,7 @@ ponder.on('FWSS:DataSetCreated', async ({ event, context }) => {
4639
})
4740

4841
ponder.on('FWSS:PieceAdded', async ({ event, context }) => {
49-
const {
50-
dataSetId,
51-
pieceId,
52-
pieceCid: pieceCidRaw,
53-
keys,
54-
values,
55-
} = event.args as {
56-
dataSetId: bigint
57-
pieceId: bigint
58-
pieceCid: { data: `0x${string}` }
59-
keys?: readonly string[]
60-
values?: readonly string[]
61-
}
42+
const { dataSetId, pieceId, pieceCid: pieceCidRaw, keys, values } = event.args
6243

6344
const decoded = decodePiece(pieceCidRaw)
6445
const metadata = metadataFromEntries(keys, values)
@@ -88,7 +69,7 @@ ponder.on('FWSS:PieceAdded', async ({ event, context }) => {
8869
})
8970

9071
ponder.on('FWSS:PDPPaymentTerminated', async ({ event, context }) => {
91-
const { dataSetId, endEpoch } = event.args as { dataSetId: bigint; endEpoch: bigint }
72+
const { dataSetId, endEpoch } = event.args
9273
const existing = await context.db.find(dataSets, { dataSetId })
9374
if (!existing) return
9475

@@ -99,7 +80,7 @@ ponder.on('FWSS:PDPPaymentTerminated', async ({ event, context }) => {
9980
})
10081

10182
ponder.on('FWSS:ProviderApproved', async ({ event, context }) => {
102-
const { providerId } = event.args as { providerId: bigint }
83+
const { providerId } = event.args
10384
const existing = await context.db.find(providers, { providerId })
10485
if (!existing) return
10586

@@ -110,7 +91,7 @@ ponder.on('FWSS:ProviderApproved', async ({ event, context }) => {
11091
})
11192

11293
ponder.on('FWSS:ProviderUnapproved', async ({ event, context }) => {
113-
const { providerId } = event.args as { providerId: bigint }
94+
const { providerId } = event.args
11495
const existing = await context.db.find(providers, { providerId })
11596
if (!existing) return
11697

apps/ponder/src/pdp-verifier.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { dataSets, pieces } from 'ponder:schema'
33
import { eventBlock } from './event-utils.ts'
44

55
ponder.on('PDPVerifier:PiecesRemoved', async ({ event, context }) => {
6-
const { setId, pieceIds } = event.args as { setId: bigint; pieceIds: readonly bigint[] }
6+
const { setId, pieceIds } = event.args
77
const block = eventBlock(event)
88

99
for (const pieceId of pieceIds) {
@@ -19,7 +19,7 @@ ponder.on('PDPVerifier:PiecesRemoved', async ({ event, context }) => {
1919
})
2020

2121
ponder.on('PDPVerifier:DataSetDeleted', async ({ event, context }) => {
22-
const { setId } = event.args as { setId: bigint }
22+
const { setId } = event.args
2323
const existing = await context.db.find(dataSets, { dataSetId: setId })
2424
if (!existing) return
2525

apps/ponder/src/provider-status.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Context } from 'ponder:registry'
12
import { ponder } from 'ponder:registry'
23
import { providers } from 'ponder:schema'
34
import { and, eq, inArray, notInArray } from 'drizzle-orm'
@@ -11,18 +12,20 @@ function endorsementSetAddress(chainId: number): `0x${string}` | null {
1112
return null
1213
}
1314

14-
async function syncEndorsements(context: { chain: { id: number }; client: any; db: any }, blockNumber: bigint) {
15+
type ProviderStatusContext = Pick<Context<'ProviderStatusSync:block'>, 'chain' | 'client' | 'db'>
16+
17+
async function syncEndorsements(context: ProviderStatusContext, blockNumber: bigint) {
1518
const address = endorsementSetAddress(context.chain.id)
1619
if (!address) return
1720

1821
let providerIds: readonly bigint[]
1922
try {
20-
providerIds = (await context.client.readContract({
23+
providerIds = await context.client.readContract({
2124
abi: ProviderIdSetAbi,
2225
address,
2326
functionName: 'getProviderIds',
2427
blockNumber,
25-
})) as readonly bigint[]
28+
})
2629
} catch (error) {
2730
// The endorsement set was deployed after FWSS. Historical syncs before that block can safely skip it.
2831
console.warn('Failed to read endorsement provider IDs; skipping endorsement sync for block', {

apps/ponder/src/sp-registry.ts

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Context, Event } from 'ponder:registry'
12
import { ponder } from 'ponder:registry'
23
import { providers } from 'ponder:schema'
34
import { PDP_PRODUCT_TYPE } from '@filoz/repair-db'
@@ -6,14 +7,13 @@ import { ServiceProviderRegistryAbi } from './abis.ts'
67
import { eventBlock } from './event-utils.ts'
78

89
interface ProviderInfo {
9-
providerAddress: string | null
10+
providerAddress: Address | null
1011
name: string | null
1112
providerActive: boolean
1213
}
1314

14-
interface ReadProviderContext {
15-
client: any
16-
}
15+
type SpRegistryContext = Pick<Context<'SPRegistry:ProviderRegistered'>, 'client' | 'db'>
16+
type SpRegistryEvent = Pick<Event<'SPRegistry:ProviderRegistered'>, 'block' | 'log'>
1717

1818
function decodeCapabilityValue(hex: `0x${string}`): string {
1919
try {
@@ -27,23 +27,23 @@ function decodeCapabilityValue(hex: `0x${string}`): string {
2727
}
2828

2929
function capabilitiesFromEntries(
30-
keys: readonly string[] | undefined,
31-
values: readonly `0x${string}`[] | undefined
30+
keys: readonly string[],
31+
values: readonly `0x${string}`[]
3232
): Record<string, string> | null {
33-
if (!keys?.length) return null
33+
if (keys.length === 0) return null
3434

3535
const capabilities: Record<string, string> = {}
3636
for (let i = 0; i < keys.length; i++) {
3737
const key = keys[i]
38-
const value = values?.[i]
38+
const value = values[i]
3939
if (key === undefined) continue
4040
capabilities[key] = value ? decodeCapabilityValue(value) : ''
4141
}
4242
return capabilities
4343
}
4444

4545
async function readProviderInfo(
46-
context: ReadProviderContext,
46+
context: Pick<SpRegistryContext, 'client'>,
4747
registryAddress: Address,
4848
providerId: bigint,
4949
blockNumber: bigint
@@ -56,7 +56,7 @@ async function readProviderInfo(
5656
blockNumber,
5757
})
5858

59-
const [providerAddress, , name, , providerActive] = result as readonly [string, string, string, string, boolean]
59+
const [providerAddress, , name, , providerActive] = result
6060

6161
return { providerAddress, name, providerActive }
6262
}
@@ -67,10 +67,10 @@ async function upsertProviderInfo({
6767
providerId,
6868
providerAddress,
6969
}: {
70-
context: ReadProviderContext & { db: any }
71-
event: { block: { number: bigint }; log: { address: string } }
70+
context: SpRegistryContext
71+
event: SpRegistryEvent
7272
providerId: bigint
73-
providerAddress?: string
73+
providerAddress?: Address
7474
}) {
7575
let info: ProviderInfo = {
7676
providerAddress: providerAddress ?? null,
@@ -79,7 +79,7 @@ async function upsertProviderInfo({
7979
}
8080

8181
try {
82-
info = await readProviderInfo(context, event.log.address as Address, providerId, event.block.number)
82+
info = await readProviderInfo(context, event.log.address, providerId, event.block.number)
8383
} catch {
8484
// The event payload is enough to keep address-level repair inventory usable.
8585
}
@@ -108,27 +108,18 @@ async function upsertProviderInfo({
108108
}
109109

110110
ponder.on('SPRegistry:ProviderRegistered', async ({ event, context }) => {
111-
const { providerId, serviceProvider } = event.args as {
112-
providerId: bigint
113-
serviceProvider: string
114-
}
111+
const { providerId, serviceProvider } = event.args
115112

116113
await upsertProviderInfo({ context, event, providerId, providerAddress: serviceProvider })
117114
})
118115

119116
ponder.on('SPRegistry:ProviderInfoUpdated', async ({ event, context }) => {
120-
const { providerId } = event.args as { providerId: bigint }
117+
const { providerId } = event.args
121118
await upsertProviderInfo({ context, event, providerId })
122119
})
123120

124121
ponder.on('SPRegistry:ProductAdded', async ({ event, context }) => {
125-
const { providerId, productType, serviceProvider, capabilityKeys, capabilityValues } = event.args as {
126-
providerId: bigint
127-
productType: number
128-
serviceProvider: string
129-
capabilityKeys?: readonly string[]
130-
capabilityValues?: readonly `0x${string}`[]
131-
}
122+
const { providerId, productType, serviceProvider, capabilityKeys, capabilityValues } = event.args
132123
if (productType !== PDP_PRODUCT_TYPE) return
133124

134125
const capabilities = capabilitiesFromEntries(capabilityKeys, capabilityValues)
@@ -157,13 +148,7 @@ ponder.on('SPRegistry:ProductAdded', async ({ event, context }) => {
157148
})
158149

159150
ponder.on('SPRegistry:ProductUpdated', async ({ event, context }) => {
160-
const { providerId, productType, serviceProvider, capabilityKeys, capabilityValues } = event.args as {
161-
providerId: bigint
162-
productType: number
163-
serviceProvider: string
164-
capabilityKeys?: readonly string[]
165-
capabilityValues?: readonly `0x${string}`[]
166-
}
151+
const { providerId, productType, serviceProvider, capabilityKeys, capabilityValues } = event.args
167152
if (productType !== PDP_PRODUCT_TYPE) return
168153

169154
const capabilities = capabilitiesFromEntries(capabilityKeys, capabilityValues)
@@ -192,7 +177,7 @@ ponder.on('SPRegistry:ProductUpdated', async ({ event, context }) => {
192177
})
193178

194179
ponder.on('SPRegistry:ProductRemoved', async ({ event, context }) => {
195-
const { providerId, productType } = event.args as { providerId: bigint; productType: number }
180+
const { providerId, productType } = event.args
196181
if (productType !== PDP_PRODUCT_TYPE) return
197182

198183
const existing = await context.db.find(providers, { providerId })
@@ -205,7 +190,7 @@ ponder.on('SPRegistry:ProductRemoved', async ({ event, context }) => {
205190
})
206191

207192
ponder.on('SPRegistry:ProviderRemoved', async ({ event, context }) => {
208-
const { providerId } = event.args as { providerId: bigint }
193+
const { providerId } = event.args
209194
const existing = await context.db.find(providers, { providerId })
210195
if (!existing) return
211196

0 commit comments

Comments
 (0)