Skip to content

Commit 6ded6bd

Browse files
committed
types
1 parent e5d7218 commit 6ded6bd

2 files changed

Lines changed: 111 additions & 39 deletions

File tree

app/src/hooks/subgraph/useTransactions.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const useFetchTransactions = (
4444
inMemoryFilters: TxsInMemoryFilters,
4545
query?: QueryTransactionsArgs,
4646
) => {
47-
const { data, error, isLoading, isValidating, mutate } = useSWR(
47+
const { data, error, isLoading, isValidating, mutate } = useSWR<Transaction[]>(
4848
query
4949
? [
5050
'useFetchTransactions',
@@ -54,7 +54,13 @@ export const useFetchTransactions = (
5454
inMemoryFilters,
5555
]
5656
: null,
57-
async ([, , _query, , _inMemoryFilters]) => fetchTransactions(_query, _inMemoryFilters),
57+
async ([, , _query, , _inMemoryFilters]: [
58+
string,
59+
string,
60+
QueryTransactionsArgs,
61+
string,
62+
TxsInMemoryFilters,
63+
]) => fetchTransactions(_query, _inMemoryFilters),
5864
{ suspense: false },
5965
)
6066

@@ -65,11 +71,11 @@ export const useFetchTransactions = (
6571
setForeignTransaction(transaction.id)
6672
// update in-memory txs
6773
mutate(
68-
(txs) =>
69-
txs?.map((tx) => (tx.id === transaction.id ? { ...transaction, isClaiming: true } : tx)),
70-
{
71-
revalidate: false,
72-
},
74+
(txs: Transaction[] | undefined) =>
75+
txs?.map((tx: Transaction) =>
76+
tx.id === transaction.id ? { ...transaction, isClaiming: true } : tx,
77+
),
78+
{ revalidate: false },
7379
)
7480
}
7581
}

app/src/utils/transactions.ts

Lines changed: 98 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -248,58 +248,89 @@ export const unifyTransactions = async (
248248
return Object.values(allTransactions).sort((a, b) => Number(b.timestamp) - Number(a.timestamp))
249249
}
250250

251-
const toEnvioWhere = (where?: any): Record<string, any> | undefined => {
251+
const toEnvioWhere = (where?: unknown): Record<string, unknown> | undefined => {
252252
if (!where) return undefined
253-
const andClauses: any[] = []
253+
type OrClause = { initiator?: unknown; receiver?: unknown }
254+
type InnerWhere = {
255+
timestamp_gte?: unknown
256+
timestamp_lte?: unknown
257+
transactionHash?: unknown
258+
initiatorNetwork?: unknown
259+
bridgeName?: unknown
260+
bridgeName_contains_nocase?: unknown
261+
or?: OrClause[]
262+
}
263+
type RawWhere = {
264+
id?: unknown
265+
transactionHash?: unknown
266+
timestamp_gte?: unknown
267+
timestamp_lte?: unknown
268+
or?: OrClause[]
269+
initiatorNetwork?: unknown
270+
bridgeName?: unknown
271+
bridgeName_contains_nocase?: unknown
272+
and?: InnerWhere[]
273+
}
274+
275+
const w = where as RawWhere
276+
const andClauses: Array<Record<string, unknown>> = []
254277

255278
// id equals
256-
if (where.id) {
257-
andClauses.push({ id: { _eq: String(where.id).toLowerCase() } })
279+
if (w.id) {
280+
andClauses.push({ id: { _eq: String(w.id).toLowerCase() } })
258281
}
259282

260283
// transactionHash equals
261-
if (where.transactionHash) {
262-
andClauses.push({ transactionHash: { _eq: String(where.transactionHash).toLowerCase() } })
284+
if (w.transactionHash) {
285+
andClauses.push({ transactionHash: { _eq: String(w.transactionHash).toLowerCase() } })
263286
}
264287

265288
// timestamp range
266-
if (where.timestamp_gte !== undefined) {
267-
andClauses.push({ timestamp: { _gte: Number(where.timestamp_gte) } })
289+
if (w.timestamp_gte !== undefined) {
290+
andClauses.push({ timestamp: { _gte: Number(w.timestamp_gte) } })
268291
}
269-
if (where.timestamp_lte !== undefined) {
270-
andClauses.push({ timestamp: { _lte: Number(where.timestamp_lte) } })
292+
if (w.timestamp_lte !== undefined) {
293+
andClauses.push({ timestamp: { _lte: Number(w.timestamp_lte) } })
271294
}
272295

273296
// initiator/receiver OR search
274-
if (Array.isArray(where.or)) {
275-
const ors = where.or
276-
.map((cl: any) => {
277-
if (cl.initiator) return { initiator: { _eq: String(cl.initiator).toLowerCase() } }
278-
if (cl.receiver) return { receiver: { _eq: String(cl.receiver).toLowerCase() } }
297+
if (Array.isArray(w.or)) {
298+
const ors = w.or
299+
.map((cl: OrClause) => {
300+
if (typeof cl.initiator === 'string') {
301+
return {
302+
initiator: { _eq: cl.initiator.toLowerCase() },
303+
}
304+
}
305+
if (typeof cl.receiver === 'string') {
306+
return {
307+
receiver: { _eq: cl.receiver.toLowerCase() },
308+
}
309+
}
279310
return undefined
280311
})
281-
.filter(Boolean)
312+
.filter(Boolean) as Array<Record<string, unknown>>
282313
if (ors.length) andClauses.push({ _or: ors })
283314
}
284315

285316
// initiatorNetwork textual mapping
286-
if (where.initiatorNetwork) {
287-
const val = String(where.initiatorNetwork).toLowerCase()
317+
if (w.initiatorNetwork) {
318+
const val = String(w.initiatorNetwork).toLowerCase()
288319
const num = val === 'gnosis' ? 100 : val === 'mainnet' ? 1 : undefined
289320
if (num !== undefined) andClauses.push({ initiatorNetwork: { _eq: num } })
290321
}
291322

292323
// bridgeName -> bridgeType enum mapping
293-
const bridgeName = where.bridgeName || where.bridgeName_contains_nocase
324+
const bridgeName = w.bridgeName || w.bridgeName_contains_nocase
294325
if (bridgeName) {
295326
const val = String(bridgeName).toUpperCase()
296327
const enumVal = val.includes('XDAI') ? 'XDAI' : 'AMB'
297328
andClauses.push({ bridgeType: { _eq: enumVal } })
298329
}
299330

300331
// Combine explicit 'and' if present
301-
if (Array.isArray(where.and)) {
302-
where.and.forEach((inner: any) => {
332+
if (Array.isArray(w.and)) {
333+
w.and.forEach((inner: InnerWhere) => {
303334
if (inner.timestamp_gte !== undefined) {
304335
andClauses.push({ timestamp: { _gte: Number(inner.timestamp_gte) } })
305336
}
@@ -322,12 +353,20 @@ const toEnvioWhere = (where?: any): Record<string, any> | undefined => {
322353
}
323354
if (Array.isArray(inner.or)) {
324355
const ors = inner.or
325-
.map((cl: any) => {
326-
if (cl.initiator) return { initiator: { _eq: String(cl.initiator).toLowerCase() } }
327-
if (cl.receiver) return { receiver: { _eq: String(cl.receiver).toLowerCase() } }
356+
.map((cl: OrClause) => {
357+
if (typeof cl.initiator === 'string') {
358+
return {
359+
initiator: { _eq: cl.initiator.toLowerCase() },
360+
}
361+
}
362+
if (typeof cl.receiver === 'string') {
363+
return {
364+
receiver: { _eq: cl.receiver.toLowerCase() },
365+
}
366+
}
328367
return undefined
329368
})
330-
.filter(Boolean)
369+
.filter(Boolean) as Array<Record<string, unknown>>
331370
if (ors.length) andClauses.push({ _or: ors })
332371
}
333372
})
@@ -359,8 +398,35 @@ const fetchTransactionsEnvio = async (
359398
const limit = query?.first ?? defaultRequestLimit
360399
const offset = query?.skip ?? 0
361400

401+
type EnvioTx = {
402+
id: string
403+
bridgeType?: string | null
404+
transactionHash?: string | null
405+
timestamp?: number | null
406+
initiator?: string | null
407+
initiatorAmount?: string | null
408+
initiatorNetwork?: number | null
409+
initiatorToken?: string | null
410+
receiver?: string | null
411+
receiverToken?: string | null
412+
receiverAmount?: string | null
413+
receiverNetwork?: number | null
414+
transactionStatus?: string | null
415+
execution?: {
416+
id: string
417+
timestamp: number
418+
transactionHash: string
419+
executorAddress?: string | null
420+
} | null
421+
validations?: Array<{
422+
id: string
423+
timestamp: number
424+
transactionHash: string
425+
validatorAddress: string
426+
}>
427+
}
362428
const request = getEnvioGraphqlClient<{
363-
Transaction: Array<any>
429+
Transaction: Array<EnvioTx>
364430
}>()
365431
const res = await request(ENVIO_TRANSACTIONS_QUERY, {
366432
where,
@@ -371,16 +437,16 @@ const fetchTransactionsEnvio = async (
371437

372438
// Drop malformed rows early (skip problematic transactions)
373439
const safeRows = (res.Transaction || []).filter(
374-
(row: any) =>
440+
(row: EnvioTx) =>
375441
row?.id &&
376442
((row?.initiatorNetwork !== null && row?.initiatorNetwork !== undefined) ||
377443
(row?.receiverNetwork !== null && row?.receiverNetwork !== undefined)),
378444
)
379445

380446
// Map to subgraph-like TransactionSG for reuse of prepareTransactionForView.
381-
const sgRows: TransactionSG[] = safeRows.map((row: any) => {
447+
const sgRows: TransactionSG[] = safeRows.map((row: EnvioTx) => {
382448
return {
383-
__typename: 'Transaction' as any,
449+
__typename: 'Transaction' as const,
384450
id: row.id,
385451
bridgeName: bridgeTypeToName(row.bridgeType),
386452
transactionHash: row.transactionHash ?? '',
@@ -396,7 +462,7 @@ const fetchTransactionsEnvio = async (
396462
transactionStatus: row.transactionStatus,
397463
execution: row.execution
398464
? {
399-
__typename: 'TransactionExecution' as any,
465+
__typename: 'TransactionExecution' as const,
400466
id: row.execution.id,
401467
timestamp: row.execution.timestamp,
402468
transactionHash: row.execution.transactionHash,
@@ -405,8 +471,8 @@ const fetchTransactionsEnvio = async (
405471
}
406472
: null,
407473
validations: Array.isArray(row.validations)
408-
? row.validations.map((v: any) => ({
409-
__typename: 'TransactionValidation' as any,
474+
? row.validations.map((v) => ({
475+
__typename: 'TransactionValidation' as const,
410476
id: v.id,
411477
timestamp: v.timestamp,
412478
transactionHash: v.transactionHash,

0 commit comments

Comments
 (0)